blob: 2dea4182477bbb0c989c01205f96bc1f96f6f6d0 [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 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 {
Jens Geyer4115e952023-11-21 23:00:01 +010086 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 {
Jens Geyer4115e952023-11-21 23:00:01 +010091 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 Geyer4115e952023-11-21 23:00:01 +0100166 private static int _clientID = -1; // use with Interlocked only!
167 public static int ClientID => Interlocked.Add(ref _clientID, 0);
Jens Geyeref0cb012021-04-02 12:18:15 +0200168
Jens Geyer98a23252022-01-09 16:50:52 +0100169 private static readonly TConfiguration Configuration = new();
Jens Geyereacd1d42019-11-20 19:03:14 +0100170
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100171 public delegate void TestLogDelegate(string msg, params object[] values);
172
173 public class MyServerEventHandler : TServerEventHandler
174 {
175 public int callCount = 0;
176
177 public Task PreServeAsync(CancellationToken cancellationToken)
178 {
179 callCount++;
180 return Task.CompletedTask;
181 }
182
Jens Geyer4115e952023-11-21 23:00:01 +0100183 public Task<object?> CreateContextAsync(TProtocol input, TProtocol output, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100184 {
185 callCount++;
Jens Geyer4115e952023-11-21 23:00:01 +0100186 return Task.FromResult<object?>(null);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100187 }
188
189 public Task DeleteContextAsync(object serverContext, TProtocol input, TProtocol output, CancellationToken cancellationToken)
190 {
191 callCount++;
192 return Task.CompletedTask;
193 }
194
195 public Task ProcessContextAsync(object serverContext, TTransport transport, CancellationToken cancellationToken)
196 {
197 callCount++;
198 return Task.CompletedTask;
199 }
200 }
201
202 public class TestHandlerAsync : ThriftTest.IAsync
203 {
Jens Geyer98a23252022-01-09 16:50:52 +0100204 //public TServer Server { get; set; }
Jens Geyer261cad32019-11-20 19:03:14 +0100205 private readonly int handlerID;
Jens Geyer261cad32019-11-20 19:03:14 +0100206 private readonly TestLogDelegate logger;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100207
208 public TestHandlerAsync()
209 {
210 handlerID = Interlocked.Increment(ref _clientID);
Jens Geyer261cad32019-11-20 19:03:14 +0100211 logger += TestConsoleLogger;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100212 logger.Invoke("New TestHandler instance created");
213 }
214
Jens Geyer261cad32019-11-20 19:03:14 +0100215 public void TestConsoleLogger(string msg, params object[] values)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100216 {
Jens Geyer60970c42022-09-09 13:39:33 +0200217 var sb = new StringBuilder();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100218 sb.AppendFormat("handler{0:D3}:", handlerID);
219 sb.AppendFormat(msg, values);
220 sb.AppendLine();
Jens Geyer60970c42022-09-09 13:39:33 +0200221 lock (typeof(Console))
222 Console.Write(sb.ToString());
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100223 }
224
Jens Geyer2b2ea622021-04-09 22:55:11 +0200225 public Task testVoid(CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100226 {
227 logger.Invoke("testVoid()");
228 return Task.CompletedTask;
229 }
230
Jens Geyer3cac3202022-01-31 18:04:35 +0100231 public Task<string> testString(string? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100232 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100233 logger.Invoke("testString({0})", thing ?? "<null>");
234 return Task.FromResult(thing ?? string.Empty);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100235 }
236
Jens Geyer2b2ea622021-04-09 22:55:11 +0200237 public Task<bool> testBool(bool thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100238 {
239 logger.Invoke("testBool({0})", thing);
240 return Task.FromResult(thing);
241 }
242
Jens Geyer2b2ea622021-04-09 22:55:11 +0200243 public Task<sbyte> testByte(sbyte thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100244 {
245 logger.Invoke("testByte({0})", thing);
246 return Task.FromResult(thing);
247 }
248
Jens Geyer2b2ea622021-04-09 22:55:11 +0200249 public Task<int> testI32(int thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100250 {
251 logger.Invoke("testI32({0})", thing);
252 return Task.FromResult(thing);
253 }
254
Jens Geyer2b2ea622021-04-09 22:55:11 +0200255 public Task<long> testI64(long thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100256 {
257 logger.Invoke("testI64({0})", thing);
258 return Task.FromResult(thing);
259 }
260
Jens Geyer2b2ea622021-04-09 22:55:11 +0200261 public Task<double> testDouble(double thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100262 {
263 logger.Invoke("testDouble({0})", thing);
264 return Task.FromResult(thing);
265 }
266
Jens Geyer3cac3202022-01-31 18:04:35 +0100267 public Task<byte[]> testBinary(byte[]? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100268 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100269 logger.Invoke("testBinary({0} bytes)", thing?.Length ?? 0);
Jens Geyer4115e952023-11-21 23:00:01 +0100270 return Task.FromResult(thing ?? []);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100271 }
272
Jens Geyer62445c12022-06-29 00:00:00 +0200273 public Task<Guid> testUuid(Guid thing, CancellationToken cancellationToken)
274 {
275 logger.Invoke("testUuid({0})", thing.ToString("B"));
276 return Task.FromResult(thing);
277 }
278
Jens Geyer3cac3202022-01-31 18:04:35 +0100279 public Task<Xtruct> testStruct(Xtruct? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100280 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100281 logger.Invoke("testStruct({{\"{0}\", {1}, {2}, {3}}})", thing?.String_thing ?? "<null>", thing?.Byte_thing ?? 0, thing?.I32_thing ?? 0, thing?.I64_thing ?? 0);
282 return Task.FromResult(thing ?? new Xtruct()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100283 }
284
Jens Geyer3cac3202022-01-31 18:04:35 +0100285 public Task<Xtruct2> testNest(Xtruct2? nest, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100286 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100287 var thing = nest?.Struct_thing;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100288 logger.Invoke("testNest({{{0}, {{\"{1}\", {2}, {3}, {4}, {5}}}}})",
Jens Geyer3cac3202022-01-31 18:04:35 +0100289 nest?.Byte_thing ?? 0,
290 thing?.String_thing ?? "<null>",
291 thing?.Byte_thing ?? 0,
292 thing?.I32_thing ?? 0,
293 thing?.I64_thing ?? 0,
294 nest?.I32_thing ?? 0);
295 return Task.FromResult(nest ?? new Xtruct2()); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100296 }
297
Jens Geyer3cac3202022-01-31 18:04:35 +0100298 public Task<Dictionary<int, int>> testMap(Dictionary<int, int>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100299 {
Jens Geyer60970c42022-09-09 13:39:33 +0200300 var sb = new StringBuilder();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100301 sb.Append("testMap({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100302 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100303 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100304 var first = true;
305 foreach (var key in thing.Keys)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100306 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100307 if (first)
308 {
309 first = false;
310 }
311 else
312 {
313 sb.Append(", ");
314 }
315 sb.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100316 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100317 }
318 sb.Append("}})");
319 logger.Invoke(sb.ToString());
Jens Geyer4115e952023-11-21 23:00:01 +0100320 return Task.FromResult(thing ?? []); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100321 }
322
Jens Geyer3cac3202022-01-31 18:04:35 +0100323 public Task<Dictionary<string, string>> testStringMap(Dictionary<string, string>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100324 {
Jens Geyer60970c42022-09-09 13:39:33 +0200325 var sb = new StringBuilder();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100326 sb.Append("testStringMap({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100327 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100328 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100329 var first = true;
330 foreach (var key in thing.Keys)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100331 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100332 if (first)
333 {
334 first = false;
335 }
336 else
337 {
338 sb.Append(", ");
339 }
340 sb.AppendFormat("{0} => {1}", key, thing[key]);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100341 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100342 }
343 sb.Append("}})");
344 logger.Invoke(sb.ToString());
Jens Geyer4115e952023-11-21 23:00:01 +0100345 return Task.FromResult(thing ?? []); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100346 }
347
Jens Geyer3cac3202022-01-31 18:04:35 +0100348 public Task<HashSet<int>> testSet(HashSet<int>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100349 {
Jens Geyer60970c42022-09-09 13:39:33 +0200350 var sb = new StringBuilder();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100351 sb.Append("testSet({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100352 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100353 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100354 var first = true;
355 foreach (int elem in thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100356 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100357 if (first)
358 {
359 first = false;
360 }
361 else
362 {
363 sb.Append(", ");
364 }
365 sb.AppendFormat("{0}", elem);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100366 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100367 }
368 sb.Append("}})");
369 logger.Invoke(sb.ToString());
Jens Geyer4115e952023-11-21 23:00:01 +0100370 return Task.FromResult(thing ?? []); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100371 }
372
Jens Geyer3cac3202022-01-31 18:04:35 +0100373 public Task<List<int>> testList(List<int>? thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100374 {
Jens Geyer60970c42022-09-09 13:39:33 +0200375 var sb = new StringBuilder();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100376 sb.Append("testList({{");
Jens Geyer3cac3202022-01-31 18:04:35 +0100377 if (thing != null)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100378 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100379 var first = true;
380 foreach (var elem in thing)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100381 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100382 if (first)
383 {
384 first = false;
385 }
386 else
387 {
388 sb.Append(", ");
389 }
390 sb.AppendFormat("{0}", elem);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100391 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100392 }
393 sb.Append("}})");
394 logger.Invoke(sb.ToString());
Jens Geyer4115e952023-11-21 23:00:01 +0100395 return Task.FromResult(thing ?? []); // null returns are not allowed in Thrift
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100396 }
397
Jens Geyer2b2ea622021-04-09 22:55:11 +0200398 public Task<Numberz> testEnum(Numberz thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100399 {
400 logger.Invoke("testEnum({0})", thing);
401 return Task.FromResult(thing);
402 }
403
Jens Geyer2b2ea622021-04-09 22:55:11 +0200404 public Task<long> testTypedef(long thing, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100405 {
406 logger.Invoke("testTypedef({0})", thing);
407 return Task.FromResult(thing);
408 }
409
Jens Geyer2b2ea622021-04-09 22:55:11 +0200410 public Task<Dictionary<int, Dictionary<int, int>>> testMapMap(int hello, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100411 {
412 logger.Invoke("testMapMap({0})", hello);
413 var mapmap = new Dictionary<int, Dictionary<int, int>>();
414
415 var pos = new Dictionary<int, int>();
416 var neg = new Dictionary<int, int>();
417 for (var i = 1; i < 5; i++)
418 {
419 pos[i] = i;
420 neg[-i] = -i;
421 }
422
423 mapmap[4] = pos;
424 mapmap[-4] = neg;
425
426 return Task.FromResult(mapmap);
427 }
428
Jens Geyer3cac3202022-01-31 18:04:35 +0100429 public Task<Dictionary<long, Dictionary<Numberz, Insanity>>> testInsanity(Insanity? argument, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100430 {
431 logger.Invoke("testInsanity()");
432
433 /** from ThriftTest.thrift:
434 * So you think you've got this all worked, out eh?
435 *
436 * Creates a the returned map with these values and prints it out:
437 * { 1 => { 2 => argument,
438 * 3 => argument,
439 * },
440 * 2 => { 6 => <empty Insanity struct>, },
441 * }
442 * @return map<UserId, map<Numberz,Insanity>> - a map with the above values
443 */
444
445 var first_map = new Dictionary<Numberz, Insanity>();
446 var second_map = new Dictionary<Numberz, Insanity>(); ;
447
Jens Geyer3cac3202022-01-31 18:04:35 +0100448 // null dict keys/values are not allowed in Thrift
449 first_map[Numberz.TWO] = argument ?? new Insanity();
450 first_map[Numberz.THREE] = argument ?? new Insanity();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100451
452 second_map[Numberz.SIX] = new Insanity();
453
454 var insane = new Dictionary<long, Dictionary<Numberz, Insanity>>
455 {
456 [1] = first_map,
457 [2] = second_map
458 };
459
460 return Task.FromResult(insane);
461 }
462
Jens Geyer3cac3202022-01-31 18:04:35 +0100463 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 +0100464 CancellationToken cancellationToken)
465 {
466 logger.Invoke("testMulti()");
467
468 var hello = new Xtruct(); ;
Jens Geyerffb97e12019-12-06 23:43:08 +0100469 hello.String_thing = "Hello2";
470 hello.Byte_thing = arg0;
471 hello.I32_thing = arg1;
472 hello.I64_thing = arg2;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100473 return Task.FromResult(hello);
474 }
475
Jens Geyer3cac3202022-01-31 18:04:35 +0100476 public Task testException(string? arg, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100477 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100478 logger.Invoke("testException({0})", arg ?? "<null>");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100479 if (arg == "Xception")
480 {
481 var x = new Xception
482 {
483 ErrorCode = 1001,
484 Message = arg
485 };
486 throw x;
487 }
488 if (arg == "TException")
489 {
490 throw new TException();
491 }
492 return Task.CompletedTask;
493 }
494
Jens Geyer3cac3202022-01-31 18:04:35 +0100495 public Task<Xtruct> testMultiException(string? arg0, string? arg1, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100496 {
Jens Geyer3cac3202022-01-31 18:04:35 +0100497 logger.Invoke("testMultiException({0}, {1})", arg0 ?? "<null>", arg1 ?? "<null>");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100498 if (arg0 == "Xception")
499 {
500 var x = new Xception
501 {
502 ErrorCode = 1001,
503 Message = "This is an Xception"
504 };
505 throw x;
506 }
507
508 if (arg0 == "Xception2")
509 {
510 var x = new Xception2
511 {
512 ErrorCode = 2002,
Jens Geyerffb97e12019-12-06 23:43:08 +0100513 Struct_thing = new Xtruct { String_thing = "This is an Xception2" }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100514 };
515 throw x;
516 }
517
Jens Geyerffb97e12019-12-06 23:43:08 +0100518 var result = new Xtruct { String_thing = arg1 };
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100519 return Task.FromResult(result);
520 }
521
Jens Geyer71569402021-11-13 23:51:16 +0100522 public async Task testOneway(int secondsToSleep, CancellationToken cancellationToken)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100523 {
524 logger.Invoke("testOneway({0}), sleeping...", secondsToSleep);
Jens Geyer71569402021-11-13 23:51:16 +0100525 await Task.Delay(secondsToSleep * 1000, cancellationToken);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100526 logger.Invoke("testOneway finished");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100527 }
528 }
529
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100530
531 private static X509Certificate2 GetServerCert()
532 {
533 var serverCertName = "server.p12";
534 var possiblePaths = new List<string>
535 {
536 "../../../keys/",
537 "../../keys/",
538 "../keys/",
539 "keys/",
540 };
541
Jens Geyer98a23252022-01-09 16:50:52 +0100542 var existingPath = string.Empty;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100543 foreach (var possiblePath in possiblePaths)
544 {
545 var path = Path.GetFullPath(possiblePath + serverCertName);
546 if (File.Exists(path))
547 {
548 existingPath = path;
549 break;
550 }
551 }
552
553 if (string.IsNullOrEmpty(existingPath))
554 {
555 throw new FileNotFoundException($"Cannot find file: {serverCertName}");
556 }
557
558 var cert = new X509Certificate2(existingPath, "thrift");
559
560 return cert;
561 }
562
Jens Geyer71569402021-11-13 23:51:16 +0100563 public static async Task<int> Execute(List<string> args)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100564 {
Jens Geyer261cad32019-11-20 19:03:14 +0100565 using (var loggerFactory = new LoggerFactory()) //.AddConsole().AddDebug();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100566 {
Jens Geyer261cad32019-11-20 19:03:14 +0100567 var logger = loggerFactory.CreateLogger("Test");
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100568
569 try
570 {
Jens Geyer261cad32019-11-20 19:03:14 +0100571 var param = new ServerParam();
572
573 try
574 {
575 param.Parse(args);
576 }
577 catch (Exception ex)
578 {
579 Console.WriteLine("*** FAILED ***");
580 Console.WriteLine("Error while parsing arguments");
Jens Geyer5e37d572022-09-08 23:07:11 +0200581 Console.WriteLine("{0} {1}\nStack:\n{2}", ex.GetType().Name, ex.Message, ex.StackTrace);
Jens Geyer261cad32019-11-20 19:03:14 +0100582 return 1;
583 }
584
585
586 // Endpoint transport (mandatory)
587 TServerTransport trans;
588 switch (param.transport)
589 {
590 case TransportChoice.NamedPipe:
591 Debug.Assert(param.pipe != null);
Jens Geyer60970c42022-09-09 13:39:33 +0200592 var numListen = (param.server == ServerChoice.Simple) ? 1 : 16;
593 trans = new TNamedPipeServerTransport(param.pipe, Configuration, NamedPipeServerFlags.OnlyLocalClients, numListen);
Jens Geyer261cad32019-11-20 19:03:14 +0100594 break;
595
596
597 case TransportChoice.TlsSocket:
598 var cert = GetServerCert();
599 if (cert == null || !cert.HasPrivateKey)
600 {
601 cert?.Dispose();
602 throw new InvalidOperationException("Certificate doesn't contain private key");
603 }
604
Jens Geyereacd1d42019-11-20 19:03:14 +0100605 trans = new TTlsServerSocketTransport(param.port, Configuration,
606 cert,
Jens Geyer261cad32019-11-20 19:03:14 +0100607 (sender, certificate, chain, errors) => true,
Jens Geyera06eedc2023-11-16 23:23:04 +0100608 null);
Jens Geyer261cad32019-11-20 19:03:14 +0100609 break;
610
611 case TransportChoice.Socket:
612 default:
Jens Geyereacd1d42019-11-20 19:03:14 +0100613 trans = new TServerSocketTransport(param.port, Configuration);
Jens Geyer261cad32019-11-20 19:03:14 +0100614 break;
615 }
616
617 // Layered transport (mandatory)
Jens Geyer98a23252022-01-09 16:50:52 +0100618 TTransportFactory? transFactory;
Jens Geyer261cad32019-11-20 19:03:14 +0100619 switch (param.buffering)
620 {
621 case BufferChoice.Framed:
622 transFactory = new TFramedTransport.Factory();
623 break;
624 case BufferChoice.Buffered:
625 transFactory = new TBufferedTransport.Factory();
626 break;
627 default:
628 Debug.Assert(param.buffering == BufferChoice.None, "unhandled case");
629 transFactory = null; // no layered transprt
630 break;
631 }
632
Jens Geyer828ffa82020-11-21 15:15:32 +0100633 TProtocolFactory proto = param.protocol switch
Jens Geyer261cad32019-11-20 19:03:14 +0100634 {
Jens Geyer828ffa82020-11-21 15:15:32 +0100635 ProtocolChoice.Compact => new TCompactProtocol.Factory(),
636 ProtocolChoice.Json => new TJsonProtocol.Factory(),
637 ProtocolChoice.Binary => new TBinaryProtocol.Factory(),
638 _ => new TBinaryProtocol.Factory(),
639 };
Jens Geyer261cad32019-11-20 19:03:14 +0100640
641 // Processor
642 var testHandler = new TestHandlerAsync();
643 var testProcessor = new ThriftTest.AsyncProcessor(testHandler);
644 var processorFactory = new TSingletonProcessorFactory(testProcessor);
645
Jens Geyer63d114d2021-05-25 23:42:35 +0200646 var poolconfig = new TThreadPoolAsyncServer.Configuration(); // use platform defaults
647 TServer serverEngine = param.server switch
648 {
649 ServerChoice.Simple => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger),
650 ServerChoice.ThreadPool => new TThreadPoolAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, poolconfig, logger),
651 _ => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger)
652 };
Jens Geyer261cad32019-11-20 19:03:14 +0100653
654 //Server event handler
655 var serverEvents = new MyServerEventHandler();
656 serverEngine.SetEventHandler(serverEvents);
657
658 // Run it
Jens Geyer63d114d2021-05-25 23:42:35 +0200659 var where = (!string.IsNullOrEmpty(param.pipe)) ? "pipe " + param.pipe : "port " + param.port;
660 Console.WriteLine("Running "+ serverEngine.GetType().Name +
661 " at "+ where +
662 " using "+ processorFactory.GetType().Name + " processor prototype factory " +
Jens Geyer261cad32019-11-20 19:03:14 +0100663 (param.buffering == BufferChoice.Buffered ? " with buffered transport" : "") +
664 (param.buffering == BufferChoice.Framed ? " with framed transport" : "") +
665 (param.transport == TransportChoice.TlsSocket ? " with encryption" : "") +
666 (param.protocol == ProtocolChoice.Compact ? " with compact protocol" : "") +
667 (param.protocol == ProtocolChoice.Json ? " with json protocol" : "") +
668 "...");
Jens Geyer71569402021-11-13 23:51:16 +0100669 await serverEngine.ServeAsync(CancellationToken.None);
Jens Geyer261cad32019-11-20 19:03:14 +0100670 Console.ReadLine();
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100671 }
Jens Geyer261cad32019-11-20 19:03:14 +0100672 catch (Exception x)
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100673 {
Jens Geyer261cad32019-11-20 19:03:14 +0100674 Console.Error.Write(x);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100675 return 1;
676 }
677
Jens Geyer261cad32019-11-20 19:03:14 +0100678 Console.WriteLine("done.");
679 return 0;
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100680 }
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100681 }
682 }
683
684}