How To Send TCP Custom Messages

If you implement custom commands on Diarkis server, you may invoke those commands by sending custom commands.

// ver = command version
// cmd = command ID
// payload = message
tcp.Send(ver, cmd, payload);

TCP Client Event - OnConnect

The event is raised when a connection is successfully established.

OnException is raised if the connection establishment fails.

tcp.OnCreate += OnTcpCreate;
private void OnTcpCreate( bool reconnected)
{
    // A connection is successfully established. Now you may interactive wit the server cluster
}

TCP Client Event - OnException

The event is raised when an exception is caught internally or the Connect method fails.

tcp.OnException += OnTcpException;
private void OnTcpException( int errorCode, string errorMessage)
{
    // Handle the exception here
}

TCP Client Event - OnHeartbeat

TCP client sends and receives a heartbeat packet at a certain interval (Default every 5 seconds) to keep the connection “alive”. The event is raised when the client receives a response back from the server cluster.

tcp.OnHeartbeat += OnTcpHeartbeat;
private void OnTcpHeartbeat()
{
    // Do something if you would like to
}

TCP Client Event - OnOffline

The event is raised when the connected server is preparing to go offline. The client is recommended to terminate the current connection and re-establish a new connection when this event is raised.

tcp.OnOffline += OnTcpOffline;
tcp.OnDisconnect += OnTcpDisconnect;
private void OnTcpOffline()
{
// We set a flag, so we can perform re-connection in OnDisconnect event listener
reconnectFlag = true;
// This is raise OnDisconnect event
tcp.Disconnect();
}
// bool reconnecting is set to true when you use tcp.Reconnect()
private void OnTcpDisconnect( bool reconnecting)
{
if (reconnectFlag)
{
    // Call the application server to indirectly obtain a new endpoint here
    // Assuming we have the new endpoint, we proceed with reconnect
    tcp.SetEncryptionKeys(newSid, newKey, newIv, newMacKey);
    tcp.Connect(newAddr, newPort);
}
}

TCP Event - OnDisconnect

The event is raised when the client successfully disconnects from Diarkis server cluster.

tcp.OnDisconnect += OnTcpDisconnect;
private void OnTcpDisconnect( bool reconnecting)
{
// bool reconnecting is set to true only when tcp.Reconnect() is used.
}

TCP Event - OnPush

The event is raised when the client receives a packet as a push (packets that are not requested by the client) from Diarkis server cluster.

tcp.OnPush += OnTcpPush;
private void OnTcpPush( uint ver, uint cmd, byte[] payload)
{
      // Consume payload accordingly using uint ver and cmd
      switch(ver)
      {
            case MY_CUSTOM_CMD_VER1:
                  HandleCustomCmdVer1(cmd, payload);
            default:
                  // Unknown command version... :(
            break;
      }
}
private void HandleCustomCmdVer1( uint cmd, byte[] payload)
{
      switch(cmd)
      {
            case CMD1:
                  HandleCmd1(payload);
            break;
      }
}

Last updated