| 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 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 18 | using Microsoft.AspNetCore.Builder; |
| 19 | using Microsoft.AspNetCore.Hosting; |
| 20 | using Microsoft.Extensions.Configuration; |
| 21 | using Microsoft.Extensions.DependencyInjection; |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 22 | using Microsoft.Extensions.Hosting; |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 23 | using Microsoft.Extensions.Logging; |
| 24 | using shared; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 25 | using System; |
| 26 | using System.Collections.Generic; |
| 27 | using System.IO; |
| 28 | using System.Linq; |
| 29 | using System.Net.Security; |
| 30 | using System.Security.Cryptography.X509Certificates; |
| 31 | using System.Threading; |
| 32 | using System.Threading.Tasks; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 33 | using Thrift; |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 34 | using Thrift.Processor; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 35 | using Thrift.Protocol; |
| 36 | using Thrift.Server; |
| 37 | using Thrift.Transport; |
| 38 | using Thrift.Transport.Server; |
| 39 | using tutorial; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 40 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 41 | #pragma warning disable IDE0057 // substr |
| 42 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 43 | namespace Server |
| 44 | { |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 45 | public static class LoggingHelper |
| 46 | { |
| 47 | public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder => { |
| 48 | ConfigureLogging(builder); |
| 49 | }); |
| 50 | |
| 51 | public static void ConfigureLogging(ILoggingBuilder logging) |
| 52 | { |
| 53 | logging.SetMinimumLevel(LogLevel.Trace); |
| 54 | logging.AddConsole(); |
| 55 | logging.AddDebug(); |
| 56 | } |
| 57 | |
| 58 | public static ILogger<T> CreateLogger<T>() => LogFactory.CreateLogger<T>(); |
| 59 | } |
| 60 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 61 | public class Program |
| 62 | { |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 63 | private static readonly ILogger Logger = LoggingHelper.CreateLogger<Program>(); |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 64 | private static readonly TConfiguration Configuration = new(); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 65 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 66 | public static async Task Main(string[] args) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 67 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 68 | args ??= []; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 69 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 70 | // -help is rather unusual but we leave it for compatibility |
| 71 | if (args.Any(x => x.Equals("-help") || x.Equals("--help") || x.Equals("-h") || x.Equals("-?"))) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 72 | { |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 73 | DisplayHelp(); |
| 74 | return; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 75 | } |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 76 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 77 | using var source = new CancellationTokenSource(); |
| 78 | await RunAsync(args, source.Token); |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 79 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 80 | Logger.LogInformation("Press any key to stop..."); |
| 81 | Console.ReadLine(); |
| 82 | source.Cancel(); |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 83 | |
| 84 | Logger.LogInformation("Server stopped"); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| Jens Geyer | b11f63c | 2019-03-14 21:12:38 +0100 | [diff] [blame] | 87 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 88 | private static void DisplayHelp() |
| 89 | { |
| 90 | Logger.LogInformation(@" |
| 91 | Usage: |
| Kengo Seki | bee4f2f | 2019-12-29 17:04:50 +0900 | [diff] [blame] | 92 | Server -help |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 93 | will diplay help information |
| 94 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 95 | Server -tr:<transport> -bf:<buffering> -pr:<protocol> [-multiplex] |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 96 | will run server with specified arguments (tcp transport, no buffering, and binary protocol by default) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 97 | |
| 98 | Options: |
| 99 | -tr (transport): |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 100 | tcp - (default) tcp transport (localhost:9090) |
| 101 | tcptls - tcp transport with tls (localhost:9090) |
| 102 | namedpipe - namedpipe transport (pipe "".test"") |
| 103 | http - http transport (localhost:9090) |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 104 | |
| 105 | -bf (buffering): |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 106 | none - (default) no buffering |
| 107 | buffered - buffered transport |
| 108 | framed - framed transport |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 109 | |
| 110 | -pr (protocol): |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 111 | binary - (default) binary protocol |
| 112 | compact - compact protocol |
| 113 | json - json protocol |
| 114 | |
| 115 | -multiplex - adds multiplexed protocol |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 116 | |
| 117 | Sample: |
| Kengo Seki | bee4f2f | 2019-12-29 17:04:50 +0900 | [diff] [blame] | 118 | Server -tr:tcp |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 119 | "); |
| 120 | } |
| 121 | |
| 122 | private static async Task RunAsync(string[] args, CancellationToken cancellationToken) |
| 123 | { |
| 124 | var selectedTransport = GetTransport(args); |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 125 | var selectedBuffering = GetBuffering(args); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 126 | var selectedProtocol = GetProtocol(args); |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 127 | var multiplex = GetMultiplex(args); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 128 | |
| 129 | if (selectedTransport == Transport.Http) |
| 130 | { |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 131 | if (multiplex) |
| Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame] | 132 | throw new Exception("This tutorial sample code does not yet allow multiplex over http (although Thrift itself of course does)"); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 133 | new HttpServerSample().Run(cancellationToken); |
| 134 | } |
| 135 | else |
| 136 | { |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 137 | await RunSelectedConfigurationAsync(selectedTransport, selectedBuffering, selectedProtocol, multiplex, cancellationToken); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 141 | |
| 142 | private static bool GetMultiplex(string[] args) |
| 143 | { |
| 144 | var mplex = args.FirstOrDefault(x => x.StartsWith("-multiplex")); |
| 145 | return !string.IsNullOrEmpty(mplex); |
| 146 | } |
| 147 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 148 | private static Protocol GetProtocol(string[] args) |
| 149 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 150 | var protocol = args.FirstOrDefault(x => x.StartsWith("-pr"))?.Split(':').Skip(1).Take(1).FirstOrDefault(); |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 151 | if (string.IsNullOrEmpty(protocol)) |
| 152 | return Protocol.Binary; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 153 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 154 | protocol = protocol.Substring(0, 1).ToUpperInvariant() + protocol.Substring(1).ToLowerInvariant(); |
| 155 | if (Enum.TryParse(protocol, true, out Protocol selectedProtocol)) |
| 156 | return selectedProtocol; |
| 157 | else |
| 158 | return Protocol.Binary; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 161 | private static Buffering GetBuffering(string[] args) |
| 162 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 163 | var buffering = args.FirstOrDefault(x => x.StartsWith("-bf"))?.Split(':').Skip(1).Take(1).FirstOrDefault(); |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 164 | if (string.IsNullOrEmpty(buffering)) |
| 165 | return Buffering.None; |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 166 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 167 | buffering = buffering.Substring(0, 1).ToUpperInvariant() + buffering.Substring(1).ToLowerInvariant(); |
| 168 | if( Enum.TryParse<Buffering>(buffering, out var selectedBuffering)) |
| 169 | return selectedBuffering; |
| 170 | else |
| 171 | return Buffering.None; |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 174 | private static Transport GetTransport(string[] args) |
| 175 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 176 | var transport = args.FirstOrDefault(x => x.StartsWith("-tr"))?.Split(':').Skip(1).Take(1).FirstOrDefault(); |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 177 | if (string.IsNullOrEmpty(transport)) |
| 178 | return Transport.Tcp; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 179 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 180 | transport = transport.Substring(0, 1).ToUpperInvariant() + transport.Substring(1).ToLowerInvariant(); |
| 181 | if( Enum.TryParse(transport, true, out Transport selectedTransport)) |
| 182 | return selectedTransport; |
| 183 | else |
| 184 | return Transport.Tcp; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 187 | private static async Task RunSelectedConfigurationAsync(Transport transport, Buffering buffering, Protocol protocol, bool multiplex, CancellationToken cancellationToken) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 188 | { |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 189 | TServerTransport serverTransport = transport switch |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 190 | { |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 191 | Transport.Tcp => new TServerSocketTransport(9090, Configuration), |
| Jens Geyer | 51fc54f | 2023-02-14 23:51:16 +0100 | [diff] [blame] | 192 | Transport.NamedPipe => new TNamedPipeServerTransport(".test", Configuration, NamedPipeServerFlags.None, 64), |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 193 | Transport.TcpTls => new TTlsServerSocketTransport(9090, Configuration, GetCertificate(), ClientCertValidator, LocalCertificateSelectionCallback), |
| 194 | _ => throw new ArgumentException("unsupported value $transport", nameof(transport)), |
| 195 | }; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 196 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 197 | TTransportFactory? transportFactory = buffering switch |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 198 | { |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 199 | Buffering.Buffered => new TBufferedTransport.Factory(), |
| 200 | Buffering.Framed => new TFramedTransport.Factory(), |
| 201 | // layered transport(s) are optional |
| 202 | Buffering.None => null, |
| 203 | _ => throw new ArgumentException("unsupported value $buffering", nameof(buffering)), |
| 204 | }; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 205 | |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 206 | TProtocolFactory protocolFactory = protocol switch |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 207 | { |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 208 | Protocol.Binary => new TBinaryProtocol.Factory(), |
| 209 | Protocol.Compact => new TCompactProtocol.Factory(), |
| 210 | Protocol.Json => new TJsonProtocol.Factory(), |
| 211 | _ => throw new ArgumentException("unsupported value $protocol", nameof(protocol)), |
| 212 | }; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 213 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 214 | var handler = new CalculatorAsyncHandler(); |
| 215 | ITAsyncProcessor processor = new Calculator.AsyncProcessor(handler); |
| 216 | |
| 217 | if (multiplex) |
| 218 | { |
| 219 | var multiplexedProcessor = new TMultiplexedProcessor(); |
| 220 | multiplexedProcessor.RegisterProcessor(nameof(Calculator), processor); |
| 221 | |
| 222 | processor = multiplexedProcessor; |
| 223 | } |
| 224 | |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 225 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 226 | try |
| 227 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 228 | if( Logger.IsEnabled(LogLevel.Information)) |
| 229 | Logger.LogInformation( |
| 230 | "TSimpleAsyncServer with \n{transport} transport\n{buffering} buffering\nmultiplex = {multiplex}\n{protocol} protocol", |
| 231 | transport, |
| 232 | buffering, |
| 233 | multiplex ? "yes" : "no", |
| 234 | protocol |
| 235 | ); |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 236 | |
| 237 | var server = new TSimpleAsyncServer( |
| 238 | itProcessorFactory: new TSingletonProcessorFactory(processor), |
| 239 | serverTransport: serverTransport, |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 240 | inputTransportFactory: transportFactory, |
| 241 | outputTransportFactory: transportFactory, |
| 242 | inputProtocolFactory: protocolFactory, |
| 243 | outputProtocolFactory: protocolFactory, |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 244 | logger: LoggingHelper.CreateLogger<TSimpleAsyncServer >()); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 245 | |
| 246 | Logger.LogInformation("Starting the server..."); |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 247 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 248 | await server.ServeAsync(cancellationToken); |
| 249 | } |
| 250 | catch (Exception x) |
| 251 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 252 | if (Logger.IsEnabled(LogLevel.Information)) |
| 253 | Logger.LogInformation("{x}",x); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | private static X509Certificate2 GetCertificate() |
| 258 | { |
| 259 | // due to files location in net core better to take certs from top folder |
| 260 | var certFile = GetCertPath(Directory.GetParent(Directory.GetCurrentDirectory())); |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 261 | //return new X509Certificate2(certFile, "ThriftTest"); |
| 262 | return X509CertificateLoader.LoadPkcs12FromFile(certFile, "ThriftTest"); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 263 | } |
| 264 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 265 | private static string GetCertPath(DirectoryInfo? di, int maxCount = 6) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 266 | { |
| 267 | var topDir = di; |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 268 | var certFile = topDir?.EnumerateFiles("ThriftTest.pfx", SearchOption.AllDirectories).FirstOrDefault(); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 269 | if (certFile == null) |
| 270 | { |
| 271 | if (maxCount == 0) |
| 272 | throw new FileNotFoundException("Cannot find file in directories"); |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 273 | return GetCertPath(di?.Parent, --maxCount); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return certFile.FullName; |
| 277 | } |
| 278 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 279 | private static X509Certificate2 LocalCertificateSelectionCallback(object sender, |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 280 | string targetHost, X509CertificateCollection localCertificates, |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 281 | X509Certificate? remoteCertificate, string[] acceptableIssuers) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 282 | { |
| 283 | return GetCertificate(); |
| 284 | } |
| 285 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 286 | private static bool ClientCertValidator(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 287 | { |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | private enum Transport |
| 292 | { |
| 293 | Tcp, |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 294 | NamedPipe, |
| 295 | Http, |
| 296 | TcpTls, |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | private enum Buffering |
| 300 | { |
| 301 | None, |
| 302 | Buffered, |
| 303 | Framed, |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | private enum Protocol |
| 307 | { |
| 308 | Binary, |
| 309 | Compact, |
| 310 | Json, |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | public class HttpServerSample |
| 314 | { |
| 315 | public void Run(CancellationToken cancellationToken) |
| 316 | { |
| 317 | var config = new ConfigurationBuilder() |
| 318 | .AddEnvironmentVariables(prefix: "ASPNETCORE_") |
| 319 | .Build(); |
| 320 | |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 321 | var host = new HostBuilder(). |
| 322 | ConfigureWebHost(webhostbuilder => { |
| 323 | webhostbuilder.UseConfiguration(config) |
| 324 | .UseUrls("http://localhost:9090") |
| 325 | .UseContentRoot(Directory.GetCurrentDirectory()) |
| 326 | .UseStartup<Startup>() |
| 327 | .UseKestrel() |
| 328 | .ConfigureLogging((ctx, logging) => LoggingHelper.ConfigureLogging(logging)) |
| 329 | ; |
| 330 | }) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 331 | .Build(); |
| 332 | |
| Jens Geyer | b11f63c | 2019-03-14 21:12:38 +0100 | [diff] [blame] | 333 | Logger.LogTrace("test"); |
| 334 | Logger.LogCritical("test"); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 335 | host.RunAsync(cancellationToken).GetAwaiter().GetResult(); |
| 336 | } |
| 337 | |
| 338 | public class Startup |
| 339 | { |
| Jens Geyer | ec43954 | 2019-11-01 19:19:44 +0100 | [diff] [blame] | 340 | public Startup(IWebHostEnvironment env) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 341 | { |
| 342 | var builder = new ConfigurationBuilder() |
| 343 | .SetBasePath(env.ContentRootPath) |
| 344 | .AddEnvironmentVariables(); |
| 345 | |
| 346 | Configuration = builder.Build(); |
| 347 | } |
| 348 | |
| 349 | public IConfigurationRoot Configuration { get; } |
| 350 | |
| 351 | // This method gets called by the runtime. Use this method to add services to the container. |
| 352 | public void ConfigureServices(IServiceCollection services) |
| 353 | { |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 354 | // NOTE: this is not really the recommended way to do it |
| 355 | // because the HTTP server cannot be configured properly to e.g. accept framed or multiplex |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 356 | services.AddTransient<Calculator.IAsync, CalculatorAsyncHandler>(); |
| 357 | services.AddTransient<ITAsyncProcessor, Calculator.AsyncProcessor>(); |
| 358 | services.AddTransient<THttpServerTransport, THttpServerTransport>(); |
| 359 | } |
| 360 | |
| 361 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| Jens Geyer | ec43954 | 2019-11-01 19:19:44 +0100 | [diff] [blame] | 362 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 363 | { |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 364 | _ = env; |
| 365 | _ = loggerFactory; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 366 | app.UseMiddleware<THttpServerTransport>(); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | public class CalculatorAsyncHandler : Calculator.IAsync |
| 372 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 373 | private readonly Dictionary<int, SharedStruct> _log = []; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 374 | |
| 375 | public CalculatorAsyncHandler() |
| 376 | { |
| 377 | } |
| 378 | |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 379 | public async Task<SharedStruct> getStruct(int key, |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 380 | CancellationToken cancellationToken) |
| 381 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 382 | if (Logger.IsEnabled(LogLevel.Information)) |
| 383 | Logger.LogInformation("GetStruct({key})", key); |
| 384 | return _log[key]; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 387 | public async Task ping(CancellationToken cancellationToken) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 388 | { |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 389 | Logger.LogInformation("Ping()"); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 392 | public async Task<int> add(int num1, int num2, CancellationToken cancellationToken) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 393 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 394 | if (Logger.IsEnabled(LogLevel.Information)) |
| 395 | Logger.LogInformation("Add({num1},{num2})", num1, num2); |
| 396 | return num1 + num2; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 399 | public async Task<int> calculate(int logid, Work? w, CancellationToken cancellationToken) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 400 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 401 | if (Logger.IsEnabled(LogLevel.Information)) |
| 402 | Logger.LogInformation("Calculate({logid}, [{w.Op},{w.Num1},{w.Num2}])", logid, w?.Op, w?.Num1, w?.Num2); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 403 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 404 | int val; |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 405 | switch (w?.Op) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 406 | { |
| 407 | case Operation.ADD: |
| 408 | val = w.Num1 + w.Num2; |
| 409 | break; |
| 410 | |
| 411 | case Operation.SUBTRACT: |
| 412 | val = w.Num1 - w.Num2; |
| 413 | break; |
| 414 | |
| 415 | case Operation.MULTIPLY: |
| 416 | val = w.Num1 * w.Num2; |
| 417 | break; |
| 418 | |
| 419 | case Operation.DIVIDE: |
| 420 | if (w.Num2 == 0) |
| 421 | { |
| 422 | var io = new InvalidOperation |
| 423 | { |
| 424 | WhatOp = (int) w.Op, |
| 425 | Why = "Cannot divide by 0" |
| 426 | }; |
| 427 | |
| 428 | throw io; |
| 429 | } |
| 430 | val = w.Num1 / w.Num2; |
| 431 | break; |
| 432 | |
| 433 | default: |
| 434 | { |
| 435 | var io = new InvalidOperation |
| 436 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 437 | WhatOp = ((int?)w?.Op) ?? -1, |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 438 | Why = "Unknown operation" |
| 439 | }; |
| 440 | |
| 441 | throw io; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | var entry = new SharedStruct |
| 446 | { |
| 447 | Key = logid, |
| 448 | Value = val.ToString() |
| 449 | }; |
| 450 | |
| 451 | _log[logid] = entry; |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 452 | return val; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 453 | } |
| 454 | |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 455 | public async Task zip(CancellationToken cancellationToken) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 456 | { |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 457 | Logger.LogInformation("Zip() with delay 100mc"); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 458 | await Task.Delay(100, CancellationToken.None); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | public class SharedServiceAsyncHandler : SharedService.IAsync |
| 463 | { |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 464 | public async Task<SharedStruct> getStruct(int key, CancellationToken cancellationToken) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 465 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 466 | if (Logger.IsEnabled(LogLevel.Information)) |
| 467 | Logger.LogInformation("GetStruct({key})", key); |
| 468 | |
| 469 | return new SharedStruct() |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 470 | { |
| 471 | Key = key, |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 472 | Value = "GetStruct" |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 473 | }; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |