...

Package udp

import "github.com/Diarkis/diarkis/server/udp"
Overview
Index

Overview ▾

Index ▾

Constants
func Command(ver uint8, cmd uint16, handlerName string, handler func(*State, func(error)))
func GetAddrByPingTryUser(sid string) (addr string)
func GetConnectionTTL() int64
func GetEndPoint() string
func HookAllCommands(handlerName string, handler func(*State, func(error)))
func HookAllWrites(handler func(*State, []byte) ([]byte, error))
func Init()
func IsCommandDuplicate(ver uint8, commandID uint16) bool
func IsEncryptionEnabled() bool
func IsP2PEnabled() bool
func Offline()
func OnDisconnect(callback func(string, string, string), last bool)
func OnEcho(handler func(*State, func(error)))
func OnNewConnection(callback func(string) bool)
func Online()
func SendPingReply(addr net.Addr) error
func SendPingTry(addr net.Addr) error
func SendTo(packets []byte, addr net.Addr) error
func SendToWithHook(addr net.Addr, packets []byte, hook func(*State, []byte) ([]byte, error)) error
func SendWithHook(state *State, packets []byte, hook func(*State, []byte) ([]byte, error)) error
func SetAddrByPingTryUser(sid string, addr string)
func SetOnPingReply(ttl int, addr string, callback func(error))
func SetPublicEndPoint(addr string)
func Setup(confpath string)
func ShutdownUDP()
func Taken()
type Connection
type State
    func (state *State) Disconnect()
    func (state *State) GetClientAddr() string
    func (state *State) GetConnection() *Connection
    func (state *State) GetSeqForDup() uint32
    func (state *State) Push(ver uint8, cmd uint16, payload []byte) (int, error)
    func (state *State) RPush(conn *Connection, ver uint8, cmd uint16, payload []byte)
    func (state *State) RSend(conn *Connection, ver uint8, cmd uint16, payload []byte, status uint8)
    func (state *State) Send(ver uint8, cmd uint16, payload []byte, status uint8) (int, error)

Constants

Bad Response packet status

const Bad = 4

Err Response packet status

const Err = 5

Ok Response packet status

const Ok = 1

Type mesh network node type of UDP server

const Type = "UDP"

func Command

func Command(ver uint8, cmd uint16, handlerName string, handler func(*State, func(error)))

Command Registers a UDP packet handler as a command

func GetAddrByPingTryUser

func GetAddrByPingTryUser(sid string) (addr string)

GetAddrByPingTryUser gets the public IP address of the user who tries to Ping Try

func GetConnectionTTL

func GetConnectionTTL() int64

GetConnectionTTL returns connection TTL

func GetEndPoint

func GetEndPoint() string

GetEndPoint Returns its end point that is actually bound

func HookAllCommands

func HookAllCommands(handlerName string, handler func(*State, func(error)))

HookAllCommands Registers a packet handler as a hook to all commands

func HookAllWrites

func HookAllWrites(handler func(*State, []byte) ([]byte, error))

HookAllWrites Registers a handler function to all TCP socket writes: Receives the payload and returns a payload

func Init

func Init()

Init [INTERNAL USE ONLY] Flag the UDP server state to be initialized to prevent registration of commands and hooks BEFORE initialization by user package

func IsCommandDuplicate

func IsCommandDuplicate(ver uint8, commandID uint16) bool

IsCommandDuplicate returns true if ver and command ID are used elsewhere when this function is called

func IsEncryptionEnabled

func IsEncryptionEnabled() bool

IsEncryptionEnabled returns true if encryption/decryption is enabled

func IsP2PEnabled

func IsP2PEnabled() bool

IsP2PEnabled returns true if P2P is enabled by the configuration

func Offline

func Offline()

Offline flag the serve to be offline to be deleted and will not take new users

func OnDisconnect

func OnDisconnect(callback func(string, string, string), last bool)

OnDisconnect Registers a callback function on RUDP disconnect

func OnEcho

func OnEcho(handler func(*State, func(error)))

OnEcho Registers a handler to be executed on echo

func OnNewConnection

func OnNewConnection(callback func(string) bool)

OnNewConnection [INTERNAL USE ONLY]

func Online

func Online()

Online flag the server to be online to take new users

func SendPingReply

func SendPingReply(addr net.Addr) error

SendPingReply sends a Ping Reply packet to the given address

func SendPingTry

func SendPingTry(addr net.Addr) error

SendPingTry sends a Ping Try packet to the given address

func SendTo

func SendTo(packets []byte, addr net.Addr) error

SendTo sends a packet to the client without a connection

func SendToWithHook

func SendToWithHook(addr net.Addr, packets []byte, hook func(*State, []byte) ([]byte, error)) error

SendToWithHook sends a packet to passed address with a hook Note: callback does not return state.

func SendWithHook

func SendWithHook(state *State, packets []byte, hook func(*State, []byte) ([]byte, error)) error

SendWithHook sends a packet to the client with a hook

func SetAddrByPingTryUser

func SetAddrByPingTryUser(sid string, addr string)

SetAddrByPingTryUser sets the public IP address of the user who tries to hole punch

func SetOnPingReply

func SetOnPingReply(ttl int, addr string, callback func(error))

SetOnPingReply sets a callback when the server receives a hole punch ack

func SetPublicEndPoint

func SetPublicEndPoint(addr string)

SetPublicEndPoint sets public end point to be used by the client to connect

func Setup

func Setup(confpath string)

Setup [INTERNAL USE ONLY] Loads a configuration file into memory - pass an empty string to load nothing

func ShutdownUDP

func ShutdownUDP()

ShutdownUDP Stops UDP server

func Taken

func Taken()

Taken flag the server to be taken and will not take new users

type Connection

Connection RUDP connection struct

type Connection struct {
    sync.RWMutex
    // contains filtered or unexported fields
}

type State

State Data structure of a client

type State struct {
    // UserID is set on decryptUDPPacket of user on every in coming packet
    UserID        string
    SID           string
    ClientAddr    string
    ClientNetAddr net.Addr
    Version       uint8
    CommandID     uint16
    Payload       []byte
    // copied from user package - encryption keys
    EncryptionKey    []byte
    EncryptionIV     []byte
    EncryptionMacKey []byte
    ClientKey        string
    P2PEnabled       bool
    // byte array of the client local address list (packet.StringListToBytes)
    ClientLocalAddrListBytes []byte
    // contains filtered or unexported fields
}

func (*State) Disconnect

func (state *State) Disconnect()

Disconnect Disconnects UDP client (RUDP) from the server

func (*State) GetClientAddr

func (state *State) GetClientAddr() string

GetClientAddr returns client public address. If it is UDP server and enableP2P is false, it returns 0.0.0.0:0 instead.

func (*State) GetConnection

func (state *State) GetConnection() *Connection

GetConnection returns RUDP connection struct

func (*State) GetSeqForDup

func (state *State) GetSeqForDup() uint32

GetSeqForDup returns sequence for packet duplicate detection.

func (*State) Push

func (state *State) Push(ver uint8, cmd uint16, payload []byte) (int, error)

Push Sends a push packet to the client

func (*State) RPush

func (state *State) RPush(conn *Connection, ver uint8, cmd uint16, payload []byte)

RPush Reliable UDP push from the server Same as RSend, we buffer the push packets and combine them into one packet at certain interval

func (*State) RSend

func (state *State) RSend(conn *Connection, ver uint8, cmd uint16, payload []byte, status uint8)

RSend Reliable UDP send with status We turn outs bytes into one RUDP packet when we actually send it it means that all combined packets are considered "ONE" RUDP packet

func (*State) Send

func (state *State) Send(ver uint8, cmd uint16, payload []byte, status uint8) (int, error)

Send Sends a response packet to the client