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; |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 20 | using Thrift.Protocol; |
| 21 | using Thrift.Transport; |
| 22 | using Thrift.Processor; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 23 | using System.Threading.Tasks; |
| 24 | using Microsoft.Extensions.Logging; |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 25 | |
| 26 | #pragma warning disable IDE0079 // remove unnecessary pragmas |
| 27 | #pragma warning disable IDE0063 // using can be simplified, we don't |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 28 | |
| 29 | namespace Thrift.Server |
| 30 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 31 | |
| 32 | // ReSharper disable once InconsistentNaming |
| 33 | public class TSimpleAsyncServer : TServer |
| 34 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 35 | private volatile bool stop = false; |
| 36 | |
| 37 | private CancellationToken ServerCancellationToken; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 38 | |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 39 | public TSimpleAsyncServer(ITProcessorFactory itProcessorFactory, |
| 40 | TServerTransport serverTransport, |
| 41 | TTransportFactory inputTransportFactory, |
| 42 | TTransportFactory outputTransportFactory, |
| 43 | TProtocolFactory inputProtocolFactory, |
| 44 | TProtocolFactory outputProtocolFactory, |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 45 | ILogger logger) |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 46 | : base(itProcessorFactory, |
| 47 | serverTransport, |
| 48 | inputTransportFactory, |
| 49 | outputTransportFactory, |
| 50 | inputProtocolFactory, |
| 51 | outputProtocolFactory, |
| 52 | logger) |
| 53 | { |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public TSimpleAsyncServer(ITProcessorFactory itProcessorFactory, |
| 57 | TServerTransport serverTransport, |
| 58 | TTransportFactory inputTransportFactory, |
| 59 | TTransportFactory outputTransportFactory, |
| 60 | TProtocolFactory inputProtocolFactory, |
| 61 | TProtocolFactory outputProtocolFactory, |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 62 | ILoggerFactory loggerFactory) |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 63 | : this(itProcessorFactory, |
| 64 | serverTransport, |
| 65 | inputTransportFactory, |
| 66 | outputTransportFactory, |
| 67 | inputProtocolFactory, |
| 68 | outputProtocolFactory, |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 69 | loggerFactory.CreateLogger<TSimpleAsyncServer>()) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 70 | { |
| 71 | } |
| 72 | |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 73 | public TSimpleAsyncServer(ITAsyncProcessor processor, |
| 74 | TServerTransport serverTransport, |
| 75 | TProtocolFactory inputProtocolFactory, |
| 76 | TProtocolFactory outputProtocolFactory, |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 77 | ILoggerFactory loggerFactory) |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 78 | : this(new TSingletonProcessorFactory(processor), |
| 79 | serverTransport, |
| 80 | null, // defaults to TTransportFactory() |
| 81 | null, // defaults to TTransportFactory() |
| 82 | inputProtocolFactory, |
| 83 | outputProtocolFactory, |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 84 | loggerFactory.CreateLogger(nameof(TSimpleAsyncServer))) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 85 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | public override async Task ServeAsync(CancellationToken cancellationToken) |
| 89 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 90 | ServerCancellationToken = cancellationToken; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 91 | try |
| 92 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 93 | try |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 94 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 95 | ServerTransport.Listen(); |
| 96 | } |
| 97 | catch (TTransportException ttx) |
| 98 | { |
| 99 | LogError("Error, could not listen on ServerTransport: " + ttx); |
| 100 | return; |
| 101 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 102 | |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 103 | //Fire the preServe server event when server is up but before any client connections |
| 104 | if (ServerEventHandler != null) |
| 105 | await ServerEventHandler.PreServeAsync(cancellationToken); |
| 106 | |
| 107 | while (!(stop || ServerCancellationToken.IsCancellationRequested)) |
| 108 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 109 | try |
| 110 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 111 | using (TTransport client = await ServerTransport.AcceptAsync(cancellationToken)) |
| 112 | { |
| 113 | await ExecuteAsync(client); |
| 114 | } |
| 115 | } |
| 116 | catch (TaskCanceledException) |
| 117 | { |
| 118 | stop = true; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 119 | } |
| 120 | catch (TTransportException ttx) |
| 121 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 122 | if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 123 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 124 | LogError(ttx.ToString()); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 125 | } |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 126 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 129 | |
| 130 | if (stop) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 131 | { |
| 132 | try |
| 133 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 134 | ServerTransport.Close(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 135 | } |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 136 | catch (TTransportException ttx) |
| 137 | { |
| 138 | LogError("TServerTransport failed on close: " + ttx.Message); |
| 139 | } |
| 140 | stop = false; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 141 | } |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 142 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 143 | } |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 144 | finally |
| 145 | { |
| 146 | ServerCancellationToken = default; |
| 147 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 150 | /// <summary> |
| 151 | /// Loops on processing a client forever |
| 152 | /// client will be a TTransport instance |
| 153 | /// </summary> |
| 154 | /// <param name="client"></param> |
| 155 | private async Task ExecuteAsync(TTransport client) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 156 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 157 | var cancellationToken = ServerCancellationToken; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 158 | |
| 159 | var processor = ProcessorFactory.GetAsyncProcessor(client, this); |
| 160 | |
| 161 | TTransport inputTransport = null; |
| 162 | TTransport outputTransport = null; |
| 163 | TProtocol inputProtocol = null; |
| 164 | TProtocol outputProtocol = null; |
| 165 | object connectionContext = null; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 166 | try |
| 167 | { |
| 168 | try |
| 169 | { |
| 170 | inputTransport = InputTransportFactory.GetTransport(client); |
| 171 | outputTransport = OutputTransportFactory.GetTransport(client); |
| 172 | inputProtocol = InputProtocolFactory.GetProtocol(inputTransport); |
| 173 | outputProtocol = OutputProtocolFactory.GetProtocol(outputTransport); |
| 174 | |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 175 | //Recover event handler (if any) and fire createContext server event when a client connects |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 176 | if (ServerEventHandler != null) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 177 | connectionContext = await ServerEventHandler.CreateContextAsync(inputProtocol, outputProtocol, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 178 | |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 179 | //Process client requests until client disconnects |
| 180 | while (!(stop || cancellationToken.IsCancellationRequested)) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 181 | { |
| 182 | if (!await inputTransport.PeekAsync(cancellationToken)) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 183 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 184 | |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 185 | //Fire processContext server event |
| 186 | //N.B. This is the pattern implemented in C++ and the event fires provisionally. |
| 187 | //That is to say it may be many minutes between the event firing and the client request |
| 188 | //actually arriving or the client may hang up without ever makeing a request. |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 189 | if (ServerEventHandler != null) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 190 | await ServerEventHandler.ProcessContextAsync(connectionContext, inputTransport, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 191 | |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 192 | //Process client request (blocks until transport is readable) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 193 | if (!await processor.ProcessAsync(inputProtocol, outputProtocol, cancellationToken)) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 194 | break; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 195 | } |
| 196 | } |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 197 | catch (TTransportException) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 198 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 199 | //Usually a client disconnect, expected |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 200 | } |
| 201 | catch (Exception x) |
| 202 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 203 | //Unexpected |
| 204 | LogError("Error: " + x); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 207 | //Fire deleteContext server event after client disconnects |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 208 | if (ServerEventHandler != null) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 209 | await ServerEventHandler.DeleteContextAsync(connectionContext, inputProtocol, outputProtocol, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 210 | |
| 211 | } |
| 212 | finally |
| 213 | { |
| 214 | //Close transports |
| 215 | inputTransport?.Close(); |
| 216 | outputTransport?.Close(); |
| 217 | |
| 218 | // disposable stuff should be disposed |
| 219 | inputProtocol?.Dispose(); |
| 220 | outputProtocol?.Dispose(); |
| 221 | inputTransport?.Dispose(); |
| 222 | outputTransport?.Dispose(); |
| 223 | } |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 224 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 225 | |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame^] | 226 | public override void Stop() |
| 227 | { |
| 228 | stop = true; |
| 229 | ServerTransport?.Close(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |