aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@google.com>2015-05-06 16:37:12 -0700
committerGravatar Jan Tattermusch <jtattermusch@google.com>2015-05-07 09:14:13 -0700
commit03e82e2cdffbc0d4c19cea9d16e24d0dbf353376 (patch)
tree77e4c484b4666391c40241d51ffeaea1c2c7ea36
parent8ab1f7ed3d2f8af7702df95139a6b62aa76b5f0e (diff)
Split address passed to AddListeningPort into host and port
-rw-r--r--src/csharp/Grpc.Core.Tests/ClientServerTest.cs2
-rw-r--r--src/csharp/Grpc.Core.Tests/ServerTest.cs2
-rw-r--r--src/csharp/Grpc.Core/Server.cs53
-rw-r--r--src/csharp/Grpc.Examples.MathServer/MathServer.cs2
-rw-r--r--src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs2
-rw-r--r--src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs2
-rw-r--r--src/csharp/Grpc.IntegrationTesting/InteropServer.cs9
7 files changed, 47 insertions, 25 deletions
diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs
index 4eb542fae8..b69b933aba 100644
--- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs
+++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs
@@ -84,7 +84,7 @@ namespace Grpc.Core.Tests
{
server = new Server();
server.AddServiceDefinition(ServiceDefinition);
- int port = server.AddListeningPort(Host + ":0");
+ int port = server.AddListeningPort(Host, Server.PickUnusedPort);
server.Start();
channel = new Channel(Host + ":" + port);
}
diff --git a/src/csharp/Grpc.Core.Tests/ServerTest.cs b/src/csharp/Grpc.Core.Tests/ServerTest.cs
index 2a1855da67..02c773c9cc 100644
--- a/src/csharp/Grpc.Core.Tests/ServerTest.cs
+++ b/src/csharp/Grpc.Core.Tests/ServerTest.cs
@@ -47,7 +47,7 @@ namespace Grpc.Core.Tests
GrpcEnvironment.Initialize();
Server server = new Server();
- server.AddListeningPort("localhost:0");
+ server.AddListeningPort("localhost", Server.PickUnusedPort);
server.Start();
server.ShutdownAsync().Wait();
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>
diff --git a/src/csharp/Grpc.Examples.MathServer/MathServer.cs b/src/csharp/Grpc.Examples.MathServer/MathServer.cs
index abc7ef05e4..cfde9b42c7 100644
--- a/src/csharp/Grpc.Examples.MathServer/MathServer.cs
+++ b/src/csharp/Grpc.Examples.MathServer/MathServer.cs
@@ -46,7 +46,7 @@ namespace math
Server server = new Server();
server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl()));
- int port = server.AddListeningPort(host + ":23456");
+ int port = server.AddListeningPort(host, 23456);
server.Start();
Console.WriteLine("MathServer listening on port " + port);
diff --git a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
index 332795e0e5..4ada95edd6 100644
--- a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
+++ b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
@@ -59,7 +59,7 @@ namespace math.Tests
server = new Server();
server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl()));
- int port = server.AddListeningPort(host + ":0");
+ int port = server.AddListeningPort(host, Server.PickUnusedPort);
server.Start();
channel = new Channel(host + ":" + port);
diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs b/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs
index 45380227c2..9e49ce0d17 100644
--- a/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs
+++ b/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs
@@ -59,7 +59,7 @@ namespace Grpc.IntegrationTesting
server = new Server();
server.AddServiceDefinition(TestServiceGrpc.BindService(new TestServiceImpl()));
- int port = server.AddListeningPort(host + ":0", TestCredentials.CreateTestServerCredentials());
+ int port = server.AddListeningPort(host, Server.PickUnusedPort, TestCredentials.CreateTestServerCredentials());
server.Start();
var channelArgs = ChannelArgs.CreateBuilder()
diff --git a/src/csharp/Grpc.IntegrationTesting/InteropServer.cs b/src/csharp/Grpc.IntegrationTesting/InteropServer.cs
index ad5200774f..ca54aed041 100644
--- a/src/csharp/Grpc.IntegrationTesting/InteropServer.cs
+++ b/src/csharp/Grpc.IntegrationTesting/InteropServer.cs
@@ -93,16 +93,17 @@ namespace Grpc.IntegrationTesting
var server = new Server();
server.AddServiceDefinition(TestServiceGrpc.BindService(new TestServiceImpl()));
- string addr = "0.0.0.0:" + options.port;
+ string host = "0.0.0.0";
+ int port = options.port.Value;
if (options.useTls)
{
- server.AddListeningPort(addr, TestCredentials.CreateTestServerCredentials());
+ server.AddListeningPort(host, port, TestCredentials.CreateTestServerCredentials());
}
else
{
- server.AddListeningPort(addr);
+ server.AddListeningPort(host, options.port.Value);
}
- Console.WriteLine("Running server on " + addr);
+ Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port));
server.Start();
server.ShutdownTask.Wait();