Esempio n. 1
0
        public override bool Stop()
        {
            if (!base.Stop())
            {
                return(false);
            }


            Logger.Log(LogType.Debug, $"Stopping {ComponentName}.");

            if (PlayerWatcherToken?.IsCancellationRequested == false)
            {
                PlayerWatcherToken.Cancel();
                PlayerWatcherLock.Wait();
            }
            if (PlayerCorrectionToken?.IsCancellationRequested == false)
            {
                PlayerCorrectionToken.Cancel();
                PlayerCorrectionLock.Wait();
            }

            ModuleManager.ClientJoined -= ModuleManager_ClientJoined;
            ModuleManager.ClientLeaved -= ModuleManager_ClientLeaved;

            Listener?.Stop();

            lock (JoiningClients)
            {
                foreach (var client in JoiningClients)
                {
                    client?.Dispose();
                }
                JoiningClients.Clear();
            }

            lock (Clients)
            {
                foreach (var client in Clients)
                {
                    client?.SendKick("Server is closing!");
                }
                Clients.Clear();
            }

            NearPlayers.Clear();


            return(true);
        }
Esempio n. 2
0
        private void ListenerCycle()
        {
            try
            {
                while (true) // Listener.Stop() will stop it.
                {
                    var client = new P3DPlayer(Listener.AcceptSocket(), this);
                    client.Ready        += OnClientReady;
                    client.Disconnected += OnClientLeave;
                    client.StartListening();

                    lock (JoiningClients)
                        JoiningClients.Add(client);
                }
            }
            catch (Exception e) when(e is SocketException)
            {
            }
        }
Esempio n. 3
0
        private void ModuleManager_ClientJoined(object sender, ClientJoinedEventArgs eventArgs)
        {
            var player = eventArgs.Client as P3DPlayer;

            lock (JoiningClients)
                JoiningClients.Remove(player);

            lock (Clients)
                Clients.Add(player);


            SendPacketToAll(new CreatePlayerPacket {
                Origin = Origin.Server, PlayerID = eventArgs.Client.ID
            });
            SendPacketToAll(eventArgs.Client.GetDataPacket());
            SendPacketToAll(new ChatMessageGlobalPacket {
                Origin = Origin.Server, Message = $"Player {eventArgs.Client.Name} joined the game!"
            });
        }