예제 #1
0
		private void HandleRakNetMessage(IPEndPoint senderEndpoint, OpenConnectionRequest1 incoming)
		{
			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;
					}

					_connectionAttemps.TryRemove(senderEndpoint, out created);
				}

				if (!_connectionAttemps.TryAdd(senderEndpoint, DateTime.UtcNow)) return;
			}

			if (Log.IsDebugEnabled)
				Log.WarnFormat("New connection from: {0} {1}, MTU: {2}", senderEndpoint.Address, senderEndpoint.Port, incoming.mtuSize);

			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);
		}
예제 #2
0
        private void HandleRakNetMessage(IPEndPoint senderEndpoint, OpenConnectionRequest1 message)
        {
            ConcurrentDictionary <IPEndPoint, RakSession> sessions           = _connectionInfo.RakSessions;
            ConcurrentDictionary <IPEndPoint, DateTime>   connectionAttempts = _connectionAttempts;

            if (Log.IsDebugEnabled)
            {
                Log.Warn($"New connection from {senderEndpoint.Address} {senderEndpoint.Port}, MTU={message.mtuSize}, RakNet version={message.raknetProtocolVersion}");
            }

            lock (sessions)
            {
                // Already connecting, then this is just a duplicate
                if (connectionAttempts.TryGetValue(senderEndpoint, out DateTime created))
                {
                    if (DateTime.UtcNow < created + TimeSpan.FromSeconds(3))
                    {
                        return;
                    }

                    connectionAttempts.TryRemove(senderEndpoint, out _);
                }

                if (!connectionAttempts.TryAdd(senderEndpoint, DateTime.UtcNow))
                {
                    return;
                }
            }

            if (message.mtuSize > MaxMtuSize)
            {
                return;
            }

            var packet = OpenConnectionReply1.CreateObject();

            packet.serverGuid        = _motdProvider.ServerId;
            packet.mtuSize           = message.mtuSize;
            packet.serverHasSecurity = 0;
            byte[] data = packet.Encode();

            TraceSend(packet);

            packet.PutPool();

            _sender.SendData(data, senderEndpoint);
        }
예제 #3
0
        public static void HandleRakNetMessage(MiNetServer server, IPEndPoint senderEndpoint, OpenConnectionRequest1 incoming)
        {
            lock (server._rakNetSessions)
            {
                // Already connecting, then this is just a duplicate
                if (server._connectionAttemps.TryGetValue(senderEndpoint, out DateTime created))
                {
                    if (DateTime.UtcNow < created + TimeSpan.FromSeconds(3))
                    {
                        return;
                    }

                    server._connectionAttemps.TryRemove(senderEndpoint, out _);
                }

                if (!server._connectionAttemps.TryAdd(senderEndpoint, DateTime.UtcNow))
                {
                    return;
                }
            }

            if (Log.IsDebugEnabled)
            {
                Log.Warn($"New connection from {senderEndpoint.Address} {senderEndpoint.Port}, MTU={incoming.mtuSize}, RakNet version={incoming.raknetProtocolVersion}");
            }

            var packet = OpenConnectionReply1.CreateObject();

            packet.serverGuid        = server.MotdProvider.ServerId;
            packet.mtuSize           = incoming.mtuSize;
            packet.serverHasSecurity = 0;
            byte[] data = packet.Encode();

            TraceSend(packet);

            packet.PutPool();

            server.SendData(data, senderEndpoint);
        }
예제 #4
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();
        }