package udp

import "github.com/Diarkis/diarkis/client/go/udp"

Index

Functions

func DecryptP2PPayload

func DecryptP2PPayload(payload []byte, addr net.Addr) []byte

DecryptP2PPayload returns a decrypted payload of the P2P packet if encryption is enabled It returns the original payload if the packet is not encrypted

func GetUIDByClient

func GetUIDByClient(cli *Client) (string, bool)

GetUIDByClient returns a UID for the given client

func LogLevel

func LogLevel(lvl int)

LogLevel sets log level

func SendHolePunching

func SendHolePunching(udp *Client, myUID string, addr string)

SendHolePunching sends a hole-punching packet to the given address

func SendPingTry

func SendPingTry(udp *Client, mySID []byte, addr string)

SendPingTry sends a hole-punching packet to the given address

func SetupP2P

func SetupP2P(cli *Client, uid string)

SetupP2P sets up P2P.

Types

type Client

type Client struct {
	ID               string
	IDBytes          []byte
	ServerEndPoint   string
	ClientKey        string
	SID              []byte
	SIDString        string
	EncryptionKey    []byte
	EncryptionIV     []byte
	EncryptionMacKey []byte

	// IsPufferEnabled is a boolean flag that enables packing/unpacking with Puffer
	IsPufferEnabled bool
}

Client UDP client

func New

func New(rcvMaxSize int, interval int64) *Client

New Creates a new UDP Client

func (*Client) CatchOnReconnect

func (cli *Client) CatchOnReconnect(ver uint8, cmd uint16, callback func(bool))

CatchOnReconnect registers a callback on reconnect or response w/ ver and cmd of your choice ONCE

func (*Client) Connect

func (cli *Client) Connect(addr string)

Connect connects UDP Client

func (*Client) Die

func (cli *Client) Die()

Die stops server communication ungracefully

func (*Client) Disconnect

func (cli *Client) Disconnect()

Disconnect disconnects the UDP Client.

func (*Client) EnablePuffer

func (cli *Client) EnablePuffer(isPufferEnabled bool)

EnablePuffer enables or disables Puffer

func (*Client) GetGlobalAddress

func (cli *Client) GetGlobalAddress() string

GetGlobalAddress returns global address of self

func (*Client) IsOffline

func (cli *Client) IsOffline() bool

IsOffline returns if the connected server is offline state

func (*Client) Migrate

func (cli *Client) Migrate()

Migrate lets UDP client reconnect to the other server

func (*Client) OnConnect

func (cli *Client) OnConnect(callback func())

OnConnect registers a callback on Connect

func (*Client) OnDisconnect

func (cli *Client) OnDisconnect(callback func())

OnDisconnect registers a callback on Disconnect

func (*Client) OnEcho

func (cli *Client) OnEcho(callback func(rtt time.Duration, address string))

OnEcho registers a callback on Echo Returns round trip time between the server and inside global IP address

func (*Client) OnOffline

func (cli *Client) OnOffline(callback func())

OnOffline registers a callback called when server gets Offline state

func (*Client) OnPush

func (cli *Client) OnPush(callback func(uint8, uint16, []byte))

OnPush Registers a callback on push receive

func (*Client) OnReceiveP2PPacket

func (cli *Client) OnReceiveP2PPacket(callback func(net.Addr, *Client, *pm.P2PPacket))

OnReceiveP2PPacket registers a callback which is triggered when P2P packet is received

func (*Client) OnReconnect

func (cli *Client) OnReconnect(callback func())

OnReconnect registers a callack on Reconnect

func (*Client) OnResponse

func (cli *Client) OnResponse(callback func(uint8, uint16, uint8, []byte))

OnResponse Registers a callback on response receive

func (*Client) Ping

func (cli *Client) Ping()

Ping sends a ping packet over UDP

func (*Client) RSend

func (cli *Client) RSend(ver uint8, cmd uint16, payload []byte)

RSend Sends a Reliable UDP packet to server

func (*Client) Reconnect

func (cli *Client) Reconnect(addr string)

Reconnect restarts UDP Client

func (*Client) RemoveAll

func (cli *Client) RemoveAll()

RemoveAll Removes all callbacks

func (*Client) RemoveAllOnPush

func (cli *Client) RemoveAllOnPush()

RemoveAllOnPush Removes all callbacks of OnPush

func (*Client) RemoveAllOnResponse

func (cli *Client) RemoveAllOnResponse()

RemoveAllOnResponse Removes all callbacks of OnResponse

func (*Client) RemoveOnPush

func (cli *Client) RemoveOnPush(callbackToRemove func(uint8, uint16, []byte))

RemoveOnPush removes a callback function on push event

func (*Client) RemoveOnReconnect

func (cli *Client) RemoveOnReconnect(callbackToRemove func())

RemoveOnReconnect removes a callback function on reconnect event

func (*Client) RemoveOnResponse

func (cli *Client) RemoveOnResponse(callbackToRemove func(uint8, uint16, uint8, []byte))

RemoveOnResponse removes a callback function on response event

func (*Client) Send

func (cli *Client) Send(ver uint8, cmd uint16, payload []byte)

Send Sends a UDP packet to server

func (*Client) SendTo

func (cli *Client) SendTo(packet []byte, addr net.Addr) error

SendTo Sends a UDP packet to server

func (*Client) SetClientKey

func (cli *Client) SetClientKey(clientKey string)

SetClientKey sets up client key

func (*Client) SetEncryptionKeys

func (cli *Client) SetEncryptionKeys(sid []byte, key []byte, iv []byte, macKey []byte)

SetEncryptionKeys Sets up encryption keys

func (*Client) SetID

func (cli *Client) SetID(id string)

SetID sets ID

func (*Client) URSend

func (cli *Client) URSend(ver uint8, cmd uint16, payload []byte)

URSend Sends a Reliable Unordered UDP packet to server FIXME: name me...

type Encryption

type Encryption struct {
}

Encryption represents a set of encryption keys

type HolePunching

type HolePunching struct {
	sync.RWMutex
}

HolePunching represents a hole-punching state

type P2PClient

type P2PClient struct {
	sync.RWMutex
}

P2PClient represents a P2P client

func ConnectP2P

func ConnectP2P(udp *Client, addrs []string, uid string, holePunchTimeout int64, shouldBufUnreliable bool) (clients []*P2PClient)

ConnectP2P creates udp clients and sends hole-punching packets to all the given addresses P2PClient.Connect will be called in this process if hole-punching is successful

func GetP2PClientByAddr

func GetP2PClientByAddr(addr string) (*P2PClient, error)

GetP2PClientByAddr returns a P2P client by the given address

func NewP2PClient

func NewP2PClient(udp *Client, shouldBufUnreliable bool) *P2PClient

NewP2PClient creates a new P2P client

func (*P2PClient) Connect

func (p2pCli *P2PClient) Connect(uid string, addr net.Addr) error

Connect starts the P2P loop for RUDP

func (*P2PClient) Disconnect

func (p2pCli *P2PClient) Disconnect()

Disconnect disconnects the peer

func (*P2PClient) Encrypt

func (p2pCli *P2PClient) Encrypt(bytes []byte) ([]byte, error)

Encrypt encrypts the given bytes if encryption is enabled

func (*P2PClient) ExecHolePunchingProcess

func (p2pCli *P2PClient) ExecHolePunchingProcess(timeout int64)

ExecHolePunchingProcess sends hole-punching packets to all the given addresses P2PClient.Connect will be called asynchronously if hole-punching is successful

func (*P2PClient) GetPeerID

func (p2pCli *P2PClient) GetPeerID() string

GetPeerID returns a UID of the peer

func (*P2PClient) RSend

func (p2pCli *P2PClient) RSend(payload []byte, order bool)

RSend Sends a Reliable UDP packet to the peer

func (*P2PClient) Send

func (p2pCli *P2PClient) Send(payload []byte)

Send sends a unreliable UDP packet to the peer

func (*P2PClient) SendHolePunching

func (p2pCli *P2PClient) SendHolePunching() error

SendHolePunching sends a hole-punching packet to the given address

func (*P2PClient) SetEncryptions

func (p2pCli *P2PClient) SetEncryptions(key, iv, mackey []byte)

SetEncryptions sets the encryption keys for the P2P client

func (*P2PClient) SetOnDisconnect

func (p2pCli *P2PClient) SetOnDisconnect(cb func(uid string, unresolvedAddress string, resolvedAddress string, port uint16))

SetOnDisconnect sets a callback function to be called when the peer is disconnected

func (*P2PClient) SetOnException

func (p2pCli *P2PClient) SetOnException(cb func(errCode uint32, message string))

SetOnException sets a callback function to be called when failing to send a packet to the peer

func (*P2PClient) SetOnFail

func (p2pCli *P2PClient) SetOnFail(cb func(uid string, unresolvedAddress string, resolvedAddress string, port uint16))

SetOnFail sets a callback function to be called when failing the hole-punching to the peer

func (*P2PClient) SetOnMessage

func (p2pCli *P2PClient) SetOnMessage(cb func(unresolvedAddress string, resolvedAddress string, port uint16, payload []byte))

SetOnMessage sets a callback function to be called when receiving a message from the peer

func (*P2PClient) SetOnReady

func (p2pCli *P2PClient) SetOnReady(cb func(uid string, unresolvedAddress string, resolvedAddress string, port uint16, holePunchTime uint64))

SetOnReady sets a callback function to be called when the peer is ready

func (*P2PClient) SetPeer

func (p2pCli *P2PClient) SetPeer(addr net.Addr, uid string) error

SetPeer registers the peer of the P2P client to the list

type Peer

type Peer struct {
	ID string
}

Peer represents a P2P peer