How to implement a message type (PayloadData)?


A packet message encapsulates the desired data by means of a class that inherits PayloadData. PayloadData represents a communication protocol and must contain all necessary methods to convert data to and from bytes.

There are some rules to create a PayloadData class. Firstly, this class must define a static PayloadInfo that describes some protocol meta-atributes: reserved bandwidth (aka PacketWeight) and priority level (PacketPriority). Below there is a code snippet showing how to implement a protocol to send one string value:

publicclass DebugData : IPayloadData 
    { 
        privatestatic PayloadInfo debugPacketInfo = new PayloadInfo () 
        { 
            Id = 0, 
            PacketWeight = 100, 
            PacketPriority = 1, 
            GeneratePayload = ( byte[] data) => 
                { 
                    returnnew DebugData(data); 
                } 
        }; 
 
        publicstaticint Id { get { return debugPacketInfo.Id; } } 
 
        privatestring m_debugMessage; 
        publicstring DebugMessage { get { return m_debugMessage; } } 
 
        public DebugData( string message) 
        { 
            m_debugMessage = message; 
        } 
 
        public DebugData( byte[] data) 
        { 
            m_debugMessage = Util.GetString(data); 
        } 
 
        publicoverridebyte[] ToBytes() 
        { 
            return Util.GetBytes(m_debugMessage); 
        } 
 
        publicoverride PayloadInfo GetInfo() 
        { 
            return debugPacketInfo; 
        } 
    } 

The following utility class shows how to convert string to bytes and bytes to string:

publicclass Util 
    { 
        ///<summary> 
        /// Convert to bytes from string 
        ///</summary> 
        ///<param name="str">string value</param> 
        ///<returns>bytes representing a string</returns> 
        publicstaticbyte[] GetBytes( string str) 
        { 
            byte[] bytes = newbyte[str.Length * sizeof( char)]; 
            System. Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); 
            return bytes; 
        } 
 
        ///<summary> 
        /// Convert to string from bytes 
        ///</summary> 
        ///<param name="bytes">bytes representing a string</param> 
        ///<param name="byteSize">size corresponding to string bytes</param> 
       /// <param name=”startPos”>initial position to string content</param> 
        ///<returns>string from bytes</returns> 
        publicstaticstring GetString( byte[] bytes, int byteSize = -1, int startPos = 0) 
        { 
            if (byteSize == -1) 
            { 
                byteSize = bytes.Length; 
            } 
 
            char[] chars = newchar[byteSize / sizeof( char)]; 
            System. Buffer.BlockCopy(bytes, startPos, chars, 0, byteSize); 
            returnnewstring(chars); 
        } 
    } 

Writing an extension method is a straightforward way to instantiate a protocol object. Such method allows to send data packets and to configure all desired values as showed in the code snippet bellow:

publicstaticclass DebugDataFactoryExtensions 
    { 
        publicstatic DataPacket GetDebugPacket(this DataPacketFactory factory, string message) 
        { 
            DebugData data = new DebugData(message); 
            returnnew DataPacket() 
            { 
                MessageType = DebugData.Id, 
                DataSize = data.ToBytes().Length, 
                Payload = data 
            }; 
        } 
    } 

Thus, all code needed to send a message could be written in a single line:
DataPacketFactory.Singleton.GetDebugPacket(message).Send(); 

A requirement of the usage of custom protocols is setup all protocols in every communication endpoint by means of the method setupProtocols of DataPacketFactory class:

DataPacketFactory.Singleton.SetupProtocols(new PayloadInfo [] { 
                new DebugData( "Teste").GetInfo() 
            }); 

Last edited Jun 26, 2014 at 2:06 PM by mrgil, version 4