/// <summary>
/// Pass recieved data into the packet buffer and try to parse.
/// </summary>
/// <param name="num_bytes">number of bytes waiting to be read</param>
protected override void OnReceive(int num_bytes)
{
try
{
short pakSize;
// decrypt the packet if needed
if (IsEncrypted)
{
pakSize = GetExpectedLength(true);
}
else
{
pakSize = GetExpectedLength(false);
}
// we received just enough data for the full message. not too litt,e not too much
if (pakSize + 2 == num_bytes)
{
byte[] fullMessage = new byte[num_bytes];
Array.Copy(m_inBuf, 0, fullMessage, 0, num_bytes);
Array.Clear(m_inBuf, 0, pakSize + 2);
m_inOffset = 0;
// we have a full packet
// process it here
return;
}
// we received an excess of data
else if (pakSize + 2 < num_bytes)
{
// get only the data we need
byte[] fullMessage = new byte[pakSize + 2];
Array.Copy(m_inBuf, 0, fullMessage, 0, pakSize + 2);
// clear the data i just took
Array.Clear(m_inBuf, 0, pakSize + 2);
// shift the remaining data to the top
Array.Copy(m_inBuf, m_inOffset, m_inBuf, 0, m_inBuf.Length - m_inOffset);
// set the offset to the end of the data that was in excess
m_inOffset = num_bytes - (pakSize + 2);
// we have enough for a full packet
// segment and process
return;
}
else
{
// partial packet. we don't touch the offset and just let it async read more data.
return;
}
}
catch (InsufficientDataException e)
{
m_srvr.Warning(this, e);
}
}