package p2p
import "github.com/Diarkis/diarkis/client/go/modules/p2p"
Index
- func GetAddressByID(uid string) (string, bool)
- func GetAllPeers() ([]*udp.P2PClient, error)
- func GetAllUIDAddressPair() (map[string]string, error)
- func GetPeer(uid string) (*udp.P2PClient, error)
- func GetPortByPeerKey(uid string, addr net.Addr) (int, bool)
- func IsConnected(uid string) bool
- func SetPortByPeerKey(uid string, addr *net.UDPAddr)
-
type P2P
- func NewP2P(uid string, udpCli *udp.Client) *P2P
- func (c *P2P) CheckNATType()
- func (c *P2P) Connect(uid string, addrs []string, timeout int64, shouldBufUnreliable bool, ...)
- func (c *P2P) ConnectClient(uid string, addrs []string, holePunchTimeout int64, shouldBufUnreliable bool, ...)
- func (c *P2P) GetConnectedNum() uint16
- func (c *P2P) NewClient(shouldBufUnreliable bool) *udp.P2PClient
- func (c *P2P) OnStartP2PSync(callback func(key, iv, mackey []byte, syncLists []*sp2p.SyncList))
- func (c *P2P) Relay(rudpType RudpType, message []byte)
- func (c *P2P) SendBroadcast(rudpType RudpType, message []byte) uint16
- func (c *P2P) SendHolePunching(addr string)
- func (c *P2P) SendMulticast(rudpType RudpType, message []byte, uids []string) uint16
- func (c *P2P) SendPingTry(addr string)
- func (c *P2P) SendSuccess()
- func (c *P2P) SendUnicast(rudpType RudpType, message []byte, uid string) uint16
- func (c *P2P) SetDefaultOnDisconnect(...)
- func (c *P2P) SetDefaultOnException(callback func(errCode uint32, message string))
- func (c *P2P) SetDefaultOnFail(...)
- func (c *P2P) SetDefaultOnMessage(...)
- func (c *P2P) SetDefaultOnReady(...)
- type PeerKey
- type RudpType
Functions
func GetAddressByID
func GetAddressByID(uid string) (string, bool)
GetAddressByID returns the address of the peer with the given UID
func GetAllPeers
func GetAllPeers() ([]*udp.P2PClient, error)
GetAllPeers returns all the connected peers
func GetAllUIDAddressPair
func GetAllUIDAddressPair() (map[string]string, error)
GetAllUIDAddressPair returns a map of UID and address pairs of all the connected peers
func GetPeer
func GetPeer(uid string) (*udp.P2PClient, error)
GetPeer returns a P2P client for the given peer
func GetPortByPeerKey
func GetPortByPeerKey(uid string, addr net.Addr) (int, bool)
GetPortByPeerKey returns a port for the given UID+Address
func IsConnected
func IsConnected(uid string) bool
IsConnected checks if the client is connected to the given peer
func SetPortByPeerKey
func SetPortByPeerKey(uid string, addr *net.UDPAddr)
SetPortByPeerKey sets a mapping between the UID+Address and the port This is used to detect symmetric NAT in case the hole-punching ACK is returned with the same address and different port
Types
type P2P
type P2P struct { sync.RWMutex }
P2P represents a P2P client For now, we only support UDP due to the nature of hole punching
func NewP2P
func NewP2P(uid string, udpCli *udp.Client) *P2P
NewP2P creates a new P2P client
func (*P2P) CheckNATType
func (c *P2P) CheckNATType()
CheckNATType asks the server to detect the NAT type of the client
func (*P2P) Connect
func (c *P2P) Connect(uid string, addrs []string, timeout int64, shouldBufUnreliable bool, crypto *sp2p.CryptoData)
Connect sends a request to the server to connect to the client
func (*P2P) ConnectClient
func (c *P2P) ConnectClient(uid string, addrs []string, holePunchTimeout int64, shouldBufUnreliable bool, crypto *sp2p.CryptoData)
ConnectClient creates a P2P client and sends hole-punching packets to all the given addresses If the hole-punching is successful, the client will be connected to the peer
func (*P2P) GetConnectedNum
func (c *P2P) GetConnectedNum() uint16
GetConnectedNum returns the number of connected peers
func (*P2P) NewClient
func (c *P2P) NewClient(shouldBufUnreliable bool) *udp.P2PClient
NewClient returns a new P2P client
func (*P2P) OnStartP2PSync
func (c *P2P) OnStartP2PSync(callback func(key, iv, mackey []byte, syncLists []*sp2p.SyncList))
OnStartP2PSync sets a callback to be called when getting a server push for init P2P.
func (*P2P) Relay
func (c *P2P) Relay(rudpType RudpType, message []byte)
Relay sends a message to the server to relay to the linked clients
func (*P2P) SendBroadcast
func (c *P2P) SendBroadcast(rudpType RudpType, message []byte) uint16
SendBroadcast sends a message to all the connected peers
func (*P2P) SendHolePunching
func (c *P2P) SendHolePunching(addr string)
SendHolePunching sends a hole-punching packet to the given address
func (*P2P) SendMulticast
func (c *P2P) SendMulticast(rudpType RudpType, message []byte, uids []string) uint16
SendMulticast sends a message to multiple peers
func (*P2P) SendPingTry
func (c *P2P) SendPingTry(addr string)
SendPingTry sends a hole-punching packet to the given address
func (*P2P) SendSuccess
func (c *P2P) SendSuccess()
SendSuccess sends a success message to count the number of successful hole punching on the server
func (*P2P) SendUnicast
func (c *P2P) SendUnicast(rudpType RudpType, message []byte, uid string) uint16
SendUnicast sends a message to the given peer
func (*P2P) SetDefaultOnDisconnect
func (c *P2P) SetDefaultOnDisconnect(callback func(uid string, unresolvedAddress string, resolvedAddress string, port uint16))
SetDefaultOnDisconnect sets a callback to be called when a client is disconnected from a peer This will be set to all P2P clients created by this module
func (*P2P) SetDefaultOnException
func (c *P2P) SetDefaultOnException(callback func(errCode uint32, message string))
SetDefaultOnException sets a callback to be called when a client fails to send a message to a peer This will be set to all P2P clients created by this module
func (*P2P) SetDefaultOnFail
func (c *P2P) SetDefaultOnFail(callback func(uid string, unresolvedAddress string, resolvedAddress string, port uint16))
SetDefaultOnFail sets a callback to be called when a client fails to connect to a peer This will be set to all P2P clients created by this module
func (*P2P) SetDefaultOnMessage
func (c *P2P) SetDefaultOnMessage(callback func(unresolvedAddress string, resolvedAddress string, port uint16, payload []byte))
SetDefaultOnMessage sets a callback to be called when a client receives a message from a peer This will be set to all P2P clients created by this module
func (*P2P) SetDefaultOnReady
func (c *P2P) SetDefaultOnReady(callback func(uid string, unresolvedAddress string, resolvedAddress string, port uint16, holePunchTime uint64))
SetDefaultOnReady sets a callback to be called when a client is connected to a peer This will be set to all P2P clients created by this module
type PeerKey
type PeerKey struct { }
PeerKey is a combination of UID and their global IP address used to detect symmetric NAT
type RudpType
type RudpType uint8
RudpType is the type of the RUDP packet
const ( // UnreliableUnordered is a flag for UDP packet UnreliableUnordered RudpType = iota // ReliableUnordered is a flag for unordered RUDP packet ReliableUnordered // ReliableOrdered is a flag for ordered RUDP packet ReliableOrdered )