/// <summary>
/// Gets triggered when the socket receives something
/// </summary>
/// <param name="ar">async result</param>
private void OnReceiveCallback(IAsyncResult ar)
{
try
{
if (_socket == null)
{
return;
}
var bytesRead = _socket.EndReceive(ar);
if (bytesRead <= 0)
{
Close();
return;
}
/**
* I thought the problem was this line because it triggers the command handler without checking if the packet
* is "complete" it only checks if "its not null"
*/
//Console.WriteLine("BytesRead: {0} | Packet Length: {1}", bytesRead, BitConverter.ToInt16(new byte[] {_readBuffer[1], _readBuffer[0]}, 0));
//if (BitConverter.ToInt16(new byte[] {_readBuffer[1], _readBuffer[0]}, 0) + 2 == bytesRead)
//{
// OnReceiveData(_readBuffer);
//}// what happens if it's incomplete just ignores it and waits for the next packet.. i'm not sure if that should work as expected
OnReceiveData(_readBuffer);
//Setup over
_readBuffer = new byte[BufferSize];
_socket.BeginReceive(_readBuffer, 0, _readBuffer.Length, 0, OnReceiveCallback, this);
}
catch { Close(); }
}