Exemple #1
0
            public PumpAsyncResult(MessagePump owner, AsyncCallback callback, object state)
                : base(TimeSpan.MaxValue, callback, state)
            {
                this.owner = owner;
                this.shouldContinuePump = true;

                this.OnCompleting += CompletingAction;
            }
Exemple #2
0
            protected override IEnumerator <AsyncStep> GetAsyncSteps()
            {
                this.owner.namespaceManager = NamespaceManager.CreateFromConnectionString(this.owner.connectionString);

                for (int partitionId = 0; partitionId < this.owner.partitionCount; partitionId++)
                {
                    // Create topic if not exists.
                    string topicPath = this.owner.GetTopicPath(partitionId);

                    bool topicExist = false;

                    yield return(this.CallAsync(
                                     (thisPtr, t, c, s) => thisPtr.owner.namespaceManager.BeginTopicExists(topicPath, c, s),
                                     (thisPtr, r) => topicExist = thisPtr.owner.namespaceManager.EndTopicExists(r),
                                     ExceptionPolicy.Transfer));

                    if (!topicExist)
                    {
                        this.topicDescription = new TopicDescription(topicPath);

                        yield return(this.CallAsync(
                                         (thisPtr, t, c, s) => thisPtr.owner.namespaceManager.BeginCreateTopic(thisPtr.topicDescription, c, s),
                                         (thisPtr, r) => thisPtr.topicDescription = thisPtr.owner.namespaceManager.EndCreateTopic(r),
                                         ExceptionPolicy.Transfer));
                    }

                    for (int nodeId = 0; nodeId < this.owner.nodeCount; nodeId++)
                    {
                        // Create subscriptions if not exist
                        string subscriptionName = GetSubscriptionName(nodeId);

                        bool subscriptionExists = false;

                        yield return(this.CallAsync(
                                         (thisPtr, t, c, s) => thisPtr.owner.namespaceManager.BeginSubscriptionExists(topicPath, subscriptionName, c, s),
                                         (thisPtr, r) => subscriptionExists = thisPtr.owner.namespaceManager.EndSubscriptionExists(r),
                                         ExceptionPolicy.Transfer));

                        if (!subscriptionExists)
                        {
                            SubscriptionDescription subscriptionDescription = new SubscriptionDescription(topicPath, subscriptionName)
                            {
                                RequiresSession = false
                            };

                            yield return(this.CallAsync(
                                             (thisPtr, t, c, s) => thisPtr.owner.namespaceManager.BeginCreateSubscription(subscriptionDescription, c, s),
                                             (thisPtr, r) => thisPtr.owner.namespaceManager.EndCreateSubscription(r),
                                             ExceptionPolicy.Transfer));
                        }
                    }
                }

                for (int partitionId = 0; partitionId < this.owner.partitionCount; partitionId++)
                {
                    this.factory = MessagingFactory.CreateFromConnectionString(this.owner.connectionString);

                    this.owner.factories.Add(partitionId, this.factory);

                    string topicPath              = this.owner.GetTopicPath(partitionId);
                    string subscriptionName       = GetSubscriptionName(this.owner.nodeId);
                    string subscriptionEntityPath = SubscriptionClient.FormatSubscriptionPath(topicPath, subscriptionName);

                    MessageSender sender = null;

                    yield return(this.CallAsync(
                                     (thisPtr, t, c, s) => thisPtr.factory.BeginCreateMessageSender(topicPath, c, s),
                                     (thisPtr, r) => sender = thisPtr.factory.EndCreateMessageSender(r),
                                     ExceptionPolicy.Transfer));

                    this.owner.senders.Add(partitionId, sender);

                    MessageReceiver receiver = null;

                    yield return(this.CallAsync(
                                     (thisPtr, t, c, s) => factory.BeginCreateMessageReceiver(subscriptionEntityPath, ReceiveMode.ReceiveAndDelete, c, s),
                                     (thisPtr, r) => receiver = factory.EndCreateMessageReceiver(r),
                                     ExceptionPolicy.Transfer));

                    var pump = new MessagePump(topicPath, receiver, this.owner.inputQueue, this.owner.semaphore);
                    this.owner.pumps.Add(partitionId, pump);

                    pump.Start();
                }

                this.owner.dispatcher = new MessageDispatcher(this.owner.inputQueue, this.owner.onReceivedAsync);
                this.owner.dispatcher.Start();
            }