Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
| 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | using System; |
| 19 | using System.Threading; |
| 20 | using System.Threading.Tasks; |
| 21 | using Microsoft.Extensions.Logging; |
| 22 | using Thrift.Protocol; |
| 23 | using Thrift.Transport; |
| 24 | using Thrift.Processor; |
| 25 | |
| 26 | namespace Thrift.Server |
| 27 | { |
| 28 | // ReSharper disable once InconsistentNaming |
| 29 | public abstract class TServer |
| 30 | { |
| 31 | protected readonly ILogger Logger; |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 32 | protected TProtocolFactory InputProtocolFactory; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 33 | protected TTransportFactory InputTransportFactory; |
| 34 | protected ITProcessorFactory ProcessorFactory; |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 35 | protected TProtocolFactory OutputProtocolFactory; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 36 | protected TTransportFactory OutputTransportFactory; |
| 37 | |
| 38 | protected TServerEventHandler ServerEventHandler; |
| 39 | protected TServerTransport ServerTransport; |
| 40 | |
| 41 | protected TServer(ITProcessorFactory processorFactory, TServerTransport serverTransport, |
| 42 | TTransportFactory inputTransportFactory, TTransportFactory outputTransportFactory, |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 43 | TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory, |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 44 | ILogger logger = null) |
| 45 | { |
| 46 | ProcessorFactory = processorFactory ?? throw new ArgumentNullException(nameof(processorFactory)); |
| 47 | ServerTransport = serverTransport; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 48 | InputTransportFactory = inputTransportFactory ?? new TTransportFactory(); |
| 49 | OutputTransportFactory = outputTransportFactory ?? new TTransportFactory(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 50 | InputProtocolFactory = inputProtocolFactory ?? throw new ArgumentNullException(nameof(inputProtocolFactory)); |
| 51 | OutputProtocolFactory = outputProtocolFactory ?? throw new ArgumentNullException(nameof(outputProtocolFactory)); |
| 52 | Logger = logger; // null is absolutely legal |
| 53 | } |
| 54 | |
| 55 | public void SetEventHandler(TServerEventHandler seh) |
| 56 | { |
| 57 | ServerEventHandler = seh; |
| 58 | } |
| 59 | |
| 60 | public TServerEventHandler GetEventHandler() |
| 61 | { |
| 62 | return ServerEventHandler; |
| 63 | } |
| 64 | |
| 65 | // Log delegation? deprecated, use ILogger |
| 66 | protected void LogError( string msg) |
| 67 | { |
| 68 | if (Logger != null) |
| 69 | Logger.LogError(msg); |
| 70 | } |
| 71 | |
| 72 | public abstract void Stop(); |
| 73 | |
| 74 | public virtual void Start() |
| 75 | { |
| 76 | // do nothing |
| 77 | } |
| 78 | |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 79 | public virtual Task ServeAsync(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 80 | { |
Jens Geyer | dce2299 | 2020-05-16 23:02:27 +0200 | [diff] [blame] | 81 | cancellationToken.ThrowIfCancellationRequested(); |
| 82 | return Task.CompletedTask; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 83 | } |
| 84 | } |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 85 | } |