Exemple #1
0
        internal void HandleOfflineRakMessage(ReadOnlyMemory <byte> receiveBytes, IPEndPoint senderEndpoint)
        {
            byte messageId   = receiveBytes.Span[0];
            var  messageType = (DefaultMessageIdTypes)messageId;

            // Increase fast, decrease slow on 1s ticks.
            if (_connectionInfo.NumberOfPlayers < _connectionInfo.RakSessions.Count)
            {
                _connectionInfo.NumberOfPlayers = _connectionInfo.RakSessions.Count;
            }

            // Shortcut to reply fast, and no parsing
            if (messageType == DefaultMessageIdTypes.ID_OPEN_CONNECTION_REQUEST_1)
            {
                if (!_greyListManager.AcceptConnection(senderEndpoint.Address))
                {
                    var noFree = NoFreeIncomingConnections.CreateObject();
                    var bytes  = noFree.Encode();

                    TraceSend(noFree);

                    noFree.PutPool();

                    _sender.SendData(bytes, senderEndpoint);
                    Interlocked.Increment(ref _connectionInfo.NumberOfDeniedConnectionRequestsPerSecond);
                    return;
                }
            }

            Packet message = null;

            try
            {
                try
                {
                    message = PacketFactory.Create(messageId, receiveBytes, "raknet");
                }
                catch (Exception)
                {
                    message = null;
                }

                if (message == null)
                {
                    _greyListManager.Blacklist(senderEndpoint.Address);
                    Log.Error($"Receive bad packet with ID: {messageId} (0x{messageId:x2}) {messageType} from {senderEndpoint.Address}");

                    return;
                }

                TraceReceive(Log, message);

                switch (messageType)
                {
                case DefaultMessageIdTypes.ID_NO_FREE_INCOMING_CONNECTIONS:
                    // Stop this client connection
                    _connection.Stop();
                    break;

                case DefaultMessageIdTypes.ID_UNCONNECTED_PING:
                case DefaultMessageIdTypes.ID_UNCONNECTED_PING_OPEN_CONNECTIONS:
                    HandleRakNetMessage(senderEndpoint, (UnconnectedPing)message);
                    break;

                case DefaultMessageIdTypes.ID_UNCONNECTED_PONG:
                    HandleRakNetMessage(senderEndpoint, (UnconnectedPong)message);
                    break;

                case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REQUEST_1:
                    HandleRakNetMessage(senderEndpoint, (OpenConnectionRequest1)message);
                    break;

                case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_1:
                    HandleRakNetMessage(senderEndpoint, (OpenConnectionReply1)message);
                    break;

                case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REQUEST_2:
                    HandleRakNetMessage(senderEndpoint, (OpenConnectionRequest2)message);
                    break;

                case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REPLY_2:
                    HandleRakNetMessage(senderEndpoint, (OpenConnectionReply2)message);
                    break;

                default:
                    _greyListManager.Blacklist(senderEndpoint.Address);
                    if (Log.IsInfoEnabled)
                    {
                        Log.Error($"Receive unexpected packet with ID: {messageId} (0x{messageId:x2}) {messageType} from {senderEndpoint.Address}");
                    }
                    break;
                }
            }
            finally
            {
                message?.PutPool();
            }
        }