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); }
private void PlayerWatcherCycle() { PlayerWatcherLock.Reset(); var watch = Stopwatch.StartNew(); while (!PlayerWatcherToken.IsCancellationRequested) { List <P3DPlayer> players; lock (Clients) players = new List <P3DPlayer>(Clients); foreach (var player in players.Where(player => player.LevelFile != null && !NearPlayers.ContainsKey(player.LevelFile))) { NearPlayers.TryAdd(player.LevelFile, null); } foreach (var level in NearPlayers.Keys) { var playerList = new List <P3DPlayer>(); foreach (var player in players.Where(player => level == player.LevelFile)) { playerList.Add(player); } var array = playerList.ToArray(); NearPlayers.AddOrUpdate(level, array, (s, players1) => players1 = array); } if (watch.ElapsedMilliseconds < 400) { PlayerWatcherThreadTime = watch.ElapsedMilliseconds; var time = (int)(400 - watch.ElapsedMilliseconds); if (time < 0) { time = 0; } Thread.Sleep(time); } watch.Reset(); watch.Start(); } PlayerWatcherLock.Set(); }
private void PlayerCorrectionCycle() { PlayerCorrectionLock.Reset(); var watch = Stopwatch.StartNew(); while (!PlayerCorrectionToken.IsCancellationRequested) { foreach (var nearPlayers in NearPlayers.Where(nearPlayers => nearPlayers.Value != null)) { foreach (var player in nearPlayers.Value.Where(player => player.Moving)) { foreach (var playerToSend in nearPlayers.Value.Where(playerToSend => player != playerToSend)) { playerToSend.SendPacket(player.GetDataPacket()); } } } if (watch.ElapsedMilliseconds < 10) { PlayerCorrectionThreadTime = watch.ElapsedMilliseconds; var time = (int)(5 - watch.ElapsedMilliseconds); if (time < 0) { time = 0; } Thread.Sleep(time); } watch.Reset(); watch.Start(); } PlayerCorrectionLock.Set(); }
// Use this for initialization void Start() { NP = this; }