public void RecvPDU()
{
Socket socket;
BinaryReader s;
byte pduType = 0;
int totalSize = 0;
byte fragFlags = 0;
UInt16 channelId = 0;
UInt16 fragSize = 0;
byte[] buffer = null;
byte[] header = new byte[PDU_HEADER_SIZE];
if (tcpClient.Connected && tcpClient.GetStream().DataAvailable)
{
socket = tcpClient.Client;
while (true)
{
RecvAll(socket, header, 0, PDU_HEADER_SIZE);
s = new BinaryReader(new MemoryStream(header));
channelId = s.ReadUInt16();
pduType = s.ReadByte();
fragFlags = s.ReadByte();
fragSize = s.ReadUInt16();
fragSize -= PDU_HEADER_SIZE;
if (fragFlags == PDU_FRAGMENT_SINGLE)
{
/* a single fragment */
buffer = new byte[fragSize];
RecvAll(socket, buffer, 0, fragSize);
totalSize = fragSize;
dispatcher.DispatchPDU(buffer, channelId, pduType);
}
else if (fragFlags == PDU_FRAGMENT_FIRST)
{
/* the first of a series of fragments */
buffer = new byte[fragSize];
RecvAll(socket, buffer, 0, fragSize);
totalSize = fragSize;
}
else if (fragFlags == PDU_FRAGMENT_NEXT)
{
/* the "in between" of a series of fragments */
Array.Resize<byte>(ref buffer, totalSize + fragSize);
RecvAll(socket, buffer, totalSize, fragSize);
totalSize += fragSize;
}
else if (fragFlags == PDU_FRAGMENT_LAST)
{
/* The last of a series of fragments */
Array.Resize<byte>(ref buffer, totalSize + fragSize);
RecvAll(socket, buffer, totalSize, fragSize);
totalSize += fragSize;
dispatcher.DispatchPDU(buffer, channelId, pduType);
}
else
{
Console.WriteLine("Invalid Fragmentation Flags: {0}", fragFlags);
return;
}
}
}
return;
}