本文整理汇总了C#中Peer.Disconnect方法的典型用法代码示例。如果您正苦于以下问题:C# Peer.Disconnect方法的具体用法?C# Peer.Disconnect怎么用?C# Peer.Disconnect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Peer
的用法示例。
在下文中一共展示了Peer.Disconnect方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public static void Run(string[] args)
{
// TODO: Assumes production network not testnet. Make it selectable.
var @params = NetworkParameters.ProdNet();
try
{
// Decode the private key from Satoshi's Base58 variant. If 51 characters long then it's from BitCoins
// "dumpprivkey" command and includes a version byte and checksum. Otherwise assume it's a raw key.
EcKey key;
if (args[0].Length == 51)
{
var dumpedPrivateKey = new DumpedPrivateKey(@params, args[0]);
key = dumpedPrivateKey.Key;
}
else
{
var privKey = Base58.DecodeToBigInteger(args[0]);
key = new EcKey(privKey);
}
Console.WriteLine("Address from private key is: " + key.ToAddress(@params));
// And the address ...
var destination = new Address(@params, args[1]);
// Import the private key to a fresh wallet.
var wallet = new Wallet(@params);
wallet.AddKey(key);
// Find the transactions that involve those coins.
using (var conn = new NetworkConnection(IPAddress.Loopback, @params, 0, 60000))
using (var blockStore = new MemoryBlockStore(@params))
{
var chain = new BlockChain(@params, wallet, blockStore);
var peer = new Peer(@params, conn, chain);
peer.Start();
peer.StartBlockChainDownload().Await();
// And take them!
Console.WriteLine("Claiming " + Utils.BitcoinValueToFriendlyString(wallet.GetBalance()) + " coins");
wallet.SendCoins(peer, destination, wallet.GetBalance());
// Wait a few seconds to let the packets flush out to the network (ugly).
Thread.Sleep(5000);
peer.Disconnect();
}
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("First arg should be private key in Base58 format. Second argument should be address to send to.");
}
}
示例2: Run
public static void Run(string[] args)
{
var file = new FileInfo(args[0]);
var wallet = Wallet.LoadFromFile(file);
Console.WriteLine(wallet.ToString());
// Set up the components and link them together.
var @params = NetworkParameters.TestNet();
var blockStore = new MemoryBlockStore(@params);
var conn = new NetworkConnection(IPAddress.Loopback, @params,
blockStore.GetChainHead().Height, 60000);
var chain = new BlockChain(@params, wallet, blockStore);
var peer = new Peer(@params, conn, chain);
peer.Start();
wallet.CoinsReceived +=
(sender, e) =>
{
Console.WriteLine();
Console.WriteLine("Received tx " + e.Tx.HashAsString);
Console.WriteLine(e.Tx.ToString());
};
// Now download and process the block chain.
var progress = peer.StartBlockChainDownload();
var max = progress.Count; // Racy but no big deal.
if (max > 0)
{
Console.WriteLine("Downloading block chain. " + (max > 1000 ? "This may take a while." : ""));
var current = max;
while (current > 0)
{
var pct = 100.0 - (100.0*(current/(double) max));
Console.WriteLine(string.Format("Chain download {0}% done", (int) pct));
progress.Await(TimeSpan.FromSeconds(1));
current = progress.Count;
}
}
peer.Disconnect();
wallet.SaveToFile(file);
Console.WriteLine();
Console.WriteLine("Done!");
Console.WriteLine();
Console.WriteLine(wallet.ToString());
}
示例3: 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();
}
}
示例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()
{
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);
}
}
示例5: DisconnectPeer
private void DisconnectPeer(Peer peer)
{
peer.Disconnect();
Events.RaiseAsync(ConnectionClosed, this, new ConnectionEventArgs(peer.EndPoint));
}
示例6: DisconnectPeer
public void DisconnectPeer(Peer peer)
{
peers.Remove(peer);
peer.Disconnect();
}
示例7: 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);
}