blob: 895c3ffd5b364a5f81f418f63da07f29ffbc12ce [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 Geyer828ffa82020-11-21 15:15:32 +010039
Jens Geyeraa0c8b32019-01-28 23:27:45 +010040namespace ThriftTest
41{
Jens Geyeradde44b2019-02-05 01:00:02 +010042 internal enum ProtocolChoice
43 {
44 Binary,
45 Compact,
46 Json
47 }
48
Jens Geyeradde44b2019-02-05 01:00:02 +010049 internal enum TransportChoice
50 {
51 Socket,
52 TlsSocket,
53 NamedPipe
54 }
55
Kyle Smith7b94dd42019-03-23 17:26:56 +010056 internal enum BufferChoice
57 {
58 None,
59 Buffered,
60 Framed
61 }
62
Jens Geyeraa0c8b32019-01-28 23:27:45 +010063 internal class ServerParam
64 {
Kyle Smith7b94dd42019-03-23 17:26:56 +010065 internal BufferChoice buffering = BufferChoice.None;
Jens Geyeradde44b2019-02-05 01:00:02 +010066 internal ProtocolChoice protocol = ProtocolChoice.Binary;
67 internal TransportChoice transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010068 internal int port = 9090;
69 internal string pipe = null;
70
71 internal void Parse(List<string> args)
72 {
73 for (var i = 0; i < args.Count; i++)
74 {
75 if (args[i].StartsWith("--pipe="))
76 {
77 pipe = args[i].Substring(args[i].IndexOf("=") + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +010078 transport = TransportChoice.NamedPipe;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010079 }
80 else if (args[i].StartsWith("--port="))
81 {
82 port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1));
Jens Geyeradde44b2019-02-05 01:00:02 +010083 if(transport != TransportChoice.TlsSocket)
84 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010085 }
86 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
87 {
Kyle Smith7b94dd42019-03-23 17:26:56 +010088 buffering = BufferChoice.Buffered;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010089 }
90 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
91 {
Kyle Smith7b94dd42019-03-23 17:26:56 +010092 buffering = BufferChoice.Framed;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010093 }
94 else if (args[i] == "--binary" || args[i] == "--protocol=binary")
95 {
Jens Geyeradde44b2019-02-05 01:00:02 +010096 protocol = ProtocolChoice.Binary;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010097 }
98 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
99 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100100 protocol = ProtocolChoice.Compact;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100101 }
102 else if (args[i] == "--json" || args[i] == "--protocol=json")
103 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100104 protocol = ProtocolChoice.Json;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100105 }
106 else if (args[i] == "--threaded" || args[i] == "--server-type=threaded")
107 {
108 throw new NotImplementedException(args[i]);
109 }
110 else if (args[i] == "--threadpool" || args[i] == "--server-type=threadpool")
111 {
112 throw new NotImplementedException(args[i]);
113 }
114 else if (args[i] == "--prototype" || args[i] == "--processor=prototype")
115 {
116 throw new NotImplementedException(args[i]);
117 }
118 else if (args[i] == "--ssl")
119 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100120 transport = TransportChoice.TlsSocket;
121 }
122 else if (args[i] == "--help")
123 {
124 PrintOptionsHelp();
125 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100126 }
127 else
128 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100129 Console.WriteLine("Invalid argument: {0}", args[i]);
130 PrintOptionsHelp();
131 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100132 }
133 }
134
135 }
Jens Geyeradde44b2019-02-05 01:00:02 +0100136
137 internal static void PrintOptionsHelp()
138 {
139 Console.WriteLine("Server options:");
140 Console.WriteLine(" --pipe=<pipe name>");
141 Console.WriteLine(" --port=<port number>");
142 Console.WriteLine(" --transport=<transport name> one of buffered,framed (defaults to none)");
143 Console.WriteLine(" --protocol=<protocol name> one of compact,json (defaults to binary)");
144 Console.WriteLine(" --server-type=<type> one of threaded,threadpool (defaults to simple)");
145 Console.WriteLine(" --processor=<prototype>");
146 Console.WriteLine(" --ssl");
147 Console.WriteLine();
148 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100149 }
150
151 public class TestServer
152 {
Jens Geyeref0cb012021-04-02 12:18:15 +0200153 #pragma warning disable CA2211
154 public static int _clientID = -1; // use with Interlocked only!
155 #pragma warning restore CA2211
156
Jens Geyereacd1d42019-11-20 19:03:14 +0100157 private static readonly TConfiguration Configuration = null; // or new TConfiguration() if needed
158
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100159 public delegate void TestLogDelegate(string msg, params object[] values);
160
161 public class MyServerEventHandler : TServerEventHandler
162 {
163 public int callCount = 0;
164
165 public Task PreServeAsync(CancellationToken cancellationToken)
166 {
167 callCount++;
168 return Task.CompletedTask;
169 }
170
171 public Task<object> CreateContextAsync(TProtocol input, TProtocol output, CancellationToken cancellationToken)
172 {
173 callCount++;
174 return Task.FromResult<object>(null);
175 }
176
177 public Task DeleteContextAsync(object serverContext, TProtocol input, TProtocol output, CancellationToken cancellationToken)
178 {
179 callCount++;
180 return Task.CompletedTask;
181 }
182
183 public Task ProcessContextAsync(object serverContext, TTransport transport, CancellationToken cancellationToken)
184 {
185 callCount++;
186 return Task.CompletedTask;
187 }
188 }
189
190 public class TestHandlerAsync : ThriftTest.IAsync
191 {
Jens Geyer261cad32019-11-20 19:03:14 +0100192 public TServer Server { get; set; }
193 private readonly int handlerID;
Jens Geyer2b2ea622021-04-09 22:55:11 +0200194 private readonly StringBuilder sb = new();
Jens Geyer261cad32019-11-20 19:03:14 +0100195 private readonly TestLogDelegate logger;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100196
197 public TestHandlerAsync()
198 {
199 handlerID = Interlocked.Increment(ref _clientID);
Jens Geyer261cad32019-11-20 19:03:14 +0100200 logger += TestConsoleLogger;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100201 logger.Invoke("New TestHandler instance created");
202 }
203
Jens Geyer261cad32019-11-20 19:03:14 +0100204 public void TestConsoleLogger(string msg, params object[] values)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100205 {
206 sb.Clear();
207 sb.AppendFormat("handler{0:D3}:", handlerID);
208 sb.AppendFormat(msg, values);
209 sb.AppendLine();
210 Console.Write(sb.ToString());
211 }
212
Jens Geyer2b2ea622021-04-09 22:55:11 +0200213 public Task testVoid(CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100214 {
215 logger.Invoke("testVoid()");
216 return Task.CompletedTask;
217 }
218
Jens Geyer2b2ea622021-04-09 22:55:11 +0200219 public Task<string> testString(string thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100220 {
221 logger.Invoke("testString({0})", thing);
222 return Task.FromResult(thing);
223 }
224
Jens Geyer2b2ea622021-04-09 22:55:11 +0200225 public Task<bool> testBool(bool thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100226 {
227 logger.Invoke("testBool({0})", thing);
228 return Task.FromResult(thing);
229 }
230
Jens Geyer2b2ea622021-04-09 22:55:11 +0200231 public Task<sbyte> testByte(sbyte thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100232 {
233 logger.Invoke("testByte({0})", thing);
234 return Task.FromResult(thing);
235 }
236
Jens Geyer2b2ea622021-04-09 22:55:11 +0200237 public Task<int> testI32(int thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100238 {
239 logger.Invoke("testI32({0})", thing);
240 return Task.FromResult(thing);
241 }
242
Jens Geyer2b2ea622021-04-09 22:55:11 +0200243 public Task<long> testI64(long thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100244 {
245 logger.Invoke("testI64({0})", thing);
246 return Task.FromResult(thing);
247 }
248
Jens Geyer2b2ea622021-04-09 22:55:11 +0200249 public Task<double> testDouble(double thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100250 {
251 logger.Invoke("testDouble({0})", thing);
252 return Task.FromResult(thing);
253 }
254
Jens Geyer2b2ea622021-04-09 22:55:11 +0200255 public Task<byte[]> testBinary(byte[] thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100256 {
Jens Geyerbd1a2732019-06-26 22:52:44 +0200257 logger.Invoke("testBinary({0} bytes)", thing.Length);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100258 return Task.FromResult(thing);
259 }
260
Jens Geyer2b2ea622021-04-09 22:55:11 +0200261 public Task<Xtruct> testStruct(Xtruct thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100262 {
Jens Geyerffb97e12019-12-06 23:43:08 +0100263 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 +0100264 return Task.FromResult(thing);
265 }
266
Jens Geyer2b2ea622021-04-09 22:55:11 +0200267 public Task<Xtruct2> testNest(Xtruct2 nest, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100268 {
Jens Geyerffb97e12019-12-06 23:43:08 +0100269 var thing = nest.Struct_thing;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100270 logger.Invoke("testNest({{{0}, {{\"{1}\", {2}, {3}, {4}, {5}}}}})",
Jens Geyerffb97e12019-12-06 23:43:08 +0100271 nest.Byte_thing,
272 thing.String_thing,
273 thing.Byte_thing,
274 thing.I32_thing,
275 thing.I64_thing,
276 nest.I32_thing);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100277 return Task.FromResult(nest);
278 }
279
Jens Geyer2b2ea622021-04-09 22:55:11 +0200280 public Task<Dictionary<int, int>> testMap(Dictionary<int, int> thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100281 {
282 sb.Clear();
283 sb.Append("testMap({{");
284 var first = true;
285 foreach (var key in thing.Keys)
286 {
287 if (first)
288 {
289 first = false;
290 }
291 else
292 {
293 sb.Append(", ");
294 }
295 sb.AppendFormat("{0} => {1}", key, thing[key]);
296 }
297 sb.Append("}})");
298 logger.Invoke(sb.ToString());
299 return Task.FromResult(thing);
300 }
301
Jens Geyer2b2ea622021-04-09 22:55:11 +0200302 public Task<Dictionary<string, string>> testStringMap(Dictionary<string, string> thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100303 {
304 sb.Clear();
305 sb.Append("testStringMap({{");
306 var first = true;
307 foreach (var key in thing.Keys)
308 {
309 if (first)
310 {
311 first = false;
312 }
313 else
314 {
315 sb.Append(", ");
316 }
317 sb.AppendFormat("{0} => {1}", key, thing[key]);
318 }
319 sb.Append("}})");
320 logger.Invoke(sb.ToString());
321 return Task.FromResult(thing);
322 }
323
Jens Geyer2b2ea622021-04-09 22:55:11 +0200324 public Task<THashSet<int>> testSet(THashSet<int> thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100325 {
326 sb.Clear();
327 sb.Append("testSet({{");
328 var first = true;
329 foreach (int elem in thing)
330 {
331 if (first)
332 {
333 first = false;
334 }
335 else
336 {
337 sb.Append(", ");
338 }
339 sb.AppendFormat("{0}", elem);
340 }
341 sb.Append("}})");
342 logger.Invoke(sb.ToString());
343 return Task.FromResult(thing);
344 }
345
Jens Geyer2b2ea622021-04-09 22:55:11 +0200346 public Task<List<int>> testList(List<int> thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100347 {
348 sb.Clear();
349 sb.Append("testList({{");
350 var first = true;
351 foreach (var elem in thing)
352 {
353 if (first)
354 {
355 first = false;
356 }
357 else
358 {
359 sb.Append(", ");
360 }
361 sb.AppendFormat("{0}", elem);
362 }
363 sb.Append("}})");
364 logger.Invoke(sb.ToString());
365 return Task.FromResult(thing);
366 }
367
Jens Geyer2b2ea622021-04-09 22:55:11 +0200368 public Task<Numberz> testEnum(Numberz thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100369 {
370 logger.Invoke("testEnum({0})", thing);
371 return Task.FromResult(thing);
372 }
373
Jens Geyer2b2ea622021-04-09 22:55:11 +0200374 public Task<long> testTypedef(long thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100375 {
376 logger.Invoke("testTypedef({0})", thing);
377 return Task.FromResult(thing);
378 }
379
Jens Geyer2b2ea622021-04-09 22:55:11 +0200380 public Task<Dictionary<int, Dictionary<int, int>>> testMapMap(int hello, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100381 {
382 logger.Invoke("testMapMap({0})", hello);
383 var mapmap = new Dictionary<int, Dictionary<int, int>>();
384
385 var pos = new Dictionary<int, int>();
386 var neg = new Dictionary<int, int>();
387 for (var i = 1; i < 5; i++)
388 {
389 pos[i] = i;
390 neg[-i] = -i;
391 }
392
393 mapmap[4] = pos;
394 mapmap[-4] = neg;
395
396 return Task.FromResult(mapmap);
397 }
398
Jens Geyer2b2ea622021-04-09 22:55:11 +0200399 public Task<Dictionary<long, Dictionary<Numberz, Insanity>>> testInsanity(Insanity argument, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100400 {
401 logger.Invoke("testInsanity()");
402
403 /** from ThriftTest.thrift:
404 * So you think you've got this all worked, out eh?
405 *
406 * Creates a the returned map with these values and prints it out:
407 * { 1 => { 2 => argument,
408 * 3 => argument,
409 * },
410 * 2 => { 6 => <empty Insanity struct>, },
411 * }
412 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
413 */
414
415 var first_map = new Dictionary<Numberz, Insanity>();
416 var second_map = new Dictionary<Numberz, Insanity>(); ;
417
418 first_map[Numberz.TWO] = argument;
419 first_map[Numberz.THREE] = argument;
420
421 second_map[Numberz.SIX] = new Insanity();
422
423 var insane = new Dictionary<long, Dictionary<Numberz, Insanity>>
424 {
425 [1] = first_map,
426 [2] = second_map
427 };
428
429 return Task.FromResult(insane);
430 }
431
Jens Geyer2b2ea622021-04-09 22:55:11 +0200432 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 +0100433 CancellationToken cancellationToken)
434 {
435 logger.Invoke("testMulti()");
436
437 var hello = new Xtruct(); ;
Jens Geyerffb97e12019-12-06 23:43:08 +0100438 hello.String_thing = "Hello2";
439 hello.Byte_thing = arg0;
440 hello.I32_thing = arg1;
441 hello.I64_thing = arg2;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100442 return Task.FromResult(hello);
443 }
444
Jens Geyer2b2ea622021-04-09 22:55:11 +0200445 public Task testException(string arg, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100446 {
447 logger.Invoke("testException({0})", arg);
448 if (arg == "Xception")
449 {
450 var x = new Xception
451 {
452 ErrorCode = 1001,
453 Message = arg
454 };
455 throw x;
456 }
457 if (arg == "TException")
458 {
459 throw new TException();
460 }
461 return Task.CompletedTask;
462 }
463
Jens Geyer2b2ea622021-04-09 22:55:11 +0200464 public Task<Xtruct> testMultiException(string arg0, string arg1, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100465 {
466 logger.Invoke("testMultiException({0}, {1})", arg0, arg1);
467 if (arg0 == "Xception")
468 {
469 var x = new Xception
470 {
471 ErrorCode = 1001,
472 Message = "This is an Xception"
473 };
474 throw x;
475 }
476
477 if (arg0 == "Xception2")
478 {
479 var x = new Xception2
480 {
481 ErrorCode = 2002,
Jens Geyerffb97e12019-12-06 23:43:08 +0100482 Struct_thing = new Xtruct { String_thing = "This is an Xception2" }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100483 };
484 throw x;
485 }
486
Jens Geyerffb97e12019-12-06 23:43:08 +0100487 var result = new Xtruct { String_thing = arg1 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100488 return Task.FromResult(result);
489 }
490
Jens Geyer2b2ea622021-04-09 22:55:11 +0200491 public Task testOneway(int secondsToSleep, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100492 {
493 logger.Invoke("testOneway({0}), sleeping...", secondsToSleep);
494 Task.Delay(secondsToSleep * 1000, cancellationToken).GetAwaiter().GetResult();
495 logger.Invoke("testOneway finished");
496
497 return Task.CompletedTask;
498 }
499 }
500
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100501
502 private static X509Certificate2 GetServerCert()
503 {
504 var serverCertName = "server.p12";
505 var possiblePaths = new List<string>
506 {
507 "../../../keys/",
508 "../../keys/",
509 "../keys/",
510 "keys/",
511 };
512
513 string existingPath = null;
514 foreach (var possiblePath in possiblePaths)
515 {
516 var path = Path.GetFullPath(possiblePath + serverCertName);
517 if (File.Exists(path))
518 {
519 existingPath = path;
520 break;
521 }
522 }
523
524 if (string.IsNullOrEmpty(existingPath))
525 {
526 throw new FileNotFoundException($"Cannot find file: {serverCertName}");
527 }
528
529 var cert = new X509Certificate2(existingPath, "thrift");
530
531 return cert;
532 }
533
534 public static int Execute(List<string> args)
535 {
Jens Geyer261cad32019-11-20 19:03:14 +0100536 using (var loggerFactory = new LoggerFactory()) //.AddConsole().AddDebug();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100537 {
Jens Geyer261cad32019-11-20 19:03:14 +0100538 var logger = loggerFactory.CreateLogger("Test");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100539
540 try
541 {
Jens Geyer261cad32019-11-20 19:03:14 +0100542 var param = new ServerParam();
543
544 try
545 {
546 param.Parse(args);
547 }
548 catch (Exception ex)
549 {
550 Console.WriteLine("*** FAILED ***");
551 Console.WriteLine("Error while parsing arguments");
552 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
553 return 1;
554 }
555
556
557 // Endpoint transport (mandatory)
558 TServerTransport trans;
559 switch (param.transport)
560 {
561 case TransportChoice.NamedPipe:
562 Debug.Assert(param.pipe != null);
Jens Geyeref0cb012021-04-02 12:18:15 +0200563 trans = new TNamedPipeServerTransport(param.pipe, Configuration, NamedPipeClientFlags.OnlyLocalClients);
Jens Geyer261cad32019-11-20 19:03:14 +0100564 break;
565
566
567 case TransportChoice.TlsSocket:
568 var cert = GetServerCert();
569 if (cert == null || !cert.HasPrivateKey)
570 {
571 cert?.Dispose();
572 throw new InvalidOperationException("Certificate doesn't contain private key");
573 }
574
Jens Geyereacd1d42019-11-20 19:03:14 +0100575 trans = new TTlsServerSocketTransport(param.port, Configuration,
576 cert,
Jens Geyer261cad32019-11-20 19:03:14 +0100577 (sender, certificate, chain, errors) => true,
578 null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
579 break;
580
581 case TransportChoice.Socket:
582 default:
Jens Geyereacd1d42019-11-20 19:03:14 +0100583 trans = new TServerSocketTransport(param.port, Configuration);
Jens Geyer261cad32019-11-20 19:03:14 +0100584 break;
585 }
586
587 // Layered transport (mandatory)
588 TTransportFactory transFactory = null;
589 switch (param.buffering)
590 {
591 case BufferChoice.Framed:
592 transFactory = new TFramedTransport.Factory();
593 break;
594 case BufferChoice.Buffered:
595 transFactory = new TBufferedTransport.Factory();
596 break;
597 default:
598 Debug.Assert(param.buffering == BufferChoice.None, "unhandled case");
599 transFactory = null; // no layered transprt
600 break;
601 }
602
Jens Geyer828ffa82020-11-21 15:15:32 +0100603 TProtocolFactory proto = param.protocol switch
Jens Geyer261cad32019-11-20 19:03:14 +0100604 {
Jens Geyer828ffa82020-11-21 15:15:32 +0100605 ProtocolChoice.Compact => new TCompactProtocol.Factory(),
606 ProtocolChoice.Json => new TJsonProtocol.Factory(),
607 ProtocolChoice.Binary => new TBinaryProtocol.Factory(),
608 _ => new TBinaryProtocol.Factory(),
609 };
Jens Geyer261cad32019-11-20 19:03:14 +0100610
611 // Processor
612 var testHandler = new TestHandlerAsync();
613 var testProcessor = new ThriftTest.AsyncProcessor(testHandler);
614 var processorFactory = new TSingletonProcessorFactory(testProcessor);
615
616 TServer serverEngine = new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger);
617
618 //Server event handler
619 var serverEvents = new MyServerEventHandler();
620 serverEngine.SetEventHandler(serverEvents);
621
622 // Run it
623 var where = (!string.IsNullOrEmpty(param.pipe)) ? "on pipe " + param.pipe : "on port " + param.port;
624 Console.WriteLine("Starting the AsyncBaseServer " + where +
625 " with processor TPrototypeProcessorFactory prototype factory " +
626 (param.buffering == BufferChoice.Buffered ? " with buffered transport" : "") +
627 (param.buffering == BufferChoice.Framed ? " with framed transport" : "") +
628 (param.transport == TransportChoice.TlsSocket ? " with encryption" : "") +
629 (param.protocol == ProtocolChoice.Compact ? " with compact protocol" : "") +
630 (param.protocol == ProtocolChoice.Json ? " with json protocol" : "") +
631 "...");
632 serverEngine.ServeAsync(CancellationToken.None).GetAwaiter().GetResult();
633 Console.ReadLine();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100634 }
Jens Geyer261cad32019-11-20 19:03:14 +0100635 catch (Exception x)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100636 {
Jens Geyer261cad32019-11-20 19:03:14 +0100637 Console.Error.Write(x);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100638 return 1;
639 }
640
Jens Geyer261cad32019-11-20 19:03:14 +0100641 Console.WriteLine("done.");
642 return 0;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100643 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100644 }
645 }
646
647}