diff options
author | Tim Emiola <tbetbetbe@users.noreply.github.com> | 2015-05-07 14:23:03 -0700 |
---|---|---|
committer | Tim Emiola <tbetbetbe@users.noreply.github.com> | 2015-05-07 14:23:03 -0700 |
commit | 57e289c103ffd8ca64bb95ad621de0643246f4ed (patch) | |
tree | 7913d7fe963701e3c2ac8bc3f2fc5f5bdfc081fb /src/csharp/Grpc.Core/Server.cs | |
parent | eb697aa12a4fa153a84b58a7e82ea5684bad067b (diff) | |
parent | 03e82e2cdffbc0d4c19cea9d16e24d0dbf353376 (diff) |
Merge pull request #1504 from jtattermusch/csharp_api_fixes
Polishing the new C# API
Diffstat (limited to 'src/csharp/Grpc.Core/Server.cs')
-rw-r--r-- | src/csharp/Grpc.Core/Server.cs | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs index a3000cee46..0df46bb25b 100644 --- a/src/csharp/Grpc.Core/Server.cs +++ b/src/csharp/Grpc.Core/Server.cs @@ -47,6 +47,11 @@ namespace Grpc.Core /// </summary> public class Server { + /// <summary> + /// Pass this value as port to have the server choose an unused listening port for you. + /// </summary> + public const int PickUnusedPort = 0; + // TODO(jtattermusch) : make sure the delegate doesn't get garbage collected while // native callbacks are in the completion queue. readonly ServerShutdownCallbackDelegate serverShutdownHandler; @@ -89,29 +94,25 @@ namespace Grpc.Core /// Add a non-secure port on which server should listen. /// Only call this before Start(). /// </summary> - public int AddListeningPort(string addr) + /// <returns>The port on which server will be listening.</returns> + /// <param name="host">the host</param> + /// <param name="port">the port. If zero, an unused port is chosen automatically.</param> + public int AddListeningPort(string host, int port) { - lock (myLock) - { - Preconditions.CheckState(!startRequested); - return handle.AddListeningPort(addr); - } + return AddListeningPortInternal(host, port, null); } /// <summary> - /// Add a secure port on which server should listen. + /// Add a non-secure port on which server should listen. /// Only call this before Start(). /// </summary> - public int AddListeningPort(string addr, ServerCredentials credentials) + /// <returns>The port on which server will be listening.</returns> + /// <param name="host">the host</param> + /// <param name="port">the port. If zero, , an unused port is chosen automatically.</param> + public int AddListeningPort(string host, int port, ServerCredentials credentials) { - lock (myLock) - { - Preconditions.CheckState(!startRequested); - using (var nativeCredentials = credentials.ToNativeCredentials()) - { - return handle.AddListeningPort(addr, nativeCredentials); - } - } + Preconditions.CheckNotNull(credentials); + return AddListeningPortInternal(host, port, credentials); } /// <summary> @@ -164,6 +165,26 @@ namespace Grpc.Core handle.Dispose(); } + private int AddListeningPortInternal(string host, int port, ServerCredentials credentials) + { + lock (myLock) + { + Preconditions.CheckState(!startRequested); + var address = string.Format("{0}:{1}", host, port); + if (credentials != null) + { + using (var nativeCredentials = credentials.ToNativeCredentials()) + { + return handle.AddListeningPort(address, nativeCredentials); + } + } + else + { + return handle.AddListeningPort(address); + } + } + } + /// <summary> /// Allows one new RPC call to be received by server. /// </summary> |