Esempio n. 1
0
            protected override IEnumerator <AsyncStep> GetAsyncSteps()
            {
                while (this.shouldContinuePump)
                {
                    // Reset status for the loop
                    this.brokeredMessage = null;

                    // Receives the message
                    yield return(this.CallAsync(
                                     (thisPtr, t, c, s) => thisPtr.owner.receiver.BeginReceive(c, s),
                                     (thisPtr, r) => thisPtr.brokeredMessage = thisPtr.owner.receiver.EndReceive(r),
                                     ExceptionPolicy.Continue));

                    if (this.LastAsyncStepException != null)
                    {
                        Log.MessagePumpReceiveException(this.owner.id, this.LastAsyncStepException);

                        if (ShouldBackoff(this.LastAsyncStepException))
                        {
                            Log.MessagePumpBackoff(BackoffAmount, this.LastAsyncStepException);
                            yield return(this.CallAsyncSleep(BackoffAmount));
                        }
                    }

                    // Retry next receive if no message was received.
                    if (this.brokeredMessage == null)
                    {
                        continue;
                    }

                    // Handles the message
                    InternalMessage internalMessage = null;

                    try
                    {
                        ulong     sequenceNumber = (ulong)brokeredMessage.SequenceNumber;
                        Message[] messages       = MessageConverter.ToMessages(brokeredMessage);
                        internalMessage = new InternalMessage(this.owner.id, sequenceNumber, messages);
                    }
                    catch (Exception e)
                    {
                        Log.MessagePumpDeserializationException(e);
                    }
                    finally
                    {
                        brokeredMessage.Dispose();
                    }

                    if (internalMessage != null)
                    {
                        if (!this.owner.semaphore.TryEnter())
                        {
                            yield return(this.CallAsync(
                                             (thisPtr, t, c, s) => thisPtr.owner.semaphore.BeginEnter(c, s),
                                             (thisPtr, r) => thisPtr.owner.semaphore.EndEnter(r),
                                             ExceptionPolicy.Transfer));
                        }

                        this.owner.inputQueue.EnqueueAndDispatch(internalMessage, this.owner.exitSemaphore);
                    }
                }
            }
Esempio n. 2
0
            protected override IEnumerator <AsyncStep> GetAsyncSteps()
            {
                if (this.owner.initializeTask.IsFaulted)
                {
                    throw this.owner.initializeTask.Exception.GetBaseException();
                }

                if (!this.owner.initializeTask.IsCompleted)
                {
                    yield return(this.CallAsync(
                                     (thisPtr, t, c, s) => new TaskAsyncResult(this.owner.initializeTask, c, s),
                                     (thisPtr, r) => TaskAsyncResult.End(r),
                                     ExceptionPolicy.Transfer));
                }

                foreach (Message message in this.messages)
                {
                    int partitionId = GetPartitionId(message);

                    List <Message> messageList;
                    if (!this.partitionedMessages.TryGetValue(partitionId, out messageList))
                    {
                        messageList = new List <Message>();
                        this.partitionedMessages.Add(partitionId, messageList);
                    }

                    messageList.Add(message);
                }

                yield return(this.CallParallelAsync(
                                 partitionedMessages,
                                 (thisPtr, i, t, c, s) => thisPtr.owner.senders[i.Key].BeginSend(MessageConverter.ToBrokeredMessages(i.Value), c, s),
                                 (thisPtr, i, r) => thisPtr.owner.senders[i.Key].EndSend(r),
                                 ExceptionPolicy.Transfer));
            }