예제 #1
0
 public void Run()
 {
     if (!TryStartListening())
     {
         return;
     }
     while (true)
     {
         try
         {
             IConnectionClient client = server.Accept();
             if (client == null)
             {
                 break;
             }
             ManagedClient managedClient = new ManagedClient(client, nextId++);
             lock (clientsLocker)
             {
                 if (state.Value == CSState.Listening || state.Value == CSState.AgentsAccepting)
                 {
                     clients.Add(managedClient);
                     tasks.Add(Task.Run(() => HandleCommunication(managedClient)));
                 }
             }
         }
         catch (SocketException)
         {
             CSLogger.LogError("Some error with TCPListener occured.\n");
             Kill();
         }
     }
     CSLogger.Log($"Communication Server stopped listening for clients.\n");
     Task.WaitAll(tasks.ToArray());
 }