blob: 7eb7f9d029c6a08f93467b2a7c603bbdbdaec4c2 [file] [log] [blame]
Jan Tattermusch31ba0632015-08-04 22:02:55 -07001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusch31ba0632015-08-04 22:02:55 -07004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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 Tattermusch31ba0632015-08-04 22:02:55 -07008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch31ba0632015-08-04 22:02:55 -070010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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 Tattermusch31ba0632015-08-04 22:02:55 -070016
17#endregion
18
19using System;
20
21using Grpc.Core.Utils;
22
23namespace 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 Tattermusch7a3ee6a2016-02-18 10:36:02 -080050 this.host = GrpcPreconditions.CheckNotNull(host, "host");
Jan Tattermusch31ba0632015-08-04 22:02:55 -070051 this.port = port;
Jan Tattermusch7a3ee6a2016-02-18 10:36:02 -080052 this.credentials = GrpcPreconditions.CheckNotNull(credentials, "credentials");
Jan Tattermusch31ba0632015-08-04 22:02:55 -070053 }
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}