Example #1
0
        private void DoChannelWork(IPushChannel channel, CancellationTokenSource cancelTokenSource)
        {
            string id = Guid.NewGuid().ToString();

            long sendCount = 0;

            while (!cancelTokenSource.IsCancellationRequested)
            {
                var waitForNotification = new ManualResetEvent(false);

                INotification notification;

                if (!queuedNotifications.TryDequeue(out notification))
                {
                    Thread.Sleep(100);
                    continue;
                }

                var msWaited = (DateTime.UtcNow - notification.EnqueuedTimestamp).TotalMilliseconds;

                lock (measurements)
                {
                    measurements.Add(new WaitTimeMeasurement((long) msWaited));
                }

                //Log.Info("Waited: {0} ms", msWaited);

                var sendStart = DateTime.UtcNow;

                sendCount++;

                Interlocked.Increment(ref totalSendCount);

                if (sendCount % 1000 == 0)
                    Log.Info("{0}> Send Count: {1} ({2})", id, sendCount, Interlocked.Read(ref totalSendCount));

                channel.SendNotification(notification, (sender, result) =>
                    {
                        Interlocked.Decrement(ref trackedNotificationCount);

                        var sendTime = DateTime.UtcNow - sendStart;

                        lock (sendTimeMeasurements)
                        {
                            sendTimeMeasurements.Add(new WaitTimeMeasurement((long)sendTime.TotalMilliseconds));
                        }

                        //Log.Info("Send Time: " + sendTime.TotalMilliseconds + " ms");

                        //Trigger
                        if (this.BlockOnMessageResult)
                            waitForNotification.Set();

                        //Handle the notification send callback here
                        if (result.ShouldRequeue)
                        {
                            var eventArgs = new NotificationRequeueEventArgs(result.Notification);
                            var evt = this.OnNotificationRequeue;
                            if (evt != null)
                                evt(this, eventArgs);

                            //See if the requeue was cancelled in the event args
                            if (!eventArgs.Cancel)
                                this.QueueNotification(result.Notification, result.CountsAsRequeue, true);
                        }
                        else
                        {
                            //Result was a success, but there are still more possible outcomes than an outright success
                            if (!result.IsSuccess)
                            {
                                //Check if the subscription was expired
                                if (result.IsSubscriptionExpired)
                                {
                                    //If there is a new id, the subscription must have changed
                                    //This is a fairly special case that only GCM should really ever raise
                                    if (!string.IsNullOrEmpty(result.NewSubscriptionId))
                                    {
                                        var evt = this.OnDeviceSubscriptionChanged;
                                        if (evt != null)
                                            evt(this, result.OldSubscriptionId, result.NewSubscriptionId, result.Notification);
                                    }
                                    else
                                    {
                                        var evt = this.OnDeviceSubscriptionExpired;
                                        if (evt != null)
                                            evt(this, result.OldSubscriptionId, result.SubscriptionExpiryUtc, result.Notification);
                                    }
                                }
                                else //Otherwise some general failure
                                {
                                    var evt = this.OnNotificationFailed;
                                    if (evt != null)
                                        evt(this, result.Notification, result.Error);
                                }
                            }
                            else
                            {
                                var evt = this.OnNotificationSent;
                                if (evt != null)
                                    evt(this, result.Notification);
                            }
                        }
                    });

                if (this.BlockOnMessageResult && !waitForNotification.WaitOne(ServiceSettings.NotificationSendTimeout))
                {
                    Interlocked.Decrement(ref trackedNotificationCount);

                    Log.Info("Notification send timeout");

                    var evt = this.OnNotificationFailed;
                    if (evt != null)
                        evt(this, notification, new TimeoutException("Notification send timed out"));
                }
            }

            channel.Dispose();
        }
Example #2
0
        private void DoChannelWork(IPushChannel channel, CancellationTokenSource cancelTokenSource)
        {
            while (!cancelTokenSource.IsCancellationRequested)
            {
                var waitForNotification = new ManualResetEvent(false);

                INotification notification;

                if (!queuedNotifications.TryDequeue(out notification))
                {
                    Thread.Sleep(100);
                    continue;
                }

                var msWaited = (DateTime.UtcNow - notification.EnqueuedTimestamp).TotalMilliseconds;
                measurements.Add(new WaitTimeMeasurement((long) msWaited));

                channel.SendNotification(notification, (sender, result) =>
                    {
                        //Trigger
                        if (this.BlockOnMessageResult)
                            waitForNotification.Set();

                        Interlocked.Decrement(ref trackedNotificationCount);

                        //Handle the notification send callback here
                        if (result.ShouldRequeue)
                            this.QueueNotification(result.Notification, result.CountsAsRequeue, true);
                        else
                        {
                            //This is a fairly special case that only GCM should really ever raise
                            if (!string.IsNullOrEmpty(result.NewSubscriptionId) && !string.IsNullOrEmpty(result.OldSubscriptionId))
                            {
                                var evt = this.OnDeviceSubscriptionChanged;
                                if (evt != null)
                                    evt(this, result.OldSubscriptionId, result.NewSubscriptionId, result.Notification);
                            }

                            if (!result.IsSuccess)
                            {
                                var evt = this.OnNotificationFailed;
                                if (evt != null)
                                    evt(this, result.Notification, result.Error);
                            }
                            else
                            {
                                var evt = this.OnNotificationSent;
                                if (evt != null)
                                    evt(this, result.Notification);
                            }
                        }
                    });

                if (this.BlockOnMessageResult && !waitForNotification.WaitOne(ServiceSettings.NotificationSendTimeout))
                {
                    Interlocked.Decrement(ref trackedNotificationCount);

                    var evt = this.OnNotificationFailed;
                    if (evt != null)
                        evt(this, notification, new TimeoutException("Notification send timed out"));
                }
            }

            channel.Dispose();
        }
Example #3
0
        private void DoChannelWork(IPushChannel channel, CancellationTokenSource cancelTokenSource)
        {
            string id = Guid.NewGuid().ToString();

            long sendCount = 0;

            while (!cancelTokenSource.IsCancellationRequested)
            {
                var waitForNotification = new ManualResetEvent(false);

                var notification = queuedNotifications.Dequeue();

                if (notification == null)
                {
                    Thread.Sleep(100);
                    continue;
                }

                var msWaited = (DateTime.UtcNow - notification.EnqueuedTimestamp).TotalMilliseconds;

                lock (measurementsLock)
                {
                    measurements.Add(new WaitTimeMeasurement((long)msWaited));
                }

                //Log.Info("Waited: {0} ms", msWaited);

                var sendStart = DateTime.UtcNow;

                sendCount++;

                Interlocked.Increment(ref totalSendCount);

                if (sendCount % 1000 == 0)
                {
                    Log.Info("{0}> Send Count: {1} ({2})", id, sendCount, Interlocked.Read(ref totalSendCount));
                }

                channel.SendNotification(notification, (sender, result) =>
                {
                    Interlocked.Decrement(ref trackedNotificationCount);

                    var sendTime = DateTime.UtcNow - sendStart;

                    lock (sendTimeMeasurementsLock)
                    {
                        sendTimeMeasurements.Add(new WaitTimeMeasurement((long)sendTime.TotalMilliseconds));
                    }

                    //Log.Info("Send Time: " + sendTime.TotalMilliseconds + " ms");

                    //Trigger
                    if (this.BlockOnMessageResult)
                    {
                        waitForNotification.Set();
                    }

                    //Handle the notification send callback here
                    if (result.ShouldRequeue)
                    {
                        var eventArgs = new NotificationRequeueEventArgs(result.Notification, result.Error);
                        var evt       = this.OnNotificationRequeue;
                        if (evt != null)
                        {
                            evt(this, eventArgs);
                        }

                        //See if the requeue was cancelled in the event args
                        if (!eventArgs.Cancel)
                        {
                            this.QueueNotification(result.Notification, result.CountsAsRequeue, true, true);
                        }
                    }
                    else
                    {
                        //Result was a success, but there are still more possible outcomes than an outright success
                        if (!result.IsSuccess)
                        {
                            //Check if the subscription was expired
                            if (result.IsSubscriptionExpired)
                            {
                                //If there is a new id, the subscription must have changed
                                //This is a fairly special case that only GCM should really ever raise
                                if (!string.IsNullOrEmpty(result.NewSubscriptionId))
                                {
                                    var evt = this.OnDeviceSubscriptionChanged;
                                    if (evt != null)
                                    {
                                        evt(this, result.OldSubscriptionId, result.NewSubscriptionId, result.Notification);
                                    }
                                }
                                else
                                {
                                    var evt = this.OnDeviceSubscriptionExpired;
                                    if (evt != null)
                                    {
                                        evt(this, result.OldSubscriptionId, result.SubscriptionExpiryUtc, result.Notification);
                                    }
                                }
                            }
                            else                                     //Otherwise some general failure
                            {
                                var evt = this.OnNotificationFailed;
                                if (evt != null)
                                {
                                    evt(this, result.Notification, result.Error);
                                }
                            }
                        }
                        else
                        {
                            var evt = this.OnNotificationSent;
                            if (evt != null)
                            {
                                evt(this, result.Notification);
                            }
                        }
                    }
                });


                if (this.BlockOnMessageResult && !waitForNotification.WaitOne(ServiceSettings.NotificationSendTimeout))
                {
                    Interlocked.Decrement(ref trackedNotificationCount);

                    Log.Info("Notification send timeout");

                    var evt = this.OnNotificationFailed;
                    if (evt != null)
                    {
                        evt(this, notification, new TimeoutException("Notification send timed out"));
                    }
                }
            }

            channel.Dispose();
        }
        private void DoChannelWork(IPushChannel channel, CancellationTokenSource cancelTokenSource)
        {
            string str = Guid.NewGuid().ToString();
            long   num = 0;

            while (!cancelTokenSource.IsCancellationRequested)
            {
                INotification notification = this.queuedNotifications.Dequeue();
                if (notification == null)
                {
                    Thread.Sleep(100);
                }
                else
                {
                    ManualResetEvent waitForNotification = (ManualResetEvent)null;
                    if (this.BlockOnMessageResult)
                    {
                        waitForNotification = new ManualResetEvent(false);
                    }
                    double totalMilliseconds = (DateTime.UtcNow - notification.EnqueuedTimestamp).TotalMilliseconds;
                    lock (this.measurementsLock)
                        this.measurements.Add(new PushServiceBase.WaitTimeMeasurement((long)totalMilliseconds));
                    DateTime sendStart = DateTime.UtcNow;
                    ++num;
                    Interlocked.Increment(ref this.totalSendCount);
                    if (num % 1000L == 0L)
                    {
                        Log.Debug("{0}> Send Count: {1} ({2})", (object)str, (object)num, (object)Interlocked.Read(ref this.totalSendCount));
                    }
                    channel.SendNotification(notification, (SendNotificationCallbackDelegate)((sender, result) =>
                    {
                        Interlocked.Decrement(ref this.trackedNotificationCount);
                        TimeSpan timeSpan = DateTime.UtcNow - sendStart;
                        lock (this.sendTimeMeasurementsLock)
                            this.sendTimeMeasurements.Add(new PushServiceBase.WaitTimeMeasurement((long)timeSpan.TotalMilliseconds));
                        waitForNotification?.Set();
                        if (result.ShouldRequeue)
                        {
                            NotificationRequeueEventArgs e = new NotificationRequeueEventArgs(result.Notification, result.Error);
                            NotificationRequeueDelegate notificationRequeue = this.OnNotificationRequeue;
                            if (notificationRequeue != null)
                            {
                                notificationRequeue((object)this, e);
                            }
                            if (e.Cancel)
                            {
                                return;
                            }
                            this.QueueNotification(result.Notification, result.CountsAsRequeue, true, true);
                        }
                        else if (!result.IsSuccess)
                        {
                            if (result.IsSubscriptionExpired)
                            {
                                if (!string.IsNullOrEmpty(result.NewSubscriptionId))
                                {
                                    DeviceSubscriptionChangedDelegate subscriptionChanged = this.OnDeviceSubscriptionChanged;
                                    if (subscriptionChanged == null)
                                    {
                                        return;
                                    }
                                    subscriptionChanged((object)this, result.OldSubscriptionId, result.NewSubscriptionId, result.Notification);
                                }
                                else
                                {
                                    DeviceSubscriptionExpiredDelegate subscriptionExpired = this.OnDeviceSubscriptionExpired;
                                    if (subscriptionExpired == null)
                                    {
                                        return;
                                    }
                                    subscriptionExpired((object)this, result.OldSubscriptionId, result.SubscriptionExpiryUtc, result.Notification);
                                }
                            }
                            else
                            {
                                NotificationFailedDelegate notificationFailed = this.OnNotificationFailed;
                                if (notificationFailed == null)
                                {
                                    return;
                                }
                                notificationFailed((object)this, result.Notification, result.Error);
                            }
                        }
                        else
                        {
                            NotificationSentDelegate notificationSent = this.OnNotificationSent;
                            if (notificationSent == null)
                            {
                                return;
                            }
                            notificationSent((object)this, result.Notification);
                        }
                    }));
                    if (waitForNotification != null && !waitForNotification.WaitOne(this.ServiceSettings.NotificationSendTimeout))
                    {
                        Interlocked.Decrement(ref this.trackedNotificationCount);
                        Log.Info("Notification send timeout");
                        NotificationFailedDelegate notificationFailed = this.OnNotificationFailed;
                        if (notificationFailed != null)
                        {
                            notificationFailed((object)this, notification, (Exception) new TimeoutException("Notification send timed out"));
                        }
                    }
                    if (waitForNotification != null)
                    {
                        waitForNotification.Close();
                        waitForNotification = (ManualResetEvent)null;
                    }
                }
            }
            channel.Dispose();
        }