/// <summary> /// Called when the client has received data. /// </summary> /// <param name="numBytes">number of bytes received in _pBuf</param> protected void OnReceive(byte[] buffer, int numBytes) { lock (this) { //End Offset of buffer int bufferSize = m_pBufOffset + numBytes; //Size < minimum if (bufferSize < PacketIn.HDR_SIZE) { m_pBufOffset = bufferSize; // undo buffer read return; } //Reset the offset m_pBufOffset = 0; //Current offset into the buffer int curOffset = 0; do { byte[] hdr = m_decoding.Peek(buffer, curOffset); int packetLength = Marshal.ConvertToInt32(hdr, 0); int dataLeft = bufferSize - curOffset; if (dataLeft < packetLength) { Buffer.BlockCopy(buffer, curOffset, buffer, 0, dataLeft); m_pBufOffset = dataLeft; break; } int packetEnd = curOffset + packetLength; byte[] d = m_decoding.Decode(buffer, curOffset, packetLength); //Console.WriteLine( Marshal.ToHexDump( //string.Format("{0} <=== <{3}> Packet 0x{1:X2} ({2}) length: {4}", m_config.PacketInTxt, Marshal.ConvertToInt16(d, 4), //Marshal.ConvertToInt16(d, 4), TcpEndpoint, Marshal.ConvertToInt32(d, 0), //d.Length), //d)); long start = Environment.TickCount; try { PacketIn packet = m_networkHandler.ProcessPacket(this, d, 0, packetLength); } catch (Exception e) { Console.WriteLine("ProcessPacket error: {0}", e.ToString()); } long timeUsed = Environment.TickCount - start; curOffset += packetLength; } while (bufferSize - 1 > curOffset); if (bufferSize - 1 == curOffset) { buffer[0] = buffer[curOffset]; m_pBufOffset = 1; } } }