Handler & Payload Registry

Register Payload

User has to register Serialize & De-serialize functions of Payloads that defines how payloads get serialized while sending Network Message and get de-serialized while receiving Network Messages. Below is the sample code to register payload.

   /// <summary>
    /// Operation serialized/deserialized using build-in serializer
    /// </summary>
    public class PingOperation 
    {
        private const ushort MsgId = (ushort)MessageId.Ping;

        // properties
        public int pingCount;

        public static void Register()
        {
            FN.RegisterPayload((ushort)MsgId, Serialize, Deserialize);
        }

        public static object Deserialize(ArraySegment<byte> buffer)
        {
            var data = BitBufferPool.GetInstance();
            data.FromArray(buffer);

            var _payload = Acquire();      // release it after use [see PingCountHandler.cs ln 21]
            _payload.pingCount = data.ReadInt();
            
            data.Clear();
            return _payload;     
        }

        public static ArraySegment<byte> Serialize(object pingOperation)
        {
            var op = pingOperation as PingOperation;
            var data = BitBufferPool.GetInstance();
            data.Clear();
            data.AddInt(op.pingCount);
            return data.ToArray();
        }

Register Handler

Handlers are object oriented way to handle incoming network messages.

Create an Handler

Register a handler

Trigger HandleMessage method of handlers with this syntax in OnNetworkReceive method of ISocketListener of IClientSocket or IServerSocket

Last updated

Was this helpful?