public IMessage CreateMessage(string address, IByteBuffer payload)
        {
            var message = new IotHubClientMessage(new Message(payload.IsReadable() ? new ReadOnlyByteBufferStream(payload, false) : null), payload);

            message.Address = address;
            return(message);
        }
        async void Receive()
        {
            try
            {
                while (true)
                {
                    Message message = await this.deviceClient.ReceiveAsync(TimeSpan.MaxValue);

                    if (message == null)
                    {
                        this.messagingChannel.Close(null);
                        return;
                    }

                    if (this.settings.MaxOutboundRetransmissionEnforced && message.DeliveryCount > this.settings.MaxOutboundRetransmissionCount)
                    {
                        await this.RejectAsync(message.LockToken);

                        continue;
                    }

                    IByteBuffer messagePayload;
                    using (Stream payloadStream = message.GetBodyStream())
                    {
                        long streamLength = payloadStream.Length;
                        if (streamLength > int.MaxValue)
                        {
                            throw new InvalidOperationException($"Message size ({streamLength.ToString()} bytes) is too big to process.");
                        }

                        int length = (int)streamLength;
                        messagePayload = this.allocator.Buffer(length, length);
                        await messagePayload.WriteBytesAsync(payloadStream, length);

                        Contract.Assert(messagePayload.ReadableBytes == length);
                    }

                    var msg = new IotHubClientMessage(message, messagePayload);
                    msg.Properties[TemplateParameters.DeviceIdTemplateParam] = this.deviceId;
                    string address;
                    if (!this.messageAddressConverter.TryDeriveAddress(msg, out address))
                    {
                        await this.RejectAsync(message.LockToken); // todo: fork await

                        continue;
                    }
                    msg.Address = address;

                    this.messagingChannel.Handle(msg);
                }
            }
            catch (IotHubException ex)
            {
                this.messagingChannel.Close(new MessagingException(ex.Message, ex.InnerException, ex.IsTransient, ex.TrackingId));
            }
            catch (Exception ex)
            {
                this.messagingChannel.Close(ex);
            }
        }
 public IMessage CreateMessage(string address, IByteBuffer payload)
 {
     var message = new IotHubClientMessage(new Message(payload.IsReadable() ? new ReadOnlyByteBufferStream(payload, false) : null), payload);
     message.Address = address;
     return message;
 }
        async void Receive()
        {
            try
            {
                while (true)
                {
                    Message message = await this.deviceClient.ReceiveAsync(TimeSpan.MaxValue);
                    if (message == null)
                    {
                        this.messagingChannel.Close(null);
                        return;
                    }

                    if (this.settings.MaxOutboundRetransmissionEnforced && message.DeliveryCount > this.settings.MaxOutboundRetransmissionCount)
                    {
                        await this.RejectAsync(message.LockToken);
                        continue;
                    }

                    IByteBuffer messagePayload;
                    using (Stream payloadStream = message.GetBodyStream())
                    {
                        long streamLength = payloadStream.Length;
                        if (streamLength > int.MaxValue)
                        {
                            throw new InvalidOperationException($"Message size ({streamLength.ToString()} bytes) is too big to process.");
                        }

                        int length = (int)streamLength;
                        messagePayload = this.allocator.Buffer(length, length);
                        await messagePayload.WriteBytesAsync(payloadStream, length);

                        Contract.Assert(messagePayload.ReadableBytes == length);
                    }

                    var msg = new IotHubClientMessage(message, messagePayload);
                    msg.Properties[TemplateParameters.DeviceIdTemplateParam] = this.deviceId;
                    string address;
                    if (!this.messageAddressConverter.TryDeriveAddress(msg, out address))
                    {
                        await this.RejectAsync(message.LockToken); // todo: fork await
                        continue;
                    }
                    msg.Address = address;

                    this.messagingChannel.Handle(msg);
                }
            }
            catch (IotHubException ex)
            {
                this.messagingChannel.Close(new MessagingException(ex.Message, ex.InnerException, ex.IsTransient, ex.TrackingId));
            }
            catch (Exception ex)
            {
                this.messagingChannel.Close(ex);
            }
        }
Ejemplo n.º 5
0
        async void Receive()
        {
            Message     message        = null;
            IByteBuffer messagePayload = null;

            try
            {
                while (true)
                {
                    message = await this.bridge.DeviceClient.ReceiveAsync(this.lifetimeCancellation.Token);

                    if (message == null)
                    {
                        this.messagingChannel.Close(null);
                        return;
                    }

                    if (this.bridge.Settings.MaxOutboundRetransmissionEnforced && message.DeliveryCount > this.bridge.Settings.MaxOutboundRetransmissionCount)
                    {
                        await this.RejectAsync(message.LockToken);

                        message.Dispose();
                        continue;
                    }

                    using (Stream payloadStream = message.GetBodyStream())
                    {
                        long streamLength = payloadStream.Length;
                        if (streamLength > int.MaxValue)
                        {
                            throw new InvalidOperationException($"Message size ({streamLength.ToString()} bytes) is too big to process.");
                        }

                        int length = (int)streamLength;
                        messagePayload = this.allocator.Buffer(length, length);
                        await messagePayload.WriteBytesAsync(payloadStream, length);

                        Contract.Assert(messagePayload.ReadableBytes == length);
                    }

                    var msg = new IotHubClientMessage(message, messagePayload);
                    msg.Properties[TemplateParameters.DeviceIdTemplateParam] = this.bridge.DeviceId;
                    string address;
                    if (!this.addressFormatter(msg, out address))
                    {
                        messagePayload.Release();
                        await this.RejectAsync(message.LockToken); // todo: fork await

                        message.Dispose();
                        continue;
                    }
                    msg.Address = address;

                    this.messagingChannel.Handle(msg, this);

                    message        = null; // ownership has been transferred to messagingChannel
                    messagePayload = null;
                }
            }
            catch (IotHubException ex)
            {
                this.messagingChannel.Close(ex.ToMessagingException());
            }
            catch (Exception ex)
            {
                this.messagingChannel.Close(ex);
            }
            finally
            {
                message?.Dispose();
                if (messagePayload != null)
                {
                    ReferenceCountUtil.SafeRelease(messagePayload);
                }
            }
        }