private async Task ReceiveMessageAsync <T>( Blockchain <T> blockchain, CancellationToken cancellationToken) where T : IAction { CheckEntered(); while (true) { NetMQMessage rawMessage = await _router.ReceiveMultipartMessageAsync( cancellationToken : cancellationToken); try { Message message = Message.Parse(rawMessage, reply: false); _logger.Debug($"Message[{message}] received."); // Queue a task per message to avoid blocking. #pragma warning disable CS4014 Task.Run(async() => { // it's still async because some method it relies are // async yet. await ProcessMessageAsync(blockchain, message, cancellationToken); }); #pragma warning restore CS4014 } catch (InvalidMessageException e) { _logger.Error(e, "Can't parsed NetMQMessage properly. ignored."); } } }
private async Task ListenerTaskAsync() { _logger.LogInformation("Command Receiver Worker Started!"); while (true) { var paramMsg = await _router.ReceiveMultipartMessageAsync(); var cmdParam = paramMsg[2].ConvertToString(Encoding.UTF8); var cmdType = paramMsg[1].ConvertToString(); _logger.LogInformation("From Pusher : {0}", cmdParam); try { var cmd = BuildCommand(cmdParam, cmdType); if (cmd != null) { await _mediator.Publish(cmd); } } catch (Exception e) { _logger.LogError(e.ToString()); // _router.SendMultipartMessage(BuildSendBackMessage(paramMsg[0].Buffer, e.ToString())); } } }
public void SupportCancellation() { async Task ReceiveAsync() { using (var server = new RouterSocket("inproc://async")) { var cts = new CancellationTokenSource(); cts.CancelAfter(1000); await Assert.ThrowsAsync <TaskCanceledException>( async() => await server.ReceiveMultipartMessageAsync(cancellationToken: cts.Token) ); await Assert.ThrowsAsync <TaskCanceledException>( async() => await server.ReceiveFrameBytesAsync(cts.Token) ); await Assert.ThrowsAsync <TaskCanceledException>( async() => await server.ReceiveFrameStringAsync(cts.Token) ); } } using (var runtime = new NetMQRuntime()) { var t = ReceiveAsync(); runtime.Run(t); if (t.IsFaulted && t.Exception is AggregateException exc) { throw exc.GetBaseException(); } } }
public void SendReceiveMultipartMessage() { async Task SendReceiveAsync() { using (var server = new RouterSocket("inproc://async")) using (var client = new DealerSocket("inproc://async")) { var req = new NetMQMessage(); req.Append("Hello"); req.Append(new byte[] { 0x00, 0x01, 0x02, 0x03 }); await client.SendMultipartMessageAsync(req); NetMQMessage received = await server.ReceiveMultipartMessageAsync(); Assert.Equal("Hello", received[1].ConvertToString()); Assert.Equal(new byte[] { 0x00, 0x01, 0x02, 0x03 }, received[2].Buffer); } } using (var runtime = new NetMQRuntime()) { var t = SendReceiveAsync(); runtime.Run(t); if (t.IsFaulted && t.Exception is AggregateException exc) { throw exc.GetBaseException(); } } }
public async Task ReceiveMessageAsync() { CheckEntered(); while (true) { NetMQMessage message = await _router.ReceiveMultipartMessageAsync(); _logger.Debug($"Message received.[f-count: {message.FrameCount}]"); byte[] addr = message[0].ToByteArray(); var type = (MessageType)message[1].Buffer[0]; switch (type) { case MessageType.Ping: _logger.Debug($"Ping received."); NetMQMessage reply = CreateMessage(MessageType.Pong, addr); await _router.SendMultipartMessageAsync(reply); break; case MessageType.PeerSetDelta: byte[] payload = message[2].ToByteArray(); var(publicKey, msg) = VerifyMessage(payload); using (var stream = new MemoryStream(msg)) { var formatter = new BinaryFormatter(); _deltas.Enqueue((PeerSetDelta)formatter.Deserialize(stream)); } break; default: _logger.Warning($"Can't recognize message type[{type}]. ignored."); break; } } }