p2pc

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package p2pc extends udpc to provide P2P functionality

Index

Constants

View Source
const (
	P2P_CONNECT_TRIES   = 3
	P2P_RECONNECT_TRIES = 3
)
View Source
const (
	RELAY_CONNECT_TRIES      = 3
	RELAY_RECONNECT_TRIES    = 4
	RELAY_PEER_AWAIT_TIMEOUT = time.Second * 20
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Relay Server Address
	// Providing an address (non-nil) will ensure that all relay connections from this client
	// will use the server with this address
	RelayAddr *net.UDPAddr
}

P2P Client Manager

type Connection

type Connection struct {

	// Client ID
	ClientId string

	// Exit Channel
	Exit chan bool
	// Closed Status
	Closed bool
	// contains filtered or unexported fields
}

P2P Client Connection

func (*Connection) Close

func (c *Connection) Close(reason string)

Close Connection

func (*Connection) IsConnected

func (c *Connection) IsConnected() bool

If Connection is active

func (*Connection) OpenMessageStream

func (c *Connection) OpenMessageStream() (ms *MessageStream, err error)

func (*Connection) OpenStream

func (c *Connection) OpenStream(data map[string]string) (stream *udp.Stream, err error)

Open Stream to Peer

func (*Connection) String

func (c *Connection) String() string

Stringify

type ConnectionHandler

type ConnectionHandler func(c *Connection)

Called on new Connection

type Manager

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

P2P Client Manager

func New

func New(
	log *logrus.Logger,
	config Config,
	client *udpc.Client,
) (m *Manager, err error)

Create new Manager

func (*Manager) CloseAll

func (m *Manager) CloseAll()

Close All Connections

func (*Manager) CloseConnection

func (m *Manager) CloseConnection(cid string, reason string)

Close Connection

func (*Manager) ConnectById

func (m *Manager) ConnectById(peerId string, mode p2p.ConnectionMode) (conn *Connection, err error)

Connect to Client by ID

func (*Manager) ConnectByTag

func (m *Manager) ConnectByTag(tag string, mode p2p.ConnectionMode) (conn *Connection, err error)

Connect to Client by Tag

func (*Manager) SetConnectionHandler

func (m *Manager) SetConnectionHandler(handler ConnectionHandler)

Set New Connection Handler

func (*Manager) SetMessageStreamHandler

func (m *Manager) SetMessageStreamHandler(handler MessageStreamHandler)

Set Message Stream Handler

func (*Manager) SetStreamHandler

func (m *Manager) SetStreamHandler(handler udp.StreamHandler)

Set New Stream Handler

type MessageHandler

type MessageHandler func(mc *MessageStream, msg *network.Message)

Message Handler

type MessageStream

type MessageStream struct {
	Conn *Connection

	// Exit channel
	Exit chan bool
	// Message Stream closed
	Closed bool
	// contains filtered or unexported fields
}

Message Stream

func NewMessageStream

func NewMessageStream(conn *Connection, stream *udp.Stream) *MessageStream

Create a MessageStream

func (*MessageStream) Close

func (mc *MessageStream) Close()

Close Message Stream

func (*MessageStream) Listen

func (mc *MessageStream) Listen(mh MessageHandler) (err error)

Listen for messages

func (*MessageStream) Send

func (mc *MessageStream) Send(msg *network.Message) (rmsg *network.Message, err error)

Send a message

func (*MessageStream) SendAndRead

func (mc *MessageStream) SendAndRead(msg *network.Message) (rmsg *network.Message, err error)

If the stream is not actively read, this function sends and reads a single message This method SHOULD NOT BE USED if the stream is actively being read. Just use the Send function.

type MessageStreamHandler

type MessageStreamHandler func(ms *MessageStream)

Called on new MessageStream

Jump to

Keyboard shortcuts

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