示例#1
0
        private void ReadMessage(IAsyncResult ar)
        {
            int read = _networkStream.EndRead(ar);

            if (read <= 0)
            {
                Disconnect("Received empty data, socket probably disconnected! Closing connection");
                return;
            }
            _dataReadDelta += read;
            if (_dataReadDelta == _dataBuffer.Length)
            {
                // Message fully received
                NetworkMessage msg = NetworkConverter.Deserialize(_dataBuffer);
                // Set the player info here again, so the client can't trick the server by sending fake data
                msg.playerInfo = playerInfo;
                _messageHandler.AddServerMessage(msg);
                ResetAndWaitForNext();
            }
            else
            {
                _logger.Debug("Parts of the message missing");
                // Wait for the rest of the message
                _networkStream.BeginRead(_dataBuffer, _dataReadDelta, _messageLength - _dataReadDelta, ReadMessage,
                                         null);
            }
        }