| 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.Extensions.Logging; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 19 | using System; |
| Mario Emmenlauer | 47e4923 | 2020-04-07 18:43:46 +0200 | [diff] [blame] | 20 | using System.Diagnostics; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 21 | using System.IO; |
| 22 | using System.Linq; |
| 23 | using System.Net; |
| 24 | using System.Net.Security; |
| 25 | using System.Security.Cryptography.X509Certificates; |
| 26 | using System.Threading; |
| 27 | using System.Threading.Tasks; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 28 | using Thrift; |
| 29 | using Thrift.Protocol; |
| 30 | using Thrift.Transport; |
| 31 | using Thrift.Transport.Client; |
| 32 | using tutorial; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 33 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 34 | #pragma warning disable IDE0057 // substr |
| 35 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 36 | namespace Client |
| 37 | { |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 38 | public static class LoggingHelper |
| 39 | { |
| 40 | public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder => { |
| 41 | ConfigureLogging(builder); |
| 42 | }); |
| 43 | |
| 44 | public static void ConfigureLogging(ILoggingBuilder logging) |
| 45 | { |
| 46 | logging.SetMinimumLevel(LogLevel.Trace); |
| 47 | logging.AddConsole(); |
| 48 | logging.AddDebug(); |
| 49 | } |
| 50 | |
| 51 | public static ILogger<T> CreateLogger<T>() => LogFactory.CreateLogger<T>(); |
| 52 | } |
| 53 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 54 | public class Program |
| 55 | { |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 56 | private static readonly ILogger Logger = LoggingHelper.CreateLogger<Program>(); |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 57 | private static readonly TConfiguration Configuration = new(); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 58 | |
| 59 | private static void DisplayHelp() |
| 60 | { |
| 61 | Logger.LogInformation(@" |
| 62 | Usage: |
| Kengo Seki | bee4f2f | 2019-12-29 17:04:50 +0900 | [diff] [blame] | 63 | Client -help |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 64 | will diplay help information |
| 65 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 66 | Client -tr:<transport> -bf:<buffering> -pr:<protocol> [-mc:<numClients>] [-multiplex] |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 67 | will run client with specified arguments (tcp transport and binary protocol by default) and with 1 client |
| 68 | |
| 69 | Options: |
| 70 | -tr (transport): |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 71 | tcp - (default) tcp transport (localhost:9090) |
| 72 | tcptls - tcp tls transport (localhost:9090) |
| 73 | namedpipe - namedpipe transport (pipe "".test"") |
| 74 | http - http transport (http://localhost:9090) |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 75 | |
| 76 | -bf (buffering): |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 77 | none - (default) no buffering |
| 78 | buffered - buffered transport |
| 79 | framed - framed transport |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 80 | |
| 81 | -pr (protocol): |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 82 | binary - (default) binary protocol |
| 83 | compact - compact protocol |
| 84 | json - json protocol |
| 85 | |
| 86 | -multiplex - adds multiplexed protocol |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 87 | |
| 88 | -mc (multiple clients): |
| 89 | <numClients> - number of multiple clients to connect to server (max 100, default 1) |
| 90 | |
| 91 | Sample: |
| Kengo Seki | bee4f2f | 2019-12-29 17:04:50 +0900 | [diff] [blame] | 92 | Client -tr:tcp -pr:binary |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 93 | "); |
| 94 | } |
| 95 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 96 | public static async Task Main(string[] args) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 97 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 98 | args ??= []; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 99 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 100 | // -help is rather unusual but we leave it for compatibility |
| 101 | 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] | 102 | { |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 103 | DisplayHelp(); |
| 104 | return; |
| 105 | } |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 106 | |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 107 | Logger.LogInformation("Starting client..."); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 108 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 109 | using var source = new CancellationTokenSource(); |
| 110 | await RunAsync(args, source.Token); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| Jens Geyer | 4c7b9fd | 2021-12-04 22:48:37 +0100 | [diff] [blame] | 113 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 114 | private static async Task RunAsync(string[] args, CancellationToken cancellationToken) |
| 115 | { |
| 116 | var numClients = GetNumberOfClients(args); |
| 117 | |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 118 | if (Logger.IsEnabled(LogLevel.Information)) |
| 119 | Logger.LogInformation("Selected # of clients: {numClients}", numClients); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 120 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 121 | var transport = GetTransport(args); |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 122 | if (Logger.IsEnabled(LogLevel.Information)) |
| 123 | Logger.LogInformation("Selected client transport: {transport}", transport); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 124 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 125 | var protocol = MakeProtocol( args, MakeTransport(args)); |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 126 | if (Logger.IsEnabled(LogLevel.Information)) |
| 127 | Logger.LogInformation("Selected client protocol: {GetProtocol(args)}", GetProtocol(args)); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 128 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 129 | var mplex = GetMultiplex(args); |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 130 | if (Logger.IsEnabled(LogLevel.Information)) |
| 131 | Logger.LogInformation("Multiplex {mplex}", mplex); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 132 | |
| 133 | var tasks = new Task[numClients]; |
| 134 | for (int i = 0; i < numClients; i++) |
| 135 | { |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 136 | var task = RunClientAsync(protocol, mplex, cancellationToken); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 137 | tasks[i] = task; |
| 138 | } |
| 139 | |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 140 | Task.WaitAll(tasks, cancellationToken); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 143 | private static bool GetMultiplex(string[] args) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 144 | { |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 145 | var mplex = args.FirstOrDefault(x => x.StartsWith("-multiplex")); |
| 146 | return !string.IsNullOrEmpty(mplex); |
| 147 | } |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 148 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 149 | private static Protocol GetProtocol(string[] args) |
| 150 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 151 | 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] | 152 | if (string.IsNullOrEmpty(protocol)) |
| 153 | return Protocol.Binary; |
| 154 | |
| 155 | protocol = protocol.Substring(0, 1).ToUpperInvariant() + protocol.Substring(1).ToLowerInvariant(); |
| 156 | if (Enum.TryParse(protocol, true, out Protocol selectedProtocol)) |
| 157 | return selectedProtocol; |
| 158 | else |
| 159 | return Protocol.Binary; |
| 160 | } |
| 161 | |
| 162 | private static Buffering GetBuffering(string[] args) |
| 163 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 164 | 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] | 165 | if (string.IsNullOrEmpty(buffering)) |
| 166 | return Buffering.None; |
| 167 | |
| 168 | buffering = buffering.Substring(0, 1).ToUpperInvariant() + buffering.Substring(1).ToLowerInvariant(); |
| 169 | if (Enum.TryParse<Buffering>(buffering, out var selectedBuffering)) |
| 170 | return selectedBuffering; |
| 171 | else |
| 172 | return Buffering.None; |
| 173 | } |
| 174 | |
| 175 | private static Transport GetTransport(string[] args) |
| 176 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 177 | 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] | 178 | if (string.IsNullOrEmpty(transport)) |
| 179 | return Transport.Tcp; |
| 180 | |
| 181 | transport = transport.Substring(0, 1).ToUpperInvariant() + transport.Substring(1).ToLowerInvariant(); |
| 182 | if (Enum.TryParse(transport, true, out Transport selectedTransport)) |
| 183 | return selectedTransport; |
| 184 | else |
| 185 | return Transport.Tcp; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | private static TTransport MakeTransport(string[] args) |
| 190 | { |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 191 | // construct endpoint transport |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 192 | TTransport? transport = null; |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 193 | Transport selectedTransport = GetTransport(args); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 194 | { |
| 195 | switch (selectedTransport) |
| 196 | { |
| 197 | case Transport.Tcp: |
| Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 198 | transport = new TSocketTransport(IPAddress.Loopback, 9090, Configuration); |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 199 | break; |
| 200 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 201 | case Transport.NamedPipe: |
| Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 202 | transport = new TNamedPipeTransport(".test", Configuration); |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 203 | break; |
| 204 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 205 | case Transport.Http: |
| Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 206 | transport = new THttpTransport(new Uri("http://localhost:9090"), Configuration); |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 207 | break; |
| 208 | |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 209 | case Transport.TcpTls: |
| Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 210 | transport = new TTlsSocketTransport(IPAddress.Loopback, 9090, Configuration, |
| 211 | GetCertificate(), CertValidator, LocalCertificateSelectionCallback); |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 212 | break; |
| 213 | |
| 214 | default: |
| 215 | Debug.Assert(false, "unhandled case"); |
| 216 | break; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 220 | // optionally add layered transport(s) |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 221 | Buffering selectedBuffering = GetBuffering(args); |
| 222 | switch (selectedBuffering) |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 223 | { |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 224 | case Buffering.Buffered: |
| 225 | transport = new TBufferedTransport(transport); |
| 226 | break; |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 227 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 228 | case Buffering.Framed: |
| 229 | transport = new TFramedTransport(transport); |
| 230 | break; |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 231 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 232 | default: // layered transport(s) are optional |
| 233 | Debug.Assert(selectedBuffering == Buffering.None, "unhandled case"); |
| 234 | break; |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return transport; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | private static int GetNumberOfClients(string[] args) |
| 241 | { |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 242 | var numClients = args.FirstOrDefault(x => x.StartsWith("-mc"))?.Split(':').Skip(1).Take(1).FirstOrDefault(); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 243 | |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 244 | if (Logger.IsEnabled(LogLevel.Information)) |
| 245 | Logger.LogInformation("Selected # of clients: {numClients}", numClients); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 246 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 247 | if (int.TryParse(numClients, out int c) && (0 < c) && (c <= 100)) |
| 248 | return c; |
| 249 | else |
| 250 | return 1; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private static X509Certificate2 GetCertificate() |
| 254 | { |
| 255 | // due to files location in net core better to take certs from top folder |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 256 | var dir = Directory.GetParent(Directory.GetCurrentDirectory()); |
| 257 | if (dir != null) |
| 258 | { |
| 259 | var certFile = GetCertPath(dir); |
| 260 | //return new X509Certificate2(certFile, "ThriftTest"); |
| 261 | return X509CertificateLoader.LoadPkcs12FromFile(certFile, "ThriftTest"); |
| 262 | } |
| 263 | else |
| 264 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 265 | if (Logger.IsEnabled(LogLevel.Error)) |
| 266 | Logger.LogError("Root path of {path} not found", Directory.GetCurrentDirectory()); |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 267 | throw new Exception($"Root path of {Directory.GetCurrentDirectory()} not found"); |
| 268 | } |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 271 | private static string GetCertPath(DirectoryInfo? di, int maxCount = 6) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 272 | { |
| 273 | var topDir = di; |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 274 | var certFile = topDir?.EnumerateFiles("ThriftTest.pfx", SearchOption.AllDirectories).FirstOrDefault(); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 275 | if (certFile == null) |
| 276 | { |
| 277 | if (maxCount == 0) |
| 278 | throw new FileNotFoundException("Cannot find file in directories"); |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 279 | return GetCertPath(di?.Parent, --maxCount); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | return certFile.FullName; |
| 283 | } |
| 284 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 285 | private static X509Certificate2 LocalCertificateSelectionCallback(object sender, |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 286 | string targetHost, X509CertificateCollection localCertificates, |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 287 | X509Certificate? remoteCertificate, string[] acceptableIssuers) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 288 | { |
| 289 | return GetCertificate(); |
| 290 | } |
| 291 | |
| Jens Geyer | e26b4a8 | 2024-11-12 23:53:04 +0100 | [diff] [blame] | 292 | private static bool CertValidator(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 293 | { |
| 294 | return true; |
| 295 | } |
| 296 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 297 | private static TProtocol MakeProtocol(string[] args, TTransport transport) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 298 | { |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 299 | Protocol selectedProtocol = GetProtocol(args); |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 300 | return selectedProtocol switch |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 301 | { |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 302 | Protocol.Binary => new TBinaryProtocol(transport), |
| 303 | Protocol.Compact => new TCompactProtocol(transport), |
| 304 | Protocol.Json => new TJsonProtocol(transport), |
| 305 | _ => throw new Exception("unhandled protocol"), |
| 306 | }; |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 309 | private static async Task RunClientAsync(TProtocol protocol, bool multiplex, CancellationToken cancellationToken) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 310 | { |
| 311 | try |
| 312 | { |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 313 | try |
| 314 | { |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 315 | if( multiplex) |
| 316 | protocol = new TMultiplexedProtocol(protocol, nameof(Calculator)); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 317 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 318 | var client = new Calculator.Client(protocol); |
| 319 | await ExecuteCalculatorClientOperations(client, cancellationToken); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 320 | } |
| 321 | catch (Exception ex) |
| 322 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 323 | if (Logger.IsEnabled(LogLevel.Error)) |
| 324 | Logger.LogError("{ex}",ex); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 325 | } |
| 326 | finally |
| 327 | { |
| 328 | protocol.Transport.Close(); |
| 329 | } |
| 330 | } |
| 331 | catch (TApplicationException x) |
| 332 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 333 | if (Logger.IsEnabled(LogLevel.Error)) |
| 334 | Logger.LogError("{x}",x); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| Jens Geyer | 0d12832 | 2021-02-25 09:42:52 +0100 | [diff] [blame] | 338 | private static async Task ExecuteCalculatorClientOperations( Calculator.Client client, CancellationToken cancellationToken) |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 339 | { |
| 340 | await client.OpenTransportAsync(cancellationToken); |
| 341 | |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 342 | if (Logger.IsEnabled(LogLevel.Information)) |
| 343 | Logger.LogInformation("{client.ClientId} Ping()", client.ClientId); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 344 | |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 345 | await client.ping(cancellationToken); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 346 | |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 347 | if (Logger.IsEnabled(LogLevel.Information)) |
| 348 | Logger.LogInformation("{client.ClientId} Add(1,1)", client.ClientId); |
| 349 | |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 350 | var sum = await client.add(1, 1, cancellationToken); |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 351 | |
| 352 | if (Logger.IsEnabled(LogLevel.Information)) |
| 353 | Logger.LogInformation("{client.ClientId} Add(1,1)={sum}", client.ClientId, sum); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 354 | |
| 355 | var work = new Work |
| 356 | { |
| 357 | Op = Operation.DIVIDE, |
| 358 | Num1 = 1, |
| 359 | Num2 = 0 |
| 360 | }; |
| 361 | |
| 362 | try |
| 363 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 364 | if (Logger.IsEnabled(LogLevel.Information)) |
| 365 | Logger.LogInformation("{client.ClientId} Calculate(1)", client.ClientId); |
| 366 | |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 367 | await client.calculate(1, work, cancellationToken); |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 368 | |
| 369 | if (Logger.IsEnabled(LogLevel.Information)) |
| 370 | Logger.LogInformation("{client.ClientId} Whoa we can divide by 0", client.ClientId); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 371 | } |
| 372 | catch (InvalidOperation io) |
| 373 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 374 | if (Logger.IsEnabled(LogLevel.Information)) |
| 375 | Logger.LogInformation("{client.ClientId} Invalid operation: {io}", client.ClientId, io); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | work.Op = Operation.SUBTRACT; |
| 379 | work.Num1 = 15; |
| 380 | work.Num2 = 10; |
| 381 | |
| 382 | try |
| 383 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 384 | if (Logger.IsEnabled(LogLevel.Information)) |
| 385 | Logger.LogInformation("{client.ClientId} Calculate(1)", client.ClientId); |
| 386 | |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 387 | var diff = await client.calculate(1, work, cancellationToken); |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 388 | |
| 389 | if (Logger.IsEnabled(LogLevel.Information)) |
| 390 | Logger.LogInformation("{client.ClientId} 15-10={diff}", client.ClientId, diff); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 391 | } |
| 392 | catch (InvalidOperation io) |
| 393 | { |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 394 | if (Logger.IsEnabled(LogLevel.Information)) |
| 395 | Logger.LogInformation("{client.ClientId} Invalid operation: {io}", client.ClientId, io); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| Jens Geyer | 2f214c2 | 2025-11-13 23:24:45 +0100 | [diff] [blame] | 398 | if (Logger.IsEnabled(LogLevel.Information)) |
| 399 | Logger.LogInformation("{client.ClientId} GetStruct(1)", client.ClientId); |
| 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 | var log = await client.getStruct(1, cancellationToken); |
| 402 | |
| 403 | if (Logger.IsEnabled(LogLevel.Information)) |
| 404 | Logger.LogInformation("{client.ClientId} Check log: {log.Value}", client.ClientId, log.Value); |
| 405 | |
| 406 | if (Logger.IsEnabled(LogLevel.Information)) |
| 407 | Logger.LogInformation("{client.ClientId} Zip() with delay 100mc on server side", client.ClientId); |
| 408 | |
| Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 409 | await client.zip(cancellationToken); |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 410 | } |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 411 | |
| 412 | |
| 413 | private enum Transport |
| 414 | { |
| 415 | Tcp, |
| 416 | NamedPipe, |
| 417 | Http, |
| 418 | TcpBuffered, |
| 419 | Framed, |
| 420 | TcpTls |
| 421 | } |
| 422 | |
| 423 | private enum Protocol |
| 424 | { |
| 425 | Binary, |
| 426 | Compact, |
| 427 | Json, |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 428 | } |
| Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 429 | |
| 430 | private enum Buffering |
| 431 | { |
| 432 | None, |
| 433 | Buffered, |
| 434 | Framed |
| 435 | } |
| Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 436 | } |
| 437 | } |