blob: 86072b0a742d9c384e8cc4355370bc6bc87d413e [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 Geyer3cac3202022-01-31 18:04:35 +0100232 public Task<string> testString(string? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100233 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100234 logger.Invoke("testString({0})", thing ?? "<null>");
235 return Task.FromResult(thing ?? string.Empty);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100236 }
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 Geyer3cac3202022-01-31 18:04:35 +0100268 public Task<byte[]> testBinary(byte[]? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100269 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100270 logger.Invoke("testBinary({0} bytes)", thing?.Length ?? 0);
271 return Task.FromResult(thing ?? Array.Empty<byte>());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100272 }
273
Jens Geyer3cac3202022-01-31 18:04:35 +0100274 public Task<Xtruct> testStruct(Xtruct? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100275 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100276 logger.Invoke("testStruct({{\"{0}\", {1}, {2}, {3}}})", thing?.String_thing ?? "<null>", thing?.Byte_thing ?? 0, thing?.I32_thing ?? 0, thing?.I64_thing ?? 0);
277 return Task.FromResult(thing ?? new Xtruct()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100278 }
279
Jens Geyer3cac3202022-01-31 18:04:35 +0100280 public Task<Xtruct2> testNest(Xtruct2? nest, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100281 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100282 var thing = nest?.Struct_thing;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100283 logger.Invoke("testNest({{{0}, {{\"{1}\", {2}, {3}, {4}, {5}}}}})",
Jens Geyer3cac3202022-01-31 18:04:35 +0100284 nest?.Byte_thing ?? 0,
285 thing?.String_thing ?? "<null>",
286 thing?.Byte_thing ?? 0,
287 thing?.I32_thing ?? 0,
288 thing?.I64_thing ?? 0,
289 nest?.I32_thing ?? 0);
290 return Task.FromResult(nest ?? new Xtruct2()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100291 }
292
Jens Geyer3cac3202022-01-31 18:04:35 +0100293 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({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100297 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100298 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100299 var first = true;
300 foreach (var key in thing.Keys)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100301 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100302 if (first)
303 {
304 first = false;
305 }
306 else
307 {
308 sb.Append(", ");
309 }
310 sb.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100311 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100312 }
313 sb.Append("}})");
314 logger.Invoke(sb.ToString());
Jens Geyer3cac3202022-01-31 18:04:35 +0100315 return Task.FromResult(thing ?? new Dictionary<int, int>()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100316 }
317
Jens Geyer3cac3202022-01-31 18:04:35 +0100318 public Task<Dictionary<string, string>> testStringMap(Dictionary<string, string>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100319 {
320 sb.Clear();
321 sb.Append("testStringMap({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100322 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100323 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100324 var first = true;
325 foreach (var key in thing.Keys)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100326 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100327 if (first)
328 {
329 first = false;
330 }
331 else
332 {
333 sb.Append(", ");
334 }
335 sb.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100336 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100337 }
338 sb.Append("}})");
339 logger.Invoke(sb.ToString());
Jens Geyer3cac3202022-01-31 18:04:35 +0100340 return Task.FromResult(thing ?? new Dictionary<string, string>()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100341 }
342
Jens Geyer3cac3202022-01-31 18:04:35 +0100343 public Task<HashSet<int>> testSet(HashSet<int>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100344 {
345 sb.Clear();
346 sb.Append("testSet({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100347 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100348 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100349 var first = true;
350 foreach (int elem in thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100351 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100352 if (first)
353 {
354 first = false;
355 }
356 else
357 {
358 sb.Append(", ");
359 }
360 sb.AppendFormat("{0}", elem);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100361 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100362 }
363 sb.Append("}})");
364 logger.Invoke(sb.ToString());
Jens Geyer3cac3202022-01-31 18:04:35 +0100365 return Task.FromResult(thing ?? new HashSet<int>()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100366 }
367
Jens Geyer3cac3202022-01-31 18:04:35 +0100368 public Task<List<int>> testList(List<int>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100369 {
370 sb.Clear();
371 sb.Append("testList({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100372 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100373 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100374 var first = true;
375 foreach (var elem in thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100376 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100377 if (first)
378 {
379 first = false;
380 }
381 else
382 {
383 sb.Append(", ");
384 }
385 sb.AppendFormat("{0}", elem);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100386 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100387 }
388 sb.Append("}})");
389 logger.Invoke(sb.ToString());
Jens Geyer3cac3202022-01-31 18:04:35 +0100390 return Task.FromResult(thing ?? new List<int>()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100391 }
392
Jens Geyer2b2ea622021-04-09 22:55:11 +0200393 public Task<Numberz> testEnum(Numberz thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100394 {
395 logger.Invoke("testEnum({0})", thing);
396 return Task.FromResult(thing);
397 }
398
Jens Geyer2b2ea622021-04-09 22:55:11 +0200399 public Task<long> testTypedef(long thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100400 {
401 logger.Invoke("testTypedef({0})", thing);
402 return Task.FromResult(thing);
403 }
404
Jens Geyer2b2ea622021-04-09 22:55:11 +0200405 public Task<Dictionary<int, Dictionary<int, int>>> testMapMap(int hello, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100406 {
407 logger.Invoke("testMapMap({0})", hello);
408 var mapmap = new Dictionary<int, Dictionary<int, int>>();
409
410 var pos = new Dictionary<int, int>();
411 var neg = new Dictionary<int, int>();
412 for (var i = 1; i < 5; i++)
413 {
414 pos[i] = i;
415 neg[-i] = -i;
416 }
417
418 mapmap[4] = pos;
419 mapmap[-4] = neg;
420
421 return Task.FromResult(mapmap);
422 }
423
Jens Geyer3cac3202022-01-31 18:04:35 +0100424 public Task<Dictionary<long, Dictionary<Numberz, Insanity>>> testInsanity(Insanity? argument, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100425 {
426 logger.Invoke("testInsanity()");
427
428 /** from ThriftTest.thrift:
429 * So you think you've got this all worked, out eh?
430 *
431 * Creates a the returned map with these values and prints it out:
432 * { 1 => { 2 => argument,
433 * 3 => argument,
434 * },
435 * 2 => { 6 => <empty Insanity struct>, },
436 * }
437 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
438 */
439
440 var first_map = new Dictionary<Numberz, Insanity>();
441 var second_map = new Dictionary<Numberz, Insanity>(); ;
442
Jens Geyer3cac3202022-01-31 18:04:35 +0100443 // null dict keys/values are not allowed in Thrift
444 first_map[Numberz.TWO] = argument ?? new Insanity();
445 first_map[Numberz.THREE] = argument ?? new Insanity();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100446
447 second_map[Numberz.SIX] = new Insanity();
448
449 var insane = new Dictionary<long, Dictionary<Numberz, Insanity>>
450 {
451 [1] = first_map,
452 [2] = second_map
453 };
454
455 return Task.FromResult(insane);
456 }
457
Jens Geyer3cac3202022-01-31 18:04:35 +0100458 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 +0100459 CancellationToken cancellationToken)
460 {
461 logger.Invoke("testMulti()");
462
463 var hello = new Xtruct(); ;
Jens Geyerffb97e12019-12-06 23:43:08 +0100464 hello.String_thing = "Hello2";
465 hello.Byte_thing = arg0;
466 hello.I32_thing = arg1;
467 hello.I64_thing = arg2;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100468 return Task.FromResult(hello);
469 }
470
Jens Geyer3cac3202022-01-31 18:04:35 +0100471 public Task testException(string? arg, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100472 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100473 logger.Invoke("testException({0})", arg ?? "<null>");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100474 if (arg == "Xception")
475 {
476 var x = new Xception
477 {
478 ErrorCode = 1001,
479 Message = arg
480 };
481 throw x;
482 }
483 if (arg == "TException")
484 {
485 throw new TException();
486 }
487 return Task.CompletedTask;
488 }
489
Jens Geyer3cac3202022-01-31 18:04:35 +0100490 public Task<Xtruct> testMultiException(string? arg0, string? arg1, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100491 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100492 logger.Invoke("testMultiException({0}, {1})", arg0 ?? "<null>", arg1 ?? "<null>");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100493 if (arg0 == "Xception")
494 {
495 var x = new Xception
496 {
497 ErrorCode = 1001,
498 Message = "This is an Xception"
499 };
500 throw x;
501 }
502
503 if (arg0 == "Xception2")
504 {
505 var x = new Xception2
506 {
507 ErrorCode = 2002,
Jens Geyerffb97e12019-12-06 23:43:08 +0100508 Struct_thing = new Xtruct { String_thing = "This is an Xception2" }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100509 };
510 throw x;
511 }
512
Jens Geyerffb97e12019-12-06 23:43:08 +0100513 var result = new Xtruct { String_thing = arg1 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100514 return Task.FromResult(result);
515 }
516
Jens Geyer71569402021-11-13 23:51:16 +0100517 public async Task testOneway(int secondsToSleep, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100518 {
519 logger.Invoke("testOneway({0}), sleeping...", secondsToSleep);
Jens Geyer71569402021-11-13 23:51:16 +0100520 await Task.Delay(secondsToSleep * 1000, cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100521 logger.Invoke("testOneway finished");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100522 }
523 }
524
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100525
526 private static X509Certificate2 GetServerCert()
527 {
528 var serverCertName = "server.p12";
529 var possiblePaths = new List<string>
530 {
531 "../../../keys/",
532 "../../keys/",
533 "../keys/",
534 "keys/",
535 };
536
Jens Geyer98a23252022-01-09 16:50:52 +0100537 var existingPath = string.Empty;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100538 foreach (var possiblePath in possiblePaths)
539 {
540 var path = Path.GetFullPath(possiblePath + serverCertName);
541 if (File.Exists(path))
542 {
543 existingPath = path;
544 break;
545 }
546 }
547
548 if (string.IsNullOrEmpty(existingPath))
549 {
550 throw new FileNotFoundException($"Cannot find file: {serverCertName}");
551 }
552
553 var cert = new X509Certificate2(existingPath, "thrift");
554
555 return cert;
556 }
557
Jens Geyer71569402021-11-13 23:51:16 +0100558 public static async Task<int> Execute(List<string> args)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100559 {
Jens Geyer261cad32019-11-20 19:03:14 +0100560 using (var loggerFactory = new LoggerFactory()) //.AddConsole().AddDebug();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100561 {
Jens Geyer261cad32019-11-20 19:03:14 +0100562 var logger = loggerFactory.CreateLogger("Test");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100563
564 try
565 {
Jens Geyer261cad32019-11-20 19:03:14 +0100566 var param = new ServerParam();
567
568 try
569 {
570 param.Parse(args);
571 }
572 catch (Exception ex)
573 {
574 Console.WriteLine("*** FAILED ***");
575 Console.WriteLine("Error while parsing arguments");
576 Console.WriteLine(ex.Message + " ST: " + ex.StackTrace);
577 return 1;
578 }
579
580
581 // Endpoint transport (mandatory)
582 TServerTransport trans;
583 switch (param.transport)
584 {
585 case TransportChoice.NamedPipe:
586 Debug.Assert(param.pipe != null);
Jens Geyeref0cb012021-04-02 12:18:15 +0200587 trans = new TNamedPipeServerTransport(param.pipe, Configuration, NamedPipeClientFlags.OnlyLocalClients);
Jens Geyer261cad32019-11-20 19:03:14 +0100588 break;
589
590
591 case TransportChoice.TlsSocket:
592 var cert = GetServerCert();
593 if (cert == null || !cert.HasPrivateKey)
594 {
595 cert?.Dispose();
596 throw new InvalidOperationException("Certificate doesn't contain private key");
597 }
598
Jens Geyereacd1d42019-11-20 19:03:14 +0100599 trans = new TTlsServerSocketTransport(param.port, Configuration,
600 cert,
Jens Geyer261cad32019-11-20 19:03:14 +0100601 (sender, certificate, chain, errors) => true,
602 null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
603 break;
604
605 case TransportChoice.Socket:
606 default:
Jens Geyereacd1d42019-11-20 19:03:14 +0100607 trans = new TServerSocketTransport(param.port, Configuration);
Jens Geyer261cad32019-11-20 19:03:14 +0100608 break;
609 }
610
611 // Layered transport (mandatory)
Jens Geyer98a23252022-01-09 16:50:52 +0100612 TTransportFactory? transFactory;
Jens Geyer261cad32019-11-20 19:03:14 +0100613 switch (param.buffering)
614 {
615 case BufferChoice.Framed:
616 transFactory = new TFramedTransport.Factory();
617 break;
618 case BufferChoice.Buffered:
619 transFactory = new TBufferedTransport.Factory();
620 break;
621 default:
622 Debug.Assert(param.buffering == BufferChoice.None, "unhandled case");
623 transFactory = null; // no layered transprt
624 break;
625 }
626
Jens Geyer828ffa82020-11-21 15:15:32 +0100627 TProtocolFactory proto = param.protocol switch
Jens Geyer261cad32019-11-20 19:03:14 +0100628 {
Jens Geyer828ffa82020-11-21 15:15:32 +0100629 ProtocolChoice.Compact => new TCompactProtocol.Factory(),
630 ProtocolChoice.Json => new TJsonProtocol.Factory(),
631 ProtocolChoice.Binary => new TBinaryProtocol.Factory(),
632 _ => new TBinaryProtocol.Factory(),
633 };
Jens Geyer261cad32019-11-20 19:03:14 +0100634
635 // Processor
636 var testHandler = new TestHandlerAsync();
637 var testProcessor = new ThriftTest.AsyncProcessor(testHandler);
638 var processorFactory = new TSingletonProcessorFactory(testProcessor);
639
Jens Geyer63d114d2021-05-25 23:42:35 +0200640 var poolconfig = new TThreadPoolAsyncServer.Configuration(); // use platform defaults
641 TServer serverEngine = param.server switch
642 {
643 ServerChoice.Simple => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger),
644 ServerChoice.ThreadPool => new TThreadPoolAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, poolconfig, logger),
645 _ => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger)
646 };
Jens Geyer261cad32019-11-20 19:03:14 +0100647
648 //Server event handler
649 var serverEvents = new MyServerEventHandler();
650 serverEngine.SetEventHandler(serverEvents);
651
652 // Run it
Jens Geyer63d114d2021-05-25 23:42:35 +0200653 var where = (!string.IsNullOrEmpty(param.pipe)) ? "pipe " + param.pipe : "port " + param.port;
654 Console.WriteLine("Running "+ serverEngine.GetType().Name +
655 " at "+ where +
656 " using "+ processorFactory.GetType().Name + " processor prototype factory " +
Jens Geyer261cad32019-11-20 19:03:14 +0100657 (param.buffering == BufferChoice.Buffered ? " with buffered transport" : "") +
658 (param.buffering == BufferChoice.Framed ? " with framed transport" : "") +
659 (param.transport == TransportChoice.TlsSocket ? " with encryption" : "") +
660 (param.protocol == ProtocolChoice.Compact ? " with compact protocol" : "") +
661 (param.protocol == ProtocolChoice.Json ? " with json protocol" : "") +
662 "...");
Jens Geyer71569402021-11-13 23:51:16 +0100663 await serverEngine.ServeAsync(CancellationToken.None);
Jens Geyer261cad32019-11-20 19:03:14 +0100664 Console.ReadLine();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100665 }
Jens Geyer261cad32019-11-20 19:03:14 +0100666 catch (Exception x)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100667 {
Jens Geyer261cad32019-11-20 19:03:14 +0100668 Console.Error.Write(x);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100669 return 1;
670 }
671
Jens Geyer261cad32019-11-20 19:03:14 +0100672 Console.WriteLine("done.");
673 return 0;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100674 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100675 }
676 }
677
678}