/// <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; } }
/// <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); }
/// <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; }
/// <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]; }