Example #1
0
        public GrpcReceiveContext(GrpcTransportMessage message, GrpcReceiveEndpointContext receiveEndpointContext)
            : base(false, receiveEndpointContext)
        {
            _body   = message.Body;
            Message = message;

            HeaderProvider = new GrpcHeaderProvider(message.Headers);
        }
            public async Task Deliver(GrpcTransportMessage message, CancellationToken cancellationToken)
            {
                if (IsStopped)
                {
                    return;
                }

                await _channel.Writer.WriteAsync(message, cancellationToken).ConfigureAwait(false);
            }
        public async Task Deliver(GrpcTransportMessage message, CancellationToken cancellationToken)
        {
            var transportMessage = new TransportMessage(message.Message)
            {
                Deliver =
                {
                    Receiver       = new ReceiverDestination
                    {
                        QueueName  = _queueName,
                        ReceiverId = _receiverId
                    }
                }
            };

            var grpcTransportMessage = new GrpcTransportMessage(transportMessage, message.Host);

            await _node.DeliverMessage(grpcTransportMessage).ConfigureAwait(false);
        }
        protected async Task Move(ReceiveContext context, Action <GrpcTransportMessage, SendHeaders> preSend)
        {
            if (context.TryGetPayload(out GrpcTransportMessage receivedMessage))
            {
                var message = new TransportMessage
                {
                    MessageId = receivedMessage.Message.MessageId,
                    Deliver   = new Deliver(receivedMessage.Message.Deliver)
                    {
                        Exchange = new ExchangeDestination {
                            Name = _exchange.Name
                        }
                    }
                };

                var transportMessage = new GrpcTransportMessage(message, receivedMessage.Host);

                preSend(transportMessage, transportMessage.SendHeaders);

                await _exchange.Send(transportMessage, CancellationToken.None).ConfigureAwait(false);
            }
        }
        async Task ISendTransport.Send <T>(T message, IPipe <SendContext <T> > pipe, CancellationToken cancellationToken)
        {
            LogContext.SetCurrentIfNull(_context.LogContext);

            var context = new TransportGrpcSendContext <T>(_context.Exchange.Name, message, cancellationToken);

            await pipe.Send(context).ConfigureAwait(false);

            StartedActivity?activity = LogContext.IfEnabled(OperationName.Transport.Send)?.StartSendActivity(context);

            try
            {
                if (_context.SendObservers.Count > 0)
                {
                    await _context.SendObservers.PreSend(context).ConfigureAwait(false);
                }

                var messageId = context.MessageId ?? NewId.NextGuid();

                var transportMessage = new TransportMessage
                {
                    Deliver = new Deliver
                    {
                        Exchange = new ExchangeDestination
                        {
                            Name       = _context.Exchange.Name,
                            RoutingKey = context.RoutingKey ?? ""
                        },
                        Envelope = new Envelope
                        {
                            MessageId          = messageId.ToString("D"),
                            RequestId          = context.RequestId?.ToString("D") ?? "",
                            ConversationId     = context.ConversationId?.ToString("D") ?? "",
                            CorrelationId      = context.CorrelationId?.ToString("D") ?? "",
                            InitiatorId        = context.InitiatorId?.ToString("D") ?? "",
                            SourceAddress      = context.SourceAddress?.ToString() ?? "",
                            DestinationAddress = context.DestinationAddress?.ToString() ?? "",
                            ResponseAddress    = context.ResponseAddress?.ToString() ?? "",
                            FaultAddress       = context.FaultAddress?.ToString() ?? "",
                            ContentType        = context.ContentType?.MediaType ?? "",
                            Body           = ByteString.CopyFrom(context.Body),
                            EnqueueTime    = context.Delay.ToFutureDateTime(),
                            ExpirationTime = context.TimeToLive.ToFutureDateTime(),
                            SentTime       = Timestamp.FromDateTime(context.SentTime ?? DateTime.UtcNow),
                        }
                    }
                };

                transportMessage.Deliver.Envelope.MessageType.AddRange(TypeMetadataCache <T> .MessageTypeNames);

                SetHeaders(transportMessage.Deliver.Envelope.Headers, context.Headers);

                var grpcTransportMessage = new GrpcTransportMessage(transportMessage, HostMetadataCache.Host);

                await _context.Exchange.Send(grpcTransportMessage, cancellationToken).ConfigureAwait(false);

                context.LogSent();
                activity.AddSendContextHeadersPostSend(context);

                if (_context.SendObservers.Count > 0)
                {
                    await _context.SendObservers.PostSend(context).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                context.LogFaulted(ex);

                if (_context.SendObservers.Count > 0)
                {
                    await _context.SendObservers.SendFault(context, ex).ConfigureAwait(false);
                }

                throw;
            }
            finally
            {
                activity?.Stop();
            }
        }
Example #6
0
 public IMessageReceiver SelectReceiver(GrpcTransportMessage message)
 {
     return(_receiver);
 }
 public static ValueTask DeliverMessage(this IGrpcNode node, GrpcTransportMessage message)
 {
     return(node.Writer.WriteAsync(message.Message));
 }