/// <summary> /// Invoke the handler pipeline and process the passed message. /// </summary> /// <param name="body">The body.</param> /// <param name="wallet">The wallet.</param> /// <param name="pool">The pool.</param> /// <returns></returns> /// <exception cref="Exception">Expected inner message to be of type 'ForwardMessage'</exception> /// <exception cref="AgentFrameworkException">Couldn't locate a message handler for type {messageType}</exception> protected async Task <byte[]> ProcessAsync(byte[] body, Wallet wallet, Pool pool = null) { EnsureConfigured(); var agentContext = new AgentContext { Wallet = wallet, Pool = pool }; agentContext.AddNext(new MessagePayload(body, true)); AgentMessage outgoingMessage = null; while (agentContext.TryGetNext(out var message) && outgoingMessage == null) { outgoingMessage = await ProcessMessage(agentContext, message); } if (outgoingMessage != null) // && dont duplex???? { //TODO what happens when I fail to transmit the message? need to roll back the state of the internal message? await MessageService.SendToConnectionAsync(wallet, outgoingMessage, agentContext.Connection); outgoingMessage = null; } byte[] response = null; if (outgoingMessage != null) { response = await MessageService.PrepareAsync(wallet, outgoingMessage, ""); } return(response); }
/// <summary> /// Invoke the handler pipeline and process the passed message. /// </summary> /// <param name="body">The body.</param> /// <param name="wallet">The wallet.</param> /// <param name="pool">The pool.</param> /// <returns></returns> /// <exception cref="Exception">Expected inner message to be of type 'ForwardMessage'</exception> /// <exception cref="AgentFrameworkException">Couldn't locate a message handler for type {messageType}</exception> protected async Task ProcessAsync(byte[] body, Wallet wallet, Pool pool = null) { EnsureConfigured(); var agentContext = new AgentContext { Wallet = wallet, Pool = pool }; agentContext.AddNext(new MessagePayload(body, true)); while (agentContext.TryGetNext(out var message)) { MessagePayload messagePayload; if (message.Packed) { var unpacked = await CryptoUtils.UnpackAsync(agentContext.Wallet, message.Payload); messagePayload = new MessagePayload(unpacked.Message, false); } else { messagePayload = message; } if (_handlers.Where(handler => handler != null).FirstOrDefault( handler => handler.SupportedMessageTypes.Any( type => type.Equals(messagePayload.GetMessageType(), StringComparison.OrdinalIgnoreCase))) is IMessageHandler messageHandler) { await messageHandler.ProcessAsync(agentContext, messagePayload); } else { throw new AgentFrameworkException(ErrorCode.InvalidMessage, $"Couldn't locate a message handler for type {messagePayload.GetMessageType()}"); } } }