Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 1 | // 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 | |
| 18 | using System; |
| 19 | using System.Collections.Generic; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 20 | using System.Diagnostics; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 21 | using System.IO; |
| 22 | using System.Linq; |
| 23 | using System.Security.Authentication; |
| 24 | using System.Security.Cryptography.X509Certificates; |
| 25 | using System.Text; |
| 26 | using System.Threading; |
| 27 | using System.Threading.Tasks; |
| 28 | using Microsoft.Extensions.Logging; |
| 29 | using Thrift; |
| 30 | using Thrift.Collections; |
| 31 | using Thrift.Processor; |
| 32 | using Thrift.Protocol; |
| 33 | using Thrift.Server; |
| 34 | using Thrift.Transport; |
| 35 | using Thrift.Transport.Server; |
| 36 | |
Jens Geyer | 828ffa8 | 2020-11-21 15:15:32 +0100 | [diff] [blame] | 37 | #pragma warning disable IDE0063 // using can be simplified, we don't |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 38 | #pragma warning disable IDE0057 // substr can be simplified, we don't |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame^] | 39 | #pragma warning disable CS1998 // await missing |
Jens Geyer | 828ffa8 | 2020-11-21 15:15:32 +0100 | [diff] [blame] | 40 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 41 | namespace ThriftTest |
| 42 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 43 | internal enum ProtocolChoice |
| 44 | { |
| 45 | Binary, |
| 46 | Compact, |
| 47 | Json |
| 48 | } |
| 49 | |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 50 | internal enum TransportChoice |
| 51 | { |
| 52 | Socket, |
| 53 | TlsSocket, |
| 54 | NamedPipe |
| 55 | } |
| 56 | |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 57 | internal enum BufferChoice |
| 58 | { |
| 59 | None, |
| 60 | Buffered, |
| 61 | Framed |
| 62 | } |
| 63 | |
Jens Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 64 | internal enum ServerChoice |
| 65 | { |
| 66 | Simple, |
| 67 | ThreadPool |
| 68 | } |
| 69 | |
| 70 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 71 | internal class ServerParam |
| 72 | { |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 73 | internal BufferChoice buffering = BufferChoice.None; |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 74 | internal ProtocolChoice protocol = ProtocolChoice.Binary; |
| 75 | internal TransportChoice transport = TransportChoice.Socket; |
Jens Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 76 | internal ServerChoice server = ServerChoice.Simple; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 77 | internal int port = 9090; |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame^] | 78 | internal string pipe = string.Empty; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 79 | |
| 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 Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 87 | transport = TransportChoice.NamedPipe; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 88 | } |
| 89 | else if (args[i].StartsWith("--port=")) |
| 90 | { |
| 91 | port = int.Parse(args[i].Substring(args[i].IndexOf("=") + 1)); |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 92 | if(transport != TransportChoice.TlsSocket) |
| 93 | transport = TransportChoice.Socket; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 94 | } |
| 95 | else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered") |
| 96 | { |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 97 | buffering = BufferChoice.Buffered; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 98 | } |
| 99 | else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed") |
| 100 | { |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 101 | buffering = BufferChoice.Framed; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 102 | } |
| 103 | else if (args[i] == "--binary" || args[i] == "--protocol=binary") |
| 104 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 105 | protocol = ProtocolChoice.Binary; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 106 | } |
| 107 | else if (args[i] == "--compact" || args[i] == "--protocol=compact") |
| 108 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 109 | protocol = ProtocolChoice.Compact; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 110 | } |
| 111 | else if (args[i] == "--json" || args[i] == "--protocol=json") |
| 112 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 113 | protocol = ProtocolChoice.Json; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 114 | } |
Jens Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 115 | else if (args[i] == "--server-type=simple") |
| 116 | { |
| 117 | server = ServerChoice.Simple; |
| 118 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 119 | 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 Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 125 | server = ServerChoice.ThreadPool; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 126 | } |
| 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 Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 133 | transport = TransportChoice.TlsSocket; |
| 134 | } |
| 135 | else if (args[i] == "--help") |
| 136 | { |
| 137 | PrintOptionsHelp(); |
| 138 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 139 | } |
| 140 | else |
| 141 | { |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 142 | Console.WriteLine("Invalid argument: {0}", args[i]); |
| 143 | PrintOptionsHelp(); |
| 144 | return; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | } |
Jens Geyer | adde44b | 2019-02-05 01:00:02 +0100 | [diff] [blame] | 149 | |
| 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 Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | public class TestServer |
| 165 | { |
Jens Geyer | ef0cb01 | 2021-04-02 12:18:15 +0200 | [diff] [blame] | 166 | #pragma warning disable CA2211 |
| 167 | public static int _clientID = -1; // use with Interlocked only! |
| 168 | #pragma warning restore CA2211 |
| 169 | |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame^] | 170 | private static readonly TConfiguration Configuration = new(); |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 171 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 172 | 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 Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame^] | 184 | public async Task<object?> CreateContextAsync(TProtocol input, TProtocol output, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 185 | { |
| 186 | callCount++; |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame^] | 187 | return null; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 188 | } |
| 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 Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame^] | 205 | //public TServer Server { get; set; } |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 206 | private readonly int handlerID; |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 207 | private readonly StringBuilder sb = new(); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 208 | private readonly TestLogDelegate logger; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 209 | |
| 210 | public TestHandlerAsync() |
| 211 | { |
| 212 | handlerID = Interlocked.Increment(ref _clientID); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 213 | logger += TestConsoleLogger; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 214 | logger.Invoke("New TestHandler instance created"); |
| 215 | } |
| 216 | |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 217 | public void TestConsoleLogger(string msg, params object[] values) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 218 | { |
| 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 Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 226 | public Task testVoid(CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 227 | { |
| 228 | logger.Invoke("testVoid()"); |
| 229 | return Task.CompletedTask; |
| 230 | } |
| 231 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 232 | public Task<string> testString(string thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 233 | { |
| 234 | logger.Invoke("testString({0})", thing); |
| 235 | return Task.FromResult(thing); |
| 236 | } |
| 237 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 238 | public Task<bool> testBool(bool thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 239 | { |
| 240 | logger.Invoke("testBool({0})", thing); |
| 241 | return Task.FromResult(thing); |
| 242 | } |
| 243 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 244 | public Task<sbyte> testByte(sbyte thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 245 | { |
| 246 | logger.Invoke("testByte({0})", thing); |
| 247 | return Task.FromResult(thing); |
| 248 | } |
| 249 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 250 | public Task<int> testI32(int thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 251 | { |
| 252 | logger.Invoke("testI32({0})", thing); |
| 253 | return Task.FromResult(thing); |
| 254 | } |
| 255 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 256 | public Task<long> testI64(long thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 257 | { |
| 258 | logger.Invoke("testI64({0})", thing); |
| 259 | return Task.FromResult(thing); |
| 260 | } |
| 261 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 262 | public Task<double> testDouble(double thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 263 | { |
| 264 | logger.Invoke("testDouble({0})", thing); |
| 265 | return Task.FromResult(thing); |
| 266 | } |
| 267 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 268 | public Task<byte[]> testBinary(byte[] thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 269 | { |
Jens Geyer | bd1a273 | 2019-06-26 22:52:44 +0200 | [diff] [blame] | 270 | logger.Invoke("testBinary({0} bytes)", thing.Length); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 271 | return Task.FromResult(thing); |
| 272 | } |
| 273 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 274 | public Task<Xtruct> testStruct(Xtruct thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 275 | { |
Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame] | 276 | logger.Invoke("testStruct({{\"{0}\", {1}, {2}, {3}}})", thing.String_thing, thing.Byte_thing, thing.I32_thing, thing.I64_thing); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 277 | return Task.FromResult(thing); |
| 278 | } |
| 279 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 280 | public Task<Xtruct2> testNest(Xtruct2 nest, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 281 | { |
Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame] | 282 | var thing = nest.Struct_thing; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 283 | logger.Invoke("testNest({{{0}, {{\"{1}\", {2}, {3}, {4}, {5}}}}})", |
Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame] | 284 | nest.Byte_thing, |
| 285 | thing.String_thing, |
| 286 | thing.Byte_thing, |
| 287 | thing.I32_thing, |
| 288 | thing.I64_thing, |
| 289 | nest.I32_thing); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 290 | return Task.FromResult(nest); |
| 291 | } |
| 292 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 293 | public Task<Dictionary<int, int>> testMap(Dictionary<int, int> thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 294 | { |
| 295 | sb.Clear(); |
| 296 | sb.Append("testMap({{"); |
| 297 | var first = true; |
| 298 | foreach (var key in thing.Keys) |
| 299 | { |
| 300 | if (first) |
| 301 | { |
| 302 | first = false; |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | sb.Append(", "); |
| 307 | } |
| 308 | sb.AppendFormat("{0} => {1}", key, thing[key]); |
| 309 | } |
| 310 | sb.Append("}})"); |
| 311 | logger.Invoke(sb.ToString()); |
| 312 | return Task.FromResult(thing); |
| 313 | } |
| 314 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 315 | public Task<Dictionary<string, string>> testStringMap(Dictionary<string, string> thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 316 | { |
| 317 | sb.Clear(); |
| 318 | sb.Append("testStringMap({{"); |
| 319 | var first = true; |
| 320 | foreach (var key in thing.Keys) |
| 321 | { |
| 322 | if (first) |
| 323 | { |
| 324 | first = false; |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | sb.Append(", "); |
| 329 | } |
| 330 | sb.AppendFormat("{0} => {1}", key, thing[key]); |
| 331 | } |
| 332 | sb.Append("}})"); |
| 333 | logger.Invoke(sb.ToString()); |
| 334 | return Task.FromResult(thing); |
| 335 | } |
| 336 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 337 | public Task<THashSet<int>> testSet(THashSet<int> thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 338 | { |
| 339 | sb.Clear(); |
| 340 | sb.Append("testSet({{"); |
| 341 | var first = true; |
| 342 | foreach (int elem in thing) |
| 343 | { |
| 344 | if (first) |
| 345 | { |
| 346 | first = false; |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | sb.Append(", "); |
| 351 | } |
| 352 | sb.AppendFormat("{0}", elem); |
| 353 | } |
| 354 | sb.Append("}})"); |
| 355 | logger.Invoke(sb.ToString()); |
| 356 | return Task.FromResult(thing); |
| 357 | } |
| 358 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 359 | public Task<List<int>> testList(List<int> thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 360 | { |
| 361 | sb.Clear(); |
| 362 | sb.Append("testList({{"); |
| 363 | var first = true; |
| 364 | foreach (var elem in thing) |
| 365 | { |
| 366 | if (first) |
| 367 | { |
| 368 | first = false; |
| 369 | } |
| 370 | else |
| 371 | { |
| 372 | sb.Append(", "); |
| 373 | } |
| 374 | sb.AppendFormat("{0}", elem); |
| 375 | } |
| 376 | sb.Append("}})"); |
| 377 | logger.Invoke(sb.ToString()); |
| 378 | return Task.FromResult(thing); |
| 379 | } |
| 380 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 381 | public Task<Numberz> testEnum(Numberz thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 382 | { |
| 383 | logger.Invoke("testEnum({0})", thing); |
| 384 | return Task.FromResult(thing); |
| 385 | } |
| 386 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 387 | public Task<long> testTypedef(long thing, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 388 | { |
| 389 | logger.Invoke("testTypedef({0})", thing); |
| 390 | return Task.FromResult(thing); |
| 391 | } |
| 392 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 393 | public Task<Dictionary<int, Dictionary<int, int>>> testMapMap(int hello, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 394 | { |
| 395 | logger.Invoke("testMapMap({0})", hello); |
| 396 | var mapmap = new Dictionary<int, Dictionary<int, int>>(); |
| 397 | |
| 398 | var pos = new Dictionary<int, int>(); |
| 399 | var neg = new Dictionary<int, int>(); |
| 400 | for (var i = 1; i < 5; i++) |
| 401 | { |
| 402 | pos[i] = i; |
| 403 | neg[-i] = -i; |
| 404 | } |
| 405 | |
| 406 | mapmap[4] = pos; |
| 407 | mapmap[-4] = neg; |
| 408 | |
| 409 | return Task.FromResult(mapmap); |
| 410 | } |
| 411 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 412 | public Task<Dictionary<long, Dictionary<Numberz, Insanity>>> testInsanity(Insanity argument, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 413 | { |
| 414 | logger.Invoke("testInsanity()"); |
| 415 | |
| 416 | /** from ThriftTest.thrift: |
| 417 | * So you think you've got this all worked, out eh? |
| 418 | * |
| 419 | * Creates a the returned map with these values and prints it out: |
| 420 | * { 1 => { 2 => argument, |
| 421 | * 3 => argument, |
| 422 | * }, |
| 423 | * 2 => { 6 => <empty Insanity struct>, }, |
| 424 | * } |
| 425 | * @return map<UserId, map<Numberz,Insanity>> - a map with the above values |
| 426 | */ |
| 427 | |
| 428 | var first_map = new Dictionary<Numberz, Insanity>(); |
| 429 | var second_map = new Dictionary<Numberz, Insanity>(); ; |
| 430 | |
| 431 | first_map[Numberz.TWO] = argument; |
| 432 | first_map[Numberz.THREE] = argument; |
| 433 | |
| 434 | second_map[Numberz.SIX] = new Insanity(); |
| 435 | |
| 436 | var insane = new Dictionary<long, Dictionary<Numberz, Insanity>> |
| 437 | { |
| 438 | [1] = first_map, |
| 439 | [2] = second_map |
| 440 | }; |
| 441 | |
| 442 | return Task.FromResult(insane); |
| 443 | } |
| 444 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 445 | public Task<Xtruct> testMulti(sbyte arg0, int arg1, long arg2, Dictionary<short, string> arg3, Numberz arg4, long arg5, |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 446 | CancellationToken cancellationToken) |
| 447 | { |
| 448 | logger.Invoke("testMulti()"); |
| 449 | |
| 450 | var hello = new Xtruct(); ; |
Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame] | 451 | hello.String_thing = "Hello2"; |
| 452 | hello.Byte_thing = arg0; |
| 453 | hello.I32_thing = arg1; |
| 454 | hello.I64_thing = arg2; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 455 | return Task.FromResult(hello); |
| 456 | } |
| 457 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 458 | public Task testException(string arg, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 459 | { |
| 460 | logger.Invoke("testException({0})", arg); |
| 461 | if (arg == "Xception") |
| 462 | { |
| 463 | var x = new Xception |
| 464 | { |
| 465 | ErrorCode = 1001, |
| 466 | Message = arg |
| 467 | }; |
| 468 | throw x; |
| 469 | } |
| 470 | if (arg == "TException") |
| 471 | { |
| 472 | throw new TException(); |
| 473 | } |
| 474 | return Task.CompletedTask; |
| 475 | } |
| 476 | |
Jens Geyer | 2b2ea62 | 2021-04-09 22:55:11 +0200 | [diff] [blame] | 477 | public Task<Xtruct> testMultiException(string arg0, string arg1, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 478 | { |
| 479 | logger.Invoke("testMultiException({0}, {1})", arg0, arg1); |
| 480 | if (arg0 == "Xception") |
| 481 | { |
| 482 | var x = new Xception |
| 483 | { |
| 484 | ErrorCode = 1001, |
| 485 | Message = "This is an Xception" |
| 486 | }; |
| 487 | throw x; |
| 488 | } |
| 489 | |
| 490 | if (arg0 == "Xception2") |
| 491 | { |
| 492 | var x = new Xception2 |
| 493 | { |
| 494 | ErrorCode = 2002, |
Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame] | 495 | Struct_thing = new Xtruct { String_thing = "This is an Xception2" } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 496 | }; |
| 497 | throw x; |
| 498 | } |
| 499 | |
Jens Geyer | ffb97e1 | 2019-12-06 23:43:08 +0100 | [diff] [blame] | 500 | var result = new Xtruct { String_thing = arg1 }; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 501 | return Task.FromResult(result); |
| 502 | } |
| 503 | |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 504 | public async Task testOneway(int secondsToSleep, CancellationToken cancellationToken) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 505 | { |
| 506 | logger.Invoke("testOneway({0}), sleeping...", secondsToSleep); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 507 | await Task.Delay(secondsToSleep * 1000, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 508 | logger.Invoke("testOneway finished"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 512 | |
| 513 | private static X509Certificate2 GetServerCert() |
| 514 | { |
| 515 | var serverCertName = "server.p12"; |
| 516 | var possiblePaths = new List<string> |
| 517 | { |
| 518 | "../../../keys/", |
| 519 | "../../keys/", |
| 520 | "../keys/", |
| 521 | "keys/", |
| 522 | }; |
| 523 | |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame^] | 524 | var existingPath = string.Empty; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 525 | foreach (var possiblePath in possiblePaths) |
| 526 | { |
| 527 | var path = Path.GetFullPath(possiblePath + serverCertName); |
| 528 | if (File.Exists(path)) |
| 529 | { |
| 530 | existingPath = path; |
| 531 | break; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | if (string.IsNullOrEmpty(existingPath)) |
| 536 | { |
| 537 | throw new FileNotFoundException($"Cannot find file: {serverCertName}"); |
| 538 | } |
| 539 | |
| 540 | var cert = new X509Certificate2(existingPath, "thrift"); |
| 541 | |
| 542 | return cert; |
| 543 | } |
| 544 | |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 545 | public static async Task<int> Execute(List<string> args) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 546 | { |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 547 | using (var loggerFactory = new LoggerFactory()) //.AddConsole().AddDebug(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 548 | { |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 549 | var logger = loggerFactory.CreateLogger("Test"); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 550 | |
| 551 | try |
| 552 | { |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 553 | var param = new ServerParam(); |
| 554 | |
| 555 | try |
| 556 | { |
| 557 | param.Parse(args); |
| 558 | } |
| 559 | catch (Exception ex) |
| 560 | { |
| 561 | Console.WriteLine("*** FAILED ***"); |
| 562 | Console.WriteLine("Error while parsing arguments"); |
| 563 | Console.WriteLine(ex.Message + " ST: " + ex.StackTrace); |
| 564 | return 1; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | // Endpoint transport (mandatory) |
| 569 | TServerTransport trans; |
| 570 | switch (param.transport) |
| 571 | { |
| 572 | case TransportChoice.NamedPipe: |
| 573 | Debug.Assert(param.pipe != null); |
Jens Geyer | ef0cb01 | 2021-04-02 12:18:15 +0200 | [diff] [blame] | 574 | trans = new TNamedPipeServerTransport(param.pipe, Configuration, NamedPipeClientFlags.OnlyLocalClients); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 575 | break; |
| 576 | |
| 577 | |
| 578 | case TransportChoice.TlsSocket: |
| 579 | var cert = GetServerCert(); |
| 580 | if (cert == null || !cert.HasPrivateKey) |
| 581 | { |
| 582 | cert?.Dispose(); |
| 583 | throw new InvalidOperationException("Certificate doesn't contain private key"); |
| 584 | } |
| 585 | |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 586 | trans = new TTlsServerSocketTransport(param.port, Configuration, |
| 587 | cert, |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 588 | (sender, certificate, chain, errors) => true, |
| 589 | null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12); |
| 590 | break; |
| 591 | |
| 592 | case TransportChoice.Socket: |
| 593 | default: |
Jens Geyer | eacd1d4 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 594 | trans = new TServerSocketTransport(param.port, Configuration); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 595 | break; |
| 596 | } |
| 597 | |
| 598 | // Layered transport (mandatory) |
Jens Geyer | 98a2325 | 2022-01-09 16:50:52 +0100 | [diff] [blame^] | 599 | TTransportFactory? transFactory; |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 600 | switch (param.buffering) |
| 601 | { |
| 602 | case BufferChoice.Framed: |
| 603 | transFactory = new TFramedTransport.Factory(); |
| 604 | break; |
| 605 | case BufferChoice.Buffered: |
| 606 | transFactory = new TBufferedTransport.Factory(); |
| 607 | break; |
| 608 | default: |
| 609 | Debug.Assert(param.buffering == BufferChoice.None, "unhandled case"); |
| 610 | transFactory = null; // no layered transprt |
| 611 | break; |
| 612 | } |
| 613 | |
Jens Geyer | 828ffa8 | 2020-11-21 15:15:32 +0100 | [diff] [blame] | 614 | TProtocolFactory proto = param.protocol switch |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 615 | { |
Jens Geyer | 828ffa8 | 2020-11-21 15:15:32 +0100 | [diff] [blame] | 616 | ProtocolChoice.Compact => new TCompactProtocol.Factory(), |
| 617 | ProtocolChoice.Json => new TJsonProtocol.Factory(), |
| 618 | ProtocolChoice.Binary => new TBinaryProtocol.Factory(), |
| 619 | _ => new TBinaryProtocol.Factory(), |
| 620 | }; |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 621 | |
| 622 | // Processor |
| 623 | var testHandler = new TestHandlerAsync(); |
| 624 | var testProcessor = new ThriftTest.AsyncProcessor(testHandler); |
| 625 | var processorFactory = new TSingletonProcessorFactory(testProcessor); |
| 626 | |
Jens Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 627 | var poolconfig = new TThreadPoolAsyncServer.Configuration(); // use platform defaults |
| 628 | TServer serverEngine = param.server switch |
| 629 | { |
| 630 | ServerChoice.Simple => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger), |
| 631 | ServerChoice.ThreadPool => new TThreadPoolAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, poolconfig, logger), |
| 632 | _ => new TSimpleAsyncServer(processorFactory, trans, transFactory, transFactory, proto, proto, logger) |
| 633 | }; |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 634 | |
| 635 | //Server event handler |
| 636 | var serverEvents = new MyServerEventHandler(); |
| 637 | serverEngine.SetEventHandler(serverEvents); |
| 638 | |
| 639 | // Run it |
Jens Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 640 | var where = (!string.IsNullOrEmpty(param.pipe)) ? "pipe " + param.pipe : "port " + param.port; |
| 641 | Console.WriteLine("Running "+ serverEngine.GetType().Name + |
| 642 | " at "+ where + |
| 643 | " using "+ processorFactory.GetType().Name + " processor prototype factory " + |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 644 | (param.buffering == BufferChoice.Buffered ? " with buffered transport" : "") + |
| 645 | (param.buffering == BufferChoice.Framed ? " with framed transport" : "") + |
| 646 | (param.transport == TransportChoice.TlsSocket ? " with encryption" : "") + |
| 647 | (param.protocol == ProtocolChoice.Compact ? " with compact protocol" : "") + |
| 648 | (param.protocol == ProtocolChoice.Json ? " with json protocol" : "") + |
| 649 | "..."); |
Jens Geyer | 7156940 | 2021-11-13 23:51:16 +0100 | [diff] [blame] | 650 | await serverEngine.ServeAsync(CancellationToken.None); |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 651 | Console.ReadLine(); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 652 | } |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 653 | catch (Exception x) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 654 | { |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 655 | Console.Error.Write(x); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 656 | return 1; |
| 657 | } |
| 658 | |
Jens Geyer | 261cad3 | 2019-11-20 19:03:14 +0100 | [diff] [blame] | 659 | Console.WriteLine("done."); |
| 660 | return 0; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 661 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | } |