static void ShutdownThread(bool restarting, string msg) { try { Logger.Log(LogType.SystemActivity, "Server shutting down ({0})", msg); } catch { } // Stop accepting new connections and disconnect existing sessions try { if (Listener != null) { Listener.Close(); } } catch (Exception ex) { Logger.LogError(ex); } try { Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { p.Leave(msg); } } catch (Exception ex) { Logger.LogError(ex); } byte[] kick = Packet.Kick(msg, false); try { INetSocket[] pending = INetSocket.pending.Items; foreach (INetSocket p in pending) { p.Send(kick, SendFlags.None); } } catch (Exception ex) { Logger.LogError(ex); } OnShuttingDownEvent.Call(restarting, msg); Plugin.UnloadAll(); try { string autoload = null; Level[] loaded = LevelInfo.Loaded.Items; foreach (Level lvl in loaded) { if (!lvl.SaveChanges) { continue; } autoload = autoload + lvl.name + "=" + lvl.physics + Environment.NewLine; lvl.Save(); lvl.SaveBlockDBChanges(); } if (Server.SetupFinished && !Server.Config.AutoLoadMaps) { File.WriteAllText("text/autoload.txt", autoload); } } catch (Exception ex) { Logger.LogError(ex); } try { Logger.Log(LogType.SystemActivity, "Server shutdown completed"); } catch { } try { FileLogger.Flush(null); } catch { } if (restarting) { // first try to use excevp to restart in CLI mode under mono // - see detailed comment in HACK_Execvp for why this is required if (HACK_TryExecvp()) { HACK_Execvp(); } Process.Start(GetRestartPath()); } Environment.Exit(0); }
void LeaveServer(string chatMsg, string discMsg, bool isKick, bool sync = false) { if (leftServer) { return; } leftServer = true; CriticalTasks.Clear(); //Umm...fixed? if (name == null || name.Length == 0) { if (Socket != null) { CloseSocket(); } connections.Remove(this); disconnected = true; Logger.Log(LogType.UserActivity, "{0} disconnected.", ip); return; } Server.reviewlist.Remove(name); try { if (disconnected) { CloseSocket(); connections.Remove(this); PlayerInfo.Online.Remove(this); return; } // FlyBuffer.Clear(); LastAction = DateTime.UtcNow; IsAfk = false; isFlying = false; aiming = false; if (chatMsg != null) { chatMsg = Colors.Escape(chatMsg); } discMsg = Colors.Escape(discMsg); string kickPacketMsg = ChatTokens.Apply(discMsg, this); Send(Packet.Kick(kickPacketMsg, hasCP437), sync); disconnected = true; if (isKick) { TimesBeenKicked++; } if (!loggedIn) { connections.Remove(this); RemoveFromPending(); PlayerInfo.Online.Remove(this); string user = name + " (" + ip + ")"; Logger.Log(LogType.UserActivity, "{0} disconnected. ({1})", user, discMsg); return; } if (Game.team != null) { Game.team.RemoveMember(this); } TntWarsGame tntwarsgame = TntWarsGame.GameIn(this); if (tntwarsgame != null) { tntwarsgame.Players.Remove(tntwarsgame.FindPlayer(this)); tntwarsgame.SendAllPlayersMessage("TNT Wars: " + ColoredName + " %Shas left TNT Wars!"); } Entities.DespawnEntities(this, false); ShowDisconnectInChat(chatMsg, isKick); save(); PlayerInfo.Online.Remove(this); Server.PlayerListUpdate(); OnPlayerDisconnectEvent.Call(this, discMsg); if (ServerConfig.AutoLoadMaps && level.Config.AutoUnload && !level.IsMuseum && !level.HasPlayers()) { level.Unload(true); } Dispose(); } catch (Exception e) { Logger.LogError(e); } finally { CloseSocket(); } }
void LeaveServer(string chatMsg, string discMsg, bool isKick, bool sync = false) { if (leftServer || IsSuper) { return; } leftServer = true; CriticalTasks.Clear(); ZoneIn = null; // Disconnected before sent handshake if (name == null) { if (Socket != null) { Socket.Close(); } Logger.Log(LogType.UserActivity, "{0} disconnected.", ip); return; } Server.reviewlist.Remove(name); try { if (Socket.Disconnected) { PlayerInfo.Online.Remove(this); return; } // FlyBuffer.Clear(); LastAction = DateTime.UtcNow; IsAfk = false; isFlying = false; if (weapon != null) { weapon.Disable(); } if (chatMsg != null) { chatMsg = Colors.Escape(chatMsg); } discMsg = Colors.Escape(discMsg); string kickPacketMsg = ChatTokens.Apply(discMsg, this); Send(Packet.Kick(kickPacketMsg, hasCP437), sync); Socket.Disconnected = true; ZoneIn = null; if (isKick) { TimesBeenKicked++; } if (!loggedIn) { PlayerInfo.Online.Remove(this); string user = truename + " (" + ip + ")"; Logger.Log(LogType.UserActivity, "{0} disconnected. ({1})", user, discMsg); return; } Entities.DespawnEntities(this, false); ShowDisconnectInChat(chatMsg, isKick); SaveStats(); PlayerInfo.Online.Remove(this); OnPlayerDisconnectEvent.Call(this, discMsg); level.AutoUnload(); Dispose(); } catch (Exception e) { Logger.LogError("Error disconnecting player", e); } finally { Socket.Close(); } }
static void ShutdownThread(bool restarting, string msg) { try { Logger.Log(LogType.SystemActivity, "Server shutting down ({0})", msg); } catch { } // Stop accepting new connections and disconnect existing sessions try { if (Listener != null) { Listener.Close(); } } catch (Exception ex) { Logger.LogError(ex); } try { Player[] players = PlayerInfo.Online.Items; SaveRestoreTokens(players); foreach (Player p in players) { p.Leave(msg); } } catch (Exception ex) { Logger.LogError(ex); } byte[] kick = Packet.Kick(msg, false); try { INetSocket[] pending = INetSocket.pending.Items; foreach (INetSocket p in pending) { p.Send(kick, false); } } catch (Exception ex) { Logger.LogError(ex); } Plugin.UnloadAll(); OnShuttingDownEvent.Call(restarting, msg); try { string autoload = null; Level[] loaded = LevelInfo.Loaded.Items; foreach (Level lvl in loaded) { if (!lvl.SaveChanges) { continue; } autoload = autoload + lvl.name + "=" + lvl.physics + Environment.NewLine; lvl.Save(false, true); lvl.SaveBlockDBChanges(); } if (Server.SetupFinished && !Server.Config.AutoLoadMaps) { File.WriteAllText("text/autoload.txt", autoload); } } catch (Exception ex) { Logger.LogError(ex); } try { Logger.Log(LogType.SystemActivity, "Server shutdown completed"); } catch { } try { FileLogger.Flush(null); } catch { } if (restarting) { Process.Start(RestartPath); } Environment.Exit(0); }