private void OnAsyncRead(IAsyncResult ar) { try { if (channel.apnsStream.EndRead(ar) == 6 && readBuffer[0] == 8) { DeliveryErrorType error = (DeliveryErrorType)readBuffer[1]; faulted = true; int index = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(readBuffer, 2)); Notification faultedNotification = notifications[index]; errors.Add(new NotificationDeliveryError(error, faultedNotification)); current = index + 1; channel.ForceReconnect(); } } catch (ObjectDisposedException) { // This is how we "cancel" the asynchronous read, so just make sure // the channel must reconnect to try again. channel.ForceReconnect(); } catch (Exception x) { faulted = true; Console.WriteLine(x.Message); } mre.Set(); }
private void workerMethod() { while (!disposing && !closing) { try { while (this.notifications.Count > 0 && !disposing) { Notification notification = this.notifications.Dequeue(); int tries = 0; bool sent = false; while (!sent && tries < this.SendRetries) { try { if (!disposing) { apnsChannel.EnsureConnection(); try { apnsChannel.Send(notification); } catch (BadDeviceTokenException btex) { var onBadDeviceToken = BadDeviceToken; if (onBadDeviceToken != null) { onBadDeviceToken(this, btex); } } catch (NotificationLengthException nlex) { var onNotificationTooLong = NotificationTooLong; if (onNotificationTooLong != null) { onNotificationTooLong(this, nlex); } } var onNotificationSuccess = NotificationSuccess; if (onNotificationSuccess != null) { onNotificationSuccess(this, notification); } sent = true; } else { apnsChannel.ForceReconnect(); } } catch (Exception ex) { var onError = Error; if (onError != null) { onError(this, ex); } apnsChannel.ForceReconnect(); } tries++; } //Didn't send in 3 tries var onNotificationFailed = NotificationFailed; if (!sent && onNotificationFailed != null) { onNotificationFailed(this, notification); } } } catch (Exception ex) { var onError = Error; if (onError != null) { onError(this, ex); } apnsChannel.ForceReconnect(); } if (!disposing) { Thread.Sleep(500); } } }