/// <summary> /// Invoked when a client sends data. /// </summary> /// <param name="async"></param> public void OnDataReceive(IAsyncResult async) { int numRecvBytes = 0; try { clientState = (SocketClient)async.AsyncState; numRecvBytes = m_SockConnection.EndReceive(async); if (numRecvBytes == 0) { clientState.Disconnect(); return; } // copy new data and process packet lock (clientState) { byte[] newData = new byte[numRecvBytes]; Buffer.BlockCopy(m_bRecvBuffer, 0, newData, 0, numRecvBytes); m_bPacketStream = newData; // process packets ProcessPacket(m_bPacketStream, clientState); ProcessQueue(clientState); } // finished receiving/processing data, go back to listening state. this.Socket.BeginReceive(m_bRecvBuffer, 0, 0x1000, SocketFlags.None, new AsyncCallback(OnDataReceive), clientState); } catch (NullReferenceException) { clientState.Disconnect(); } catch (ObjectDisposedException) { clientState.Disconnect(); } catch (SocketException se) { clientState.Disconnect(); Logger.Log(Logger.LogLevel.Error, "Server", "{0}", se.Message); } }