First thing i would use is a circular buffer, Reasons dont rely on getting all the packets from the client everytime u call recv, u will mostly only get part if the buffer is full, this is why tq prefix there packets with the sizes, so later when you extract the packet from the buffer u know if its all there. Secondly Have a base packet type, i dunno lets go along with Packet wich has some basic functionality i.e type field Build and read functions, and then just have a class for each packet type you want to parse, inheriting from Packet, and call them something along the lines of
Code:
public class Packet
{
public short PacketType;
public virtual void Read(byte[] buffer){}
public virtual byte[] Build(){return null;}
}
Code:
Public class DataPacket:Packet
{
//I do this next part in the constructor.
public int UID; //Example Purpose
public DataPacket(){PacketType=10010;}
public override void Read(byte[] buffer)
{
//Watever reading method you want to use here
UID = BitConverter.ToInt32(buffer,4);
}
}
i found it to be easier, and regarding the Circular Buffer, You have Put and Get
Put(byte[] Data);
Get(int length);
In my case it would be.
Code:
public Packet ParseBuffer()
{
byte[] p = Buffer.Get(BitConverter.ToInt16(Buffer.Get(2),0));
//this will cause a "wait" action untill the whole packet is there if its not received complete.
tqpacket= null;
switch (packettype)
{
case 10010:tqpacket=new DataPacket();break;
}
if (tqpacket != null)
{
tqpacket.Read(p);
}
return tqpacket;
}
this is just my 2 cents worth, Hope it helps