Diarkis C# Client SDK
Classes | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Events | List of all members
Diarkis.Udp Class Reference

UDP client for Diarkis server that handles UDP messaging and RUDP messages. The class receives messages from the server via event callbacks. More...

Inheritance diagram for Diarkis.Udp:
Diarkis.Core

Public Member Functions

delegate void OfflineHandler ()
 
delegate void PacketHandler (byte[] packet, string senderAddress)
 
delegate void ResponseHandler (uint ver, uint cmd, uint status, byte[] payload, bool reliable)
 
delegate void PushHandler (uint ver, uint cmd, byte[] payload, bool reliable)
 
delegate void EchoHandler (double echoTime, string myAddress)
 
delegate void PingHandler (double pingTime, string myAddress)
 
delegate void ConnectHandler (int status, bool reconnected)
 
delegate void DisconnectHandler (bool reconnecting)
 
delegate void ExceptionHandler (int errCode, string errMsg)
 
delegate void ResetHandler ()
 
delegate void AddressChangeHandler (string oldAddr, string newAddr)
 
 Udp (int sendInterval=200, int echoInterval=5000)
 Initializes an instance of . More...
 
void EnableSendImmediate ()
 Enables UDP messages to be sent immediately instead of being bufferred. More...
 
string GetServerEndPoint ()
 Returns the server endpoint that the client is conencted to as a string. More...
 
int GetDisconnectionReason ()
 Returns the cause of disconnection as enum DISCONN_EXPECTED Disconnection was expected and there was no error DISCONN_NO_RESPONSE Could not get a response from the server DISCONN_RETRY_TIMEOUT RUDP retries took too long More...
 
void SetClientKey (string clientKey)
 Sets up client key and send it to the server on connection. More...
 
void SetEchoTimeout (double timeoutInMilliseconds)
 Sets echo timeout in milliseconds. More...
 
void SetEncryptionKeys (byte[] sid, byte[] key, byte[] iv, byte[] mackey)
 Sets up encryption and decryption of packets. More...
 
bool IsConnected ()
 Returns true if the client is connected to the server. More...
 
void Connect (string addr, int port, double timeout=1000)
 Attemps to connect to the server. Udp.OnConnect will be invoked if connection is successfully made. More...
 
void ConnectDualMode (string addr, int port, double timeout=1000)
 Attemps to connect to the server using socket dual mode (explicit support for IPv6) turned on. Udp.OnConnect will be invoked if connection is successfully made. When connected to the server with this method, P2P is disabled. More...
 
void Reset ()
 Remove all event listeners and triggers an even Udp.OnReset. This method MUST be called from the main thread ONLY. More...
 
void CatchOnReconnect (Action< bool > 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 addr, int port)
 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 Udp.OnDisconnect. OnDisconnect event will be raised when Disconnect is successful. More...
 
string GetAddress ()
 Returns the client's external address as a string. This is shared with other client(s) for class for peer-to-peer communication. More...
 
List< string > GetLocalAddress ()
 Returns a list of local address and port More...
 
void RSend (uint ver, uint cmd, byte[] payload)
 Sends a packet as an RUDP (Reliable UDP) packet that is guaranteed its delivery to the server. More...
 
void RSend (uint ver, uint cmd, byte[] payload, int payloadLength)
 Sends a packet as an RUDP (Reliable UDP) packet that is guaranteed its delivery to the server. More...
 
void Send (uint ver, uint cmd, byte[] payload)
 Sends a packet as a plain UDP packet. The packet maybe lost. More...
 
void Send (uint ver, uint cmd, byte[] payload, int payloadLength)
 Sends a packet as a plain UDP packet. The packet maybe lost. More...
 
void Migrate ()
 Migrate to a new server node. Useful when handling OnOffline event. OnConnect event with reconnected = true will be raised 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 Echo ()
 Immediately sends an echo packet. Echo packets are sent as RUDP packets. More...
 
void Ping ()
 Sends a ping packet. Ping packets are sent as UDP packets. Raises OnPing event. If the packet is lost, however, OnPing event will not be raised. More...
 
void Update ()
 Must be called a certain interval from the main thread. For Unity application this must be called in Update. More...
 
void SendTo (byte[] msg, IPEndPoint ip)
 Sends a UDP packet to a specified target IP end point 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...
 
const int MAX_PACKET_SIZE = 1300
 Actual max size of a UDP packet is 65507 but we set the value to be lower. Typically a UDP packet that is beigger than 1400 bytes starts to split increasing the chance of packet loss. More...
 
const int DISCONN_INIT = -1
 Disconnection reason enums. This is the initial reason state. More...
 
const int DISCONN_EXPECTED = 0
 Disconnection reason enums. This indicates the disconnect was intentional and expected. More...
 
const int DISCONN_NO_RESPONSE = 1
 Disconnection reason enums. Indicates that the disconnect was instructed by the server. The server has discarded the client connection state. This usually means that the client was not responsive to the server. More...
 
const int DISCONN_RETRY_TIMEOUT = 2
 Disconnection reason enums. Indicates the RUDP retries have timedout. More...
 
const int DISCONN_FORCED = 3
 Disconnection reason enums. This is not used internally. Reserved for forced disconnection. More...
 
- Static Public Attributes inherited from Diarkis.Core
const int CONN_TIMEOUT = -1
 Status of OnConnect event. This status indicates the connection has timedout. More...
 
const int CONN_ERR = -2
 Status of OnConnect event. This status indicates the failure of opening a TCP socket. More...
 
const int CONN_SUCCESS = 0
 Status of OnConnect event. This status indicates the connection has been successfully established. 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 ()
 
string FindLocalAddress (IPAddress ip)
 
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)
 

Protected Attributes

double EchoGiveUpCount = ECHO_SEND_GIVEUP_COUNT
 The max echo failure allowed before disconnect. More...
 
- Protected Attributes inherited from Diarkis.Core
bool IsDualMode = false
 

Events

OfflineHandler OnOffline
 The event raised when the connected server is ready to shutdown. The client is recommanded to either re-connect or disconnect. More...
 
PacketHandler OnPacket
 The event raised when inbound packet is ready. This event is intended for internal use only. More...
 
ResponseHandler OnResponse
 The event raised when a server response has been received. This event is raised for both reliable and unreliable (UDP and RUDP). More...
 
EchoHandler OnEcho
 The event faised when a response of an echo has been received. The event passes the traveling time of an echo and the client's public address. The client public addess is used for P2P. NOTE: Echo is sent and received as RUDP. More...
 
PingHandler OnPing
 The event raised by a response of a ping has been received. Ping behaves similar to echo, but it is handled as UDP. More...
 
PushHandler OnPush
 The event raised when server push has been received. This event is raised for both reliable and unreliable (UDP and RUDP). 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...
 
ExceptionHandler OnException
 The event raised when a socket exception is caught. More...
 
ResetHandler OnReset
 The event raised when is invoked to reset all event listeners. This event is meant for internal use. More...
 
AddressChangeHandler OnAddressChange
 The event is raised when client public address has changed. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Diarkis.Core
static string GetVersion ()
 

Detailed Description

UDP client for Diarkis server that handles UDP messaging and RUDP messages. The class receives messages from the server via event callbacks.

UDP Packet Structure: Below is the break down of a UDP/RUDP packet data strcuture. Endianesss is Big Endian. Packet size over head:

Header and security data66 byte
Encryption padding0 ~ 15 byte
Payload sizeN byte

UDP/RUDP Packet

SIDMac KeyOriginal Packet SizePaddingEncrypted Data
16 byte32 byte4 byte0 ~ 15 byteN byte

Encrypted Data

SymbolPacket Size + Command VerCommand IDUPD TypePayload
4 byte4 byte2 byte4 byteN byte

Constructor & Destructor Documentation

◆ Udp()

Diarkis.Udp.Udp ( int  sendInterval = 200,
int  echoInterval = 5000 
)
inline

Initializes an instance of .

Parameters
sendIntervalInterval in milliseconds to dictate packets being sent. Default is 200ms. all packets sent within this interval will be combined into one packet. Minimum interval allowed is 10ms. Setting it to 10ms or less will make UDP packets to be sent immediately.
echoIntervalInterval in milliseconds to send an echo packet. Default is 5000ms and if you change this value, you need to change server configuration called connectionTTL accordingly.

Member Function Documentation

◆ CatchOnReconnect()

void Diarkis.Udp.CatchOnReconnect ( Action< bool >  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.Udp.Connect ( string  addr,
int  port,
double  timeout = 1000 
)
inline

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

Parameters
addrUDP server address (hostname) to connect to.
portUDP server port to connect to.
timeoutConnection timeout in milliseconds.

◆ ConnectDualMode()

void Diarkis.Udp.ConnectDualMode ( string  addr,
int  port,
double  timeout = 1000 
)
inline

Attemps to connect to the server using socket dual mode (explicit support for IPv6) turned on. Udp.OnConnect will be invoked if connection is successfully made. When connected to the server with this method, P2P is disabled.

Parameters
addrUDP server address (hostname) to connect to.
portUDP server port to connect to.

◆ Disconnect()

void Diarkis.Udp.Disconnect ( )
inline

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

◆ Echo()

void Diarkis.Udp.Echo ( )
inline

Immediately sends an echo packet. Echo packets are sent as RUDP packets.

◆ EnableSendImmediate()

void Diarkis.Udp.EnableSendImmediate ( )
inline

Enables UDP messages to be sent immediately instead of being bufferred.

◆ GetAddress()

string Diarkis.Udp.GetAddress ( )
inline

Returns the client's external address as a string. This is shared with other client(s) for class for peer-to-peer communication.

◆ GetDisconnectionReason()

int Diarkis.Udp.GetDisconnectionReason ( )
inline

Returns the cause of disconnection as enum DISCONN_EXPECTED Disconnection was expected and there was no error DISCONN_NO_RESPONSE Could not get a response from the server DISCONN_RETRY_TIMEOUT RUDP retries took too long

◆ GetLocalAddress()

List<string> Diarkis.Udp.GetLocalAddress ( )
inline

Returns a list of local address and port

◆ GetServerEndPoint()

string Diarkis.Udp.GetServerEndPoint ( )
inline

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

◆ IsConnected()

bool Diarkis.Udp.IsConnected ( )
inline

Returns true if the client is connected to the server.

◆ Migrate() [1/2]

void Diarkis.Udp.Migrate ( )
inline

Migrate to a new server node. Useful when handling OnOffline event. OnConnect event with reconnected = true will be raised

◆ Migrate() [2/2]

void Diarkis.Udp.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.

◆ Ping()

void Diarkis.Udp.Ping ( )
inline

Sends a ping packet. Ping packets are sent as UDP packets. Raises OnPing event. If the packet is lost, however, OnPing event will not be raised.

◆ Reconnect()

void Diarkis.Udp.Reconnect ( string  addr,
int  port 
)
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
addrUDP server address (hostname) to connect to.
portUDP server port to connect to.

◆ Reset()

void Diarkis.Udp.Reset ( )
inline

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

◆ RSend() [1/2]

void Diarkis.Udp.RSend ( uint  ver,
uint  cmd,
byte[]  payload 
)
inline

Sends a packet as an RUDP (Reliable UDP) packet that is guaranteed its delivery to the server.

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

◆ RSend() [2/2]

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

Sends a packet as an RUDP (Reliable UDP) packet that is guaranteed its delivery 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.

◆ Send() [1/2]

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

Sends a packet as a plain UDP packet. The packet maybe lost.

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

◆ Send() [2/2]

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

Sends a packet as a plain UDP packet. The packet maybe lost.

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

◆ SendTo()

void Diarkis.Udp.SendTo ( byte[]  msg,
IPEndPoint  ip 
)
inline

Sends a UDP packet to a specified target IP end point

◆ SetClientKey()

void Diarkis.Udp.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.

◆ SetEchoTimeout()

void Diarkis.Udp.SetEchoTimeout ( double  timeoutInMilliseconds)
inline

Sets echo timeout in milliseconds.

Parameters
timeoutInMillisecondsEcho timeout in milliseconds.

◆ SetEncryptionKeys()

void Diarkis.Udp.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.

◆ Update()

void Diarkis.Udp.Update ( )
inline

Must be called a certain interval from the main thread. For Unity application this must be called in Update.

Member Data Documentation

◆ DISCONN_EXPECTED

const int Diarkis.Udp.DISCONN_EXPECTED = 0
static

Disconnection reason enums. This indicates the disconnect was intentional and expected.

◆ DISCONN_FORCED

const int Diarkis.Udp.DISCONN_FORCED = 3
static

Disconnection reason enums. This is not used internally. Reserved for forced disconnection.

◆ DISCONN_INIT

const int Diarkis.Udp.DISCONN_INIT = -1
static

Disconnection reason enums. This is the initial reason state.

◆ DISCONN_NO_RESPONSE

const int Diarkis.Udp.DISCONN_NO_RESPONSE = 1
static

Disconnection reason enums. Indicates that the disconnect was instructed by the server. The server has discarded the client connection state. This usually means that the client was not responsive to the server.

◆ DISCONN_RETRY_TIMEOUT

const int Diarkis.Udp.DISCONN_RETRY_TIMEOUT = 2
static

Disconnection reason enums. Indicates the RUDP retries have timedout.

◆ EchoGiveUpCount

double Diarkis.Udp.EchoGiveUpCount = ECHO_SEND_GIVEUP_COUNT
protected

The max echo failure allowed before disconnect.

◆ MAX_PACKET_SIZE

const int Diarkis.Udp.MAX_PACKET_SIZE = 1300
static

Actual max size of a UDP packet is 65507 but we set the value to be lower. Typically a UDP packet that is beigger than 1400 bytes starts to split increasing the chance of packet loss.

◆ STATUS_BAD

const uint Diarkis.Udp.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.Udp.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.Udp.STATUS_OK = 1
static

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

Event Documentation

◆ OnAddressChange

AddressChangeHandler Diarkis.Udp.OnAddressChange

The event is raised when client public address has changed.

◆ OnConnect

ConnectHandler Diarkis.Udp.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.Udp.OnDisconnect

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

◆ OnEcho

EchoHandler Diarkis.Udp.OnEcho

The event faised when a response of an echo has been received. The event passes the traveling time of an echo and the client's public address. The client public addess is used for P2P. NOTE: Echo is sent and received as RUDP.

◆ OnException

ExceptionHandler Diarkis.Udp.OnException

The event raised when a socket exception is caught.

◆ OnOffline

OfflineHandler Diarkis.Udp.OnOffline

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

◆ OnPacket

PacketHandler Diarkis.Udp.OnPacket

The event raised when inbound packet is ready. This event is intended for internal use only.

◆ OnPing

PingHandler Diarkis.Udp.OnPing

The event raised by a response of a ping has been received. Ping behaves similar to echo, but it is handled as UDP.

◆ OnPush

PushHandler Diarkis.Udp.OnPush

The event raised when server push has been received. This event is raised for both reliable and unreliable (UDP and RUDP).

◆ OnReset

ResetHandler Diarkis.Udp.OnReset

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

◆ OnResponse

ResponseHandler Diarkis.Udp.OnResponse

The event raised when a server response has been received. This event is raised for both reliable and unreliable (UDP and RUDP).


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