本文整理汇总了C#中Peer.ConnectAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Peer.ConnectAsync方法的具体用法?C# Peer.ConnectAsync怎么用?C# Peer.ConnectAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Peer
的用法示例。
在下文中一共展示了Peer.ConnectAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConnectAndHandshake
private async Task ConnectAndHandshake(Peer peer)
{
peer.OnDisconnect += DisconnectPeer;
if (peer.IsIncoming)
Interlocked.Increment(ref this.incomingCount);
// connect
await peer.ConnectAsync();
// notify peer is connected
PeerConnected?.Invoke(peer);
// setup task to wait for verack
var verAckTask = peer.Receiver.WaitForMessage(x => x.Command == "verack", HANDSHAKE_TIMEOUT_MS);
// setup task to wait for version
var versionTask = peer.Receiver.WaitForMessage(x => x.Command == "version", HANDSHAKE_TIMEOUT_MS);
// start listening for messages after tasks have been setup
peer.Receiver.Listen();
// send our local version
var nodeId = random.NextUInt64(); //TODO should be generated and verified on version message
var currentHeight = this.coreDaemon.CurrentChain.Height;
await peer.Sender.SendVersion(Messaging.GetExternalIPEndPoint(), peer.RemoteEndPoint, nodeId, (UInt32)currentHeight);
// wait for our local version to be acknowledged by the remote peer
// wait for remote peer to send their version
await Task.WhenAll(verAckTask, versionTask);
//TODO shouldn't have to decode again
var versionMessage = versionTask.Result;
var versionPayload = NetworkEncoder.DecodeVersionPayload(versionMessage.Payload.ToArray(), versionMessage.Payload.Length);
var remoteAddressWithTime = new NetworkAddressWithTime
(
Time: DateTimeOffset.Now,
NetworkAddress: new NetworkAddress
(
Services: versionPayload.LocalAddress.Services,
IPv6Address: versionPayload.LocalAddress.IPv6Address,
Port: versionPayload.LocalAddress.Port
)
);
// acknowledge their version
await peer.Sender.SendVersionAcknowledge();
}