/// <summary> /// Sets profile server's network identifier. /// </summary> /// <param name="NetworkId">Profile server's network identifier.</param> public void SetNetworkId(byte[] NetworkId) { log.Trace("(NetworkId:'{0}')", Crypto.ToHex(NetworkId)); List <ProfileServer> serversToNotify = null; lock (internalLock) { networkId = NetworkId; initialized = true; if (initializationNeighborhoodNotificationList.Count != 0) { serversToNotify = initializationNeighborhoodNotificationList.ToList(); initializationNeighborhoodNotificationList.Clear(); } } if (serversToNotify != null) { log.Debug("Sending neighborhood notification to {0} profile servers.", serversToNotify.Count); foreach (ProfileServer ps in serversToNotify) { ps.LocServer.AddNeighborhood(new List <ProfileServer>() { this }); } } log.Trace("(-)"); }
/// <summary> /// Thread procedure that is responsible for accepting new clients on the TCP server port. /// </summary> public void AcceptThread() { log.Trace("()"); acceptThreadFinished.Reset(); AutoResetEvent acceptTaskEvent = new AutoResetEvent(false); while (!isShutdown) { log.Debug("Waiting for new client."); Task <TcpClient> acceptTask = listener.AcceptTcpClientAsync(); acceptTask.ContinueWith(t => acceptTaskEvent.Set()); WaitHandle[] handles = new WaitHandle[] { acceptTaskEvent, shutdownEvent }; int index = WaitHandle.WaitAny(handles); if (handles[index] == shutdownEvent) { log.Debug("Shutdown detected."); break; } try { // acceptTask is finished here, asking for Result won't block. TcpClient client = acceptTask.Result; log.Debug("New client '{0}' accepted.", client.Client.RemoteEndPoint); ClientHandlerAsync(client); } catch (Exception e) { log.Error("Exception occurred: {0}", e.ToString()); } } acceptThreadFinished.Set(); log.Trace("(-)"); }