/// <summary> /// Constructor of the server /// </summary> /// <param name="operationSettings"></param> public GameServer(ref ServerSettings operationSettings)///Need to be set the buffer of receiving data { #if PROFILING this.profilerOutgoingMessages = new Profiler("OutgoingMessages"); #endif ///Assigning settings to work. this.lowLevelOperationSettings = operationSettings; if (this.lowLevelOperationSettings == null) { this.ableToRun = false; return; } ///Used to receive the first command. this.tcpBuffer = new byte[ServerSettings.ServerBufferSize]; ///Creating a new buffered CommandQueue capable to suport up to 1000 messages, each one of 1024 bytes length. this.commandsQueue = new BufferedCommandQueue((uint)ServerSettings.ServerBufferSize * 1000); ///Creating the local commands queue, capable to hold up to 100 messages, each one of 1024 bytes length. this.localCommandsQueue = new BufferedCommandQueue((uint)ServerSettings.ServerBufferSize * 100); this.priorityOutgoingMessagesQueue = new CommandQueue(); this.outgoingMessagesQueue = new CommandQueue(); ///Pool of pre-allocated messages with an initial capacity if 2000 messages. this.incomingMessagesPool = new MessagesPool(2000, new BufferedMessage(Message.CommandType.Null, 0, 0)); ///Pool of pre-allocated messages used in the connection process. Up to 100 messages. this.priorityMessagesPool = new MessagesPool(100, new BufferedMessage(Message.CommandType.Null, 0, 0)); this.commandsThread = new Thread(new ThreadStart(this.HandleCommandsThreadMethod)); this.outgoingMessagesThread = new Thread(new ThreadStart(this.HandleOutgoingMessagesThreadMethod)); this.localCommandsThread = new Thread(new ThreadStart(this.HandleLocalCommandsThreadMethod)); this.priorityOutgoingMessagesThread = new Thread(new ThreadStart(this.HandleOutgoingPriorityMessagesThreadMethod)); this.defaultUserManagementSystem = new LowlevelUserManagmentSystem(); this.clientsHandler = new ClientsHandler(); ///It still missing the filter this.usersAccountManager = new AccountManager(); ///Creating a chat manager in non-persistent mode. It means that none of the chat messages will be stored. this.chatManager = new ChatManager(ChatManager.DefaultStorageMode.NonPersistent); this.incomingConnectionsPool = new SocketAsyncEventArgsPool((uint)this.lowLevelOperationSettings.connectionsBackog); ///TCP Purge Timer this.tcpPurgeTimer = new Timer(this.HandleTCPPurgeTimerCallback); this.tcpPurgeTimeInterval = (int)ServerSettings.PurgeTimeIterval; this.tcpMinimumMessagesAllowedAfterPurge = (int)(this.commandsQueue.MaxCommandAllowed * (1.0f - ServerSettings.AvailablePercentAfterPurge)); this.ableToRun = true; this.alive = false; }
public void ShutdownServer() { KSPMGlobals.Globals.Log.WriteTo("Shuttingdown the KSPM server ."); ///*************************Killing threads code this.alive = false; this.commandsThread.Abort(); this.outgoingMessagesThread.Abort(); this.localCommandsThread.Abort(); this.priorityOutgoingMessagesThread.Abort(); this.commandsThread.Join(); KSPMGlobals.Globals.Log.WriteTo("Killed commandsTread ."); this.localCommandsThread.Join(); KSPMGlobals.Globals.Log.WriteTo("Killed localCommandsTread ."); this.outgoingMessagesThread.Join(); KSPMGlobals.Globals.Log.WriteTo("Killed outgoingMessagesTread ."); this.priorityOutgoingMessagesThread.Join(); KSPMGlobals.Globals.Log.WriteTo("Killed priorityOutgoingMessagesTread ."); ///*************************Killing TCP socket code if (this.tcpSocket.Connected) { this.tcpSocket.Shutdown(SocketShutdown.Both); this.tcpSocket.Close(); } this.tcpBuffer = null; this.tcpIpEndPoint = null; KSPMGlobals.Globals.Log.WriteTo("Killing chat system!!!"); this.chatManager.Release(); this.chatManager = null; KSPMGlobals.Globals.Log.WriteTo("Killing conected clients!!!"); this.clientsHandler.Release(); this.clientsHandler = null; ///*********************Killing server itself this.ableToRun = false; ///Releasing command queues. this.commandsQueue.Purge(false); this.outgoingMessagesQueue.Purge(false); this.localCommandsQueue.Purge(false); this.priorityOutgoingMessagesQueue.Purge(false); this.commandsQueue = null; this.localCommandsQueue = null; this.outgoingMessagesQueue = null; this.priorityOutgoingMessagesQueue = null; ///Releasing messages pools. this.priorityMessagesPool.Release(); this.incomingMessagesPool.Release(); this.priorityMessagesPool = null; this.incomingMessagesPool = null; ///Releasing SAEA pool. this.incomingConnectionsPool.Release(false); this.incomingConnectionsPool = null; this.usersAccountManager = null; this.lowLevelOperationSettings = null; ///Releasing the TCP purge timer. this.tcpPurgeTimer.Dispose(); this.tcpPurgeTimer = null; KSPMGlobals.Globals.Log.WriteTo(string.Format("Server KSPM killed after {0} miliseconds alive!!!", RealTimer.Timer.ElapsedMilliseconds)); #if PROFILING this.profilerOutgoingMessages.Dispose(); #endif }