Beispiel #1
0
        /*
         * /// <summary>
         * /// Reconnects the underlying broker.
         * /// </summary>
         * /// <returns></returns>
         * private async Task ReconnectAsync() {
         *  // shutdown broker
         *  lock (_broker) {
         *      // add broker wait
         *      _brokerWait = new TaskCompletionSource<Broker>();
         *
         *      // trigger all reply waits
         *      lock (_replyWaits) {
         *          // copy dictionary over
         *          foreach (KeyValuePair<Guid, ReplyWait> kv in _replyWaits) {
         *              kv.Value.CompletionSource.TrySetException(new Exception("The underlying broker connection was lost"));
         *          }
         *
         *          _replyWaits.Clear();
         *      }
         *
         #if DEBUG
         *      Debug.WriteLine(":Reconnect -> Cancelled all waits");
         #endif
         *  }
         *
         *  // wait for reconnect
         *  _broker = await _broker.Context.CreateBrokerAsync(_configuration.ApplicationId).ConfigureAwait(false);
         *
         *  // setup
         *  await SetupAsync().ConfigureAwait(false);
         *
         *  // resetup services
         *  List<Task> resetupTasks = new List<Task>();
         *
         *  lock (_services) {
         *      foreach (Service service in _services) {
         *          resetupTasks.Add(service.ResetupAsync(_broker));
         *      }
         *  }
         *
         *  // wait until all services are setup again
         *  await Task.WhenAll(resetupTasks).ConfigureAwait(false);
         *
         #if DEBUG
         *  Debug.WriteLine(string.Format(":Reconnect -> Recreated broker with {0} services", resetupTasks.Count));
         #endif
         *
         *  // cancel broker wait
         *  lock (_broker) {
         *      _brokerWait.SetResult(_broker);
         *      _brokerWait = null;
         *
         #if DEBUG
         *      Debug.WriteLine(":Reconnect -> Set broker and resumed waits");
         #endif
         *  }
         * }*/

        /// <summary>
        /// Setup the node, called internally.
        /// </summary>
        /// <returns></returns>
        internal async Task SetupAsync()
        {
            // setup all namespaces
            Task[] setupTasks = _namespaces.Select(n => n.SetupAsync()).ToArray();

            await Task.WhenAll(setupTasks);

            // setup service
            if (_queryService == null)
            {
                _queryService = await AttachAsync(string.Format("node:{0}", _uuid), ServiceType.Singleton, ServiceExecution.Parallel, RpcBehaviour.Bind <INodeQuery001>(new NodeQueryImpl(this))).ConfigureAwait(false);
            }
        }
Beispiel #2
0
        static async Task AsyncMain(string[] args)
        {
            // attach node
            TestNode = await Node.CreateFromEnvironmentAsync(new NodeConfiguration()
            {
                ThrowUnhandledExceptions = true
            });

            // attach
            Service service = null;

            try {
                service = await TestNode.AttachAsync("auth:login", ServiceType.Balanced, RpcBehaviour.Bind <ITest001>(new Test001(Guid.NewGuid())));
            } catch (Exception) {
                Console.WriteLine();
            }

            var    proxy = TestNode.Proxy <ITest001>("auth:login");
            string a     = await proxy.Login(new LoginRequestMsg()
            {
                Username = "******",
                Password = "******"
            });

            await Task.Delay(50000);
        }
Beispiel #3
0
        /// <summary>
        /// Setup the node, called internally.
        /// </summary>
        /// <returns></returns>
        internal async Task SetupAsync()
        {
            // add shutdown handler
            _broker.Shutdown += async delegate(object s, BrokerShutdownEventArgs e) {
                Debug.WriteLine(":Reconnect -> Reconnecting");

                try {
                    await ReconnectAsync().ConfigureAwait(false);

#if DEBUG
                } catch (Exception ex) {
                    Debug.Fail(":Reconnect -> Failed to reconnect: " + ex.Message);
#else
                } catch (Exception ex) {
#endif
                    Console.WriteLine("messaging", "failed to reconnect to broker: {0}", ex.ToString());
                    Dispose();
                }
            };

            // add returned handler
            _broker.Returned += delegate(object s, BrokerReturnedEventArgs e) {
                if (e.ID != Guid.Empty)
                {
                    TaskCompletionSource <Envelope> tcs;

                    lock (_replyWaits) {
                        // try and get the reply wait
                        if (!_replyWaits.TryGetValue(e.ID, out ReplyWait replyWait))
                        {
                            return;
                        }

                        // if this is a multi-reply do nothing
                        if (replyWait.Results != null)
                        {
                            return;
                        }

                        // remove and set exception
                        tcs = replyWait.CompletionSource;
                        _replyWaits.Remove(e.ID);
                    }

                    tcs.TrySetException(new Exception("The envelope was returned before delivery"));
                }
            };

            // create reply queue
            try {
                // declare queue with unique name
                using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) {
                    // get unique string
                    byte[] uniqueId = new byte[20];
                    rng.GetBytes(uniqueId);
                    string uniqueIdStr = BitConverter.ToString(uniqueId).Replace("-", "").ToLower();

                    _replyQueue = await _broker.CreateQueueAsync(string.Format("~reply:{1}%{0}", _uuid, uniqueIdStr), false, true, "", "", true, true, new Dictionary <string, object>() {
                        { "x-expires", (int)TimeSpan.FromMinutes(15).TotalMilliseconds }
                    }).ConfigureAwait(false);
                }
            } catch (Exception ex) {
                throw new InvalidOperationException("Failed to create node reply queue", ex);
            }

            // setup service
            if (_queryService == null)
            {
                _queryService = await AttachAsync(string.Format("node:{0}", _uuid), ServiceType.Singleton, ServiceExecution.Parallel, RpcBehaviour.Bind <INodeQuery001>(new NodeQueryImpl(this))).ConfigureAwait(false);
            }

            // start reply processor
            ReplyLoop();
        }