Exemplo n.º 1
0
        /// <summary>
        /// Handles the event when a client times out. Disconnects the UDP client and cleans up any references
        /// to the client.
        /// </summary>
        /// <param name="client">The client that timed out.</param>
        private void HandleClientTimeout(NetServerClient client)
        {
            var id = client.Id;

            // Only execute the client timeout callback if the client is registered and thus has an ID
            if (client.IsRegistered)
            {
                ClientTimeoutEvent?.Invoke(id);
            }

            client.Disconnect();
            _registeredClients.Remove(id);
            _clients.Remove(client);

            Logger.Get().Info(this, $"Client {id} timed out");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handle a list of packets from an unregistered client.
        /// </summary>
        /// <param name="client">The unregistered client.</param>
        /// <param name="packets">The list of packets to handle.</param>
        private void HandlePacketsUnregisteredClient(NetServerClient client, List <Packet.Packet> packets)
        {
            for (var i = 0; i < packets.Count; i++)
            {
                var packet = packets[i];

                // Create a server update packet from the raw packet instance
                var serverUpdatePacket = new ServerUpdatePacket(packet);
                if (!serverUpdatePacket.ReadPacket())
                {
                    // If ReadPacket returns false, we received a malformed packet, which we simply ignore for now
                    // Logger.Get().Warn(this, "Received malformed packet, ignoring");
                    continue;
                }

                client.UpdateManager.OnReceivePacket <ServerUpdatePacket, ServerPacketId>(serverUpdatePacket);

                if (!serverUpdatePacket.GetPacketData().TryGetValue(
                        ServerPacketId.LoginRequest,
                        out var packetData
                        ))
                {
                    continue;
                }

                var loginRequest = (LoginRequest)packetData;

                Logger.Get().Info(this, $"Received login request from '{loginRequest.Username}'");

                // Invoke the handler of the login request and decide what to do with the client based on the result
                var allowClient = LoginRequestEvent?.Invoke(
                    client.Id,
                    client.EndPoint,
                    loginRequest,
                    client.UpdateManager
                    );
                if (!allowClient.HasValue)
                {
                    Logger.Get().Error(this, "Login request has no handler");
                    return;
                }

                if (allowClient.Value)
                {
                    // Logger.Get().Info(this, $"Login request from '{loginRequest.Username}' approved");
                    // client.UpdateManager.SetLoginResponseData(LoginResponseStatus.Success);

                    // Register the client and add them to the dictionary
                    client.IsRegistered           = true;
                    _registeredClients[client.Id] = client;

                    // Now that the client is registered, we forward the rest of the packets to the other handler
                    var leftoverPackets = packets.GetRange(
                        i + 1,
                        packets.Count - i - 1
                        );

                    HandlePacketsRegisteredClient(client, leftoverPackets);
                }
                else
                {
                    client.Disconnect();
                    _clients.Remove(client);
                }

                break;
            }
        }