blob: 515a299f8202eb0215c5cb4eaf88cabf08aebf83 [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.Collections.Generic;
Jens Geyeradde44b2019-02-05 01:00:02 +010020using System.Diagnostics;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010021using System.IO;
22using System.Linq;
23using System.Security.Authentication;
24using System.Security.Cryptography.X509Certificates;
25using System.Text;
26using System.Threading;
27using System.Threading.Tasks;
28using Microsoft.Extensions.Logging;
29using Thrift;
30using Thrift.Collections;
31using Thrift.Processor;
32using Thrift.Protocol;
33using Thrift.Server;
34using Thrift.Transport;
35using Thrift.Transport.Server;
36
Jens Geyer828ffa82020-11-21 15:15:32 +010037#pragma warning disable IDE0063 // using can be simplified, we don't
Jens Geyer2b2ea622021-04-09 22:55:11 +020038#pragma warning disable IDE0057 // substr can be simplified, we don't
Jens Geyer98a23252022-01-09 16:50:52 +010039#pragma warning disable CS1998 // await missing
Jens Geyer828ffa82020-11-21 15:15:32 +010040
Jens Geyeraa0c8b32019-01-28 23:27:45 +010041namespace ThriftTest
42{
Jens Geyeradde44b2019-02-05 01:00:02 +010043 internal enum ProtocolChoice
44 {
45 Binary,
46 Compact,
47 Json
48 }
49
Jens Geyeradde44b2019-02-05 01:00:02 +010050 internal enum TransportChoice
51 {
52 Socket,
53 TlsSocket,
54 NamedPipe
55 }
56
Kyle Smith7b94dd42019-03-23 17:26:56 +010057 internal enum BufferChoice
58 {
59 None,
60 Buffered,
61 Framed
62 }
63
Jens Geyer63d114d2021-05-25 23:42:35 +020064 internal enum ServerChoice
65 {
66 Simple,
67 ThreadPool
68 }
69
70
Jens Geyeraa0c8b32019-01-28 23:27:45 +010071 internal class ServerParam
72 {
Kyle Smith7b94dd42019-03-23 17:26:56 +010073 internal BufferChoice buffering = BufferChoice.None;
Jens Geyeradde44b2019-02-05 01:00:02 +010074 internal ProtocolChoice protocol = ProtocolChoice.Binary;
75 internal TransportChoice transport = TransportChoice.Socket;
Jens Geyer63d114d2021-05-25 23:42:35 +020076 internal ServerChoice server = ServerChoice.Simple;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010077 internal int port = 9090;
Jens Geyer98a23252022-01-09 16:50:52 +010078 internal string pipe = string.Empty;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010079
80 internal void Parse(List<string> args)
81 {
82 for (var i = 0; i < args.Count; i++)
83 {
84 if (args[i].StartsWith("--pipe="))
85 {
86 pipe = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +010087 transport = TransportChoice.NamedPipe;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010088 }
89 else if (args[i].StartsWith("--port="))
90 {
91 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyeradde44b2019-02-05 01:00:02 +010092 if(transport != TransportChoice.TlsSocket)
93 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010094 }
95 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
96 {
Kyle Smith7b94dd42019-03-23 17:26:56 +010097 buffering = BufferChoice.Buffered;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010098 }
99 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
100 {
Kyle Smith7b94dd42019-03-23 17:26:56 +0100101 buffering = BufferChoice.Framed;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100102 }
103 else if (args[i] == "--binary" || args[i] == "--protocol=binary")
104 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100105 protocol = ProtocolChoice.Binary;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100106 }
107 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
108 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100109 protocol = ProtocolChoice.Compact;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100110 }
111 else if (args[i] == "--json" || args[i] == "--protocol=json")
112 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100113 protocol = ProtocolChoice.Json;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100114 }
Jens Geyer63d114d2021-05-25 23:42:35 +0200115 else if (args[i] == "--server-type=simple")
116 {
117 server = ServerChoice.Simple;
118 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100119 else if (args[i] == "--threaded" || args[i] == "--server-type=threaded")
120 {
121 throw new NotImplementedException(args[i]);
122 }
123 else if (args[i] == "--threadpool" || args[i] == "--server-type=threadpool")
124 {
Jens Geyer63d114d2021-05-25 23:42:35 +0200125 server = ServerChoice.ThreadPool;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100126 }
127 else if (args[i] == "--prototype" || args[i] == "--processor=prototype")
128 {
129 throw new NotImplementedException(args[i]);
130 }
131 else if (args[i] == "--ssl")
132 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100133 transport = TransportChoice.TlsSocket;
134 }
135 else if (args[i] == "--help")
136 {
137 PrintOptionsHelp();
138 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100139 }
140 else
141 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100142 Console.WriteLine("Invalid argument: {0}", args[i]);
143 PrintOptionsHelp();
144 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100145 }
146 }
147
148 }
Jens Geyeradde44b2019-02-05 01:00:02 +0100149
150 internal static void PrintOptionsHelp()
151 {
152 Console.WriteLine("Server options:");
153 Console.WriteLine(" --pipe=<pipe name>");
154 Console.WriteLine(" --port=<port number>");
155 Console.WriteLine(" --transport=<transport name> one of buffered,framed (defaults to none)");
156 Console.WriteLine(" --protocol=<protocol name> one of compact,json (defaults to binary)");
157 Console.WriteLine(" --server-type=<type> one of threaded,threadpool (defaults to simple)");
158 Console.WriteLine(" --processor=<prototype>");
159 Console.WriteLine(" --ssl");
160 Console.WriteLine();
161 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100162 }
163
164 public class TestServer
165 {
Jens Geyeref0cb012021-04-02 12:18:15 +0200166 #pragma warning disable CA2211
167 public static int _clientID = -1; // use with Interlocked only!
168 #pragma warning restore CA2211
169
Jens Geyer98a23252022-01-09 16:50:52 +0100170 private static readonly TConfiguration Configuration = new();
Jens Geyereacd1d42019-11-20 19:03:14 +0100171
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100172 public delegate void TestLogDelegate(string msg, params object[] values);
173
174 public class MyServerEventHandler : TServerEventHandler
175 {
176 public int callCount = 0;
177
178 public Task PreServeAsync(CancellationToken cancellationToken)
179 {
180 callCount++;
181 return Task.CompletedTask;
182 }
183
Jens Geyer98a23252022-01-09 16:50:52 +0100184 public async Task<object?> CreateContextAsync(TProtocol input, TProtocol output, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100185 {
186 callCount++;
Jens Geyer98a23252022-01-09 16:50:52 +0100187 return null;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100188 }
189
190 public Task DeleteContextAsync(object serverContext, TProtocol input, TProtocol output, CancellationToken cancellationToken)
191 {
192 callCount++;
193 return Task.CompletedTask;
194 }
195
196 public Task ProcessContextAsync(object serverContext, TTransport transport, CancellationToken cancellationToken)
197 {
198 callCount++;
199 return Task.CompletedTask;
200 }
201 }
202
203 public class TestHandlerAsync : ThriftTest.IAsync
204 {
Jens Geyer98a23252022-01-09 16:50:52 +0100205 //public TServer Server { get; set; }
Jens Geyer261cad32019-11-20 19:03:14 +0100206 private readonly int handlerID;
Jens Geyer2b2ea622021-04-09 22:55:11 +0200207 private readonly StringBuilder sb = new();
Jens Geyer261cad32019-11-20 19:03:14 +0100208 private readonly TestLogDelegate logger;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100209
210 public TestHandlerAsync()
211 {
212 handlerID = Interlocked.Increment(ref _clientID);
Jens Geyer261cad32019-11-20 19:03:14 +0100213 logger += TestConsoleLogger;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100214 logger.Invoke("New TestHandler instance created");
215 }
216
Jens Geyer261cad32019-11-20 19:03:14 +0100217 public void TestConsoleLogger(string msg, params object[] values)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100218 {
219 sb.Clear();
220 sb.AppendFormat("handler{0:D3}:", handlerID);
221 sb.AppendFormat(msg, values);
222 sb.AppendLine();
223 Console.Write(sb.ToString());
224 }
225
Jens Geyer2b2ea622021-04-09 22:55:11 +0200226 public Task testVoid(CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100227 {
228 logger.Invoke("testVoid()");
229 return Task.CompletedTask;
230 }
231
Jens Geyer2b2ea622021-04-09 22:55:11 +0200232 public Task<string> testString(string thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100233 {
234 logger.Invoke("testString({0})", thing);
235 return Task.FromResult(thing);
236 }
237
Jens Geyer2b2ea622021-04-09 22:55:11 +0200238 public Task<bool> testBool(bool thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100239 {
240 logger.Invoke("testBool({0})", thing);
241 return Task.FromResult(thing);
242 }
243
Jens Geyer2b2ea622021-04-09 22:55:11 +0200244 public Task<sbyte> testByte(sbyte thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100245 {
246 logger.Invoke("testByte({0})", thing);
247 return Task.FromResult(thing);
248 }
249
Jens Geyer2b2ea622021-04-09 22:55:11 +0200250 public Task<int> testI32(int thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100251 {
252 logger.Invoke("testI32({0})", thing);
253 return Task.FromResult(thing);
254 }
255
Jens Geyer2b2ea622021-04-09 22:55:11 +0200256 public Task<long> testI64(long thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100257 {
258 logger.Invoke("testI64({0})", thing);
259 return Task.FromResult(thing);
260 }
261
Jens Geyer2b2ea622021-04-09 22:55:11 +0200262 public Task<double> testDouble(double thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100263 {
264 logger.Invoke("testDouble({0})", thing);
265 return Task.FromResult(thing);
266 }
267
Jens Geyer2b2ea622021-04-09 22:55:11 +0200268 public Task<byte[]> testBinary(byte[] thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100269 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200270 logger.Invoke("testBinary({0} bytes)", thing.Length);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100271 return Task.FromResult(thing);
272 }
273
Jens Geyer2b2ea622021-04-09 22:55:11 +0200274 public Task<Xtruct> testStruct(Xtruct thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100275 {
Jens Geyerffb97e12019-12-06 23:43:08 +0100276 logger.Invoke("testStruct({{\"{0}\", {1}, {2}, {3}}})", thing.String_thing, thing.Byte_thing, thing.I32_thing, thing.I64_thing);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100277 return Task.FromResult(thing);
278 }
279
Jens Geyer2b2ea622021-04-09 22:55:11 +0200280 public Task<Xtruct2> testNest(Xtruct2 nest, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100281 {
Jens Geyerffb97e12019-12-06 23:43:08 +0100282 var thing = nest.Struct_thing;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100283 logger.Invoke("testNest({{{0}, {{\"{1}\", {2}, {3}, {4}, {5}}}}})",
Jens Geyerffb97e12019-12-06 23:43:08 +0100284 nest.Byte_thing,
285 thing.String_thing,
286 thing.Byte_thing,
287 thing.I32_thing,
288 thing.I64_thing,
289 nest.I32_thing);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100290 return Task.FromResult(nest);
291 }
292
Jens Geyer2b2ea622021-04-09 22:55:11 +0200293 public Task<Dictionary<int, int>> testMap(Dictionary<int, int> thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100294 {
295 sb.Clear();
296 sb.Append("testMap({{");
297 var first = true;
298 foreach (var key in thing.Keys)
299 {
300 if (first)
301 {
302 first = false;
303 }
304 else
305 {
306 sb.Append(", ");
307 }
308 sb.AppendFormat("{0} => {1}", key, thing[key]);
309 }
310 sb.Append("}})");
311 logger.Invoke(sb.ToString());
312 return Task.FromResult(thing);
313 }
314
Jens Geyer2b2ea622021-04-09 22:55:11 +0200315 public Task<Dictionary<string, string>> testStringMap(Dictionary<string, string> thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100316 {
317 sb.Clear();
318 sb.Append("testStringMap({{");
319 var first = true;
320 foreach (var key in thing.Keys)
321 {
322 if (first)
323 {
324 first = false;
325 }
326 else
327 {
328 sb.Append(", ");
329 }
330 sb.AppendFormat("{0} => {1}", key, thing[key]);
331 }
332 sb.Append("}})");
333 logger.Invoke(sb.ToString());
334 return Task.FromResult(thing);
335 }
336
Jens Geyer2b2ea622021-04-09 22:55:11 +0200337 public Task<THashSet<int>> testSet(THashSet<int> thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100338 {
339 sb.Clear();
340 sb.Append("testSet({{");
341 var first = true;
342 foreach (int elem in thing)
343 {
344 if (first)
345 {
346 first = false;
347 }
348 else
349 {
350 sb.Append(", ");
351 }
352 sb.AppendFormat("{0}", elem);
353 }
354 sb.Append("}})");
355 logger.Invoke(sb.ToString());
356 return Task.FromResult(thing);
357 }
358
Jens Geyer2b2ea622021-04-09 22:55:11 +0200359 public Task<List<int>> testList(List<int> thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100360 {
361 sb.Clear();
362 sb.Append("testList({{");
363 var first = true;
364 foreach (var elem in thing)
365 {
366 if (first)
367 {
368 first = false;
369 }
370 else
371 {
372 sb.Append(", ");
373 }
374 sb.AppendFormat("{0}", elem);
375 }
376 sb.Append("}})");
377 logger.Invoke(sb.ToString());
378 return Task.FromResult(thing);
379 }
380
Jens Geyer2b2ea622021-04-09 22:55:11 +0200381 public Task<Numberz> testEnum(Numberz thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100382 {
383 logger.Invoke("testEnum({0})", thing);
384 return Task.FromResult(thing);
385 }
386
Jens Geyer2b2ea622021-04-09 22:55:11 +0200387 public Task<long> testTypedef(long thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100388 {
389 logger.Invoke("testTypedef({0})", thing);
390 return Task.FromResult(thing);
391 }
392
Jens Geyer2b2ea622021-04-09 22:55:11 +0200393 public Task<Dictionary<int, Dictionary<int, int>>> testMapMap(int hello, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100394 {
395 logger.Invoke("testMapMap({0})", hello);
396 var mapmap = new Dictionary<int, Dictionary<int, int>>();
397
398 var pos = new Dictionary<int, int>();
399 var neg = new Dictionary<int, int>();
400 for (var i = 1; i < 5; i++)
401 {
402 pos[i] = i;
403 neg[-i] = -i;
404 }
405
406 mapmap[4] = pos;
407 mapmap[-4] = neg;
408
409 return Task.FromResult(mapmap);
410 }
411
Jens Geyer2b2ea622021-04-09 22:55:11 +0200412 public Task<Dictionary<long, Dictionary<Numberz, Insanity>>> testInsanity(Insanity argument, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100413 {
414 logger.Invoke("testInsanity()");
415
416 /** from ThriftTest.thrift:
417 * So you think you've got this all worked, out eh?
418 *
419 * Creates a the returned map with these values and prints it out:
420 * { 1 => { 2 => argument,
421 * 3 => argument,
422 * },
423 * 2 => { 6 => <empty Insanity struct>, },
424 * }
425 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
426 */
427
428 var first_map = new Dictionary<Numberz, Insanity>();
429 var second_map = new Dictionary<Numberz, Insanity>(); ;
430
431 first_map[Numberz.TWO] = argument;
432 first_map[Numberz.THREE] = argument;
433
434 second_map[Numberz.SIX] = new Insanity();
435
436 var insane = new Dictionary<long, Dictionary<Numberz, Insanity>>
437 {
438 [1] = first_map,
439 [2] = second_map
440 };
441
442 return Task.FromResult(insane);
443 }
444
Jens Geyer2b2ea622021-04-09 22:55:11 +0200445 public Task<Xtruct> testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5,
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100446 CancellationToken cancellationToken)
447 {
448 logger.Invoke("testMulti()");
449
450 var hello = new Xtruct(); ;
Jens Geyerffb97e12019-12-06 23:43:08 +0100451 hello.String_thing = "Hello2";
452 hello.Byte_thing = arg0;
453 hello.I32_thing = arg1;
454 hello.I64_thing = arg2;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100455 return Task.FromResult(hello);
456 }
457
Jens Geyer2b2ea622021-04-09 22:55:11 +0200458 public Task testException(string arg, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100459 {
460 logger.Invoke("testException({0})", arg);
461 if (arg == "Xception")
462 {
463 var x = new Xception
464 {
465 ErrorCode = 1001,
466 Message = arg
467 };
468 throw x;
469 }
470 if (arg == "TException")
471 {
472 throw new TException();
473 }
474 return Task.CompletedTask;
475 }
476
Jens Geyer2b2ea622021-04-09 22:55:11 +0200477 public Task<Xtruct> testMultiException(string arg0, string arg1, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100478 {
479 logger.Invoke("testMultiException({0}, {1})", arg0, arg1);
480 if (arg0 == "Xception")
481 {
482 var x = new Xception
483 {
484 ErrorCode = 1001,
485 Message = "This is an Xception"
486 };
487 throw x;
488 }
489
490 if (arg0 == "Xception2")
491 {
492 var x = new Xception2
493 {
494 ErrorCode = 2002,
Jens Geyerffb97e12019-12-06 23:43:08 +0100495 Struct_thing = new Xtruct { String_thing = "This is an Xception2" }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100496 };
497 throw x;
498 }
499
Jens Geyerffb97e12019-12-06 23:43:08 +0100500 var result = new Xtruct { String_thing = arg1 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100501 return Task.FromResult(result);
502 }
503
Jens Geyer71569402021-11-13 23:51:16 +0100504 public async Task testOneway(int secondsToSleep, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100505 {
506 logger.Invoke("testOneway({0}), sleeping...", secondsToSleep);
Jens Geyer71569402021-11-13 23:51:16 +0100507 await Task.Delay(secondsToSleep * 1000, cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100508 logger.Invoke("testOneway finished");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100509 }
510 }
511
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100512
513 private static X509Certificate2 GetServerCert()
514 {
515 var serverCertName = "server.p12";
516 var possiblePaths = new List<string>
517 {
518 "../../../keys/",
519 "../../keys/",
520 "../keys/",
521 "keys/",
522 };
523
Jens Geyer98a23252022-01-09 16:50:52 +0100524 var existingPath = string.Empty;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100525 foreach (var possiblePath in possiblePaths)
526 {
527 var path = Path.GetFullPath(possiblePath + serverCertName);
528 if (File.Exists(path))
529 {
530 existingPath = path;
531 break;
532 }
533 }
534
535 if (string.IsNullOrEmpty(existingPath))
536 {
537 throw new FileNotFoundException($"Cannot find file: {serverCertName}");
538 }
539
540 var cert = new X509Certificate2(existingPath, "thrift");
541
542 return cert;
543 }
544
Jens Geyer71569402021-11-13 23:51:16 +0100545 public static async Task<int> Execute(List<string> args)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100546 {
Jens Geyer261cad32019-11-20 19:03:14 +0100547 using (var loggerFactory = new LoggerFactory()) //.AddConsole().AddDebug();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100548 {
Jens Geyer261cad32019-11-20 19:03:14 +0100549 var logger = loggerFactory.CreateLogger("Test");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100550
551 try
552 {
Jens Geyer261cad32019-11-20 19:03:14 +0100553 var param = new ServerParam();
554
555 try
556 {
557 param.Parse(args);
558 }
559 catch (Exception ex)
560 {
561 Console.WriteLine("*** FAILED ***");
562 Console.WriteLine("Error while parsing arguments");
563 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
564 return 1;
565 }
566
567
568 // Endpoint transport (mandatory)
569 TServerTransport trans;
570 switch (param.transport)
571 {
572 case TransportChoice.NamedPipe:
573 Debug.Assert(param.pipe != null);
Jens Geyeref0cb012021-04-02 12:18:15 +0200574 trans = new TNamedPipeServerTransport(param.pipe, Configuration, NamedPipeClientFlags.OnlyLocalClients);
Jens Geyer261cad32019-11-20 19:03:14 +0100575 break;
576
577
578 case TransportChoice.TlsSocket:
579 var cert = GetServerCert();
580 if (cert == null || !cert.HasPrivateKey)
581 {
582 cert?.Dispose();
583 throw new InvalidOperationException("Certificate doesn't contain private key");
584 }
585
Jens Geyereacd1d42019-11-20 19:03:14 +0100586 trans = new TTlsServerSocketTransport(param.port, Configuration,
587 cert,
Jens Geyer261cad32019-11-20 19:03:14 +0100588 (sender, certificate, chain, errors) => true,
589 null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
590 break;
591
592 case TransportChoice.Socket:
593 default:
Jens Geyereacd1d42019-11-20 19:03:14 +0100594 trans = new TServerSocketTransport(param.port, Configuration);
Jens Geyer261cad32019-11-20 19:03:14 +0100595 break;
596 }
597
598 // Layered transport (mandatory)
Jens Geyer98a23252022-01-09 16:50:52 +0100599 TTransportFactory? transFactory;
Jens Geyer261cad32019-11-20 19:03:14 +0100600 switch (param.buffering)
601 {
602 case BufferChoice.Framed:
603 transFactory = new TFramedTransport.Factory();
604 break;
605 case BufferChoice.Buffered:
606 transFactory = new TBufferedTransport.Factory();
607 break;
608 default:
609 Debug.Assert(param.buffering == BufferChoice.None, "unhandled case");
610 transFactory = null; // no layered transprt
611 break;
612 }
613
Jens Geyer828ffa82020-11-21 15:15:32 +0100614 TProtocolFactory proto = param.protocol switch
Jens Geyer261cad32019-11-20 19:03:14 +0100615 {
Jens Geyer828ffa82020-11-21 15:15:32 +0100616 ProtocolChoice.Compact => new TCompactProtocol.Factory(),
617 ProtocolChoice.Json => new TJsonProtocol.Factory(),
618 ProtocolChoice.Binary => new TBinaryProtocol.Factory(),
619 _ => new TBinaryProtocol.Factory(),
620 };
Jens Geyer261cad32019-11-20 19:03:14 +0100621
622 // Processor
623 var testHandler = new TestHandlerAsync();
624 var testProcessor = new ThriftTest.AsyncProcessor(testHandler);
625 var processorFactory = new TSingletonProcessorFactory(testProcessor);
626
Jens Geyer63d114d2021-05-25 23:42:35 +0200627 var poolconfig = new TThreadPoolAsyncServer.Configuration(); // use platform defaults
628 TServer serverEngine = param.server switch
629 {
630 ServerChoice.Simple => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger),
631 ServerChoice.ThreadPool => new TThreadPoolAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, poolconfig, logger),
632 _ => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger)
633 };
Jens Geyer261cad32019-11-20 19:03:14 +0100634
635 //Server event handler
636 var serverEvents = new MyServerEventHandler();
637 serverEngine.SetEventHandler(serverEvents);
638
639 // Run it
Jens Geyer63d114d2021-05-25 23:42:35 +0200640 var where = (!string.IsNullOrEmpty(param.pipe)) ? "pipe " + param.pipe : "port " + param.port;
641 Console.WriteLine("Running "+ serverEngine.GetType().Name +
642 " at "+ where +
643 " using "+ processorFactory.GetType().Name + " processor prototype factory " +
Jens Geyer261cad32019-11-20 19:03:14 +0100644 (param.buffering == BufferChoice.Buffered ? " with buffered transport" : "") +
645 (param.buffering == BufferChoice.Framed ? " with framed transport" : "") +
646 (param.transport == TransportChoice.TlsSocket ? " with encryption" : "") +
647 (param.protocol == ProtocolChoice.Compact ? " with compact protocol" : "") +
648 (param.protocol == ProtocolChoice.Json ? " with json protocol" : "") +
649 "...");
Jens Geyer71569402021-11-13 23:51:16 +0100650 await serverEngine.ServeAsync(CancellationToken.None);
Jens Geyer261cad32019-11-20 19:03:14 +0100651 Console.ReadLine();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100652 }
Jens Geyer261cad32019-11-20 19:03:14 +0100653 catch (Exception x)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100654 {
Jens Geyer261cad32019-11-20 19:03:14 +0100655 Console.Error.Write(x);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100656 return 1;
657 }
658
Jens Geyer261cad32019-11-20 19:03:14 +0100659 Console.WriteLine("done.");
660 return 0;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100661 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100662 }
663 }
664
665}