Diarkis C# Client SDK
Public Member Functions | Static Public Attributes | Protected Member Functions | Events | List of all members
Diarkis.Tcp Class Reference
Inheritance diagram for Diarkis.Tcp:
Diarkis.Core

Public Member Functions

delegate void HeartbeatHandler ()
 
delegate void OfflineHandler ()
 
delegate void ExceptionHandler (int errCode, string errMsg)
 
delegate void ConnectHandler (bool reconnected)
 
delegate void DisconnectHandler (bool reconnecting)
 
delegate void PushHandler (uint ver, uint cmd, byte[] payload, bool notused)
 
delegate void ResponseHandler (uint ver, uint cmd, uint status, byte[] payload, bool notused)
 
delegate void ResetHandler ()
 
 Tcp (int heartbeatInterval=5000)
 Initializes a new instance of Tcp class. More...
 
void SetNoDelay (bool noDelay)
 Disables Nagle's algorithm of TCP if noDelay is set to true. Disabling Nable's algorithm will change the behavior of TCP not to use buffer when sending packets giving it less delay, of ppacket delivery, but comes with a cost of higher CPU usage. More...
 
void SetEncryptionKeys (byte[] sid, byte[] key, byte[] iv, byte[] mackey)
 Sets up encryption and decryption of packets. More...
 
void SetClientKey (string clientKey)
 Sets up client key and send it to the server on connection. More...
 
string GetServerEndPoint ()
 Returns the server endpoint that the client is conencted to as a string. More...
 
bool IsConnected ()
 Returns true if the client is connected to the server. More...
 
void Connect (string addr, int port)
 Attemps to connect to the server. Tcp.OnConnect will be invoked if connection is successfully made. More...
 
void ConnectDualMode (string addr, int port)
 Attemps to connect to the server using socket dual mode (explicit support for IPv6). Tcp.OnConnect will be invoked if connection is successfully made. More...
 
void Reset ()
 Remove all event listeners and triggers an even Tcp.OnReset. This method MUST be called from the main thread ONLY. More...
 
void CatchOnReconnect (Action action, uint ver, uint cmd)
 Registers a callback function to be triggered when the client is re-connected and a command response of ver and cmd is returned. Unlike other events, you must use this just before invoking a method that would trigger this event. This event is raised only once. You must call this again if you need this event again. More...
 
void Reconnect (string tcpAddr, int tcpPort)
 IMPORTANT: This is internal use ONLY. Reconnects to the server. The server MUST be different from the one the client is currently connected to. More...
 
void Disconnect ()
 Disconnects from the server the client is connected to and triggers Tcp.OnDisconnect. OnDisconnect event will be raised when Disconnect is succuessful. More...
 
void Send (uint ver, uint cmd, byte[] payload)
 Sends a packet as a command to the server. More...
 
void Send (uint ver, uint cmd, byte[] payload, int payloadLength)
 Sends a packet as a command to the server. More...
 
void Heartbeat ()
 Immediately sends a heartbeat packet to the server. Heartbeat is invoked internally at a fixed interval. By default the interval is 5 seconds. More...
 
void Migrate ()
 Migrate to a new server node. Useful when handling OnOffline event. The current server session and encryption keys will be securely carried over. OnConnect event with reconnected = true will be raised. NOTE: This method does not require Auth from Diarkis HTTP server. More...
 
void Migrate (string serverType)
 Migrate to a new server node. Useful when handling OnOffline event. The current server session and encryption keys will be securely carried over. OnConnect event with reconnected = true will be raised New server node is decided within the server type given. NOTE: This method does not require Auth from Diarkis HTTP server. More...
 
void Update ()
 Must be called a certain interval from the main thread. This must be kept until termination of the application. For Unity application, this must be called in Update. More...
 
- Public Member Functions inherited from Diarkis.Core
void Dispose ()
 

Static Public Attributes

const uint STATUS_OK = 1
 Command response status that indicates command execution on the server was successful. More...
 
const uint STATUS_BAD = 4
 Command response status that indicates command execution on the server failed. The error indicates the cause of the failure is client inputs. More...
 
const uint STATUS_ERR = 5
 Command response status that indicates command execution on the server failed. The error indicates the cause of the failure is the server. More...
 

Protected Member Functions

override void Dispose (bool disposing)
 
- Protected Member Functions inherited from Diarkis.Core
void NewTCP (string addr, int port)
 
void NewTCPDualMode (string addr, int port)
 
void ConnectTCP (string addr, int port)
 
void CloseTCP ()
 
NetworkStream GetTCPStream ()
 
void SetTCPNoDelay (bool noDelay)
 
bool IsTCPConnected ()
 
int GetTCPAvailable ()
 
void ConnectUDP (string addr, int port)
 
void ConnectUDPDualMode (string addr, int port)
 
void CloseUDP ()
 
bool IsUDPConnected ()
 
bool UDPPolling (int pollingTime)
 
int GetUDPAvailable ()
 
int UDPRcvFrom (byte[] buff, ref EndPoint ep)
 
void SendUDP (byte[] msg, int length)
 
void SendUDPTo (byte[] msg, int length, IPEndPoint ip)
 
IPEndPoint GetRemoteIP ()
 
EndPoint GetConnetedUDPEndPoint ()
 
IPAddress GetIPAddress_ (string addr, int port)
 

Events

HeartbeatHandler OnHeartbeat
 The event raised when a response of HeartBeat. More...
 
OfflineHandler OnOffline
 The event raised when the connected server is ready to shutdown. The client is recommanded to either re-connect or disconnect. More...
 
ExceptionHandler OnException
 The event raised when a socket exception is caught. More...
 
ConnectHandler OnConnect
 The event raised when successful connection to the server is made by . The argument reconnecting indicates either re-connect or connect. More...
 
DisconnectHandler OnDisconnect
 The event raised when disconnect from the server has been completed. More...
 
PushHandler OnPush
 The event raised when server push has been received. More...
 
ResponseHandler OnResponse
 The event raised when a server response has been received. More...
 
ResetHandler OnReset
 The event raised when is invoked to reset all event listeners. This event is meant for internal use. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Diarkis.Core
static string GetVersion ()
 
- Protected Attributes inherited from Diarkis.Core
bool IsDualMode = false
 

Constructor & Destructor Documentation

◆ Tcp()

Diarkis.Tcp.Tcp ( int  heartbeatInterval = 5000)
inline

Initializes a new instance of Tcp class.

Parameters
heartbeatIntervalInterval in milliseconds to send "heartbeat" packet to server. Default is 5000 milliseconds. If you change heartbeatInterval, you also need to change the configuration for the server accordingly.

Member Function Documentation

◆ CatchOnReconnect()

void Diarkis.Tcp.CatchOnReconnect ( Action  action,
uint  ver,
uint  cmd 
)
inline

Registers a callback function to be triggered when the client is re-connected and a command response of ver and cmd is returned. Unlike other events, you must use this just before invoking a method that would trigger this event. This event is raised only once. You must call this again if you need this event again.

Parameters
actionCallback function.
verCommand version of response to trigger the event
cmdCommand ID of response to trigger the event

◆ Connect()

void Diarkis.Tcp.Connect ( string  addr,
int  port 
)
inline

Attemps to connect to the server. Tcp.OnConnect will be invoked if connection is successfully made.

Parameters
addrTCP server address (hostname) to connect to.
portTCP server port to connect to.

◆ ConnectDualMode()

void Diarkis.Tcp.ConnectDualMode ( string  addr,
int  port 
)
inline

Attemps to connect to the server using socket dual mode (explicit support for IPv6). Tcp.OnConnect will be invoked if connection is successfully made.

Parameters
addrTCP server address (hostname) to connect to.
portTCP server port to connect to.

◆ Disconnect()

void Diarkis.Tcp.Disconnect ( )
inline

Disconnects from the server the client is connected to and triggers Tcp.OnDisconnect. OnDisconnect event will be raised when Disconnect is succuessful.

◆ GetServerEndPoint()

string Diarkis.Tcp.GetServerEndPoint ( )
inline

Returns the server endpoint that the client is conencted to as a string.

◆ Heartbeat()

void Diarkis.Tcp.Heartbeat ( )
inline

Immediately sends a heartbeat packet to the server. Heartbeat is invoked internally at a fixed interval. By default the interval is 5 seconds.

◆ IsConnected()

bool Diarkis.Tcp.IsConnected ( )
inline

Returns true if the client is connected to the server.

◆ Migrate() [1/2]

void Diarkis.Tcp.Migrate ( )
inline

Migrate to a new server node. Useful when handling OnOffline event. The current server session and encryption keys will be securely carried over. OnConnect event with reconnected = true will be raised. NOTE: This method does not require Auth from Diarkis HTTP server.

◆ Migrate() [2/2]

void Diarkis.Tcp.Migrate ( string  serverType)
inline

Migrate to a new server node. Useful when handling OnOffline event. The current server session and encryption keys will be securely carried over. OnConnect event with reconnected = true will be raised New server node is decided within the server type given. NOTE: This method does not require Auth from Diarkis HTTP server.

◆ Reconnect()

void Diarkis.Tcp.Reconnect ( string  tcpAddr,
int  tcpPort 
)
inline

IMPORTANT: This is internal use ONLY. Reconnects to the server. The server MUST be different from the one the client is currently connected to.

Parameters
tcpAddrTCP server address (hostname) to connect to.
tcpPortTCP server port to connect to.

◆ Reset()

void Diarkis.Tcp.Reset ( )
inline

Remove all event listeners and triggers an even Tcp.OnReset. This method MUST be called from the main thread ONLY.

◆ Send() [1/2]

void Diarkis.Tcp.Send ( uint  ver,
uint  cmd,
byte[]  payload 
)
inline

Sends a packet as a command to the server.

Parameters
verCommand version that is defined on the server.
cmdCommand ID that is defined on the server.
payloadCommand payload.

◆ Send() [2/2]

void Diarkis.Tcp.Send ( uint  ver,
uint  cmd,
byte[]  payload,
int  payloadLength 
)
inline

Sends a packet as a command to the server.

Parameters
verCommand version that is defined on the server.
cmdCommand ID that is defined on the server.
payloadCommand payload.
payloadLengthManually given command payload length.

◆ SetClientKey()

void Diarkis.Tcp.SetClientKey ( string  clientKey)
inline

Sets up client key and send it to the server on connection.

Parameters
clientKeyClient key to be sent to the server. Client key must match on the server or connection will be terminated.

◆ SetEncryptionKeys()

void Diarkis.Tcp.SetEncryptionKeys ( byte[]  sid,
byte[]  key,
byte[]  iv,
byte[]  mackey 
)
inline

Sets up encryption and decryption of packets.

Parameters
sidSession ID that is valid until connection is disconnected.
keyEncryption key.
ivEncryption initialization vector (IV).
mackeyMac key used sign encrypted payload of a packet.

◆ SetNoDelay()

void Diarkis.Tcp.SetNoDelay ( bool  noDelay)
inline

Disables Nagle's algorithm of TCP if noDelay is set to true. Disabling Nable's algorithm will change the behavior of TCP not to use buffer when sending packets giving it less delay, of ppacket delivery, but comes with a cost of higher CPU usage.

Parameters
noDelayDisables Nagle's algorithm if true.

◆ Update()

void Diarkis.Tcp.Update ( )
inline

Must be called a certain interval from the main thread. This must be kept until termination of the application. For Unity application, this must be called in Update.

Member Data Documentation

◆ STATUS_BAD

const uint Diarkis.Tcp.STATUS_BAD = 4
static

Command response status that indicates command execution on the server failed. The error indicates the cause of the failure is client inputs.

◆ STATUS_ERR

const uint Diarkis.Tcp.STATUS_ERR = 5
static

Command response status that indicates command execution on the server failed. The error indicates the cause of the failure is the server.

◆ STATUS_OK

const uint Diarkis.Tcp.STATUS_OK = 1
static

Command response status that indicates command execution on the server was successful.

Event Documentation

◆ OnConnect

ConnectHandler Diarkis.Tcp.OnConnect

The event raised when successful connection to the server is made by . The argument reconnecting indicates either re-connect or connect.

◆ OnDisconnect

DisconnectHandler Diarkis.Tcp.OnDisconnect

The event raised when disconnect from the server has been completed.

◆ OnException

ExceptionHandler Diarkis.Tcp.OnException

The event raised when a socket exception is caught.

◆ OnHeartbeat

HeartbeatHandler Diarkis.Tcp.OnHeartbeat

The event raised when a response of HeartBeat.

◆ OnOffline

OfflineHandler Diarkis.Tcp.OnOffline

The event raised when the connected server is ready to shutdown. The client is recommanded to either re-connect or disconnect.

◆ OnPush

PushHandler Diarkis.Tcp.OnPush

The event raised when server push has been received.

◆ OnReset

ResetHandler Diarkis.Tcp.OnReset

The event raised when is invoked to reset all event listeners. This event is meant for internal use.

◆ OnResponse

ResponseHandler Diarkis.Tcp.OnResponse

The event raised when a server response has been received.


The documentation for this class was generated from the following file: