Package tcp

import "github.com/Diarkis/diarkis/server/tcp"

Overview ▾


Bad Response packet status

const Bad = 4

Err Response packet status

const Err = 5

Ok Response packet status

const Ok = 1

Type Mesh network type for TCP server

const Type = "TCP"

func Command

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

Command Registers a packet handler as a command

func GetConnectionTTL

func GetConnectionTTL() int64

GetConnectionTTL returns connection TTL

func GetEndPoint

func GetEndPoint() string

GetEndPoint Returns TCP server 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 TCP 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 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, error), last bool)

OnDisconnect Registers a callback on connection disconnect

func OnHeartbeat

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

OnHeartbeat Registers a handler to be executed on heartbeat

func OnNewConnection

func OnNewConnection(callback func(*State))

OnNewConnection Registers a callback on new TCP connection w/ a client

func Online

func Online()

Online flag the server to be online to take new users

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 configuration file into memory - pass an empty string to load nothing

func ShutdownTCP

func ShutdownTCP(next func(error))

ShutdownTCP Stops TCP server

func Taken

func Taken()

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

type State

State Client state data structure

type State struct {
    // UserID is set on ecryptTCPPacket of user on every in coming packet
    UserID      string
    SID         string
    Conn        *net.TCPConn
    ClientAddr  string
    TTLDuration int64
    TTL         int64
    Connected   bool
    // overwritten on every command
    Version uint8
    // overwritten on every command
    CommandID uint16
    // payload of a command
    Payload []byte
    // copied from user package - encryption keys
    EncryptionKey    []byte
    EncryptionIV     []byte
    EncryptionMacKey []byte
    ClientKey        string
    // contains filtered or unexported fields

func GetStateByAddr

func GetStateByAddr(addr string) *State

GetStateByAddr Returns a client state struct by client address

func (*State) Disconnect

func (state *State) Disconnect()

Disconnect Disconnects the socket connection to the client

func (*State) Kill

func (state *State) Kill(err error)

Kill Forcefully terminates the socket connection to the client

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) Send

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

Send Sends a response packet to the client