Esempio n. 1
0
 private void Connect()
 {
     while (!this.connected && !this.cancelToken.IsCancellationRequested)
     {
         ++this.connectionAttemptCounter;
         try
         {
             this.connect();
             this.connected = true;
             this.connectionAttemptCounter = 0;
         }
         catch (ConnectionFailureException ex)
         {
             this.connected = false;
             ApplePushChannel.ConnectionFailureDelegate connectionFailure = this.OnConnectionFailure;
             if (connectionFailure != null)
             {
                 connectionFailure(ex);
             }
             PushChannelExceptionDelegate onException = this.OnException;
             if (onException != null)
             {
                 onException((object)this, (Exception)ex);
             }
         }
         if (!this.connected && this.connectionAttemptCounter >= this.appleSettings.MaxConnectionAttempts)
         {
             throw new ConnectionFailureException(string.Format("Maximum number of attempts ({0}) to connect to {1}:{2} was reached!", (object)this.appleSettings.MaxConnectionAttempts, (object)this.appleSettings.Host, (object)this.appleSettings.Port), (Exception) new TimeoutException());
         }
         if (!this.connected)
         {
             ApplePushChannel.WaitBeforeReconnectDelegate waitBeforeReconnect = this.OnWaitBeforeReconnect;
             if (waitBeforeReconnect != null)
             {
                 waitBeforeReconnect(this.reconnectDelay);
             }
             int num = 0;
             while (num <= this.reconnectDelay && !this.cancelToken.IsCancellationRequested)
             {
                 Thread.Sleep(250);
                 num += 250;
             }
             this.reconnectDelay = (int)((double)this.reconnectBackoffMultiplier * (double)this.reconnectDelay);
         }
         else
         {
             this.reconnectDelay = 3000;
             ApplePushChannel.ConnectedDelegate onConnected = this.OnConnected;
             if (onConnected != null)
             {
                 onConnected(this.appleSettings.Host, this.appleSettings.Port);
             }
         }
     }
 }
Esempio n. 2
0
        private void Cleanup()
        {
            int num = -1;

            try
            {
                num = Interlocked.CompareExchange(ref this.cleanupSync, 1, 0);
                if (num != 0)
                {
                    return;
                }
                bool flag;
                do
                {
                    lock (this.connectLock)
                    {
                        try
                        {
                            this.Connect();
                        }
                        catch (Exception ex)
                        {
                            PushChannelExceptionDelegate onException = this.OnException;
                            if (onException != null)
                            {
                                onException((object)this, ex);
                            }
                        }
                    }
                    flag = false;
                    lock (this.sentLock)
                    {
                        if (this.sentNotifications.Count > 0)
                        {
                            if (this.connected)
                            {
                                SentNotification sentNotification = this.sentNotifications[0];
                                if (sentNotification.SentAt < DateTime.UtcNow.AddMilliseconds((double)(-1 * this.appleSettings.MillisecondsToWaitBeforeMessageDeclaredSuccess)))
                                {
                                    flag = true;
                                    Interlocked.Decrement(ref this.trackedNotificationCount);
                                    if (sentNotification.Callback != null)
                                    {
                                        sentNotification.Callback((object)this, new SendNotificationResult((INotification)sentNotification.Notification, false, (Exception)null));
                                    }
                                    this.sentNotifications.RemoveAt(0);
                                    Interlocked.Increment(ref this.cleanedUp);
                                }
                                else
                                {
                                    flag = false;
                                }
                            }
                            else
                            {
                                try
                                {
                                    this.sentNotifications[0].SentAt = DateTime.UtcNow;
                                }
                                catch
                                {
                                }
                            }
                        }
                    }
                }while (flag);
            }
            finally
            {
                if (num == 0)
                {
                    this.cleanupSync = 0;
                }
            }
        }