Exemple #1
0
        /// <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()}");
                }
            }
        }