async Task IEventDataReceiver.Handle(Microsoft.Azure.EventHubs.EventData message, Action <ReceiveContext> contextCallback)
        {
            var context = new EventDataReceiveContext(message, _receiveEndpointContext);

            contextCallback?.Invoke(context);

            try
            {
                await _receiveEndpointContext.ReceiveObservers.PreReceive(context).ConfigureAwait(false);

                await _receiveEndpointContext.ReceivePipe.Send(context).ConfigureAwait(false);

                await context.ReceiveCompleted.ConfigureAwait(false);

                await _receiveEndpointContext.ReceiveObservers.PostReceive(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await _receiveEndpointContext.ReceiveObservers.ReceiveFault(context, ex).ConfigureAwait(false);
            }
            finally
            {
                context.Dispose();
            }
        }
        /// <summary>
        /// Stores the offset and sequenceNumber from the provided received
        /// <see cref="IEventData" /> instance, then writes those values to the
        /// checkpoint store via the checkpoint manager.
        /// </summary>
        /// <param name="eventData">The <see cref="IEventData" /> instance.</param>
        /// <returns>The asynchronous task.</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown if the given <see cref="IEventData"/> instance is null.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Thrown if the the sequenceNumber from given <see cref="IEventData"/>
        /// instance is less than the last checkpointed value.
        /// </exception>
        public async Task CheckpointAsync(IEventData eventData)
        {
            Checks.Parameter(nameof(eventData), eventData)
            .NotNull();

            AzureEventData azureEventData = eventData.ToAzureEventData();

            await this.azurePartitionContext.CheckpointAsync(azureEventData);
        }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EventData"/> class.
        /// </summary>
        /// <param name="azureEventData">The <see cref="AzureEventData"/> instance.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown if the given <see cref="AzureEventData"/> instance is null.
        /// </exception>
        public EventData(AzureEventData azureEventData)
        {
            Checks.Parameter(nameof(azureEventData), azureEventData)
            .NotNull();

            this.azureEventData = azureEventData;

            this.lazyProperties =
                new Lazy <IReadOnlyDictionary <string, object> >(
                    () => this.azureEventData.Properties as IReadOnlyDictionary <string, object>);

            this.lazyBodyBytes =
                new Lazy <IReadOnlyList <byte> >(
                    () => azureEventData.Body.Array as IReadOnlyList <byte>);

            this.lazyBodyString =
                new Lazy <string>(
                    () => Encoding.UTF8.GetString(this.BodyBytes));
        }
        private async Task RunSvcAsync()
        {
            string errorMsg  = "";
            string updateMsg = "";

            using (cancellationTokenSourceManual = new CancellationTokenSource())
            {
                ClientWebSocket stream = null;
                try
                {
                    DeviceStreamRequest deviceStreamRequest = new DeviceStreamRequest(
                        streamName: "TestStream"
                        );
                    updateMsg = "Starting Svc TestStream";
                    UpdateStatus(updateMsg);

                    cancellationTokenSourceManual.Token.Register(() =>
                    {
                        _serviceClient?.CloseAsync();
                        _serviceClient?.Dispose();
                    });

                    DeviceStreamResponse result = await _serviceClient.CreateStreamAsync(_deviceId, deviceStreamRequest).ConfigureAwait(false);

                    updateMsg = string.Format("Svc Stream response received: Name={0} IsAccepted={1}", deviceStreamRequest.StreamName, result.IsAccepted);
                    UpdateStatus(updateMsg);

                    if (result.IsAccepted)
                    {
                        using (cancellationTokenSourceTimeout = new CancellationTokenSource(DeviceStreamingCommon.DeviceTimeout))
                        {
                            try
                            {
                                using (stream = await DeviceStreamingCommon.GetStreamingDeviceAsync(result.Url, result.AuthorizationToken, cancellationTokenSourceTimeout.Token).ConfigureAwait(false))
                                {
                                    updateMsg = "Stream is open.";
                                    UpdateStatus(updateMsg);
                                    bool keepAlive = false;
                                    MsgOutWaitHandle = new AutoResetEvent(true);
                                    do
                                    {
                                        //Nb: Not waited on first entry as waiting for msgOut, which we already have.
                                        updateMsg = "Stream is open. Waiting for msg to send.";
                                        UpdateStatus(updateMsg);

                                        MsgOutWaitHandle.WaitOne();
                                        updateMsg = "Sending msg.";
                                        UpdateStatus(updateMsg);
                                        bool caught = false;
                                        try
                                        {
                                            MsgOut = SvcCurrentSettings.ProcessMsgOut(MsgOut, SvcCurrentSettings.KeepAlive, SvcCurrentSettings.ResponseExpected, DevKeepListening, DevAutoStart);
                                        } catch (NotImplementedException)
                                        {
                                            errorMsg += "DeviceCurrentSettings not properly implemented";
                                            keepAlive = false;
                                            caught    = true;
                                        }
                                        if (!caught)
                                        {
                                            await SendMsg(stream, MsgOut, cancellationTokenSourceTimeout);

                                            updateMsg = "Sent msg.";
                                            UpdateStatus(updateMsg);

                                            if (this.SvcCurrentSettings.ResponseExpected)
                                            {
                                                byte[] receiveBuffer = new byte[1024];
                                                System.ArraySegment <byte> ReceiveBuffer = new ArraySegment <byte>(receiveBuffer);

                                                var receiveResult = await stream.ReceiveAsync(ReceiveBuffer, cancellationTokenSourceTimeout.Token).ConfigureAwait(false);

                                                MsgIn = Encoding.UTF8.GetString(receiveBuffer, 0, receiveResult.Count);
                                                string subStrn    = AzIoTHubDeviceStreams.DeviceStreamingCommon.DeiceInSimuatedDeviceModeStrn;
                                                int    subStrnLen = subStrn.Length;
                                                if (MsgIn.Length >= subStrnLen)
                                                {
                                                    if (MsgIn.Substring(0, subStrnLen) == subStrn)
                                                    {
                                                        MsgIn = MsgIn.Substring(subStrnLen);
                                                        AzIoTHubModules.SyntheticIoTMessage    iotHubMessage = AzIoTHubModules.SyntheticIoTMessage.Deserialize(MsgIn);
                                                        Microsoft.Azure.Devices.Client.Message message       = iotHubMessage.ToMessage();
                                                        Microsoft.Azure.EventHubs.EventData    eventData     = AzIoTHubModules.SyntheticIoTMessage.ToEventData(message);
                                                        MsgIn = AzIoTHubModules.SyntheticIoTMessage.EventData_ToString(eventData);
                                                    }
                                                }
                                                keepAlive = false;
                                                if (SvcCurrentSettings != null)
                                                {
                                                    keepAlive = this.SvcCurrentSettings.KeepAlive;
                                                }
                                                try
                                                {
                                                    if (OnRecvdTextD != null)
                                                    {
                                                        OnRecvdTextD(MsgIn);
                                                    }
                                                }
                                                catch (Exception exx)
                                                {
                                                    errorMsg += "OnRecvdTextD not properly implemented: " + exx.Message;
                                                    keepAlive = false;
                                                }

                                                updateMsg = string.Format("Svc Received stream data: {0}", MsgIn);
                                                UpdateStatus(updateMsg);
                                            }
                                            MsgOutWaitHandle.Reset();
                                        }
                                    } while (keepAlive);
                                    MsgOutWaitHandle = null;
                                    updateMsg        = "Closing Svc Socket";
                                    UpdateStatus(updateMsg);
                                    await stream.CloseAsync(WebSocketCloseStatus.NormalClosure, String.Empty, cancellationTokenSourceTimeout.Token).ConfigureAwait(false);

                                    stream = null;
                                }
                            }
                            catch (Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException)
                            {
                                System.Diagnostics.Debug.WriteLine("1 Error RunSvcAsync(): Hub connection failure");
                                errorMsg = "Hub connection failure";
                            }
                            catch (Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException)
                            {
                                System.Diagnostics.Debug.WriteLine("1 Error RunSvcAsync(): Device not found");
                                errorMsg = "Device not found";
                            }
                            catch (TaskCanceledException)
                            {
                                System.Diagnostics.Debug.WriteLine("1 Error RunSvcAsync(): Task cancelled");
                                errorMsg = "Task cancelled";
                            }
                            catch (OperationCanceledException)
                            {
                                System.Diagnostics.Debug.WriteLine("1 Error RunSvcAsync(): Operation cancelled");
                                errorMsg = "Operation cancelled";
                            }
                            catch (Exception ex)
                            {
                                if ((bool)cancellationTokenSourceManual?.IsCancellationRequested)
                                {
                                    System.Diagnostics.Debug.WriteLine("1 Error RunSvcAsync(): Cancelled.");
                                    errorMsg = "Cancelled";
                                }
                                else if ((bool)cancellationTokenSourceTimeout?.IsCancellationRequested)
                                {
                                    System.Diagnostics.Debug.WriteLine("1 Error RunSvcAsync(): Timed Out.");
                                    errorMsg = "Timed Out";
                                }
                                else if (!ex.Message.Contains("Timed out"))
                                {
                                    System.Diagnostics.Debug.WriteLine("1 Error RunSvcAsync(): " + ex.Message);
                                    errorMsg = ex.Message;
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine("2 Error RunSvcAsync(): Timed out");
                                    errorMsg = "Timed Out";
                                }
                            }
                        }
                    }
                }
                catch (Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException)
                {
                    System.Diagnostics.Debug.WriteLine("2 Error RunSvcAsync(): Hub connection failure");
                    errorMsg += " Hub connection failure";
                }
                catch (Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException)
                {
                    System.Diagnostics.Debug.WriteLine("2 Error RunSvcAsync(): Device not found");
                    errorMsg += " Device not found";
                }
                catch (TaskCanceledException)
                {
                    System.Diagnostics.Debug.WriteLine("2 Error RunSvcAsync(): Task cancelled");
                    errorMsg += " Task cancelled";
                }
                catch (OperationCanceledException)
                {
                    System.Diagnostics.Debug.WriteLine("2 Error RunSvcAsync(): Operation cancelled");
                    errorMsg += " Operation cancelled";
                }
                catch (Exception ex)
                { if ((bool)cancellationTokenSourceManual?.IsCancellationRequested)
                  {
                      System.Diagnostics.Debug.WriteLine("2 Error RunSvcAsync(): Cancelled.");
                      errorMsg += " Cancelled";
                  }
                  else if ((bool)cancellationTokenSourceTimeout?.IsCancellationRequested)
                  {
                      System.Diagnostics.Debug.WriteLine("2 Error RunSvcAsync(): Timed Out.");
                      errorMsg += " Timed Out";
                  }
                  else if (!ex.Message.Contains("Timed out"))
                  {
                      System.Diagnostics.Debug.WriteLine("2 Error RunSvcAsync(): " + ex.Message);
                      errorMsg += " " + ex.Message;
                  }
                  else
                  {
                      System.Diagnostics.Debug.WriteLine("2 Error RunSvcAsync(): Timed out");
                      errorMsg += " Timed Out";
                  } };

                if (stream != null)
                {
                    if (stream.CloseStatus != WebSocketCloseStatus.NormalClosure)
                    {
                        updateMsg = "Aborting Svc Socket as is errant or cancelled: " + errorMsg;
                        UpdateStatus(updateMsg);
                        stream.Abort();
                        updateMsg = "Aborted Svc Socket as was errant or cancelled:" + errorMsg;
                        UpdateStatus(updateMsg);
                    }
                    else
                    {
                        updateMsg = "Socket closed normally: " + errorMsg;
                        UpdateStatus(updateMsg);
                    }
                    stream = null;
                }
                else
                {
                    updateMsg = "Socket closed Normally: " + errorMsg;
                    UpdateStatus(updateMsg);
                }

                deviceStream_Svc = null;
                MsgOutWaitHandle = null;
                cancellationTokenSourceTimeout = null;
            }
        }