/// <summary> /// Implements the receive method to process received data from <see cref="Socket"/>. /// Returns true to free up SocketAsyncEventArgs. /// </summary> internal override bool processClientReceive(SocketAsyncEventArgs e) { if (e.BytesTransferred == 0) { // BytesTransferred = 0 means connection was closed ClientHandler?.ConnectionClosed(this); return(true); } switch (e.SocketError) { case SocketError.ConnectionReset: case SocketError.ConnectionAborted: case SocketError.OperationAborted: // ConnectionReset is raised when the connection was closed - no reason to continue receive ClientHandler?.ConnectionClosed(this); return(true); } if (e.SocketError != SocketError.Success) { // invoke the client handler to process the error in the implementation ClientHandler?.ConnectionError(this, e); return(true); } // UserToken should contain the the implementation to manage the NetworkMessage var messageHandler = e.UserToken as NetworkMessageHandler; // process the received data in the message handler var isReceiveCompleted = messageHandler.CompleteReceive(e.BytesTransferred); if (isReceiveCompleted == null) { // the specified message size is over the limit e.SocketError = SocketError.MessageSize; // invoke the client handler to process the error in the implementation ClientHandler?.ConnectionError(this, e); return(true); } if ((bool)isReceiveCompleted) { // finalize (deobfuscation etc), deserialize to complete message, and process in protocol ClientHandler?.ConnectionMessage(this, SerializationHandler.Deserialize(messageHandler.GetFinalized())); // message has been processed, so handler can be resetted messageHandler.Reset(); } // resume receive receiveAsync(e); // the receive operation always continues on the same event object, so do not free it up return(false); }