示例#1
0
        public async Task Connect(NodeInfo nodeInfo, Uri endPoint = null)
        {
            if (_node.HasQuit)
            {
                return;
            }

            if (nodeInfo == null)
            {
                return;
            }

            var accept = false;

            lock (_lock)
                accept = _outgoingConnections.Count <= _maxOutgoingConnections;

            if (!accept)
            {
                return;
            }

            if (IsConnected(nodeInfo, true))
            {
                return;
            }

            if (endPoint == null)
            {
                endPoint = nodeInfo.PublicEndPoint;
            }

            var client = new NodeClient(endPoint);

            var connection = await client.OpenNodeConnection();

            if (connection.Connected)
            {
                if (Log.LogTrace)
                {
                    Log.Trace($"NodeServer ({_kademlia.LocalNodeInfo.PublicEndPoint}) connecting {nodeInfo.PublicEndPoint}");
                }

                connection.NodeInfo              = nodeInfo;
                connection.OutgoingConnection    = true;
                connection.ConnectionClosedEvent = ConnectionClosed;
                connection.ConnectionList        = _pendingConnections;

                lock (_lock)
                    _pendingConnections.Add(connection);

                TaskRunner.Run(() => connection.Receive(this));

                await connection.Send(new NodeInfoMessage(_kademlia.NodeConfiguration, nodeInfo.NodeId) { SignKey = _kademlia.LocalKey });
            }
        }