Example #1
0
        /// <summary>
        /// Creates a ServerSideReference, only initializes those properties required to work with TCP connections.
        /// </summary>
        protected ServerSideClient() : base()
        {
            this.currentStatus = ClientStatus.Handshaking;

            this.ableToRun          = true;
            this.usingUdpConnection = false;

            this.udpCollection = null;
            ///Setting an invalid pairing code.
            this.pairingCode = -1;

            ///Set to null, because inside GameServer this property is set to a proper reference.
            this.gameUser = null;

            this.connected = false;

            ///TCP Buffering
            this.tcpBuffer        = new IO.Memory.CyclicalMemoryBuffer(ServerSettings.PoolingCacheSize, (uint)ServerSettings.ServerBufferSize);
            this.packetizer       = new PacketHandler(this.tcpBuffer);
            this.tcpInEventsPool  = new SocketAsyncEventArgsPool(ServerSettings.PoolingCacheSize / 2, this.OnTCPIncomingDataComplete);
            this.tcpOutEventsPool = new SocketAsyncEventArgsPool(ServerSettings.PoolingCacheSize / 2, KSPMGlobals.Globals.KSPMServer.OnSendingOutgoingDataComplete);

            ///Setting UDP queues.
            this.incomingPackets = new CommandQueue();
            this.outgoingPackets = new CommandQueue();

            ///UDP Buffering
            this.udpCollection     = new ConnectionlessNetworkCollection(ServerSettings.ServerBufferSize);
            this.udpBuffer         = new IO.Memory.CyclicalMemoryBuffer(ServerSettings.PoolingCacheSize, (uint)ServerSettings.ServerBufferSize);
            this.udpPacketizer     = new PacketHandler(this.udpBuffer);
            this.udpInputSAEAPool  = new SharedBufferSAEAPool(ServerSettings.PoolingCacheSize, this.udpCollection.secondaryRawBuffer, this.OnUDPIncomingDataComplete);
            this.udpOutSAEAPool    = new SocketAsyncEventArgsPool(ServerSettings.PoolingCacheSize, this.OnUDPSendingDataComplete);
            this.udpIOMessagesPool = new MessagesPool(ServerSettings.PoolingCacheSize * 1000, new RawMessage(Message.CommandType.Null, null, 0));

            this.markedToDie = false;

            ///UDP Purge Timer
            this.udpPurgeTimer        = new Timer(this.HandleUDPPurgeTimerCallback);
            this.udpPurgeTimeInterval = (int)ServerSettings.PurgeTimeIterval;
            this.udpMinimumMessagesAllowedAfterPurge = (int)(this.incomingPackets.MaxCommandAllowed * (1.0f - ServerSettings.AvailablePercentAfterPurge));
            this.udpPurgeFlag = 0;

            this.timer = new System.Diagnostics.Stopwatch();
            this.timer.Start();

#if PROFILING
            this.profilerOutgoingMessages = new Profiler("UDP_ReceivingMessages");
            this.profilerPacketizer       = new Profiler("UDP_Packetizer");
#endif
        }
Example #2
0
        /// <summary>
        /// Stops the current client and make it unable to run again.
        /// </summary>
        public void ShutdownClient()
        {
            Message disposingMessage = null;

            ///***********************Killing threads code
            this.aliveFlag   = false;
            this.ableToRun   = false;
            this.connected   = false;
            this.markedToDie = true;///To avoid killing twice or more the same reference.

            ///***********************TCP Sockets code
            if (this.ownerNetworkCollection.socketReference != null)
            {
                if (this.ownerNetworkCollection.socketReference.Connected)
                {
                    this.ownerNetworkCollection.socketReference.Disconnect(false);
                }
                this.ownerNetworkCollection.socketReference.Close();
            }
            this.ownerNetworkCollection.Dispose();
            this.ownerNetworkCollection = null;

            ///****************UDP sockets code.
            if (this.udpCollection.socketReference != null)
            {
                this.udpCollection.socketReference.Close();
            }
            this.udpCollection.Dispose();
            this.udpCollection = null;

            ///User release.
            if (this.gameUser != null)
            {
                this.gameUser.Release();
                this.gameUser = null;
            }

            ///Sleeping some time to give the oportunity to complete the asynchronous methods.
            Thread.Sleep(500);

            ///Cleaning TCP buffers
            this.tcpBuffer.Release();
            this.tcpBuffer = null;
            this.packetizer.Release();
            this.packetizer = null;

            ///Cleaning TCP SAEAs
            this.tcpInEventsPool.Release(false);
            this.tcpInEventsPool = null;
            this.tcpOutEventsPool.Release(false);
            this.tcpOutEventsPool = null;

            ///Cleaning the UDP queues.
            this.incomingPackets.DequeueCommandMessage(out disposingMessage);
            while (disposingMessage != null)
            {
                this.udpIOMessagesPool.Recycle(disposingMessage);
                this.incomingPackets.DequeueCommandMessage(out disposingMessage);
            }
            this.outgoingPackets.DequeueCommandMessage(out disposingMessage);
            while (disposingMessage != null)
            {
                this.udpIOMessagesPool.Recycle(disposingMessage);
                this.outgoingPackets.DequeueCommandMessage(out disposingMessage);
            }
            this.incomingPackets.Purge(false);
            this.outgoingPackets.Purge(false);
            this.incomingPackets = null;
            this.outgoingPackets = null;

            ///Cleaning UDP buffers.
            this.udpBuffer.Release();
            this.udpBuffer = null;
            this.udpPacketizer.Release();
            this.udpPacketizer = null;

            ///Cleaning UDP SAEAs
            this.udpInputSAEAPool.Release(false);
            this.udpOutSAEAPool.Release(false);
            this.udpInputSAEAPool = null;
            this.udpOutSAEAPool   = null;

            ///Cleaning up udpPurgeTimer
            this.udpPurgeTimer.Dispose();
            this.udpPurgeTimer = null;

            KSPMGlobals.Globals.Log.WriteTo(string.Format("[{0}] ServerSide Client killed after {1} seconds alive.", this.id, this.AliveTime / 1000));

            this.timer.Reset();

#if PROFILING
            this.profilerPacketizer.Dispose();
            this.profilerPacketizer = null;
            this.profilerOutgoingMessages.Dispose();
            this.profilerOutgoingMessages = null;
#endif
        }