tcp

package
v0.0.0-...-f6b832a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2022 License: MIT Imports: 12 Imported by: 1

Documentation

Overview

Tcp connection builder

Bind to a port, connect to a rendezvous server. Wait for peer connection request or initiate a peer connection request.

Example (Client1)
c := tcp.NewClient(rndzServer, "c1", netip.AddrPort{})
defer c.Close()
l, _ := c.Listen(context.Background())
defer l.Close()
for {
	conn, _ := l.Accept()
	defer conn.Close()
	//    ...
}
Output:

Example (Client2)
c := tcp.NewClient(rndzServer, "c2", netip.AddrPort{})
defer c.Close()
conn, _ := c.Connect(context.Background(), "c1")
defer conn.Close()
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Tcp connection builder

func New

func New(rndzServer, id string, localAddr netip.AddrPort) *Client

set rendezvous server, peer identity, local bind address. if no local address set, choose according server address type(ipv4 or ipv6).

func (*Client) Close

func (c *Client) Close()

stop internal goroutine

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, targetId string) (net.Conn, error)

connect to rendezvous server and request a connection to target node.

it will return a net.Conn with remote peer.

the connection with rendezvous server will be drop after return.

func (*Client) Listen

func (c *Client) Listen(ctx context.Context) (net.Listener, error)

put socket in listen mode, create connection with rendezvous server, wait for peer connection request. if connection with server broken it will auto reconnect.

when received `Fsync` request from server, attempt to connect remote peer with a very short timeout, this will open the firwall and nat rule for the peer connection that will follow immediately. When the peer connection finally come, the listening socket then accept it as normal.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL