Documentation ¶
Overview ¶
Package client contains the implementation of the Relay client.
The Relay client is responsible for establishing a connection with the Relay server and sending and receiving messages, Keep persistent connection with the Relay server and handle the connection issues. It uses the WebSocket protocol for communication and optionally supports TLS (Transport Layer Security).
If a peer wants to communicate with a peer on a different relay server, the manager will establish a new connection to the relay server. The connection with these relay servers will be closed if there is no active connection. The peers negotiate the common relay instance via signaling service.
Index ¶
- Variables
- type Client
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) Read(b []byte) (n int, err error)
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) SetDeadline(t time.Time) error
- func (c *Conn) SetReadDeadline(t time.Time) error
- func (c *Conn) SetWriteDeadline(t time.Time) error
- func (c *Conn) Write(p []byte) (n int, err error)
- type Guard
- type Manager
- func (m *Manager) AddCloseListener(serverAddress string, onClosedListener OnServerCloseListener) error
- func (m *Manager) HasRelayAddress() bool
- func (m *Manager) OpenConn(serverAddress, peerKey string) (net.Conn, error)
- func (m *Manager) RelayInstanceAddress() (string, error)
- func (m *Manager) Serve() error
- func (m *Manager) ServerURLs() []string
- func (m *Manager) UpdateToken(token *relayAuth.Token) error
- type ManagerService
- type Msg
- type OnServerCloseListener
- type RelayAddr
- type RelayTrack
Constants ¶
This section is empty.
Variables ¶
var (
ErrConnAlreadyExists = fmt.Errorf("connection already exists")
)
var (
ErrRelayClientNotConnected = fmt.Errorf("relay client not connected")
)
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a client for the relay server. It is responsible for establishing a connection to the relay server and managing connections to other peers. All exported functions are safe to call concurrently. After close the connection, the client can be reused by calling Connect again. When the client is closed, all connections are closed too. While the Connect is in progress, the OpenConn function will block until the connection is established with relay server.
func NewClient ¶
func NewClient(ctx context.Context, serverURL string, authTokenStore *auth.TokenStore, peerID string) *Client
NewClient creates a new client for the relay server. The client is not connected to the server until the Connect
func (*Client) Close ¶
Close closes the connection to the relay server and all connections to other peers.
func (*Client) Connect ¶
Connect establishes a connection to the relay server. It blocks until the connection is established or an error occurs.
func (*Client) OpenConn ¶
OpenConn create a new net.Conn for the destination peer ID. In case if the connection is in progress to the relay server, the function will block until the connection is established or timed out. Otherwise, it will return immediately. todo: what should happen if call with the same peerID with multiple times?
func (*Client) ServerInstanceURL ¶
ServerInstanceURL returns the address of the relay server. It could change after the close and reopen the connection.
func (*Client) SetOnDisconnectListener ¶
func (c *Client) SetOnDisconnectListener(fn func())
SetOnDisconnectListener sets a function that will be called when the connection to the relay server is closed.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn represent a connection to a relayed remote peer.
func NewConn ¶
func NewConn(client *Client, dstID []byte, dstStringID string, messageChan chan Msg, instanceURL *RelayAddr) *Conn
NewConn creates a new connection to a relayed remote peer. client: the client instance, it used to send messages to the destination peer dstID: the destination peer ID dstStringID: the destination peer ID in string format messageChan: the channel where the messages will be received instanceURL: the relay instance URL, it used to get the proper server instance address for the remote peer
func (*Conn) RemoteAddr ¶
type Guard ¶
type Guard struct {
// contains filtered or unexported fields
}
Guard manage the reconnection tries to the Relay server in case of disconnection event.
func (*Guard) OnDisconnected ¶
func (g *Guard) OnDisconnected()
OnDisconnected is called when the relay client is disconnected from the relay server. It will trigger the reconnection todo prevent multiple reconnection instances. In the current usage it should not happen, but it is better to prevent
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is a manager for the relay client instances. It establishes one persistent connection to the given relay URL and automatically reconnect to them in case disconnection. The manager also manage temporary relay connection. If a client wants to communicate with a client on a different relay server, the manager will establish a new connection to the relay server. The connection with these relay servers will be closed if there is no active connection. Periodically the manager will check if there is any unused relay connection and close it.
func NewManager ¶
NewManager creates a new manager instance. The serverURL address can be empty. In this case, the manager will not serve.
func (*Manager) AddCloseListener ¶
func (m *Manager) AddCloseListener(serverAddress string, onClosedListener OnServerCloseListener) error
AddCloseListener adds a listener to the given server instance address. The listener will be called if the connection closed.
func (*Manager) HasRelayAddress ¶
HasRelayAddress returns true if the manager is serving. With this method can check if the peer can communicate with Relay service.
func (*Manager) OpenConn ¶
OpenConn opens a connection to the given peer key. If the peer is on the same relay server, the connection will be established via the relay server. If the peer is on a different relay server, the manager will establish a new connection to the relay server. It returns back with a net.Conn what represent the remote peer connection.
func (*Manager) RelayInstanceAddress ¶
RelayInstanceAddress returns the address of the permanent relay server. It could change if the network connection is lost. This address will be sent to the target peer to choose the common relay server for the communication.
func (*Manager) Serve ¶
Serve starts the manager. It will establish a connection to the relay server and start the relay cleanup loop for the unused relay connections. The manager will automatically reconnect to the relay server in case of disconnection.
func (*Manager) ServerURLs ¶
ServerURLs returns the addresses of the relay servers.
type ManagerService ¶
type ManagerService interface { Serve() error OpenConn(serverAddress, peerKey string) (net.Conn, error) AddCloseListener(serverAddress string, onClosedListener OnServerCloseListener) error RelayInstanceAddress() (string, error) ServerURLs() []string HasRelayAddress() bool UpdateToken(token *relayAuth.Token) error }
ManagerService is the interface for the relay manager.
type Msg ¶
type Msg struct { Payload []byte // contains filtered or unexported fields }
Msg carry the payload from the server to the client. With this struct, the net.Conn can free the buffer.
type OnServerCloseListener ¶
type OnServerCloseListener func()
type RelayTrack ¶
RelayTrack hold the relay clients for the foreign relay servers. With the mutex can ensure we can open new connection in case the relay connection has been established with the relay server.
func NewRelayTrack ¶
func NewRelayTrack() *RelayTrack