blob: 3fd0bc5312acfc038b2a67a74c895385fc62cf8d [file] [log] [blame]
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001// 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
18using System;
19using System.Threading;
20using System.Threading.Tasks;
21using Microsoft.Extensions.Logging;
22using Thrift.Protocol;
23using Thrift.Transport;
24using Thrift.Processor;
25
26namespace Thrift.Server
27{
28 // ReSharper disable once InconsistentNaming
29 public abstract class TServer
30 {
31 protected readonly ILogger Logger;
Jens Geyer421444f2019-03-20 22:13:25 +010032 protected TProtocolFactory InputProtocolFactory;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010033 protected TTransportFactory InputTransportFactory;
34 protected ITProcessorFactory ProcessorFactory;
Jens Geyer421444f2019-03-20 22:13:25 +010035 protected TProtocolFactory OutputProtocolFactory;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010036 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 Geyer421444f2019-03-20 22:13:25 +010043 TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory,
Jens Geyeraa0c8b32019-01-28 23:27:45 +010044 ILogger logger = null)
45 {
46 ProcessorFactory = processorFactory ?? throw new ArgumentNullException(nameof(processorFactory));
47 ServerTransport = serverTransport;
Jens Geyeradde44b2019-02-05 01:00:02 +010048 InputTransportFactory = inputTransportFactory ?? new TTransportFactory();
49 OutputTransportFactory = outputTransportFactory ?? new TTransportFactory();
Jens Geyeraa0c8b32019-01-28 23:27:45 +010050 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 Geyerdce22992020-05-16 23:02:27 +020079 public virtual Task ServeAsync(CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +010080 {
Jens Geyerdce22992020-05-16 23:02:27 +020081 cancellationToken.ThrowIfCancellationRequested();
82 return Task.CompletedTask;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010083 }
84 }
Jens Geyeradde44b2019-02-05 01:00:02 +010085}