当前位置: 首页>>代码示例>>C#>>正文


C# Peer.Connect方法代码示例

本文整理汇总了C#中Peer.Connect方法的典型用法代码示例。如果您正苦于以下问题:C# Peer.Connect方法的具体用法?C# Peer.Connect怎么用?C# Peer.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Peer的用法示例。


在下文中一共展示了Peer.Connect方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        public static void Run(string[] args)
        {
            Console.WriteLine("Connecting to node");
            var networkParams = NetworkParameters.ProdNet();

            using (var blockStore = new MemoryBlockStore(networkParams))
            {
                var chain = new BlockChain(networkParams, blockStore);
                var peer = new Peer(networkParams, new PeerAddress(IPAddress.Loopback), chain);
                peer.Connect();
                new Thread(() => peer.Run(CancellationToken.None)).Start();

                var blockHash = new Sha256Hash(args[0]);
                var future = peer.BeginGetBlock(blockHash, null, null);
                Console.WriteLine("Waiting for node to send us the requested block: " + blockHash);
                var block = peer.EndGetBlock(future);
                Console.WriteLine(block);
                peer.Disconnect();
            }
        }
开发者ID:Virus-X,项目名称:CoinSharp,代码行数:20,代码来源:FetchBlock.cs

示例2: ConnectToPeers

 private static void ConnectToPeers(List<PeerInfo> peers, Torrent torrent)
 {
     foreach(PeerInfo peerInfo in peers)
     {
         Peer remotePeer = new Peer(peerInfo, torrent, PeerId.GetDefaultPeerId());
         Console.WriteLine("Connecting to {0}:{1}", peerInfo.IpAddress, peerInfo.Port);
         remotePeer.Connected += delegate(object sender, EventArgs args)
             {
                 Peer peer = sender as Peer;
                 Console.WriteLine("Connected to peer at {0} on port {1}", peer.Info.IpAddress, (ushort)peer.Info.Port);
             };
         remotePeer.HandshakeReceived += delegate(object sender, PeerEventArgs args)
             {
                 Peer peer = sender as Peer;
                 Console.WriteLine("Received handshake from {0}", peer.Info.IpAddress);
             };
         remotePeer.Error += delegate(object sender, PeerEventArgs args)
             {
                 Peer peer = sender as Peer;
                 System.Diagnostics.Debug.WriteLine(string.Format("Error from {0}: {1}", peer.Info.IpAddress, args.Exception.Message));
             };
         remotePeer.Connect();
     }
 }
开发者ID:worldspuppet,项目名称:torrentnet,代码行数:24,代码来源:Program.cs

示例3: TryNextPeer

        /// <summary>
        /// Try connecting to a peer. If we exceed the number of connections, delay and try
        /// again.
        /// </summary>
        /// <exception cref="ThreadInterruptedException"/>
        private void TryNextPeer()
        {
            var address = _inactives.Take();
            while (true)
            {
                try
                {
                    var peer = new Peer(_params, address, _blockStore.GetChainHead().Height, _chain);
                    _peerPool.Execute(
                        () =>
                        {
                            try
                            {
                                _log.Info("Connecting to " + peer);
                                peer.Connect();
                                _peers.Add(peer);
                                HandleNewPeer(peer);
                                peer.Run();
                            }
                            catch (PeerException ex)
                            {
                                // Do not propagate PeerException - log and try next peer. Suppress stack traces for
                                // exceptions we expect as part of normal network behaviour.
                                var cause = ex.InnerException;
                                if (cause is SocketException)
                                {
                                    if (((SocketException) cause).SocketErrorCode == SocketError.TimedOut)
                                        _log.Info("Timeout talking to " + peer + ": " + cause.Message);
                                    else
                                        _log.Info("Could not connect to " + peer + ": " + cause.Message);
                                }
                                else if (cause is IOException)
                                {
                                    _log.Info("Error talking to " + peer + ": " + cause.Message);
                                }
                                else
                                {
                                    _log.Error("Unexpected exception whilst talking to " + peer, ex);
                                }
                            }
                            finally
                            {
                                // In all cases, disconnect and put the address back on the queue.
                                // We will retry this peer after all other peers have been tried.
                                peer.Disconnect();

                                _inactives.Add(address);
                                if (_peers.Remove(peer))
                                    HandlePeerDeath(peer);
                            }
                        });
                    break;
                }
                catch (RejectedExecutionException)
                {
                    // Reached maxConnections, try again after a delay

                    // TODO - consider being smarter about retry. No need to retry
                    // if we reached maxConnections or if peer queue is empty. Also consider
                    // exponential backoff on peers and adjusting the sleep time according to the
                    // lowest backoff value in queue.
                }
                catch (BlockStoreException e)
                {
                    // Fatal error
                    _log.Error("Block store corrupt?", e);
                    _running = false;
                    throw new Exception(e.Message, e);
                }

                // If we got here, we should retry this address because an error unrelated
                // to the peer has occurred.
                Thread.Sleep(_connectionDelayMillis);
            }
        }
开发者ID:TangibleCryptography,项目名称:BitSharper,代码行数:80,代码来源:PeerGroup.cs

示例4: TryNextPeer

        /// <summary>
        ///     Try connecting to a peer. If we exceed the number of connections, delay and try
        ///     again.
        /// </summary>
        /// <exception cref="ThreadInterruptedException" />
        private void TryNextPeer()
        {
            PeerAddress address = _inactives.Take();
            try
            {
                var peer = new Peer(_networkParameters, address, _blockStore.GetChainHead().Height, _blockChain);
                Task.Factory.StartNew(
                    () =>
                    {
                        while (true)
                        {
                            try
                            {
                                //Log.Info("Connecting to " + peer);
                                peer.Connect();
                                //Log.Info("Connected to " + peer);
                                _peers.Add(peer);
                                Log.Info("Addded peer to list of peers " + peer);
                                HandleNewPeer(peer);
                                Log.Info("Handled new peer " + peer);
                                peer.Run();
                                Log.Info("Peer is running " + peer);
                            }
                            catch (PeerException ex)
                            {
                                //Do not propagate PeerException - log and try next peer. Suppress stack traces for
                                //exceptions we expect as part of normal network behaviour.
                                Exception cause = ex.InnerException;
                                var exception = cause as SocketException;
                                if (exception != null)
                                {
                                    if (exception.SocketErrorCode == SocketError.TimedOut)
                                        Log.Info("Timeout talking to " + peer + ": " + cause.Message);
                                    else
                                        Log.Info("Could not connect to " + peer + ": " + cause.Message);
                                }
                                else if (cause is IOException)
                                {
                                    Log.Info("Error talking to " + peer + ": " + cause.Message);
                                }
                                else
                                {
                                    Log.Error("Unexpected exception whilst talking to " + peer, ex);
                                }
                            }
                            catch (Exception exception)
                            {
                                Log.Error("Boom: " + peer, exception);
                            }
                            finally
                            {
                                //In all cases, disconnect and put the address back on the queue.
                                //We will retry this peer after all other peers have been tried.
                                peer.Disconnect();

                                _inactives.Add(address);
                                //TODO: Ensure this is the logic that we expect.
                                if (_peers.TryTake(out peer))
                                    HandlePeerDeath(peer);
                            }
                        }
                    }, TaskCreationOptions.LongRunning);
            }
                //catch (RejectedExecutionException)
                //{
                //    // Reached maxConnections, try again after a delay

                //    // TODO - consider being smarter about retry. No need to retry
                //    // if we reached maxConnections or if peer queue is empty. Also consider
                //    // exponential backoff on peers and adjusting the sleep time according to the
                //    // lowest backoff value in queue.
                //}
            catch (BlockStoreException e)
            {
                // Fatal error
                Log.Error("Block store corrupt?", e);
                _running = false;
                throw new Exception(e.Message, e);
            }

            // If we got here, we should retry this address because an error unrelated
            // to the peer has occurred.
            // TODO: Code is unreachable?
            Thread.Sleep(_connectionDelayMillis);
        }
开发者ID:perljedi,项目名称:BitcoinSharp,代码行数:90,代码来源:PeerGroup.cs


注:本文中的Peer.Connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。