Esempio n. 1
0
        public void StartServer()
        {
            try
            {
                var configuration = GetConfiguration();
                Log.Logger = CreateSerilogLogger(configuration);
                IHost host       = CreateHostBuilder(configuration, Log.Logger);
                int   serverPort = configuration.GetValue <int>(STR_ThriftServerDefaultPort);

                Log.Information("Starting {0} at port {1}", STR_AppName, serverPort);

                //Prepare the thrift server processor
                ThriftGatewayServerImpl serverHandler = new ThriftGatewayServerImpl();
                var processor = new ThriftAPIGatewayService.AsyncProcessor(serverHandler);

                var protocolFactory     = new TBinaryProtocol.Factory();
                var thriftConfiguration = new TConfiguration();
                var serverTransport     = new TServerSocketTransport(serverPort, thriftConfiguration);
                var transportFactory    = new TBufferedTransport.Factory();
                var threadConfiguration = new TThreadPoolAsyncServer.Configuration();
                var loggerFactory       = new SerilogLoggerFactory(Log.Logger);

                var server = new TThreadPoolAsyncServer(
                    processorFactory: new TSingletonProcessorFactory(processor),
                    serverTransport: serverTransport,
                    inputTransportFactory: transportFactory,
                    outputTransportFactory: transportFactory,
                    inputProtocolFactory: protocolFactory,
                    outputProtocolFactory: protocolFactory,
                    threadConfig: threadConfiguration,
                    loggerFactory.CreateLogger <Startup>());

                Log.Information("Starting {0} at port {1}", STR_AppName, serverPort);
                Log.Information(STR_QuitText);

                var source = new CancellationTokenSource();
                server.ServeAsync(CancellationToken.None).GetAwaiter().GetResult();
                Console.ReadLine();
                source.Cancel();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Startup terminated unexpectedly ({ApplicationContext})!", STR_AppName);
            }
        }
Esempio n. 2
0
        public Task StartAsync(EndPoint endPoint)
        {
            try
            {
                CancellationToken cancellationToken = new CancellationToken();
                var ipEndPoint = endPoint as IPEndPoint;
                TMultiplexedServiceProcessor processor = new TMultiplexedServiceProcessor(_logger, _transportMessageDecoder, _transportMessageEncoder, new ServerHandler(async(contenxt, message) =>
                {
                    var sender = new ThriftServerMessageSender(_transportMessageEncoder, contenxt);
                    await OnReceived(sender, message);
                }, _logger));
                foreach (var entry in _entries)
                {
                    var attr = entry.Type.GetCustomAttribute <BindProcessorAttribute>();

                    if (attr != null)
                    {
                        var asyncProcessor = Activator.CreateInstance(attr.ProcessorType, new object[] { entry.Behavior }) as ITAsyncProcessor;
                        processor.RegisterProcessor(entry.Type.Name, asyncProcessor);
                    }
                }
                var factory1        = new TBinaryProtocol.Factory();
                var factory2        = new TBinaryProtocol.Factory();
                var serverTransport = new TServerSocketTransport(new TcpListener(ipEndPoint));
                var config          = new TThreadPoolAsyncServer.Configuration();
                config.MaxWorkerThreads = 200;
                config.MinWorkerThreads = 10;
                var server = new TThreadPoolAsyncServer(new TSingletonProcessorFactory(processor), serverTransport, new TFramedTransport.Factory(), new TFramedTransport.Factory(), factory1, factory2, config, _logger);
                server.ServeAsync(cancellationToken);

                if (_logger.IsEnabled(LogLevel.Information))
                {
                    _logger.LogInformation($"Thrift服务主机启动成功,监听地址:{endPoint}。");
                }
            }
            catch
            {
                _logger.LogError($"Thrift服务主机启动失败,监听地址:{endPoint}。 ");
            }
            return(Task.CompletedTask);
        }