Example #1
0
        /// <summary>
        /// Downloads messages from the server.
        /// </summary>
        /// <param name="longPoll"><c>true</c> to asynchronously wait for messages if there are none immediately available for download.</param>
        /// <param name="progress">A callback that receives messages as they are retrieved.</param>
        /// <param name="cancellationToken">A token whose cancellation signals lost interest in the result of this method.</param>
        /// <returns>A collection of all messages that were waiting at the time this method was invoked.</returns>
        /// <exception cref="HttpRequestException">Thrown when a connection to the server could not be established, or was terminated.</exception>
        public async Task <IReadOnlyList <PayloadReceipt> > ReceiveAsync(bool longPoll = false, IProgress <PayloadReceipt> progress = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var inboxItems = await this.DownloadIncomingItemsAsync(longPoll, cancellationToken);

            var payloads = new List <PayloadReceipt>();

            foreach (var item in inboxItems)
            {
                try {
                    try {
                        var invite = await this.DownloadPayloadReferenceAsync(item, cancellationToken);

                        if (invite == null)
                        {
                            continue;
                        }

                        var message = await this.DownloadPayloadAsync(invite, cancellationToken);

                        var receipt = new PayloadReceipt(message, item.DatePostedUtc);
                        payloads.Add(receipt);
                        if (progress != null)
                        {
                            progress.Report(receipt);
                        }
                    } catch (SerializationException ex) {
                        throw new InvalidMessageException(Strings.InvalidMessage, ex);
                    } catch (DecoderFallbackException ex) {
                        throw new InvalidMessageException(Strings.InvalidMessage, ex);
                    } catch (OverflowException ex) {
                        throw new InvalidMessageException(Strings.InvalidMessage, ex);
                    } catch (OutOfMemoryException ex) {
                        throw new InvalidMessageException(Strings.InvalidMessage, ex);
                    } catch (InvalidMessageException) {
                        throw;
                    } catch (Exception ex) {
                        // all those platform-specific exceptions that aren't available to portable libraries.
                        throw new InvalidMessageException(Strings.InvalidMessage, ex);
                    }
                } catch (InvalidMessageException ex) {
                    Debug.WriteLine(ex);

                    // Delete the payload reference since it was an invalid message.
                    var nowait = this.DeletePayloadReferenceAsync(item.Location, cancellationToken);
                }
            }

            return(payloads);
        }
Example #2
0
        /// <summary>
        /// Downloads messages from the server.
        /// </summary>
        /// <param name="longPoll"><c>true</c> to asynchronously wait for messages if there are none immediately available for download.</param>
        /// <param name="progress">A callback that receives messages as they are retrieved.</param>
        /// <param name="cancellationToken">A token whose cancellation signals lost interest in the result of this method.</param>
        /// <returns>A collection of all messages that were waiting at the time this method was invoked.</returns>
        /// <exception cref="HttpRequestException">Thrown when a connection to the server could not be established, or was terminated.</exception>
        public async Task<IReadOnlyList<PayloadReceipt>> ReceiveAsync(bool longPoll = false, IProgress<PayloadReceipt> progress = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var inboxItems = await this.DownloadIncomingItemsAsync(longPoll, cancellationToken).ConfigureAwait(false);

            var payloads = new List<PayloadReceipt>();
            foreach (var item in inboxItems)
            {
                try
                {
                    try
                    {
                        var invite = await this.DownloadPayloadReferenceAsync(item, cancellationToken).ConfigureAwait(false);
                        if (invite == null)
                        {
                            continue;
                        }

                        var message = await this.DownloadPayloadAsync(invite, cancellationToken).ConfigureAwait(false);
                        var receipt = new PayloadReceipt(message, item.DatePostedUtc);
                        payloads.Add(receipt);
                        if (progress != null)
                        {
                            progress.Report(receipt);
                        }
                    }
                    catch (SerializationException ex)
                    {
                        throw new InvalidMessageException(Strings.InvalidMessage, ex);
                    }
                    catch (DecoderFallbackException ex)
                    {
                        throw new InvalidMessageException(Strings.InvalidMessage, ex);
                    }
                    catch (OverflowException ex)
                    {
                        throw new InvalidMessageException(Strings.InvalidMessage, ex);
                    }
                    catch (OutOfMemoryException ex)
                    {
                        throw new InvalidMessageException(Strings.InvalidMessage, ex);
                    }
                    catch (InvalidMessageException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        // all those platform-specific exceptions that aren't available to portable libraries.
                        throw new InvalidMessageException(Strings.InvalidMessage, ex);
                    }
                }
                catch (InvalidMessageException ex)
                {
                    Debug.WriteLine(ex);

                    // Delete the payload reference since it was an invalid message.
                    var nowait = this.DeletePayloadReferenceAsync(item.Location, cancellationToken);
                }
            }

            return payloads;
        }