ServerPort.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Grpc.Core.Utils;
  18. namespace Grpc.Core
  19. {
  20. /// <summary>
  21. /// A port exposed by a server.
  22. /// </summary>
  23. public class ServerPort
  24. {
  25. /// <summary>
  26. /// Pass this value as port to have the server choose an unused listening port for you.
  27. /// Ports added to a server will contain the bound port in their <see cref="BoundPort"/> property.
  28. /// </summary>
  29. public const int PickUnused = 0;
  30. readonly string host;
  31. readonly int port;
  32. readonly ServerCredentials credentials;
  33. readonly int boundPort;
  34. /// <summary>
  35. /// Creates a new port on which server should listen.
  36. /// </summary>
  37. /// <returns>The port on which server will be listening.</returns>
  38. /// <param name="host">the host</param>
  39. /// <param name="port">the port. If zero, an unused port is chosen automatically.</param>
  40. /// <param name="credentials">credentials to use to secure this port.</param>
  41. public ServerPort(string host, int port, ServerCredentials credentials)
  42. {
  43. this.host = GrpcPreconditions.CheckNotNull(host, "host");
  44. this.port = port;
  45. this.credentials = GrpcPreconditions.CheckNotNull(credentials, "credentials");
  46. }
  47. /// <summary>
  48. /// Creates a port from an existing <c>ServerPort</c> instance and boundPort value.
  49. /// </summary>
  50. internal ServerPort(ServerPort serverPort, int boundPort)
  51. {
  52. this.host = serverPort.host;
  53. this.port = serverPort.port;
  54. this.credentials = serverPort.credentials;
  55. this.boundPort = boundPort;
  56. }
  57. /// <value>The host.</value>
  58. public string Host
  59. {
  60. get
  61. {
  62. return host;
  63. }
  64. }
  65. /// <value>The port.</value>
  66. public int Port
  67. {
  68. get
  69. {
  70. return port;
  71. }
  72. }
  73. /// <value>The server credentials.</value>
  74. public ServerCredentials Credentials
  75. {
  76. get
  77. {
  78. return credentials;
  79. }
  80. }
  81. /// <value>
  82. /// The port actually bound by the server. This is useful if you let server
  83. /// pick port automatically. <see cref="PickUnused"/>
  84. /// </value>
  85. public int BoundPort
  86. {
  87. get
  88. {
  89. return boundPort;
  90. }
  91. }
  92. }
  93. }