-
Notifications
You must be signed in to change notification settings - Fork 27
PacketSerializer
juhgiyo edited this page Oct 26, 2014
·
5 revisions
- Serialize the instance of a serializable class to byte array.
- Deserialize the byte array to given class.
// Serializable Class example
[Serializable]
public class SomeData: ISerializable
{
public int packetCode;
public long data;
public SomeData()
{}
public SomeData(int iPacketcode, long iData)
{
packetCode= iPacketcode;
data= iData;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("packetCode", packetCode, typeof(int));
info.AddValue("data", data,typeof(long));
}
public SomeData(SerializationInfo info, StreamingContext context)
{
packetCode = (int)info.GetValue("packetCode", typeof(int));
data = (long)info.GetValue("data",typeof(long));
}
};
...
// Class Serializing example
PacketSerializer<SomeData> serializer =new PacketSerializer<SomeData>(new SomeData(1,10));
byte[] byetarr=serializer.GetPacketRaw();
...
// Class Deserialize example
void OnReceived(INetworkSocket socekt, Packet receivedPacket)
{
PacketSerializer<SomeData> serializer = new PacketSerializer<SomeData>(receivedPacket.GetPacket(), 0, receivedPacketByteSize());
SomeData someData = serializer.GetPacket();
Debug.Log(someData.packetCode+" " + someData.data);
}