Beispiel #1
0
        private void HandleRakNetMessage(IPEndPoint senderEndpoint, OpenConnectionRequest2 incoming)
        {
            PlayerNetworkSession session;

            lock (_playerSessions)
            {
                DateTime trash;
                if (!_connectionAttemps.TryRemove(senderEndpoint, out trash))
                {
                    Log.WarnFormat("Unexpected connection request packet from {0}. Probably a resend.", senderEndpoint.Address);
                    return;
                }

                if (_playerSessions.TryGetValue(senderEndpoint, out session))
                {
                    // Already connecting, then this is just a duplicate
                    if (session.State == ConnectionState.Connecting /* && DateTime.UtcNow < session.LastUpdatedTime + TimeSpan.FromSeconds(2)*/)
                    {
                        return;
                    }

                    Log.InfoFormat("Unexpected session from {0}. Removing old session and disconnecting old player.", senderEndpoint.Address);

                    session.Disconnect("Reconnecting.", false);

                    _playerSessions.TryRemove(senderEndpoint, out session);
                }

                session = new PlayerNetworkSession(this, null, senderEndpoint, incoming.mtuSize)
                {
                    State             = ConnectionState.Connecting,
                    LastUpdatedTime   = DateTime.UtcNow,
                    MtuSize           = incoming.mtuSize,
                    NetworkIdentifier = incoming.clientGuid
                };

                _playerSessions.TryAdd(senderEndpoint, session);
            }

            //Player player = PlayerFactory.CreatePlayer(this, senderEndpoint);
            //player.ClientGuid = incoming.clientGuid;
            //player.NetworkHandler = session;
            //session.Player = player;
            session.MessageHandler = new LoginMessageHandler(session);

            var reply = OpenConnectionReply2.CreateObject();

            reply.serverGuid             = MotdProvider.ServerId;
            reply.clientEndpoint         = senderEndpoint;
            reply.mtuSize                = incoming.mtuSize;
            reply.doSecurityAndHandshake = new byte[1];
            var data = reply.Encode();

            TraceSend(reply);

            reply.PutPool();


            SendData(data, senderEndpoint);
        }
Beispiel #2
0
        private void HandleRakNetMessage(IPEndPoint senderEndpoint, OpenConnectionRequest2 incoming)
        {
            ConcurrentDictionary <IPEndPoint, RakSession> sessions           = _connectionInfo.RakSessions;
            ConcurrentDictionary <IPEndPoint, DateTime>   connectionAttempts = _connectionAttempts;

            lock (sessions)
            {
                if (!connectionAttempts.ContainsKey(senderEndpoint))
                {
                    Log.Warn($"Unexpected connection request packet from {senderEndpoint}. Probably a resend.");
                    return;
                }

                if (sessions.TryGetValue(senderEndpoint, out _))
                {
                    Log.Warn($"Trying to create session where session already exist. Please wait for timeout on {senderEndpoint}. Ignoring this request.");
                    return;
                }

                var session = new RakSession(_connectionInfo, _sender, senderEndpoint, incoming.mtuSize)
                {
                    State             = ConnectionState.Connecting,
                    LastUpdatedTime   = DateTime.UtcNow,
                    MtuSize           = incoming.mtuSize,
                    NetworkIdentifier = incoming.clientGuid,
                };

                session.CustomMessageHandler = _connection.CustomMessageHandlerFactory?.Invoke(session);

                sessions.TryAdd(senderEndpoint, session);
            }

            var reply = OpenConnectionReply2.CreateObject();

            reply.serverGuid             = _motdProvider.ServerId;
            reply.clientEndpoint         = senderEndpoint;
            reply.mtuSize                = incoming.mtuSize;
            reply.doSecurityAndHandshake = new byte[1];
            byte[] data = reply.Encode();

            TraceSend(reply);

            reply.PutPool();

            _sender.SendData(data, senderEndpoint);
        }
Beispiel #3
0
        private void HandleRakNetMessage(byte[] receiveBytes, IPEndPoint senderEndpoint, byte msgId)
        {
            DefaultMessageIdTypes msgIdType = (DefaultMessageIdTypes)msgId;

            Package message = PackageFactory.CreatePackage(msgId, receiveBytes);

            if (message == null)
            {
                if (!_badPacketBans.ContainsKey(senderEndpoint.Address))
                {
                    _badPacketBans.Add(senderEndpoint.Address, true);
                }
                Log.ErrorFormat("Receive bad packet with ID: {0} (0x{0:x2}) {2} from {1}", msgId, senderEndpoint.Address, (DefaultMessageIdTypes)msgId);
                return;
            }

            message.Source = "RakNet";

            TraceReceive(message);

            switch (msgIdType)
            {
            case DefaultMessageIdTypes.ID_UNCONNECTED_PING:
            case DefaultMessageIdTypes.ID_UNCONNECTED_PING_OPEN_CONNECTIONS:
            {
                UnconnectedPing incoming = (UnconnectedPing)message;

                //TODO: This needs to be verified with RakNet first
                //response.sendpingtime = msg.sendpingtime;
                //response.sendpongtime = DateTimeOffset.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

                var packet = UnconnectedPong.CreateObject();
                packet.serverId   = 22345;
                packet.pingId     = incoming.pingId;
                packet.serverName = MotdProvider.GetMotd(ServerInfo);
                var data = packet.Encode();
                packet.PutPool();
                TraceSend(packet);
                SendData(data, senderEndpoint, new object());
                break;
            }

            case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REQUEST_1:
            {
                OpenConnectionRequest1 incoming = (OpenConnectionRequest1)message;
                Log.DebugFormat("New connection from: {0} {1}", senderEndpoint.Address, senderEndpoint.Port);

                lock (_playerSessions)
                {
                    // Already connecting, then this is just a duplicate
                    if (_connectionAttemps.ContainsKey(senderEndpoint))
                    {
                        DateTime created;
                        _connectionAttemps.TryGetValue(senderEndpoint, out created);

                        if (DateTime.UtcNow < created + TimeSpan.FromSeconds(3))
                        {
                            return;
                        }
                    }

                    //PlayerNetworkSession session;
                    //if (_playerSessions.TryGetValue(senderEndpoint, out session))
                    //{

                    //	Log.DebugFormat("Reconnection detected from {0}. Removing old session and disconnecting old player.", senderEndpoint.Address);

                    //	Player oldPlayer = session.Player;
                    //	if (oldPlayer != null)
                    //	{
                    //		oldPlayer.Disconnect("Reconnecting.");
                    //	}
                    //	else
                    //	{
                    //		_playerSessions.TryRemove(session.EndPoint, out session);
                    //	}
                    //}

                    if (!_connectionAttemps.ContainsKey(senderEndpoint))
                    {
                        _connectionAttemps.Add(senderEndpoint, DateTime.UtcNow);
                    }
                }

                var packet = OpenConnectionReply1.CreateObject();
                packet.serverGuid        = 12345;
                packet.mtuSize           = incoming.mtuSize;
                packet.serverHasSecurity = 0;
                var data = packet.Encode();
                packet.PutPool();

                TraceSend(packet);

                SendData(data, senderEndpoint, new object());
                break;
            }

            case DefaultMessageIdTypes.ID_OPEN_CONNECTION_REQUEST_2:
            {
                OpenConnectionRequest2 incoming = (OpenConnectionRequest2)message;

                PlayerNetworkSession session;
                lock (_playerSessions)
                {
                    if (_connectionAttemps.ContainsKey(senderEndpoint))
                    {
                        _connectionAttemps.Remove(senderEndpoint);
                    }
                    else
                    {
                        Log.ErrorFormat("Unexpected connection request packet from {0}.", senderEndpoint.Address);
                        return;
                    }

                    //PlayerNetworkSession session;
                    if (_playerSessions.TryGetValue(senderEndpoint, out session))
                    {
                        Log.WarnFormat("Reconnection detected from {0}. Removing old session and disconnecting old player.", senderEndpoint.Address);

                        Player oldPlayer = session.Player;
                        if (oldPlayer != null)
                        {
                            oldPlayer.Disconnect("Reconnecting.");
                        }
                        else
                        {
                            _playerSessions.TryRemove(session.EndPoint, out session);
                        }
                    }


                    if (_playerSessions.TryGetValue(senderEndpoint, out session))
                    {
                        // Already connecting, then this is just a duplicate
                        if (session.State == ConnectionState.Connecting /* && DateTime.UtcNow < session.LastUpdatedTime + TimeSpan.FromSeconds(2)*/)
                        {
                            return;
                        }

                        Log.ErrorFormat("Unexpected session from {0}. Removing old session and disconnecting old player.", senderEndpoint.Address);

                        Player oldPlayer = session.Player;
                        if (oldPlayer != null)
                        {
                            oldPlayer.Disconnect("Reconnecting.");
                        }
                        else
                        {
                            _playerSessions.TryRemove(session.EndPoint, out session);
                        }
                    }

                    session = new PlayerNetworkSession(null, senderEndpoint)
                    {
                        State           = ConnectionState.Connecting,
                        LastUpdatedTime = DateTime.UtcNow,
                        Mtuize          = incoming.mtuSize
                    };

                    _playerSessions.TryAdd(senderEndpoint, session);
                }

                Player player = PlayerFactory.CreatePlayer(this, senderEndpoint, incoming.mtuSize);
                player.ClientGuid     = incoming.clientGuid;
                player.NetworkSession = session;
                session.Player        = player;

                var reply = OpenConnectionReply2.CreateObject();
                reply.serverGuid             = 12345;
                reply.mtuSize                = incoming.mtuSize;
                reply.doSecurityAndHandshake = new byte[0];
                var data = reply.Encode();
                reply.PutPool();

                TraceSend(reply);

                SendData(data, senderEndpoint, session.SyncRoot);
                break;
            }

            default:
                if (!_badPacketBans.ContainsKey(senderEndpoint.Address))
                {
                    _badPacketBans.Add(senderEndpoint.Address, true);
                }
                Log.ErrorFormat("Receive unexpected packet with ID: {0} (0x{0:x2}) {2} from {1}", msgId, senderEndpoint.Address, (DefaultMessageIdTypes)msgId);
                break;
            }

            message.PutPool();
        }
Beispiel #4
0
        private void HandleRakNetMessage(IPEndPoint senderEndpoint, OpenConnectionRequest2 incoming)
        {
            PlayerNetworkSession session;

            lock (_playerSessions)
            {
                DateTime trash;
                if (!_connectionAttemps.TryRemove(senderEndpoint, out trash))
                {
                    Log.ErrorFormat("Unexpected connection request packet from {0}. Proboble resend.", senderEndpoint.Address);
                    return;
                }

                if (_playerSessions.TryGetValue(senderEndpoint, out session))
                {
                    // Already connecting, then this is just a duplicate
                    if (session.State == ConnectionState.Connecting /* && DateTime.UtcNow < session.LastUpdatedTime + TimeSpan.FromSeconds(2)*/)
                    {
                        return;
                    }

                    Log.ErrorFormat("Unexpected session from {0}. Removing old session and disconnecting old player.", senderEndpoint.Address);

                    Player oldPlayer = session.Player;
                    if (oldPlayer != null)
                    {
                        oldPlayer.Disconnect("Reconnecting.", false);
                    }

                    _playerSessions.TryRemove(session.EndPoint, out session);
                }

                session = new PlayerNetworkSession(null, senderEndpoint)
                {
                    State           = ConnectionState.Connecting,
                    LastUpdatedTime = DateTime.UtcNow,
                    Mtuize          = incoming.mtuSize
                };

                _playerSessions.TryAdd(senderEndpoint, session);
            }

            Player player = PlayerFactory.CreatePlayer(this, senderEndpoint, incoming.mtuSize);

            player.ClientGuid     = incoming.clientGuid;
            player.NetworkSession = session;
            session.Player        = player;

            var reply = OpenConnectionReply2.CreateObject();

            reply.serverGuid             = 12345;
            reply.clientendpoint         = senderEndpoint;
            reply.mtuSize                = incoming.mtuSize;
            reply.doSecurityAndHandshake = new byte[1];
            var data = reply.Encode();

            reply.PutPool();

            TraceSend(reply);

            SendData(data, senderEndpoint, session.SyncRoot);
        }