InteropServer.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.IO;
  20. using System.Text.RegularExpressions;
  21. using System.Threading.Tasks;
  22. using CommandLine;
  23. using CommandLine.Text;
  24. using Grpc.Core;
  25. using Grpc.Core.Logging;
  26. using Grpc.Core.Utils;
  27. using Grpc.Testing;
  28. using NUnit.Framework;
  29. namespace Grpc.IntegrationTesting
  30. {
  31. public class InteropServer
  32. {
  33. private class ServerOptions
  34. {
  35. [Option("port", Default = 8070)]
  36. public int Port { get; set; }
  37. // Deliberately using nullable bool type to allow --use_tls=true syntax (as opposed to --use_tls)
  38. [Option("use_tls", Default = false)]
  39. public bool? UseTls { get; set; }
  40. }
  41. ServerOptions options;
  42. private InteropServer(ServerOptions options)
  43. {
  44. this.options = options;
  45. }
  46. public static void Run(string[] args)
  47. {
  48. GrpcEnvironment.SetLogger(new ConsoleLogger());
  49. var parserResult = Parser.Default.ParseArguments<ServerOptions>(args)
  50. .WithNotParsed(errors => Environment.Exit(1))
  51. .WithParsed(options =>
  52. {
  53. var interopServer = new InteropServer(options);
  54. interopServer.Run();
  55. });
  56. }
  57. private void Run()
  58. {
  59. var server = new Server
  60. {
  61. Services = { TestService.BindService(new TestServiceImpl()) }
  62. };
  63. string host = "0.0.0.0";
  64. int port = options.Port;
  65. if (options.UseTls.Value)
  66. {
  67. server.Ports.Add(host, port, TestCredentials.CreateSslServerCredentials());
  68. }
  69. else
  70. {
  71. server.Ports.Add(host, options.Port, ServerCredentials.Insecure);
  72. }
  73. Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port));
  74. server.Start();
  75. server.ShutdownTask.Wait();
  76. }
  77. }
  78. }