Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
| 20 | |
| 21 | using Grpc.Core.Utils; |
| 22 | |
| 23 | namespace Grpc.Core |
| 24 | { |
| 25 | /// <summary> |
| 26 | /// A port exposed by a server. |
| 27 | /// </summary> |
| 28 | public class ServerPort |
| 29 | { |
| 30 | /// <summary> |
| 31 | /// Pass this value as port to have the server choose an unused listening port for you. |
| 32 | /// Ports added to a server will contain the bound port in their <see cref="BoundPort"/> property. |
| 33 | /// </summary> |
| 34 | public const int PickUnused = 0; |
| 35 | |
| 36 | readonly string host; |
| 37 | readonly int port; |
| 38 | readonly ServerCredentials credentials; |
| 39 | readonly int boundPort; |
| 40 | |
| 41 | /// <summary> |
| 42 | /// Creates a new port on which server should listen. |
| 43 | /// </summary> |
| 44 | /// <returns>The port on which server will be listening.</returns> |
| 45 | /// <param name="host">the host</param> |
| 46 | /// <param name="port">the port. If zero, an unused port is chosen automatically.</param> |
| 47 | /// <param name="credentials">credentials to use to secure this port.</param> |
| 48 | public ServerPort(string host, int port, ServerCredentials credentials) |
| 49 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 50 | this.host = GrpcPreconditions.CheckNotNull(host, "host"); |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 51 | this.port = port; |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 52 | this.credentials = GrpcPreconditions.CheckNotNull(credentials, "credentials"); |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /// <summary> |
| 56 | /// Creates a port from an existing <c>ServerPort</c> instance and boundPort value. |
| 57 | /// </summary> |
| 58 | internal ServerPort(ServerPort serverPort, int boundPort) |
| 59 | { |
| 60 | this.host = serverPort.host; |
| 61 | this.port = serverPort.port; |
| 62 | this.credentials = serverPort.credentials; |
| 63 | this.boundPort = boundPort; |
| 64 | } |
| 65 | |
| 66 | /// <value>The host.</value> |
| 67 | public string Host |
| 68 | { |
| 69 | get |
| 70 | { |
| 71 | return host; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /// <value>The port.</value> |
| 76 | public int Port |
| 77 | { |
| 78 | get |
| 79 | { |
| 80 | return port; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /// <value>The server credentials.</value> |
| 85 | public ServerCredentials Credentials |
| 86 | { |
| 87 | get |
| 88 | { |
| 89 | return credentials; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// <value> |
| 94 | /// The port actually bound by the server. This is useful if you let server |
| 95 | /// pick port automatically. <see cref="PickUnused"/> |
| 96 | /// </value> |
| 97 | public int BoundPort |
| 98 | { |
| 99 | get |
| 100 | { |
| 101 | return boundPort; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |