private async Task EstablishingConnectionAsync(NetMQMessage message, CancellationToken token) { try { _dealerSocket.SendMultipartMessage(message); var msg = await _dealerSocket.ReceiveMultipartMessageAsync(cancellationToken : token); Console.WriteLine($"From Server : {msg[1].ConvertToString()}"); } catch (OperationCanceledException e) { Console.WriteLine("服务器无响应"); } }
internal IAsyncEnumerable <Block <T> > GetBlocksAsync <T>( DealerSocket sock, IEnumerable <HashDigest <SHA256> > blockHashes, CancellationToken cancellationToken) where T : IAction { return(new AsyncEnumerable <Block <T> >(async yield => { var request = new GetBlocks(blockHashes); await sock.SendMultipartMessageAsync( request.ToNetMQMessage(_privateKey), cancellationToken: cancellationToken); int hashCount = blockHashes.Count(); while (hashCount > 0) { NetMQMessage response = await sock.ReceiveMultipartMessageAsync( cancellationToken: cancellationToken); Message parsedMessage = Message.Parse(response, true); if (parsedMessage is Block blockMessage) { Block <T> block = Block <T> .FromBencodex( blockMessage.Payload); await yield.ReturnAsync(block); hashCount--; } else { throw new InvalidMessageException( $"The response of getdata isn't block. " + $"but {parsedMessage}"); } } })); }
internal IAsyncEnumerable <Transaction <T> > GetTxsAsync <T>( DealerSocket socket, IEnumerable <TxId> txIds, CancellationToken cancellationToken = default(CancellationToken)) where T : IAction { return(new AsyncEnumerable <Transaction <T> >(async yield => { var request = new GetTxs(txIds); await socket.SendMultipartMessageAsync( request.ToNetMQMessage(_privateKey), cancellationToken: cancellationToken); int hashCount = txIds.Count(); while (hashCount > 0) { NetMQMessage response = await socket.ReceiveMultipartMessageAsync( cancellationToken: cancellationToken); Message parsedMessage = Message.Parse(response, true); if (parsedMessage is Messages.Tx parsed) { Transaction <T> tx = Transaction <T> .FromBencodex( parsed.Payload); await yield.ReturnAsync(tx); hashCount--; } else { throw new InvalidMessageException( $"The response of getdata isn't block. " + $"but {parsedMessage}"); } } })); }
internal async Task <IEnumerable <HashDigest <SHA256> > > GetBlockHashesAsync( DealerSocket sock, BlockLocator locator, HashDigest <SHA256>?stop, CancellationToken cancellationToken) { var request = new GetBlockHashes(locator, stop); await sock.SendMultipartMessageAsync( request.ToNetMQMessage(_privateKey), cancellationToken : cancellationToken); NetMQMessage response = await sock.ReceiveMultipartMessageAsync(); Message parsedMessage = Message.Parse(response, reply: true); if (parsedMessage is BlockHashes blockHashes) { return(blockHashes.Hashes); } throw new InvalidMessageException( $"The response of GetBlockHashes isn't BlockHashes. " + $"but {parsedMessage}"); }
public async Task GetMultipleBlocksAtOnce() { var privateKey = new PrivateKey(); BlockChain <DumbAction> chainA = _blockchains[0]; BlockChain <DumbAction> chainB = _blockchains[1]; Swarm <DumbAction> swarmA = _swarms[0]; Swarm <DumbAction> swarmB = new Swarm <DumbAction>( chainB, privateKey, 1, host: IPAddress.Loopback.ToString()); Block <DumbAction> genesis = chainA.MineBlock(_fx1.Address1); chainB.Append(genesis); // chainA and chainB shares genesis block. chainA.MineBlock(_fx1.Address1); chainA.MineBlock(_fx1.Address1); try { await StartAsync(swarmA); await StartAsync(swarmB); var peer = swarmA.AsPeer; await swarmB.AddPeersAsync(new[] { peer }); IEnumerable <HashDigest <SHA256> > hashes = await swarmB.GetBlockHashesAsync( peer, new BlockLocator(new[] { genesis.Hash }), null); var netMQAddress = $"tcp://{peer.EndPoint.Host}:{peer.EndPoint.Port}"; using (var socket = new DealerSocket(netMQAddress)) { var request = new GetBlocks(hashes, 2); await socket.SendMultipartMessageAsync( request.ToNetMQMessage(privateKey)); NetMQMessage response = await socket.ReceiveMultipartMessageAsync(); Message parsedMessage = Message.Parse(response, true); Libplanet.Net.Messages.Blocks blockMessage = (Libplanet.Net.Messages.Blocks)parsedMessage; Assert.Equal(2, blockMessage.Payloads.Count); response = await socket.ReceiveMultipartMessageAsync(); parsedMessage = Message.Parse(response, true); blockMessage = (Libplanet.Net.Messages.Blocks)parsedMessage; Assert.Single(blockMessage.Payloads); } } finally { await Task.WhenAll( swarmA.StopAsync(), swarmB.StopAsync()); } }