Ejemplo n.º 1
0
        public EventThread(TransportEngine engine)
        {
            _engine = engine;
            //_appLifetime = engine.AppLifetime;
            _threadPool  = engine.ThreadPool;
            _loop        = new UvLoopHandle();
            _post        = new UvAsyncHandle();
            _thread      = new Thread(ThreadStart);
            _thread.Name = "Event Thread";
#if !DEBUG
            // Mark the thread as being as unimportant to keeping the process alive.
            // Don't do this for debug builds, so we know if the thread isn't terminating.
            _thread.IsBackground = true;
#endif
            QueueCloseHandle      = PostCloseHandle;
            QueueCloseAsyncHandle = EnqueueCloseHandle;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Begins running the main Emitter Engine loop on the current thread.
        /// </summary>
        /// <param name="bindings">Network binding configuration to use</param>
        public override void Listen(params IBinding[] bindings)
        {
            // Is it already started?
            if (this.Listeners != null)
            {
                throw new InvalidOperationException("Server has already started.");
            }
            this.Listeners = new Stack <IDisposable>();

            // Check the bindings
            if (bindings == null || bindings.Length == 0)
            {
                throw new ArgumentNullException("bindings");
            }

            // Create an engine per binding
            foreach (var binding in bindings)
            {
                try
                {
                    // Get the addresses to listen to
                    var addresses = ServiceAddress.FromBinding(binding);

                    // Start a new transport engine for the binding
                    this.Engine = new TransportEngine(new ServiceContext
                    {
                        ThreadPool = new EventThreadPool()
                    });

                    Listeners.Push(this.Engine);

                    // Use only one thread for mesh
                    var threadCount = binding is MeshBinding ? 1 : ThreadCount;
                    if (threadCount <= 0)
                    {
                        throw new ArgumentOutOfRangeException(nameof(threadCount),
                                                              threadCount,
                                                              "ThreadCount must be positive.");
                    }

                    this.Engine.Start(threadCount);
                    var atLeastOneListener = false;

                    // Add a listener per address
                    foreach (var address in addresses)
                    {
                        try
                        {
                            atLeastOneListener = true;
                            Listeners.Push(
                                this.Engine.CreateServer(address, binding)
                                );

                            Service.Logger.Log("Listening: " + address);
                        }
                        catch (AggregateException aggr)
                        {
                            var ex = aggr.InnerException;
                            if (ex.Message.Contains("EADDRINUSE"))
                            {
                                Service.Logger.Log(LogLevel.Error, "Address In Use: " + address);
                            }
                            else
                            {
                                Service.Logger.Log(LogLevel.Error, ex.Message + ": " + address);
                            }
                        }
                    }

                    if (!atLeastOneListener)
                    {
                        throw new InvalidOperationException("No recognized listening addresses were configured.");
                    }
                }
                catch (Exception ex)
                {
                    Service.Logger.Log(ex);
                    Dispose();
                    throw;
                }
            }
        }