blob: fdbaa9718a4e5bb9a67b0c750abde8ebacf3624f [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 Geyer261cad32019-11-20 19:03:14 +0100207 private readonly TestLogDelegate logger;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100208
209 public TestHandlerAsync()
210 {
211 handlerID = Interlocked.Increment(ref _clientID);
Jens Geyer261cad32019-11-20 19:03:14 +0100212 logger += TestConsoleLogger;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100213 logger.Invoke("New TestHandler instance created");
214 }
215
Jens Geyer261cad32019-11-20 19:03:14 +0100216 public void TestConsoleLogger(string msg, params object[] values)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100217 {
Jens Geyer60970c42022-09-09 13:39:33 +0200218 var sb = new StringBuilder();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100219 sb.AppendFormat("handler{0:D3}:", handlerID);
220 sb.AppendFormat(msg, values);
221 sb.AppendLine();
Jens Geyer60970c42022-09-09 13:39:33 +0200222 lock (typeof(Console))
223 Console.Write(sb.ToString());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100224 }
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 Geyer62445c12022-06-29 00:00:00 +0200274 public Task<Guid> testUuid(Guid thing, CancellationToken cancellationToken)
275 {
276 logger.Invoke("testUuid({0})", thing.ToString("B"));
277 return Task.FromResult(thing);
278 }
279
Jens Geyer3cac3202022-01-31 18:04:35 +0100280 public Task<Xtruct> testStruct(Xtruct? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100281 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100282 logger.Invoke("testStruct({{\"{0}\", {1}, {2}, {3}}})", thing?.String_thing ?? "<null>", thing?.Byte_thing ?? 0, thing?.I32_thing ?? 0, thing?.I64_thing ?? 0);
283 return Task.FromResult(thing ?? new Xtruct()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100284 }
285
Jens Geyer3cac3202022-01-31 18:04:35 +0100286 public Task<Xtruct2> testNest(Xtruct2? nest, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100287 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100288 var thing = nest?.Struct_thing;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100289 logger.Invoke("testNest({{{0}, {{\"{1}\", {2}, {3}, {4}, {5}}}}})",
Jens Geyer3cac3202022-01-31 18:04:35 +0100290 nest?.Byte_thing ?? 0,
291 thing?.String_thing ?? "<null>",
292 thing?.Byte_thing ?? 0,
293 thing?.I32_thing ?? 0,
294 thing?.I64_thing ?? 0,
295 nest?.I32_thing ?? 0);
296 return Task.FromResult(nest ?? new Xtruct2()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100297 }
298
Jens Geyer3cac3202022-01-31 18:04:35 +0100299 public Task<Dictionary<int, int>> testMap(Dictionary<int, int>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100300 {
Jens Geyer60970c42022-09-09 13:39:33 +0200301 var sb = new StringBuilder();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100302 sb.Append("testMap({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100303 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100304 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100305 var first = true;
306 foreach (var key in thing.Keys)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100307 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100308 if (first)
309 {
310 first = false;
311 }
312 else
313 {
314 sb.Append(", ");
315 }
316 sb.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100317 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100318 }
319 sb.Append("}})");
320 logger.Invoke(sb.ToString());
Jens Geyer3cac3202022-01-31 18:04:35 +0100321 return Task.FromResult(thing ?? new Dictionary<int, int>()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100322 }
323
Jens Geyer3cac3202022-01-31 18:04:35 +0100324 public Task<Dictionary<string, string>> testStringMap(Dictionary<string, string>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100325 {
Jens Geyer60970c42022-09-09 13:39:33 +0200326 var sb = new StringBuilder();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100327 sb.Append("testStringMap({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100328 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100329 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100330 var first = true;
331 foreach (var key in thing.Keys)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100332 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100333 if (first)
334 {
335 first = false;
336 }
337 else
338 {
339 sb.Append(", ");
340 }
341 sb.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100342 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100343 }
344 sb.Append("}})");
345 logger.Invoke(sb.ToString());
Jens Geyer3cac3202022-01-31 18:04:35 +0100346 return Task.FromResult(thing ?? new Dictionary<string, string>()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100347 }
348
Jens Geyer3cac3202022-01-31 18:04:35 +0100349 public Task<HashSet<int>> testSet(HashSet<int>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100350 {
Jens Geyer60970c42022-09-09 13:39:33 +0200351 var sb = new StringBuilder();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100352 sb.Append("testSet({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100353 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100354 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100355 var first = true;
356 foreach (int elem in thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100357 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100358 if (first)
359 {
360 first = false;
361 }
362 else
363 {
364 sb.Append(", ");
365 }
366 sb.AppendFormat("{0}", elem);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100367 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100368 }
369 sb.Append("}})");
370 logger.Invoke(sb.ToString());
Jens Geyer3cac3202022-01-31 18:04:35 +0100371 return Task.FromResult(thing ?? new HashSet<int>()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100372 }
373
Jens Geyer3cac3202022-01-31 18:04:35 +0100374 public Task<List<int>> testList(List<int>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100375 {
Jens Geyer60970c42022-09-09 13:39:33 +0200376 var sb = new StringBuilder();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100377 sb.Append("testList({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100378 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100379 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100380 var first = true;
381 foreach (var elem in thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100382 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100383 if (first)
384 {
385 first = false;
386 }
387 else
388 {
389 sb.Append(", ");
390 }
391 sb.AppendFormat("{0}", elem);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100392 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100393 }
394 sb.Append("}})");
395 logger.Invoke(sb.ToString());
Jens Geyer3cac3202022-01-31 18:04:35 +0100396 return Task.FromResult(thing ?? new List<int>()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100397 }
398
Jens Geyer2b2ea622021-04-09 22:55:11 +0200399 public Task<Numberz> testEnum(Numberz thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100400 {
401 logger.Invoke("testEnum({0})", thing);
402 return Task.FromResult(thing);
403 }
404
Jens Geyer2b2ea622021-04-09 22:55:11 +0200405 public Task<long> testTypedef(long thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100406 {
407 logger.Invoke("testTypedef({0})", thing);
408 return Task.FromResult(thing);
409 }
410
Jens Geyer2b2ea622021-04-09 22:55:11 +0200411 public Task<Dictionary<int, Dictionary<int, int>>> testMapMap(int hello, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100412 {
413 logger.Invoke("testMapMap({0})", hello);
414 var mapmap = new Dictionary<int, Dictionary<int, int>>();
415
416 var pos = new Dictionary<int, int>();
417 var neg = new Dictionary<int, int>();
418 for (var i = 1; i < 5; i++)
419 {
420 pos[i] = i;
421 neg[-i] = -i;
422 }
423
424 mapmap[4] = pos;
425 mapmap[-4] = neg;
426
427 return Task.FromResult(mapmap);
428 }
429
Jens Geyer3cac3202022-01-31 18:04:35 +0100430 public Task<Dictionary<long, Dictionary<Numberz, Insanity>>> testInsanity(Insanity? argument, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100431 {
432 logger.Invoke("testInsanity()");
433
434 /** from ThriftTest.thrift:
435 * So you think you've got this all worked, out eh?
436 *
437 * Creates a the returned map with these values and prints it out:
438 * { 1 => { 2 => argument,
439 * 3 => argument,
440 * },
441 * 2 => { 6 => <empty Insanity struct>, },
442 * }
443 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
444 */
445
446 var first_map = new Dictionary<Numberz, Insanity>();
447 var second_map = new Dictionary<Numberz, Insanity>(); ;
448
Jens Geyer3cac3202022-01-31 18:04:35 +0100449 // null dict keys/values are not allowed in Thrift
450 first_map[Numberz.TWO] = argument ?? new Insanity();
451 first_map[Numberz.THREE] = argument ?? new Insanity();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100452
453 second_map[Numberz.SIX] = new Insanity();
454
455 var insane = new Dictionary<long, Dictionary<Numberz, Insanity>>
456 {
457 [1] = first_map,
458 [2] = second_map
459 };
460
461 return Task.FromResult(insane);
462 }
463
Jens Geyer3cac3202022-01-31 18:04:35 +0100464 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 +0100465 CancellationToken cancellationToken)
466 {
467 logger.Invoke("testMulti()");
468
469 var hello = new Xtruct(); ;
Jens Geyerffb97e12019-12-06 23:43:08 +0100470 hello.String_thing = "Hello2";
471 hello.Byte_thing = arg0;
472 hello.I32_thing = arg1;
473 hello.I64_thing = arg2;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100474 return Task.FromResult(hello);
475 }
476
Jens Geyer3cac3202022-01-31 18:04:35 +0100477 public Task testException(string? arg, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100478 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100479 logger.Invoke("testException({0})", arg ?? "<null>");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100480 if (arg == "Xception")
481 {
482 var x = new Xception
483 {
484 ErrorCode = 1001,
485 Message = arg
486 };
487 throw x;
488 }
489 if (arg == "TException")
490 {
491 throw new TException();
492 }
493 return Task.CompletedTask;
494 }
495
Jens Geyer3cac3202022-01-31 18:04:35 +0100496 public Task<Xtruct> testMultiException(string? arg0, string? arg1, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100497 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100498 logger.Invoke("testMultiException({0}, {1})", arg0 ?? "<null>", arg1 ?? "<null>");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100499 if (arg0 == "Xception")
500 {
501 var x = new Xception
502 {
503 ErrorCode = 1001,
504 Message = "This is an Xception"
505 };
506 throw x;
507 }
508
509 if (arg0 == "Xception2")
510 {
511 var x = new Xception2
512 {
513 ErrorCode = 2002,
Jens Geyerffb97e12019-12-06 23:43:08 +0100514 Struct_thing = new Xtruct { String_thing = "This is an Xception2" }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100515 };
516 throw x;
517 }
518
Jens Geyerffb97e12019-12-06 23:43:08 +0100519 var result = new Xtruct { String_thing = arg1 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100520 return Task.FromResult(result);
521 }
522
Jens Geyer71569402021-11-13 23:51:16 +0100523 public async Task testOneway(int secondsToSleep, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100524 {
525 logger.Invoke("testOneway({0}), sleeping...", secondsToSleep);
Jens Geyer71569402021-11-13 23:51:16 +0100526 await Task.Delay(secondsToSleep * 1000, cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100527 logger.Invoke("testOneway finished");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100528 }
529 }
530
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100531
532 private static X509Certificate2 GetServerCert()
533 {
534 var serverCertName = "server.p12";
535 var possiblePaths = new List<string>
536 {
537 "../../../keys/",
538 "../../keys/",
539 "../keys/",
540 "keys/",
541 };
542
Jens Geyer98a23252022-01-09 16:50:52 +0100543 var existingPath = string.Empty;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100544 foreach (var possiblePath in possiblePaths)
545 {
546 var path = Path.GetFullPath(possiblePath + serverCertName);
547 if (File.Exists(path))
548 {
549 existingPath = path;
550 break;
551 }
552 }
553
554 if (string.IsNullOrEmpty(existingPath))
555 {
556 throw new FileNotFoundException($"Cannot find file: {serverCertName}");
557 }
558
559 var cert = new X509Certificate2(existingPath, "thrift");
560
561 return cert;
562 }
563
Jens Geyer71569402021-11-13 23:51:16 +0100564 public static async Task<int> Execute(List<string> args)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100565 {
Jens Geyer261cad32019-11-20 19:03:14 +0100566 using (var loggerFactory = new LoggerFactory()) //.AddConsole().AddDebug();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100567 {
Jens Geyer261cad32019-11-20 19:03:14 +0100568 var logger = loggerFactory.CreateLogger("Test");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100569
570 try
571 {
Jens Geyer261cad32019-11-20 19:03:14 +0100572 var param = new ServerParam();
573
574 try
575 {
576 param.Parse(args);
577 }
578 catch (Exception ex)
579 {
580 Console.WriteLine("*** FAILED ***");
581 Console.WriteLine("Error while parsing arguments");
Jens Geyer5e37d572022-09-08 23:07:11 +0200582 Console.WriteLine("{0} {1}\nStack:\n{2}", ex.GetType().Name, ex.Message, ex.StackTrace);
Jens Geyer261cad32019-11-20 19:03:14 +0100583 return 1;
584 }
585
586
587 // Endpoint transport (mandatory)
588 TServerTransport trans;
589 switch (param.transport)
590 {
591 case TransportChoice.NamedPipe:
592 Debug.Assert(param.pipe != null);
Jens Geyer60970c42022-09-09 13:39:33 +0200593 var numListen = (param.server == ServerChoice.Simple) ? 1 : 16;
594 trans = new TNamedPipeServerTransport(param.pipe, Configuration, NamedPipeServerFlags.OnlyLocalClients, numListen);
Jens Geyer261cad32019-11-20 19:03:14 +0100595 break;
596
597
598 case TransportChoice.TlsSocket:
599 var cert = GetServerCert();
600 if (cert == null || !cert.HasPrivateKey)
601 {
602 cert?.Dispose();
603 throw new InvalidOperationException("Certificate doesn't contain private key");
604 }
605
Jens Geyereacd1d42019-11-20 19:03:14 +0100606 trans = new TTlsServerSocketTransport(param.port, Configuration,
607 cert,
Jens Geyer261cad32019-11-20 19:03:14 +0100608 (sender, certificate, chain, errors) => true,
Jens Geyer51fc54f2023-02-14 23:51:16 +0100609 null, SslProtocols.Tls12);
Jens Geyer261cad32019-11-20 19:03:14 +0100610 break;
611
612 case TransportChoice.Socket:
613 default:
Jens Geyereacd1d42019-11-20 19:03:14 +0100614 trans = new TServerSocketTransport(param.port, Configuration);
Jens Geyer261cad32019-11-20 19:03:14 +0100615 break;
616 }
617
618 // Layered transport (mandatory)
Jens Geyer98a23252022-01-09 16:50:52 +0100619 TTransportFactory? transFactory;
Jens Geyer261cad32019-11-20 19:03:14 +0100620 switch (param.buffering)
621 {
622 case BufferChoice.Framed:
623 transFactory = new TFramedTransport.Factory();
624 break;
625 case BufferChoice.Buffered:
626 transFactory = new TBufferedTransport.Factory();
627 break;
628 default:
629 Debug.Assert(param.buffering == BufferChoice.None, "unhandled case");
630 transFactory = null; // no layered transprt
631 break;
632 }
633
Jens Geyer828ffa82020-11-21 15:15:32 +0100634 TProtocolFactory proto = param.protocol switch
Jens Geyer261cad32019-11-20 19:03:14 +0100635 {
Jens Geyer828ffa82020-11-21 15:15:32 +0100636 ProtocolChoice.Compact => new TCompactProtocol.Factory(),
637 ProtocolChoice.Json => new TJsonProtocol.Factory(),
638 ProtocolChoice.Binary => new TBinaryProtocol.Factory(),
639 _ => new TBinaryProtocol.Factory(),
640 };
Jens Geyer261cad32019-11-20 19:03:14 +0100641
642 // Processor
643 var testHandler = new TestHandlerAsync();
644 var testProcessor = new ThriftTest.AsyncProcessor(testHandler);
645 var processorFactory = new TSingletonProcessorFactory(testProcessor);
646
Jens Geyer63d114d2021-05-25 23:42:35 +0200647 var poolconfig = new TThreadPoolAsyncServer.Configuration(); // use platform defaults
648 TServer serverEngine = param.server switch
649 {
650 ServerChoice.Simple => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger),
651 ServerChoice.ThreadPool => new TThreadPoolAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, poolconfig, logger),
652 _ => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger)
653 };
Jens Geyer261cad32019-11-20 19:03:14 +0100654
655 //Server event handler
656 var serverEvents = new MyServerEventHandler();
657 serverEngine.SetEventHandler(serverEvents);
658
659 // Run it
Jens Geyer63d114d2021-05-25 23:42:35 +0200660 var where = (!string.IsNullOrEmpty(param.pipe)) ? "pipe " + param.pipe : "port " + param.port;
661 Console.WriteLine("Running "+ serverEngine.GetType().Name +
662 " at "+ where +
663 " using "+ processorFactory.GetType().Name + " processor prototype factory " +
Jens Geyer261cad32019-11-20 19:03:14 +0100664 (param.buffering == BufferChoice.Buffered ? " with buffered transport" : "") +
665 (param.buffering == BufferChoice.Framed ? " with framed transport" : "") +
666 (param.transport == TransportChoice.TlsSocket ? " with encryption" : "") +
667 (param.protocol == ProtocolChoice.Compact ? " with compact protocol" : "") +
668 (param.protocol == ProtocolChoice.Json ? " with json protocol" : "") +
669 "...");
Jens Geyer71569402021-11-13 23:51:16 +0100670 await serverEngine.ServeAsync(CancellationToken.None);
Jens Geyer261cad32019-11-20 19:03:14 +0100671 Console.ReadLine();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100672 }
Jens Geyer261cad32019-11-20 19:03:14 +0100673 catch (Exception x)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100674 {
Jens Geyer261cad32019-11-20 19:03:14 +0100675 Console.Error.Write(x);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100676 return 1;
677 }
678
Jens Geyer261cad32019-11-20 19:03:14 +0100679 Console.WriteLine("done.");
680 return 0;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100681 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100682 }
683 }
684
685}