Ejemplo n.º 1
0
        /// <summary>
        /// Use new ThreadPool thread for each new client connection.
        /// </summary>
        public override async Task ServeAsync(CancellationToken cancellationToken)
        {
            ServerCancellationToken = cancellationToken;
            try
            {
                try
                {
                    ServerTransport.Listen();
                }
                catch (TTransportException ttx)
                {
                    LogError("Error, could not listen on ServerTransport: " + ttx);
                    return;
                }

                //Fire the preServe server event when server is up but before any client connections
                if (ServerEventHandler != null)
                {
                    await ServerEventHandler.PreServeAsync(cancellationToken);
                }

                while (!(stop || ServerCancellationToken.IsCancellationRequested))
                {
                    try
                    {
                        TTransport client = await ServerTransport.AcceptAsync(cancellationToken);

                        _ = Task.Run(async() => await ExecuteAsync(client), cancellationToken);   // intentionally ignoring retval
                    }
                    catch (TaskCanceledException)
                    {
                        stop = true;
                    }
                    catch (TTransportException ttx)
                    {
                        if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted)
                        {
                            LogError(ttx.ToString());
                        }
                    }
                }

                if (stop)
                {
                    try
                    {
                        ServerTransport.Close();
                    }
                    catch (TTransportException ttx)
                    {
                        LogError("TServerTransport failed on close: " + ttx.Message);
                    }
                    stop = false;
                }
            }
            finally
            {
                ServerCancellationToken = default;
            }
        }
        /// <summary>
        /// Use new ThreadPool thread for each new client connection.
        /// </summary>
        public override async Task ServeAsync(CancellationToken cancellationToken)
        {
            ServerCancellationToken = cancellationToken;
            try
            {
                try
                {
                    ServerTransport.Listen();
                }
                catch (TTransportException ttx)
                {
                    LogError("Error, could not listen on ServerTransport: " + ttx);
                    return;
                }

                //Fire the preServe server event when server is up but before any client connections
                if (ServerEventHandler != null)
                {
                    await ServerEventHandler.PreServeAsync(cancellationToken);
                }

                while (!stop)
                {
                    int failureCount = 0;
                    try
                    {
                        TTransport client = await ServerTransport.AcceptAsync(cancellationToken);

                        ThreadPool.QueueUserWorkItem(this.Execute, client);
                    }
                    catch (TTransportException ttx)
                    {
                        if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted)
                        {
                            ++failureCount;
                            LogError(ttx.ToString());
                        }
                    }
                }

                if (stop)
                {
                    try
                    {
                        ServerTransport.Close();
                    }
                    catch (TTransportException ttx)
                    {
                        LogError("TServerTransport failed on close: " + ttx.Message);
                    }
                    stop = false;
                }
            }
            finally
            {
                ServerCancellationToken = default(CancellationToken);
            }
        }
Ejemplo n.º 3
0
        private async Task StartListening(CancellationToken cancellationToken)
        {
            ServerTransport.Listen();

            Logger.LogTrace("Started listening at server");

            if (ServerEventHandler != null)
            {
                await ServerEventHandler.PreServeAsync(cancellationToken);
            }

            while (!cancellationToken.IsCancellationRequested)
            {
                if (ServerTransport.IsClientPending())
                {
                    Logger.LogTrace("Waiting for client connection");

                    try
                    {
                        var client = await ServerTransport.AcceptAsync(cancellationToken);

                        await Task.Factory.StartNew(() => Execute(client, cancellationToken), cancellationToken);
                    }
                    catch (TTransportException ttx)
                    {
                        Logger.LogTrace($"Transport exception: {ttx}");

                        if (ttx.Type != TTransportException.ExceptionType.Interrupted)
                        {
                            Logger.LogError(ttx.ToString());
                        }
                    }
                }
                else
                {
                    try
                    {
                        await Task.Delay(TimeSpan.FromMilliseconds(_clientWaitingDelay), cancellationToken);
                    }
                    catch (TaskCanceledException) { }
                }
            }

            ServerTransport.Close();

            Logger.LogTrace("Completed listening at server");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Use new ThreadPool thread for each new client connection.
        /// </summary>
        public override async Task ServeAsync(CancellationToken cancellationToken)
        {
            ServerCancellationToken = cancellationToken;
            try
            {
                try
                {
                    ServerTransport.Listen();
                }
                catch (TTransportException ttx)
                {
                    LogError("Error, could not listen on ServerTransport: " + ttx);
                    return;
                }

                //Fire the preServe server event when server is up but before any client connections
                if (ServerEventHandler != null)
                {
                    await ServerEventHandler.PreServeAsync(cancellationToken);
                }

                while (!stop)
                {
                    int failureCount = 0;
                    try
                    {
                        TTransport client = await ServerTransport.AcceptAsync(cancellationToken);

#if WINDOWS_UWP
                        await Windows.System.Threading.ThreadPool.RunAsync((source) => this.Execute(client));
#elif NETSTANDARD1_3
                        Debug.WriteLine("ThreadPool IS NOT SUPPORTED IN NETSTANDARD 1.3");
#else
                        ThreadPool.QueueUserWorkItem(this.Execute, client);
#endif
                    }
                    catch (TTransportException ttx)
                    {
                        if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted)
                        {
                            ++failureCount;
                            LogError(ttx.ToString());
                        }
                    }
                }

                if (stop)
                {
                    try
                    {
                        ServerTransport.Close();
                    }
                    catch (TTransportException ttx)
                    {
                        LogError("TServerTransport failed on close: " + ttx.Message);
                    }
                    stop = false;
                }
            }
            finally
            {
                ServerCancellationToken = default(CancellationToken);
            }
        }