Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Linq;
- using System.Text;
- namespace PartyZ1Packet
- {
- public enum PacketType : byte
- {
- Request, Response
- }
- public enum PacketRequestCommand : byte
- {
- RequestError, CreateChannel, JoinChannel, LeaveChannel, UpdatePosition, UpdateIcon, CreatePOI, DeletePOI
- }
- public enum PacketResponseCommand : byte
- {
- ResponseError, Welcome, ChannelCreated, JoinedChannel, LeftChannel
- }
- //General Packets
- public class GeneralPacket
- {
- public PacketType packetType;
- public int packetDataSize;
- public byte[] packetData;
- //Constructor
- public GeneralPacket()
- {
- }
- public GeneralPacket(ref byte[] serializedData)
- {
- GeneralPacket generalPacket = new GeneralPacket();
- GeneralPacket.DeserializeData(ref serializedData, ref generalPacket);
- packetType = generalPacket.packetType;
- packetDataSize = generalPacket.packetDataSize;
- packetData = generalPacket.packetData;
- }
- //Serialization / Deserialisation
- public byte[] SerializeData()
- {
- byte[] serializedData = new Byte[sizeof(PacketType)];
- serializedData[0] = (byte)packetType;
- Array.Resize(ref serializedData, serializedData.Length + sizeof(int));
- Array.Copy(BitConverter.GetBytes(packetData.Length), 0, serializedData, 1, sizeof(int));
- Array.Resize(ref serializedData, serializedData.Length + packetData.Length);
- Array.Copy(packetData, 0, serializedData, sizeof(PacketType) + sizeof(int), packetData.Length);
- return serializedData;
- }
- static public bool DeserializeData(ref byte[] serializedData, ref GeneralPacket generalPacket)
- {
- try
- {
- generalPacket = new GeneralPacket();
- generalPacket.packetType = (PacketType)serializedData[0];
- byte[] byteArray = new byte[4];
- Array.Copy(serializedData, 1, byteArray, 0, sizeof(int));
- generalPacket.packetDataSize = BitConverter.ToInt32(byteArray, 0);
- generalPacket.packetData = new byte[generalPacket.packetDataSize];
- Array.Copy(serializedData, sizeof(byte) + sizeof(int), generalPacket.packetData, 0, generalPacket.packetDataSize);
- return true;
- }
- catch
- {
- return false;
- }
- }
- }
- public class RequestPacket
- {
- public PacketRequestCommand packetRequestCommand;
- public int packetDataSize;
- public byte[] packetData;
- //Constructor
- public RequestPacket()
- {
- }
- public RequestPacket(ref byte[] serializedData)
- {
- RequestPacket requestPacket = new RequestPacket();
- RequestPacket.DeserializeData(ref serializedData, ref requestPacket);
- packetRequestCommand = requestPacket.packetRequestCommand;
- packetDataSize = requestPacket.packetDataSize;
- packetData = requestPacket.packetData;
- }
- //Serialization / Deserialisation
- public byte[] SerializeData()
- {
- byte[] serializedData = new Byte[sizeof(PacketRequestCommand)];
- serializedData[0] = (byte)packetRequestCommand;
- Array.Resize(ref serializedData, serializedData.Length + sizeof(int));
- Array.Copy(BitConverter.GetBytes(packetData.Length), 0, serializedData, 1, sizeof(int));
- Array.Resize(ref serializedData, serializedData.Length + packetData.Length);
- Array.Copy(packetData, 0, serializedData, sizeof(PacketType) + sizeof(int), packetData.Length);
- return serializedData;
- }
- static public bool DeserializeData(ref byte[] serializedData, ref RequestPacket requestPacket)
- {
- try
- {
- requestPacket = new RequestPacket();
- requestPacket.packetRequestCommand = (PacketRequestCommand)serializedData[0];
- byte[] byteArray = new byte[4];
- Array.Copy(serializedData, 1, byteArray, 0, sizeof(int));
- requestPacket.packetDataSize = BitConverter.ToInt32(byteArray, 0);
- requestPacket.packetData = new byte[requestPacket.packetDataSize];
- Array.Copy(serializedData, sizeof(byte) + sizeof(int), requestPacket.packetData, 0, requestPacket.packetDataSize);
- return true;
- }
- catch
- {
- return false;
- }
- }
- }
- public class ResponsePacket
- {
- public PacketResponseCommand packetResponseCommand;
- public int packetDataSize;
- public byte[] packetData;
- //Constructor
- public ResponsePacket()
- {
- }
- public ResponsePacket(ref byte[] serializedData)
- {
- ResponsePacket responsePacket = new ResponsePacket();
- ResponsePacket.DeserializeData(ref serializedData, ref responsePacket);
- packetResponseCommand = responsePacket.packetResponseCommand;
- packetDataSize = responsePacket.packetDataSize;
- packetData = responsePacket.packetData;
- }
- //Serialization / Deserialisation
- public byte[] SerializeData()
- {
- byte[] serializedData = new Byte[sizeof(PacketResponseCommand)];
- serializedData[0] = (byte)packetResponseCommand;
- Array.Resize(ref serializedData, serializedData.Length + sizeof(int));
- Array.Copy(BitConverter.GetBytes(packetData.Length), 0, serializedData, 1, sizeof(int));
- Array.Resize(ref serializedData, serializedData.Length + packetData.Length);
- Array.Copy(packetData, 0, serializedData, sizeof(PacketType) + sizeof(int), packetData.Length);
- return serializedData;
- }
- static public bool DeserializeData(ref byte[] serializedData, ref ResponsePacket responsePacket)
- {
- try
- {
- responsePacket = new ResponsePacket();
- responsePacket.packetResponseCommand = (PacketResponseCommand)serializedData[0];
- byte[] byteArray = new byte[4];
- Array.Copy(serializedData, 1, byteArray, 0, sizeof(int));
- responsePacket.packetDataSize = BitConverter.ToInt32(byteArray, 0);
- responsePacket.packetData = new byte[responsePacket.packetDataSize];
- Array.Copy(serializedData, sizeof(byte) + sizeof(int), responsePacket.packetData, 0, responsePacket.packetDataSize);
- return true;
- }
- catch
- {
- return false;
- }
- }
- }
- //Specific Packets
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement