blob: 54654c85967349c5daac9f5e8c150f9eb57f4d38 [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;
Jens Geyer4115e952023-11-21 23:00:01 +010023using System.Reflection.Metadata.Ecma335;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010024using System.Security.Authentication;
25using System.Security.Cryptography.X509Certificates;
26using System.Text;
27using System.Threading;
28using System.Threading.Tasks;
29using Microsoft.Extensions.Logging;
30using Thrift;
31using Thrift.Collections;
32using Thrift.Processor;
33using Thrift.Protocol;
34using Thrift.Server;
35using Thrift.Transport;
36using Thrift.Transport.Server;
37
Jens Geyer828ffa82020-11-21 15:15:32 +010038#pragma warning disable IDE0063 // using can be simplified, we don't
Jens Geyer2b2ea622021-04-09 22:55:11 +020039#pragma warning disable IDE0057 // substr can be simplified, we don't
Jens Geyere26b4a82024-11-12 23:53:04 +010040#pragma warning disable IDE0130 // unexpected folder structure
Jens Geyer828ffa82020-11-21 15:15:32 +010041
Jens Geyeraa0c8b32019-01-28 23:27:45 +010042namespace ThriftTest
43{
Jens Geyeradde44b2019-02-05 01:00:02 +010044 internal enum ProtocolChoice
45 {
46 Binary,
47 Compact,
48 Json
49 }
50
Jens Geyeradde44b2019-02-05 01:00:02 +010051 internal enum TransportChoice
52 {
53 Socket,
54 TlsSocket,
55 NamedPipe
56 }
57
Kyle Smith7b94dd42019-03-23 17:26:56 +010058 internal enum BufferChoice
59 {
60 None,
61 Buffered,
62 Framed
63 }
64
Jens Geyer63d114d2021-05-25 23:42:35 +020065 internal enum ServerChoice
66 {
67 Simple,
68 ThreadPool
69 }
70
71
Jens Geyeraa0c8b32019-01-28 23:27:45 +010072 internal class ServerParam
73 {
Kyle Smith7b94dd42019-03-23 17:26:56 +010074 internal BufferChoice buffering = BufferChoice.None;
Jens Geyeradde44b2019-02-05 01:00:02 +010075 internal ProtocolChoice protocol = ProtocolChoice.Binary;
76 internal TransportChoice transport = TransportChoice.Socket;
Jens Geyer63d114d2021-05-25 23:42:35 +020077 internal ServerChoice server = ServerChoice.Simple;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010078 internal int port = 9090;
Jens Geyer98a23252022-01-09 16:50:52 +010079 internal string pipe = string.Empty;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010080
81 internal void Parse(List<string> args)
82 {
83 for (var i = 0; i < args.Count; i++)
84 {
85 if (args[i].StartsWith("--pipe="))
86 {
Jens Geyer4115e952023-11-21 23:00:01 +010087 pipe = args[i].Substring(args[i].IndexOf('=') + 1);
Jens Geyeradde44b2019-02-05 01:00:02 +010088 transport = TransportChoice.NamedPipe;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010089 }
90 else if (args[i].StartsWith("--port="))
91 {
Jens Geyer4115e952023-11-21 23:00:01 +010092 port = int.Parse(args[i].Substring(args[i].IndexOf('=') + 1));
Jens Geyeradde44b2019-02-05 01:00:02 +010093 if(transport != TransportChoice.TlsSocket)
94 transport = TransportChoice.Socket;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010095 }
96 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
97 {
Kyle Smith7b94dd42019-03-23 17:26:56 +010098 buffering = BufferChoice.Buffered;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010099 }
100 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
101 {
Kyle Smith7b94dd42019-03-23 17:26:56 +0100102 buffering = BufferChoice.Framed;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100103 }
104 else if (args[i] == "--binary" || args[i] == "--protocol=binary")
105 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100106 protocol = ProtocolChoice.Binary;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100107 }
108 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
109 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100110 protocol = ProtocolChoice.Compact;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100111 }
112 else if (args[i] == "--json" || args[i] == "--protocol=json")
113 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100114 protocol = ProtocolChoice.Json;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100115 }
Jens Geyer63d114d2021-05-25 23:42:35 +0200116 else if (args[i] == "--server-type=simple")
117 {
118 server = ServerChoice.Simple;
119 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100120 else if (args[i] == "--threaded" || args[i] == "--server-type=threaded")
121 {
122 throw new NotImplementedException(args[i]);
123 }
124 else if (args[i] == "--threadpool" || args[i] == "--server-type=threadpool")
125 {
Jens Geyer63d114d2021-05-25 23:42:35 +0200126 server = ServerChoice.ThreadPool;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100127 }
128 else if (args[i] == "--prototype" || args[i] == "--processor=prototype")
129 {
130 throw new NotImplementedException(args[i]);
131 }
132 else if (args[i] == "--ssl")
133 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100134 transport = TransportChoice.TlsSocket;
135 }
136 else if (args[i] == "--help")
137 {
138 PrintOptionsHelp();
139 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100140 }
141 else
142 {
Jens Geyeradde44b2019-02-05 01:00:02 +0100143 Console.WriteLine("Invalid argument: {0}", args[i]);
144 PrintOptionsHelp();
145 return;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100146 }
147 }
148
149 }
Jens Geyeradde44b2019-02-05 01:00:02 +0100150
151 internal static void PrintOptionsHelp()
152 {
153 Console.WriteLine("Server options:");
154 Console.WriteLine(" --pipe=<pipe name>");
155 Console.WriteLine(" --port=<port number>");
156 Console.WriteLine(" --transport=<transport name> one of buffered,framed (defaults to none)");
157 Console.WriteLine(" --protocol=<protocol name> one of compact,json (defaults to binary)");
158 Console.WriteLine(" --server-type=<type> one of threaded,threadpool (defaults to simple)");
159 Console.WriteLine(" --processor=<prototype>");
160 Console.WriteLine(" --ssl");
161 Console.WriteLine();
162 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100163 }
164
165 public class TestServer
166 {
Jens Geyer4115e952023-11-21 23:00:01 +0100167 private static int _clientID = -1; // use with Interlocked only!
168 public static int ClientID => Interlocked.Add(ref _clientID, 0);
Jens Geyeref0cb012021-04-02 12:18:15 +0200169
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 Geyer4115e952023-11-21 23:00:01 +0100184 public Task<object?> CreateContextAsync(TProtocol input, TProtocol output, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100185 {
186 callCount++;
Jens Geyer4115e952023-11-21 23:00:01 +0100187 return Task.FromResult<object?>(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);
Jens Geyer4115e952023-11-21 23:00:01 +0100271 return Task.FromResult(thing ?? []);
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 Geyer4115e952023-11-21 23:00:01 +0100321 return Task.FromResult(thing ?? []); // 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 Geyer4115e952023-11-21 23:00:01 +0100346 return Task.FromResult(thing ?? []); // 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 Geyer4115e952023-11-21 23:00:01 +0100371 return Task.FromResult(thing ?? []); // 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 Geyer4115e952023-11-21 23:00:01 +0100396 return Task.FromResult(thing ?? []); // 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 }
Jens Geyere26b4a82024-11-12 23:53:04 +0100558
559 //var cert = new X509Certificate2(existingPath, "thrift");
560 var cert = X509CertificateLoader.LoadPkcs12FromFile(existingPath, "thrift");
561
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100562 return cert;
563 }
564
Jens Geyer71569402021-11-13 23:51:16 +0100565 public static async Task<int> Execute(List<string> args)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100566 {
Jens Geyer261cad32019-11-20 19:03:14 +0100567 using (var loggerFactory = new LoggerFactory()) //.AddConsole().AddDebug();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100568 {
Jens Geyer261cad32019-11-20 19:03:14 +0100569 var logger = loggerFactory.CreateLogger("Test");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100570
571 try
572 {
Jens Geyer261cad32019-11-20 19:03:14 +0100573 var param = new ServerParam();
574
575 try
576 {
577 param.Parse(args);
578 }
579 catch (Exception ex)
580 {
581 Console.WriteLine("*** FAILED ***");
582 Console.WriteLine("Error while parsing arguments");
Jens Geyer5e37d572022-09-08 23:07:11 +0200583 Console.WriteLine("{0} {1}\nStack:\n{2}", ex.GetType().Name, ex.Message, ex.StackTrace);
Jens Geyer261cad32019-11-20 19:03:14 +0100584 return 1;
585 }
586
587
588 // Endpoint transport (mandatory)
589 TServerTransport trans;
590 switch (param.transport)
591 {
592 case TransportChoice.NamedPipe:
593 Debug.Assert(param.pipe != null);
Jens Geyer60970c42022-09-09 13:39:33 +0200594 var numListen = (param.server == ServerChoice.Simple) ? 1 : 16;
595 trans = new TNamedPipeServerTransport(param.pipe, Configuration, NamedPipeServerFlags.OnlyLocalClients, numListen);
Jens Geyer261cad32019-11-20 19:03:14 +0100596 break;
597
598
599 case TransportChoice.TlsSocket:
600 var cert = GetServerCert();
601 if (cert == null || !cert.HasPrivateKey)
602 {
603 cert?.Dispose();
604 throw new InvalidOperationException("Certificate doesn't contain private key");
605 }
606
Jens Geyereacd1d42019-11-20 19:03:14 +0100607 trans = new TTlsServerSocketTransport(param.port, Configuration,
608 cert,
Jens Geyer261cad32019-11-20 19:03:14 +0100609 (sender, certificate, chain, errors) => true,
Jens Geyera06eedc2023-11-16 23:23:04 +0100610 null);
Jens Geyer261cad32019-11-20 19:03:14 +0100611 break;
612
613 case TransportChoice.Socket:
614 default:
Jens Geyereacd1d42019-11-20 19:03:14 +0100615 trans = new TServerSocketTransport(param.port, Configuration);
Jens Geyer261cad32019-11-20 19:03:14 +0100616 break;
617 }
618
619 // Layered transport (mandatory)
Jens Geyer98a23252022-01-09 16:50:52 +0100620 TTransportFactory? transFactory;
Jens Geyer261cad32019-11-20 19:03:14 +0100621 switch (param.buffering)
622 {
623 case BufferChoice.Framed:
624 transFactory = new TFramedTransport.Factory();
625 break;
626 case BufferChoice.Buffered:
627 transFactory = new TBufferedTransport.Factory();
628 break;
629 default:
630 Debug.Assert(param.buffering == BufferChoice.None, "unhandled case");
631 transFactory = null; // no layered transprt
632 break;
633 }
634
Jens Geyer828ffa82020-11-21 15:15:32 +0100635 TProtocolFactory proto = param.protocol switch
Jens Geyer261cad32019-11-20 19:03:14 +0100636 {
Jens Geyer828ffa82020-11-21 15:15:32 +0100637 ProtocolChoice.Compact => new TCompactProtocol.Factory(),
638 ProtocolChoice.Json => new TJsonProtocol.Factory(),
639 ProtocolChoice.Binary => new TBinaryProtocol.Factory(),
640 _ => new TBinaryProtocol.Factory(),
641 };
Jens Geyer261cad32019-11-20 19:03:14 +0100642
643 // Processor
644 var testHandler = new TestHandlerAsync();
645 var testProcessor = new ThriftTest.AsyncProcessor(testHandler);
646 var processorFactory = new TSingletonProcessorFactory(testProcessor);
647
Jens Geyer63d114d2021-05-25 23:42:35 +0200648 var poolconfig = new TThreadPoolAsyncServer.Configuration(); // use platform defaults
649 TServer serverEngine = param.server switch
650 {
651 ServerChoice.Simple => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger),
652 ServerChoice.ThreadPool => new TThreadPoolAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, poolconfig, logger),
653 _ => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger)
654 };
Jens Geyer261cad32019-11-20 19:03:14 +0100655
656 //Server event handler
657 var serverEvents = new MyServerEventHandler();
658 serverEngine.SetEventHandler(serverEvents);
659
660 // Run it
Jens Geyer63d114d2021-05-25 23:42:35 +0200661 var where = (!string.IsNullOrEmpty(param.pipe)) ? "pipe " + param.pipe : "port " + param.port;
662 Console.WriteLine("Running "+ serverEngine.GetType().Name +
663 " at "+ where +
664 " using "+ processorFactory.GetType().Name + " processor prototype factory " +
Jens Geyer261cad32019-11-20 19:03:14 +0100665 (param.buffering == BufferChoice.Buffered ? " with buffered transport" : "") +
666 (param.buffering == BufferChoice.Framed ? " with framed transport" : "") +
667 (param.transport == TransportChoice.TlsSocket ? " with encryption" : "") +
668 (param.protocol == ProtocolChoice.Compact ? " with compact protocol" : "") +
669 (param.protocol == ProtocolChoice.Json ? " with json protocol" : "") +
670 "...");
Jens Geyer71569402021-11-13 23:51:16 +0100671 await serverEngine.ServeAsync(CancellationToken.None);
Jens Geyer261cad32019-11-20 19:03:14 +0100672 Console.ReadLine();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100673 }
Jens Geyer261cad32019-11-20 19:03:14 +0100674 catch (Exception x)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100675 {
Jens Geyer261cad32019-11-20 19:03:14 +0100676 Console.Error.Write(x);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100677 return 1;
678 }
679
Jens Geyer261cad32019-11-20 19:03:14 +0100680 Console.WriteLine("done.");
681 return 0;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100682 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100683 }
684 }
685
686}