示例#1
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing,
        /// or resetting unmanaged resources.
        /// </summary>
        protected virtual void Dispose(bool aDisposing)
        {
            if (!mIsDisposed)
            {
                if (aDisposing)
                {
                    if (Player != null)
                    {
                        Player.Dispose();
                    }

                    if (mSocket != null)
                    {
                        mSocket.Dispose();
                    }
                }

                Player         = null;
                CurTask        = null;
                mNetworkWorker = null;
                mSocket        = null;

                mIsDisposed = true;
            }
        }
示例#2
0
        /// <summary>
        /// Create a new client based on the newly connected socket.
        /// </summary>
        /// <param name="aSocket">The socket of the client.</param>
        public Client(TcpSocket aSocket)
        {
            mSocket = aSocket;
            mCipher = new TqCipher();

            Account   = null;
            AccountID = 0;

            mNetworkWorker = null;
            Server.NetworkIO.AddClient(this, ref mNetworkWorker);
        }
示例#3
0
        /// <summary>
        /// Unassign the client to a worker.
        /// </summary>
        /// <param name="aClient">The client to unassign.</param>
        /// <param name="aWorker">The worker handling the networking I/O of the client.</param>
        public void DelClient(Client aClient, ref INetworkWorker aWorker)
        {
            if (aWorker == null)
            {
                return;
            }

            bool success = false;

            while (!success)
            {
                int nbClients;
                if (!mClients.TryGetValue(aWorker.Id, out nbClients))
                {
                    break;
                }

                success = mClients.TryUpdate(aWorker.Id, nbClients - 1, nbClients);
            }

            aWorker = null;
        }
示例#4
0
        /// <summary>
        /// Assign the client to one worker.
        /// </summary>
        /// <param name="aClient">The client to assign.</param>
        /// <param name="aWorker">The worker that will handle the networking I/O of the client.</param>
        public void AddClient(Client aClient, ref INetworkWorker aWorker)
        {
            if (aWorker != null)
            {
                return;
            }

            int workerId      = 0;
            int lastNbClients = int.MaxValue;

            foreach (var kv in mClients)
            {
                if (kv.Value < lastNbClients)
                {
                    workerId      = kv.Key;
                    lastNbClients = kv.Value;
                }
            }

            sLogger.Debug("{0}:{1} will use the I/O worker n° {2}.",
                          aClient.IPAddress,
                          aClient.Port,
                          workerId);

            bool success = false;

            while (!success)
            {
                int nbClients;
                if (!mClients.TryGetValue(workerId, out nbClients))
                {
                    break;
                }

                success = mClients.TryUpdate(workerId, nbClients + 1, nbClients);
            }

            aWorker = mWorkers[workerId];
        }