2024-05-17 17:43:28 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2024-06-09 20:27:40 +02:00
|
|
|
"io"
|
2024-05-17 17:43:28 +02:00
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Conn struct {
|
2024-05-27 10:25:08 +02:00
|
|
|
client *Client
|
|
|
|
dstID []byte
|
|
|
|
dstStringID string
|
2024-06-09 20:27:40 +02:00
|
|
|
messageChan chan Msg
|
2024-07-16 11:02:32 +02:00
|
|
|
instanceURL *RelayAddr
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 11:02:32 +02:00
|
|
|
func NewConn(client *Client, dstID []byte, dstStringID string, messageChan chan Msg, instanceURL *RelayAddr) *Conn {
|
2024-05-17 17:43:28 +02:00
|
|
|
c := &Conn{
|
2024-05-27 10:25:08 +02:00
|
|
|
client: client,
|
|
|
|
dstID: dstID,
|
|
|
|
dstStringID: dstStringID,
|
2024-06-09 20:27:40 +02:00
|
|
|
messageChan: messageChan,
|
2024-07-16 11:02:32 +02:00
|
|
|
instanceURL: instanceURL,
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) Write(p []byte) (n int, err error) {
|
2024-07-18 13:16:50 +02:00
|
|
|
return c.client.writeTo(c, c.dstStringID, c.dstID, p)
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) Read(b []byte) (n int, err error) {
|
2024-06-09 20:27:40 +02:00
|
|
|
msg, ok := <-c.messageChan
|
|
|
|
if !ok {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
n = copy(b, msg.Payload)
|
|
|
|
msg.Free()
|
|
|
|
return n, nil
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) Close() error {
|
2024-07-18 13:16:50 +02:00
|
|
|
return c.client.closeConn(c, c.dstStringID)
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) LocalAddr() net.Addr {
|
2024-05-19 12:41:06 +02:00
|
|
|
return c.client.relayConn.LocalAddr()
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) RemoteAddr() net.Addr {
|
2024-07-16 11:02:32 +02:00
|
|
|
return c.instanceURL
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) SetDeadline(t time.Time) error {
|
|
|
|
//TODO implement me
|
2024-07-09 16:48:50 +02:00
|
|
|
panic("SetDeadline is not implemented")
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) SetReadDeadline(t time.Time) error {
|
|
|
|
//TODO implement me
|
2024-07-09 16:48:50 +02:00
|
|
|
panic("SetReadDeadline is not implemented")
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conn) SetWriteDeadline(t time.Time) error {
|
|
|
|
//TODO implement me
|
2024-07-09 16:48:50 +02:00
|
|
|
panic("SetReadDeadline is not implemented")
|
2024-05-17 17:43:28 +02:00
|
|
|
}
|