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
9 // http://www.apache.org/licenses/LICENSE-2.0
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
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;
26 namespace Thrift.Server
28 // ReSharper disable once InconsistentNaming
29 public abstract class TServer
31 protected readonly ILogger Logger;
32 protected TProtocolFactory InputProtocolFactory;
33 protected TTransportFactory InputTransportFactory;
34 protected ITProcessorFactory ProcessorFactory;
35 protected TProtocolFactory OutputProtocolFactory;
36 protected TTransportFactory OutputTransportFactory;
38 protected TServerEventHandler ServerEventHandler;
39 protected TServerTransport ServerTransport;
41 protected TServer(ITProcessorFactory processorFactory, TServerTransport serverTransport,
42 TTransportFactory inputTransportFactory, TTransportFactory outputTransportFactory,
43 TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory,
44 ILogger logger = null)
46 ProcessorFactory = processorFactory ?? throw new ArgumentNullException(nameof(processorFactory));
47 ServerTransport = serverTransport;
48 InputTransportFactory = inputTransportFactory ?? new TTransportFactory();
49 OutputTransportFactory = outputTransportFactory ?? new TTransportFactory();
50 InputProtocolFactory = inputProtocolFactory ?? throw new ArgumentNullException(nameof(inputProtocolFactory));
51 OutputProtocolFactory = outputProtocolFactory ?? throw new ArgumentNullException(nameof(outputProtocolFactory));
52 Logger = logger; // null is absolutely legal
55 public void SetEventHandler(TServerEventHandler seh)
57 ServerEventHandler = seh;
60 public TServerEventHandler GetEventHandler()
62 return ServerEventHandler;
65 // Log delegation? deprecated, use ILogger
66 protected void LogError( string msg)
72 public abstract void Stop();
74 public virtual void Start()
79 public virtual async Task ServeAsync(CancellationToken cancellationToken)
81 if (cancellationToken.IsCancellationRequested)
83 await Task.FromCanceled(cancellationToken);