public THttpServerTransport(ITAsyncProcessor processor, ITProtocolFactory inputProtocolFactory, ITProtocolFactory outputProtocolFactory, RequestDelegate next, ILoggerFactory loggerFactory) { if (processor == null) { throw new ArgumentNullException(nameof(processor)); } if (inputProtocolFactory == null) { throw new ArgumentNullException(nameof(inputProtocolFactory)); } if (outputProtocolFactory == null) { throw new ArgumentNullException(nameof(outputProtocolFactory)); } if (loggerFactory == null) { throw new ArgumentNullException(nameof(loggerFactory)); } Processor = processor; InputProtocolFactory = inputProtocolFactory; OutputProtocolFactory = outputProtocolFactory; _next = next; _logger = loggerFactory.CreateLogger<THttpServerTransport>(); }
public TThreadPoolAsyncServer(ITAsyncProcessor processor, TServerTransport serverTransport, ILogger logger = null) : this(new TSingletonProcessorFactory(processor), serverTransport, null, null, // defaults to TTransportFactory() new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), new Configuration(), logger) { }
public THttpServerTransport( ITAsyncProcessor processor, TConfiguration config, RequestDelegate next = null, ILoggerFactory loggerFactory = null) : this(processor, config, new TBinaryProtocol.Factory(), null, next, loggerFactory) { }
public THttpServerTransport( ITAsyncProcessor processor, TProtocolFactory protocolFactory, TTransportFactory transFactory = null, RequestDelegate next = null, ILoggerFactory loggerFactory = null) : this(processor, protocolFactory, protocolFactory, transFactory, transFactory, next, loggerFactory) { }
public AsyncBaseServer(ITAsyncProcessor processor, TServerTransport serverTransport, ITProtocolFactory inputProtocolFactory, ITProtocolFactory outputProtocolFactory, ILoggerFactory loggerFactory, int clientWaitingDelay = 10) : this(new SingletonTProcessorFactory(processor), serverTransport, new TTransportFactory(), new TTransportFactory(), inputProtocolFactory, outputProtocolFactory, loggerFactory.CreateLogger(nameof(AsyncBaseServer)), clientWaitingDelay) { }
public void RegisterProcessor(string serviceName, ITAsyncProcessor processor) { if (_serviceProcessorMap.ContainsKey(serviceName)) { throw new InvalidOperationException( $"Processor map already contains processor with name: '{serviceName}'"); } _serviceProcessorMap.Add(serviceName, processor); }
public TThreadPoolAsyncServer(ITAsyncProcessor processor, TServerTransport serverTransport, TTransportFactory transportFactory, TProtocolFactory protocolFactory) : this(new TSingletonProcessorFactory(processor), serverTransport, transportFactory, transportFactory, protocolFactory, protocolFactory, new Configuration()) { }
public THttpServerTransport(ITAsyncProcessor processor, ITProtocolFactory inputProtocolFactory, ITProtocolFactory outputProtocolFactory, RequestDelegate next = null, ILoggerFactory loggerFactory = null) { // loggerFactory == null is not illegal anymore Processor = processor ?? throw new ArgumentNullException(nameof(processor)); InputProtocolFactory = inputProtocolFactory ?? throw new ArgumentNullException(nameof(inputProtocolFactory)); OutputProtocolFactory = outputProtocolFactory ?? throw new ArgumentNullException(nameof(outputProtocolFactory)); _next = next; _logger = (loggerFactory != null) ? loggerFactory.CreateLogger <THttpServerTransport>() : new NullLogger <THttpServerTransport>(); }
public TSimpleAsyncServer(ITAsyncProcessor processor, TServerTransport serverTransport, TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory, ILoggerFactory loggerFactory) : this(new TSingletonProcessorFactory(processor), serverTransport, null, // defaults to TTransportFactory() null, // defaults to TTransportFactory() inputProtocolFactory, outputProtocolFactory, loggerFactory.CreateLogger(nameof(TSimpleAsyncServer))) { }
public THttpServerTransport(ITAsyncProcessor processor, ITProtocolFactory inputProtocolFactory, ITProtocolFactory outputProtocolFactory, RequestDelegate next, ILoggerFactory loggerFactory) { if (loggerFactory == null) { throw new ArgumentNullException(nameof(loggerFactory)); } Processor = processor ?? throw new ArgumentNullException(nameof(processor)); InputProtocolFactory = inputProtocolFactory ?? throw new ArgumentNullException(nameof(inputProtocolFactory)); OutputProtocolFactory = outputProtocolFactory ?? throw new ArgumentNullException(nameof(outputProtocolFactory)); _next = next; _logger = loggerFactory.CreateLogger <THttpServerTransport>(); }
static async Task RunNotificationServiceAsync() { var fabric = new LoggerFactory().AddConsole(LogLevel.Trace); var handler = new NotificationHandler(); ITAsyncProcessor processor = null; TServerTransport serverTransport = null; serverTransport = new TServerSocketTransport(9090); ITProtocolFactory inputProtocolFactory; ITProtocolFactory outputProtocolFactory; inputProtocolFactory = new TBinaryProtocol.Factory(); outputProtocolFactory = new TBinaryProtocol.Factory(); processor = new NotificationService.AsyncProcessor(handler); var server = new AsyncBaseServer(processor, serverTransport, inputProtocolFactory, outputProtocolFactory, fabric); await server.ServeAsync(new CancellationToken()); }
private static async Task RunSelectedConfigurationAsync(Transport transport, Protocol protocol, CancellationToken cancellationToken) { var fabric = new LoggerFactory().AddConsole(LogLevel.Trace).AddDebug(LogLevel.Trace); var handler = new ThriftService(); ITAsyncProcessor processor = null; TServerTransport serverTransport = null; switch (transport) { case Transport.Tcp: serverTransport = new TServerSocketTransport(9090); break; case Transport.TcpBuffered: serverTransport = new TServerSocketTransport(9090, 10000, true); break; case Transport.NamedPipe: serverTransport = new TNamedPipeServerTransport(".test"); break; case Transport.TcpTls: serverTransport = new TTlsServerSocketTransport(9090, false, GetCertificate(), ClientCertValidator, LocalCertificateSelectionCallback); break; case Transport.Framed: serverTransport = new TServerFramedTransport(9090); break; } ITProtocolFactory inputProtocolFactory; ITProtocolFactory outputProtocolFactory; switch (protocol) { case Protocol.Binary: inputProtocolFactory = new TBinaryProtocol.Factory(); outputProtocolFactory = new TBinaryProtocol.Factory(); processor = new Calculator.AsyncProcessor(handler); break; case Protocol.Compact: inputProtocolFactory = new TCompactProtocol.Factory(); outputProtocolFactory = new TCompactProtocol.Factory(); processor = new Calculator.AsyncProcessor(handler); break; case Protocol.Json: inputProtocolFactory = new TJsonProtocol.Factory(); outputProtocolFactory = new TJsonProtocol.Factory(); processor = new Calculator.AsyncProcessor(handler); break; case Protocol.Multiplexed: inputProtocolFactory = new TBinaryProtocol.Factory(); outputProtocolFactory = new TBinaryProtocol.Factory(); var calcHandler = new ThriftService(); var calcProcessor = new Calculator.AsyncProcessor(calcHandler); var calcHandlerShared = new SharedServiceAsyncHandler(); var calcProcessorShared = new SharedService.AsyncProcessor(calcHandlerShared); var multiplexedProcessor = new TMultiplexedProcessor(); multiplexedProcessor.RegisterProcessor(nameof(Calculator), calcProcessor); multiplexedProcessor.RegisterProcessor(nameof(SharedService), calcProcessorShared); processor = multiplexedProcessor; break; default: throw new ArgumentOutOfRangeException(nameof(protocol), protocol, null); } try { Logger.LogInformation( $"Selected TAsyncServer with {serverTransport} transport, {processor} processor and {inputProtocolFactory} protocol factories"); var server = new AsyncBaseServer(processor, serverTransport, inputProtocolFactory, outputProtocolFactory, fabric); Logger.LogInformation("Starting the server ..."); await server.ServeAsync(cancellationToken); } catch (Exception ex) { Logger.LogInformation(ex, ex.Message); } }
public RpcServiceHost(IConfiguration configuration, ITAsyncProcessor processor, ILoggerFactory loggerFactory) { Configuration = configuration; Processor = processor; LoggerFactory = loggerFactory; }
public THttpServerTransport(ITAsyncProcessor processor, ITProtocolFactory protocolFactory, RequestDelegate next, ILoggerFactory loggerFactory) : this(processor, protocolFactory, protocolFactory, next, loggerFactory) { }
private async Task RunSelectedConfigurationAsync(Transport transport, Buffering buffering, Protocol protocol, bool multiplex, CancellationToken cancellationToken) { var port = _thriftServerOption.CurrentValue.Port; var configuration = _thriftServerOption.CurrentValue.Configuration; TServerTransport serverTransport = transport switch { Transport.Tcp => new TServerSocketTransport(port, configuration), Transport.NamedPipe => new TNamedPipeServerTransport(".test", configuration),//, NamedPipeClientFlags.None), Transport.TcpTls => new TTlsServerSocketTransport(9090, configuration, GetCertificate(), ClientCertValidator, LocalCertificateSelectionCallback), _ => throw new ArgumentException("unsupported value $transport", nameof(transport)), }; TTransportFactory transportFactory = buffering switch { Buffering.Buffered => new TBufferedTransport.Factory(), Buffering.Framed => new TFramedTransport.Factory(), // layered transport(s) are optional Buffering.None => null, _ => throw new ArgumentException("unsupported value $buffering", nameof(buffering)), }; TProtocolFactory protocolFactory = protocol switch { Protocol.Binary => new TBinaryProtocol.Factory(), Protocol.Compact => new TCompactProtocol.Factory(), Protocol.Json => new TJsonProtocol.Factory(), Protocol.BinaryHeader => new TBinaryHeaderServerProtocol.Factory(), Protocol.CompactHeader => new TCompactHeaderServerProtocol.Factory(), Protocol.JsonHeader => new TJsonHeaderServerProtocol.Factory(), _ => throw new ArgumentException("unsupported value $protocol", nameof(protocol)), }; //var handler = new CalculatorAsyncHandler(); //ITAsyncProcessor processor = new Calculator.AsyncProcessor(handler); ITAsyncProcessor processor = _asyncProcessorList.FirstOrDefault(); if (multiplex) { var multiplexedProcessor = new TMultiplexedProcessor(); foreach (var item in _asyncProcessorList) { multiplexedProcessor.RegisterProcessor(item.GetType().FullName, item); } processor = multiplexedProcessor; } try { _logger.LogInformation( string.Format( "TSimpleAsyncServer with \n{0} transport\n{1} buffering\nmultiplex = {2}\n{3} protocol", transport, buffering, multiplex ? "yes" : "no", protocol )); _server = new TSimpleAsyncServer( itProcessorFactory: new TSingletonProcessorFactory(processor), serverTransport: serverTransport, inputTransportFactory: transportFactory, outputTransportFactory: transportFactory, inputProtocolFactory: protocolFactory, outputProtocolFactory: protocolFactory, logger: _loggerFactory.CreateLogger <TSimpleAsyncServer>()); //var threadConfig = new TThreadPoolAsyncServer.Configuration(); //var server = new TThreadPoolAsyncServer( // processorFactory: new TSingletonProcessorFactory(processor), // serverTransport: serverTransport, // inputTransportFactory: transportFactory, // outputTransportFactory: transportFactory, // inputProtocolFactory: protocolFactory, // outputProtocolFactory: protocolFactory, // threadConfig: threadConfig, // logger: loggerFactory.CreateLogger<TThreadPoolAsyncServer>()); _logger.LogInformation("Starting the server..."); await _server.ServeAsync(cancellationToken); } catch (Exception x) { _logger.LogInformation(x.ToString()); } }
public THttpServerTransport(ITAsyncProcessor processor, RequestDelegate next, ILoggerFactory loggerFactory) : this(processor, new TBinaryProtocol.Factory(), next, loggerFactory) { }
public HttpServerSample(ITAsyncProcessor asyncProcessor) { _asyncProcessor = asyncProcessor; }
public SingletonTProcessorFactory(ITAsyncProcessor tAsyncProcessor) { _tAsyncProcessor = tAsyncProcessor; }
/// <summary> /// Loops on processing a client forever /// client will be a TTransport instance /// </summary> /// <param name="client"></param> private async Task ExecuteAsync(TTransport client) { var cancellationToken = ServerCancellationToken; using (client) { ITAsyncProcessor processor = ProcessorFactory.GetAsyncProcessor(client, this); TTransport inputTransport = null; TTransport outputTransport = null; TProtocol inputProtocol = null; TProtocol outputProtocol = null; object connectionContext = null; try { try { inputTransport = InputTransportFactory.GetTransport(client); outputTransport = OutputTransportFactory.GetTransport(client); inputProtocol = InputProtocolFactory.GetProtocol(inputTransport); outputProtocol = OutputProtocolFactory.GetProtocol(outputTransport); //Recover event handler (if any) and fire createContext server event when a client connects if (ServerEventHandler != null) { connectionContext = await ServerEventHandler.CreateContextAsync(inputProtocol, outputProtocol, cancellationToken); } //Process client requests until client disconnects while (!(stop || cancellationToken.IsCancellationRequested)) { if (!await inputTransport.PeekAsync(cancellationToken)) { break; } //Fire processContext server event //N.B. This is the pattern implemented in C++ and the event fires provisionally. //That is to say it may be many minutes between the event firing and the client request //actually arriving or the client may hang up without ever makeing a request. if (ServerEventHandler != null) { await ServerEventHandler.ProcessContextAsync(connectionContext, inputTransport, cancellationToken); } //Process client request (blocks until transport is readable) if (!await processor.ProcessAsync(inputProtocol, outputProtocol, cancellationToken)) { break; } } } catch (TTransportException) { //Usually a client disconnect, expected } catch (Exception x) { //Unexpected LogError("Error: " + x); } //Fire deleteContext server event after client disconnects if (ServerEventHandler != null) { await ServerEventHandler.DeleteContextAsync(connectionContext, inputProtocol, outputProtocol, cancellationToken); } } finally { //Close transports inputTransport?.Close(); outputTransport?.Close(); // disposable stuff should be disposed inputProtocol?.Dispose(); outputProtocol?.Dispose(); inputTransport?.Dispose(); outputTransport?.Dispose(); } } }
public THeaderProcessor(ITAsyncProcessor realProcessor) { this.realProcessor = realProcessor; }
public TSingletonProcessorFactory(ITAsyncProcessor asyncProcessor) { _asyncProcessor = asyncProcessor; }
private static async Task RunSelectedConfigurationAsync(Transport transport, Buffering buffering, Protocol protocol, CancellationToken cancellationToken) { var handler = new CalculatorAsyncHandler(); TServerTransport serverTransport = null; switch (transport) { case Transport.Tcp: serverTransport = new TServerSocketTransport(9090); break; case Transport.NamedPipe: serverTransport = new TNamedPipeServerTransport(".test"); break; case Transport.TcpTls: serverTransport = new TTlsServerSocketTransport(9090, GetCertificate(), ClientCertValidator, LocalCertificateSelectionCallback); break; } TTransportFactory inputTransportFactory = null; TTransportFactory outputTransportFactory = null; switch (buffering) { case Buffering.Buffered: inputTransportFactory = new TBufferedTransport.Factory(); outputTransportFactory = new TBufferedTransport.Factory(); break; case Buffering.Framed: inputTransportFactory = new TFramedTransport.Factory(); outputTransportFactory = new TFramedTransport.Factory(); break; default: // layered transport(s) are optional Debug.Assert(buffering == Buffering.None, "unhandled case"); break; } TProtocolFactory inputProtocolFactory = null; TProtocolFactory outputProtocolFactory = null; ITAsyncProcessor processor = null; switch (protocol) { case Protocol.Binary: inputProtocolFactory = new TBinaryProtocol.Factory(); outputProtocolFactory = new TBinaryProtocol.Factory(); processor = new Calculator.AsyncProcessor(handler); break; case Protocol.Compact: inputProtocolFactory = new TCompactProtocol.Factory(); outputProtocolFactory = new TCompactProtocol.Factory(); processor = new Calculator.AsyncProcessor(handler); break; case Protocol.Json: inputProtocolFactory = new TJsonProtocol.Factory(); outputProtocolFactory = new TJsonProtocol.Factory(); processor = new Calculator.AsyncProcessor(handler); break; case Protocol.Multiplexed: inputProtocolFactory = new TBinaryProtocol.Factory(); outputProtocolFactory = new TBinaryProtocol.Factory(); var calcHandler = new CalculatorAsyncHandler(); var calcProcessor = new Calculator.AsyncProcessor(calcHandler); var sharedServiceHandler = new SharedServiceAsyncHandler(); var sharedServiceProcessor = new SharedService.AsyncProcessor(sharedServiceHandler); var multiplexedProcessor = new TMultiplexedProcessor(); multiplexedProcessor.RegisterProcessor(nameof(Calculator), calcProcessor); multiplexedProcessor.RegisterProcessor(nameof(SharedService), sharedServiceProcessor); processor = multiplexedProcessor; break; default: throw new ArgumentOutOfRangeException(nameof(protocol), protocol, null); } try { Logger.LogInformation( $"Selected TAsyncServer with {serverTransport} transport, {processor} processor and {inputProtocolFactory} protocol factories"); var loggerFactory = ServiceCollection.BuildServiceProvider().GetService <ILoggerFactory>(); var server = new TSimpleAsyncServer( itProcessorFactory: new TSingletonProcessorFactory(processor), serverTransport: serverTransport, inputTransportFactory: inputTransportFactory, outputTransportFactory: outputTransportFactory, inputProtocolFactory: inputProtocolFactory, outputProtocolFactory: outputProtocolFactory, logger: loggerFactory.CreateLogger <TSimpleAsyncServer>()); Logger.LogInformation("Starting the server..."); await server.ServeAsync(cancellationToken); } catch (Exception x) { Logger.LogInformation(x.ToString()); } }
private static async Task RunSelectedConfigurationAsync(Transport transport, Protocol protocol, CancellationToken cancellationToken) { var handler = new CalculatorAsyncHandler(); ITAsyncProcessor processor = null; TServerTransport serverTransport = null; switch (transport) { case Transport.Tcp: serverTransport = new TServerSocketTransport(9090); break; case Transport.TcpBuffered: serverTransport = new TServerSocketTransport(port: 9090, clientTimeout: 10000, buffering: Buffering.BufferedTransport); break; case Transport.NamedPipe: serverTransport = new TNamedPipeServerTransport(".test"); break; case Transport.TcpTls: serverTransport = new TTlsServerSocketTransport(9090, GetCertificate(), Buffering.None, ClientCertValidator, LocalCertificateSelectionCallback); break; case Transport.Framed: serverTransport = new TServerFramedTransport(9090); break; } ITProtocolFactory inputProtocolFactory; ITProtocolFactory outputProtocolFactory; switch (protocol) { case Protocol.Binary: { inputProtocolFactory = new TBinaryProtocol.Factory(); outputProtocolFactory = new TBinaryProtocol.Factory(); processor = new Calculator.AsyncProcessor(handler); } break; case Protocol.Compact: { inputProtocolFactory = new TCompactProtocol.Factory(); outputProtocolFactory = new TCompactProtocol.Factory(); processor = new Calculator.AsyncProcessor(handler); } break; case Protocol.Json: { inputProtocolFactory = new TJsonProtocol.Factory(); outputProtocolFactory = new TJsonProtocol.Factory(); processor = new Calculator.AsyncProcessor(handler); } break; case Protocol.Multiplexed: { inputProtocolFactory = new TBinaryProtocol.Factory(); outputProtocolFactory = new TBinaryProtocol.Factory(); var calcHandler = new CalculatorAsyncHandler(); var calcProcessor = new Calculator.AsyncProcessor(calcHandler); var sharedServiceHandler = new SharedServiceAsyncHandler(); var sharedServiceProcessor = new SharedService.AsyncProcessor(sharedServiceHandler); var multiplexedProcessor = new TMultiplexedProcessor(); multiplexedProcessor.RegisterProcessor(nameof(Calculator), calcProcessor); multiplexedProcessor.RegisterProcessor(nameof(SharedService), sharedServiceProcessor); processor = multiplexedProcessor; } break; default: throw new ArgumentOutOfRangeException(nameof(protocol), protocol, null); } try { Logger.LogInformation( $"Selected TAsyncServer with {serverTransport} transport, {processor} processor and {inputProtocolFactory} protocol factories"); var fabric = ServiceCollection.BuildServiceProvider().GetService <ILoggerFactory>(); var server = new TSimpleAsyncServer(processor, serverTransport, inputProtocolFactory, outputProtocolFactory, fabric); Logger.LogInformation("Starting the server..."); await server.ServeAsync(cancellationToken); } catch (Exception x) { Logger.LogInformation(x.ToString()); } }