Ejemplo n.º 1
0
        private void ListenerLoop()
        {
            while (_listener != null)
            {
                var incoming = new NodeConnection(this, _listener.AcceptTcpClient());
                Debug.WriteLine("Подключение к серверу c " + incoming.NetworkAddress.StreamHostPort);

                lock (_nodeConnections)
                {
                    foreach (var client in _nodeConnections)
                    {
                        if (client.NetworkAddress.StreamHostPort == incoming.NetworkAddress.StreamHostPort)
                        {
                            client.Stop();
                        }
                    }

                    _nodeConnections.Add(incoming);
                }
                incoming.Listen();
            }
        }
Ejemplo n.º 2
0
        private void ListenerLoop()
        {
            while (_listener != null)
            {
                var incoming = new NodeConnection(this, _listener.AcceptTcpClient());
                Debug.WriteLine("Подключение к серверу c " + incoming.NetworkAddress.StreamHostPort);

                lock (_nodeConnections)
                {
                    foreach (var client in _nodeConnections)
                        if (client.NetworkAddress.StreamHostPort == incoming.NetworkAddress.StreamHostPort)
                            client.Stop();

                    _nodeConnections.Add(incoming);
                }
                incoming.Listen();
            }
        }
Ejemplo n.º 3
0
 private void ClientsControlLoop()
 {
     // проверяю _listener, т.к. по такому же условию заканчивается цикл ListenerLoop
     while (_listener != null)
     {
         lock (_nodeConnections)
         {
             // Удаляю неподключенные клиенты
             for (int i = _nodeConnections.Count - 1; i >= 0; --i)
                 if (!_nodeConnections[i].Connected)
                     _nodeConnections.RemoveAt(i);
             // Довожу количество подключенных клиентов до 8
             if (_nodeConnections.Count < 8)
             {
                 var nodes = DB.Table<NetworkAddress>().OrderByDescending(x => x.TimeLastSeen).ToListAsync().Result;
                 foreach (NetworkAddress node in nodes)
                 {
                     bool find = _nodeConnections.Any(c => c.NetworkAddress.StreamHostPort == node.StreamHostPort);
                     if (!find)
                     {
                         var c = new NodeConnection(this, node);
                         try
                         {
                             //Debug.WriteLine("Пытаюсь подключиться к " + node.HostStreamPort);
                             c.Connect();
                         }
                         catch (Exception e)
                         {
                             Debug.WriteLine(node + " " + e);
                         }
                         if (c.Connected)
                         {
                             //Debug.WriteLine("Подключился к " + node.HostStreamPort);
                             _nodeConnections.Add(c);
                         }
                         if (_nodeConnections.Count >= 8) break;
                     }
                 }
             }
         }
         // Сплю минуту, или если уведомят из Helper.ReadHeaderMagic
         NodeIsDisconnected.WaitOne(60*1000);
     }
 }