public override void OnReceiveData(ConnectionState state) { byte[] buffer = new byte[1024]; while (state.AvailableData > 0) { int readCount = state.Read(buffer, 0, (int)(mReceiveBuffer.Capacity - mReceiveBuffer.Position)); if (readCount > 0) { //Drop connection if we are missing a packet parser if (!mParsers.ContainsKey(state)) { mLogger.LogMessage(Logger.LoggerChannels.Network, string.Format("Dropped connection to {0} because its parser was missing.", GetIPFor(state.RemoteEndPoint))); state._server.DropConnection(state); return; } ProsthesisPacketParser parser = mParsers[state]; parser.AddData(buffer, readCount); try { while (parser.MoveNext()) { ProsthesisMessage msg = parser.Current; if (MessageAvailable != null) { MessageAvailable(msg, state); } } } catch (Exception e) { mLogger.LogMessage(Logger.LoggerChannels.Faults, string.Format("Caught protobuf exception {0} receiving data from {1}. Closing connection", e, GetIPFor(state.RemoteEndPoint))); state._server.DropConnection(state); } } else { //If read fails then close connection mLogger.LogMessage(Logger.LoggerChannels.Network, string.Format("Read failed from {0}. Dropping connection", GetIPFor(state.RemoteEndPoint))); state._server.DropConnection(state); } } }