public void OnReceivedData(IAsyncResult AR) { //base.OnReceivedData(AR); //Not needed for this application! try { Socket Sock = (Socket)AR.AsyncState; int NumBytesRead = Sock.EndReceive(AR); if (NumBytesRead > 0) { byte[] TmpBuf = new byte[NumBytesRead]; Buffer.BlockCopy(m_RecvBuffer, 0, TmpBuf, 0, NumBytesRead); //The packet is given an ID of 0x00 because its ID is currently unknown. PacketStream TempPacket = new PacketStream(0x00, NumBytesRead, TmpBuf); /** Get the packet type **/ byte ID = TempPacket.PeekByte(0); var handler = FindPacketHandler(ID); if (handler != null) { var PacketLength = handler.Length; Logger.LogInfo("Found matching PacketID!\r\n\r\n"); if (NumBytesRead == PacketLength) { Logger.LogInfo("Got packet - exact length!\r\n\r\n"); m_RecvBuffer = new byte[11024]; m_Listener.OnReceivedData(new PacketStream(ID, PacketLength, TempPacket.ToArray()), this); } else if (NumBytesRead < PacketLength) { m_TempPacket = new PacketStream(ID, PacketLength); byte[] TmpBuffer = new byte[NumBytesRead]; //Store the number of bytes that were read in the temporary buffer. Logger.LogInfo("Got data, but not a full packet - stored " + NumBytesRead.ToString() + "bytes!\r\n\r\n"); Buffer.BlockCopy(m_RecvBuffer, 0, TmpBuffer, 0, NumBytesRead); m_TempPacket.WriteBytes(TmpBuffer); //And reset the buffers! m_RecvBuffer = new byte[11024]; TmpBuffer = null; } else if (PacketLength == 0) { Logger.LogInfo("Received variable length packet!\r\n"); if (NumBytesRead > (int)PacketHeaders.UNENCRYPTED) //Header is 3 bytes. { PacketLength = TempPacket.PeekUShort(1); if (NumBytesRead == PacketLength) { Logger.LogInfo("Received exact number of bytes for packet!\r\n"); m_RecvBuffer = new byte[11024]; m_TempPacket = null; m_Listener.OnReceivedData(new PacketStream(ID, PacketLength, TempPacket.ToArray()), this); } else if (NumBytesRead < PacketLength) { Logger.LogInfo("Didn't receive entire packet - stored: " + PacketLength + " bytes!\r\n"); TempPacket.SetLength(PacketLength); m_TempPacket = TempPacket; m_RecvBuffer = new byte[11024]; } else if (NumBytesRead > PacketLength) { Logger.LogInfo("Received more bytes than needed for packet. Excess: " + (NumBytesRead - PacketLength) + "\r\n"); byte[] TmpBuffer = new byte[NumBytesRead - PacketLength]; Buffer.BlockCopy(TempPacket.ToArray(), 0, TmpBuffer, 0, TmpBuffer.Length); m_TempPacket = new PacketStream(TmpBuffer[0], NumBytesRead - PacketLength, TmpBuffer); byte[] PacketBuffer = new byte[PacketLength]; Buffer.BlockCopy(TempPacket.ToArray(), 0, PacketBuffer, 0, PacketBuffer.Length); m_RecvBuffer = new byte[11024]; m_Listener.OnReceivedData(new PacketStream(ID, PacketLength, PacketBuffer), this); } } } } else { if (m_TempPacket != null) { if (m_TempPacket.Length < m_TempPacket.BufferLength) { //Received the exact number of bytes needed to complete the stored packet. if ((m_TempPacket.BufferLength + NumBytesRead) == m_TempPacket.Length) { byte[] TmpBuffer = new byte[NumBytesRead]; Buffer.BlockCopy(m_RecvBuffer, 0, TmpBuffer, 0, NumBytesRead); m_RecvBuffer = new byte[11024]; TmpBuffer = null; } //Received more than the number of bytes needed to complete the packet! else if ((m_TempPacket.BufferLength + NumBytesRead) > m_TempPacket.Length) { int Target = (int)((m_TempPacket.BufferLength + NumBytesRead) - m_TempPacket.Length); byte[] TmpBuffer = new byte[Target]; Buffer.BlockCopy(m_RecvBuffer, 0, TmpBuffer, 0, Target); m_TempPacket.WriteBytes(TmpBuffer); //Now we have a full packet, so call the received event! m_Listener.OnReceivedData(new PacketStream(m_TempPacket.PacketID, (int)m_TempPacket.Length, m_TempPacket.ToArray()), this); //Copy the remaining bytes in the receiving buffer. TmpBuffer = new byte[NumBytesRead - Target]; Buffer.BlockCopy(m_RecvBuffer, Target, TmpBuffer, 0, (NumBytesRead - Target)); //Give the temporary packet an ID of 0x00 since we don't know its ID yet. TempPacket = new PacketStream(0x00, NumBytesRead - Target, TmpBuffer); ID = TempPacket.PeekByte(0); handler = FindPacketHandler(ID); //This SHOULD be an existing ID, but let's sanity-check it... if (handler != null) { m_TempPacket = new PacketStream(ID, handler.Length, TempPacket.ToArray()); //Congratulations, you just received another packet! if (m_TempPacket.Length == m_TempPacket.BufferLength) { m_Listener.OnReceivedData(new PacketStream(m_TempPacket.PacketID, (int)m_TempPacket.Length, m_TempPacket.ToArray()), this); //No more data to store on this read, so reset everything... m_TempPacket = null; TmpBuffer = null; m_RecvBuffer = new byte[11024]; } } else { //Houston, we have a problem (this should never occur)! } } } } } } else { //Client disconnected! //TODO: Figure out if this client was successfully authenticated before transferring. m_Listener.TransferClient(this); } m_Socket.BeginReceive(m_RecvBuffer, 0, m_RecvBuffer.Length, SocketFlags.None, new AsyncCallback(OnReceivedData), m_Socket); } catch (SocketException) { Disconnect(); } }