...

Package server

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

Overview ▾

Package server ▷

Server - UDP/RUDP TCP WebSocket Server

Sets up a server as either UDP/RUDP, TCP, or WebSocket server.

Configurations

UDP/RUDP, TCP, WebSocket, and Connector configurations are explained below.

UDP and RUDP Configurations

UDP/RUDP configurations detail

{
  "enableP2P": true,
  "address": "127.0.0.1",
  "nic": "eth0",
  "port": "7000",
  "connectionTTL": 10,
  "sendUDPInterval": 100,
  "handleRUDPInterval": 100,
  "rcvWorkers": 1,
  "retryInterval": 1000,
  "maxRetry": 10,
  "enableEncryption": true
}

▶︎ Configuration Values

enableP2P - Default is true

Enables UDP client to use P2P. Default is true.

address - Default is "127.0.0.1"

Address for UDP server to bind with.

nic - Default is "eth0"

Name of a network interface to retrieve address from.

port - Default is "7000"

Port for UDP/RUDP server to bind with. Starts from the given port and scans to find an open poet to bind.

connectionTTL - Default is 10

RUDP connection TTL in seconds. Default is 10 seconds.

sendUDPInterval - Default is 100

UDP packet send interval in milliseconds. If set to lower than 10, outbound packets will not be buffered - No delay but higher CPU load.

handleRUDPInterval - Default is 100

RUDP packet send/receive interval in milliseconds.

rcvWorkers - Default is 1

Number of UDP packet receiver GoRoutines. Default is the number of CPU cores.

retryInterval - Default is 1000

RUDP packet retry interval in milliseconds.

maxRetry - Default is 10

RUDP packet max retry attempts. When it exceeds this value, RUDP connection is considered timeout and discard.

enableEncryption - Default is true

Default is true, but if you set this to false, HTTP must set this to false also.

IMPORTANT - If you are using Docker, you need to "use" external IP address of Docker.

NOTE - You may use either address or nic in your configurations. If none is used, the default is localhost.

NOTE - rcvWorker cannot be higher than the available CPU cores of the server. More workers, you have the better in bound throughput, and higher CPU load.

TCP Configurations

TCP configurations detail

{
  "connectionTTL": 10,
  "address": "127.0.0.1",
  "nic": "eth0",
  "port": "7000",
  "maxRcvSize": 8000,
  "noDelay": false,
  "enableEncryption": true
}

▶︎ Configuration Values

connectionTTL - Default is 10

Connection TTL in seconds. Default is 10 seconds.

address - Default is "127.0.0.1"

Address for TCP server to bind with.

nic - Default is "eth0"

Name of a network interface to retrieve address from.

port - Default is "7000"

Port for TCP server to bind with. Starts from the given port and scans to find the open port to bind.

maxRcvSize - Default is 8000

Maximum TCP packet size of each request packet in bytes.

noDelay - Default is false

If true, it disables Nagle's algorithm.

enableEncryption - Default is true

Default is true, but if you set this to false, HTTP must set this to false also.

IMPORTANT - If you are using Docker, you need to "use" external IP address of Docker.

NOTE - You may use either address or nic in your configurations. If none is used, the default is localhost.

WebSocket Configurations

WebSocket configurations detail.

{
  "address": "127.0.0.1",
  "nic": "eth0",
  "port": "7000",
  "uri": "/ws",
  "readInterval": 400,
  "maxRcvSize": 8000,
  "ttl": 30,
  "allowOrigin": "*"
}

▶︎ Configuration Values

address - Default is "127.0.0.1"

Address for WebSocket server to bind with.

nic - Default is "eth0"

Name of a network interface to retrieve address from.

port - Default is "7000"

Port for WebSocket server to bind with. Starts with the given port and scans to find an open port to bind.

uri - Default is "/ws"

URI of the WebSocket communication from the client. Default is "/ws".

readInterval - Default is 400

Interval of server reading incoming packets in milliseconds. Default is 400.

maxRcvSize - Default is 8000

Maximum WebSocket packet size of each request packet in bytes.

ttl - Default is 30

WebSocket connection TTL in seconds.

IMPORTANT - WebSocket server does NOT provide built-in encryption/decryption like other protocols. Please be sure to use encryption/decryption such as SSL etc.

NOTE - Diarkis comes with jacascript client SDK for WebSocket.

allowOrigin - Default is disabled

Enables CROS.

Constants

Bad Send status for server response.

const Bad = 4

Err Send status for server response.

const Err = 5

Ok Send status for server response.

const Ok = 1

TCPType a type of TCP server used by mesh module

const TCPType = tcp.Type

UDPType a type of UDP server used by mesh module

const UDPType = udp.Type

WSType a type of WebSocket server used by mesh module

const WSType = ws.Type

func Command

func Command(ver uint8, cmd uint16, handler func(*user.User, func(error)))

Command [WebSocket ONLY] use HandleCommand instead for other servers.

ver uint8 - Command version of the handler.

cmd uint16 - Command ID of the handler.

handler func(userData *user.User, next func(error))

Returns an error if it fails to assign a handler.

NOTE: If you assign multiple handles with the same ver and cmd, all handlers will be executed in the order of assignment.

func GetEndPoint

func GetEndPoint() string

GetEndPoint returns the end point address the server is bound with

func GetServerType

func GetServerType() string

GetServerType returns server type as a string

func HandleCommand

func HandleCommand(ver uint8, cmd uint16, handler func(uint8, uint16, []byte, *user.User, func(error))) error

HandleCommand registers a command handler function for TCP and UDP/RUDP server.

ver uint8 - Command version of the handler.

cmd uint16 - Command ID of the handler.

handler func(ver uint8, cmd uint16, payload []byte, userData *user.User, next func(error)) - Handler function to be executed when the server receives the command.

Returns an error if it fails to assign a handler.

NOTE: If you assign multiple handles with the same ver and cmd, all handlers will be executed in the order of assignment.

func HandleWSCommand

func HandleWSCommand(ver float64, cmd float64, handler func(float64, float64, map[string]interface{}, *user.User, func(error))) error

HandleWSCommand registers WebSocket command function.

ver float64 - Command version of the handler.

cmd float64 - Command ID of the handler.

handler func(ver float64, cmd float64, data map[string]interfae{}, userData *user.User, next func(error)) - Handler function to be executed when the server receives the command.

Returns an error if it fails to assign a handler.

NOTE: If you assign multiple handles with the same ver and cmd, all handlers will be executed in the order of assignment.

func HookAllCommands

func HookAllCommands(handler func(*user.User, func(error)))

HookAllCommands Registers a packet handler as a hook to all commands

handler func(userData *user.User, next func(error)) - Function to be invoked on command hook.

NOTE: The second argument next func(error) must be called at the end of handler to move the operation to next.

func IsCommandDuplicate

func IsCommandDuplicate(ver uint8, commandID uint16) bool

IsCommandDuplicate returns true if ver and commandID are used elsewhere when this function is invoked

ver uint8 - Command version to be checked for duplication.

cmd uint16 - Command ID to be checked for duplication.

func IsTCP

func IsTCP() bool

IsTCP Returns true if the server is setup as TCP server

func IsUDP

func IsUDP() bool

IsUDP Returns true if the server is setup as UDP server

func IsWebSocket

func IsWebSocket() bool

IsWebSocket Returns true if the server is setup as WebSocket server

func Offline

func Offline()

Offline Set server status to offline

func OnDisconnect

func OnDisconnect(callback func(string, *user.User))

OnDisconnect Registers a callback on connection termination for TCP/UDP

func OnKeepAlive

func OnKeepAlive(handler func(*user.User, func(error)))

OnKeepAlive Registers a handler on TCP heartbeat or UDP echo

handler func(userData *user.User, next func(error)) - Function to be invoked on keep alive message from the clients.

NOTE: next func(error) must be called at the end of handler's operation.

func Online

func Online()

Online Set server status to online

func SendPM

func SendPM(nodeAddr string, sid string, ver uint8, cmd uint16, message []byte)

SendPM sends a direct message to another user - The sent private message is a reliable delivery

nodeAddr string - This is the node (server) internal address of the target user.

sid string - This is the sid (session ID) of the target user.

ver uint8 - This is the command version to be used with the message sent.

cmd uint16 - This is the command version to be used with the message sent.

message []byte - This is the message data byte array.

func SetPublicEndPoint

func SetPublicEndPoint(addr string)

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

addr string - This is the public endpoint for the server to be registered and used by the clients.

This is usually used internally.

func SetupAsConnector

func SetupAsConnector(path string)

SetupAsConnector Initialize the server as a Connector server for an external real-time server. This is meant to be used with an external real-time server such as Unreal Engine Dedicated server etc. Diarkis itself will not communicate wit the clients, but focuses on autoscaling and horizontal scaling on K8s.

path string - This is the absolute path of the configuration file to read.

func SetupAsTCPServer

func SetupAsTCPServer(path string)

SetupAsTCPServer Initialize as a TCP server. Load configurations from the file path given. Pass an empty string, if there is no need for configurations.

path string - This is the absolute path of the configuration file to read.

func SetupAsUDPServer

func SetupAsUDPServer(path string)

SetupAsUDPServer Initialize as a UDP server. Load configurations from the file path given. Pass an empty string, if there is no need for configurations.

path string - This is the absolute path of the configuration file to read.

func SetupAsWebSocketServer

func SetupAsWebSocketServer(path string)

SetupAsWebSocketServer Initialize as a WebSocket server. Load configurations from the file path given. The config name is "WS". Pass an empty string, if there is no need for configurations.

path string - This is the absolute path of the configuration file to read.

func SetupServerForAWS

func SetupServerForAWS() error

SetupServerForAWS sets public endpoint in AWS environement Must be called in diarkis.OnReady() as a callback

func SetupServerForAzure

func SetupServerForAzure() error

SetupServerForAzure sets public endpoint in Microsoft Azure environement Must be called in diarkis.OnReady() as a callback

func SetupServerForGCP

func SetupServerForGCP() error

SetupServerForGCP sets public endpoint in GCP environement Must be called in diarkis.OnReady() as a callback

Subdirectories

Name Synopsis
..
connector
http Package http ▷ HTTP - Client entry point and router Routes clients to UDP/RUDP, TCP, and/or, WebSocket servers.
tcp
udp
ws