コード例 #1
0
ファイル: NodesGroup.cs プロジェクト: PlumpMath/LushCoin
 public NodesGroup(
     Network network,
     NodeConnectionParameters connectionParameters = null,
     NodeRequirement requirements = null)
 {
     AllowSameGroup        = false;
     MaximumNodeConnection = 8;
     _Network              = network;
     cs                    = new object();
     _ConnectedNodes       = new NodesCollection();
     _ConnectionParameters = connectionParameters ?? new NodeConnectionParameters();
     _ConnectionParameters = _ConnectionParameters.Clone();
     _Requirements         = requirements ?? new NodeRequirement();
     _Disconnect           = new CancellationTokenSource();
 }
コード例 #2
0
        public NodeConnectionParameters(NodeConnectionParameters other)
        {
            Version             = other.Version;
            IsRelay             = other.IsRelay;
            Services            = other.Services;
            ReceiveBufferSize   = other.ReceiveBufferSize;
            SendBufferSize      = other.SendBufferSize;
            ConnectCancellation = other.ConnectCancellation;
            UserAgent           = other.UserAgent;
            AddressFrom         = other.AddressFrom;
            Nonce       = other.Nonce;
            Advertize   = other.Advertize;
            ReuseBuffer = other.ReuseBuffer;
            PreferredTransactionOptions = other.PreferredTransactionOptions;

            foreach (var behavior in other.TemplateBehaviors)
            {
                TemplateBehaviors.Add(behavior.Clone());
            }
        }
コード例 #3
0
        public NodeServer(Network network, ProtocolVersion version = ProtocolVersion.PROTOCOL_VERSION,
                          int internalPort = -1)
        {
            AllowLocalPeers = true;
            InboundNodeConnectionParameters = new NodeConnectionParameters();
            internalPort      = internalPort == -1 ? network.DefaultPort : internalPort;
            _LocalEndpoint    = new IPEndPoint(IPAddress.Parse("0.0.0.0").MapToIPv6Ex(), internalPort);
            _Network          = network;
            _ExternalEndpoint = new IPEndPoint(_LocalEndpoint.Address, Network.DefaultPort);
            _Version          = version;
            var listener = new EventLoopMessageListener <IncomingMessage>(ProcessMessage);

            _MessageProducer.AddMessageListener(listener);
            OwnResource(listener);
            _ConnectedNodes          = new NodesCollection();
            _ConnectedNodes.Added   += _Nodes_NodeAdded;
            _ConnectedNodes.Removed += _Nodes_NodeRemoved;
            _ConnectedNodes.MessageProducer.AddMessageListener(listener);
            _Trace = new TraceCorrelation(NodeServerTrace.Trace, "Node server listening on " + LocalEndpoint);
        }
コード例 #4
0
ファイル: AddressManager.cs プロジェクト: PlumpMath/LushCoin
        internal void DiscoverPeers(Network network, NodeConnectionParameters parameters, int peerToFind)
        {
            TraceCorrelation traceCorrelation = new TraceCorrelation(NodeServerTrace.Trace, "Discovering nodes");
            int found = 0;

            using (traceCorrelation.Open())
            {
                while (found < peerToFind)
                {
                    parameters.ConnectCancellation.ThrowIfCancellationRequested();
                    NodeServerTrace.PeerTableRemainingPeerToGet(-found + peerToFind);
                    List <NetworkAddress> peers = new List <NetworkAddress>();
                    peers.AddRange(this.GetAddr());
                    if (peers.Count == 0)
                    {
                        PopulateTableWithDNSNodes(network, peers);
                        PopulateTableWithHardNodes(network, peers);
                        peers = new List <NetworkAddress>(peers.OrderBy(a => RandomUtils.GetInt32()));
                        if (peers.Count == 0)
                        {
                            return;
                        }
                    }


                    CancellationTokenSource peerTableFull = new CancellationTokenSource();
                    CancellationToken       loopCancel    = CancellationTokenSource.CreateLinkedTokenSource(peerTableFull.Token, parameters.ConnectCancellation).Token;
                    try
                    {
                        Parallel.ForEach(peers, new ParallelOptions()
                        {
                            MaxDegreeOfParallelism = 5,
                            CancellationToken      = loopCancel,
                        }, p =>
                        {
                            CancellationTokenSource timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
                            var cancelConnection            = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, loopCancel);
                            Node n = null;
                            try
                            {
                                var param2 = parameters.Clone();
                                param2.ConnectCancellation = cancelConnection.Token;
                                var addrman = param2.TemplateBehaviors.Find <AddressManagerBehavior>();
                                param2.TemplateBehaviors.Clear();
                                param2.TemplateBehaviors.Add(addrman);
                                n = Node.Connect(network, p.Endpoint, param2);
                                n.VersionHandshake(cancelConnection.Token);
                                n.MessageReceived += (s, a) =>
                                {
                                    var addr = (a.Message.Payload as AddrPayload);
                                    if (addr != null)
                                    {
                                        Interlocked.Add(ref found, addr.Addresses.Length);
                                        if (found >= peerToFind)
                                        {
                                            peerTableFull.Cancel();
                                        }
                                    }
                                };
                                n.SendMessageAsync(new GetAddrPayload());
                                loopCancel.WaitHandle.WaitOne(2000);
                            }
                            catch
                            {
                            }
                            finally
                            {
                                if (n != null)
                                {
                                    n.DisconnectAsync();
                                }
                            }
                            if (found >= peerToFind)
                            {
                                peerTableFull.Cancel();
                            }
                            else
                            {
                                NodeServerTrace.Information("Need " + (-found + peerToFind) + " more peers");
                            }
                        });
                    }
                    catch (OperationCanceledException)
                    {
                        if (parameters.ConnectCancellation.IsCancellationRequested)
                        {
                            throw;
                        }
                    }
                }
            }
        }
コード例 #5
0
ファイル: NodesGroup.cs プロジェクト: PlumpMath/LushCoin
 public static NodesGroup GetNodeGroup(NodeConnectionParameters parameters)
 {
     return(GetNodeGroup(parameters.TemplateBehaviors));
 }