Skip to content
Snippets Groups Projects
Commit 03e82e2c authored by Jan Tattermusch's avatar Jan Tattermusch
Browse files

Split address passed to AddListeningPort into host and port

parent 8ab1f7ed
No related branches found
No related tags found
No related merge requests found
...@@ -84,7 +84,7 @@ namespace Grpc.Core.Tests ...@@ -84,7 +84,7 @@ namespace Grpc.Core.Tests
{ {
server = new Server(); server = new Server();
server.AddServiceDefinition(ServiceDefinition); server.AddServiceDefinition(ServiceDefinition);
int port = server.AddListeningPort(Host + ":0"); int port = server.AddListeningPort(Host, Server.PickUnusedPort);
server.Start(); server.Start();
channel = new Channel(Host + ":" + port); channel = new Channel(Host + ":" + port);
} }
......
...@@ -47,7 +47,7 @@ namespace Grpc.Core.Tests ...@@ -47,7 +47,7 @@ namespace Grpc.Core.Tests
GrpcEnvironment.Initialize(); GrpcEnvironment.Initialize();
Server server = new Server(); Server server = new Server();
server.AddListeningPort("localhost:0"); server.AddListeningPort("localhost", Server.PickUnusedPort);
server.Start(); server.Start();
server.ShutdownAsync().Wait(); server.ShutdownAsync().Wait();
......
...@@ -47,6 +47,11 @@ namespace Grpc.Core ...@@ -47,6 +47,11 @@ namespace Grpc.Core
/// </summary> /// </summary>
public class Server 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 // TODO(jtattermusch) : make sure the delegate doesn't get garbage collected while
// native callbacks are in the completion queue. // native callbacks are in the completion queue.
readonly ServerShutdownCallbackDelegate serverShutdownHandler; readonly ServerShutdownCallbackDelegate serverShutdownHandler;
...@@ -89,29 +94,25 @@ namespace Grpc.Core ...@@ -89,29 +94,25 @@ namespace Grpc.Core
/// Add a non-secure port on which server should listen. /// Add a non-secure port on which server should listen.
/// Only call this before Start(). /// Only call this before Start().
/// </summary> /// </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) return AddListeningPortInternal(host, port, null);
{
Preconditions.CheckState(!startRequested);
return handle.AddListeningPort(addr);
}
} }
/// <summary> /// <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(). /// Only call this before Start().
/// </summary> /// </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.CheckNotNull(credentials);
{ return AddListeningPortInternal(host, port, credentials);
Preconditions.CheckState(!startRequested);
using (var nativeCredentials = credentials.ToNativeCredentials())
{
return handle.AddListeningPort(addr, nativeCredentials);
}
}
} }
/// <summary> /// <summary>
...@@ -164,6 +165,26 @@ namespace Grpc.Core ...@@ -164,6 +165,26 @@ namespace Grpc.Core
handle.Dispose(); 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> /// <summary>
/// Allows one new RPC call to be received by server. /// Allows one new RPC call to be received by server.
/// </summary> /// </summary>
......
...@@ -46,7 +46,7 @@ namespace math ...@@ -46,7 +46,7 @@ namespace math
Server server = new Server(); Server server = new Server();
server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl())); server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl()));
int port = server.AddListeningPort(host + ":23456"); int port = server.AddListeningPort(host, 23456);
server.Start(); server.Start();
Console.WriteLine("MathServer listening on port " + port); Console.WriteLine("MathServer listening on port " + port);
......
...@@ -59,7 +59,7 @@ namespace math.Tests ...@@ -59,7 +59,7 @@ namespace math.Tests
server = new Server(); server = new Server();
server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl())); server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl()));
int port = server.AddListeningPort(host + ":0"); int port = server.AddListeningPort(host, Server.PickUnusedPort);
server.Start(); server.Start();
channel = new Channel(host + ":" + port); channel = new Channel(host + ":" + port);
......
...@@ -59,7 +59,7 @@ namespace Grpc.IntegrationTesting ...@@ -59,7 +59,7 @@ namespace Grpc.IntegrationTesting
server = new Server(); server = new Server();
server.AddServiceDefinition(TestServiceGrpc.BindService(new TestServiceImpl())); 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(); server.Start();
var channelArgs = ChannelArgs.CreateBuilder() var channelArgs = ChannelArgs.CreateBuilder()
......
...@@ -93,16 +93,17 @@ namespace Grpc.IntegrationTesting ...@@ -93,16 +93,17 @@ namespace Grpc.IntegrationTesting
var server = new Server(); var server = new Server();
server.AddServiceDefinition(TestServiceGrpc.BindService(new TestServiceImpl())); 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) if (options.useTls)
{ {
server.AddListeningPort(addr, TestCredentials.CreateTestServerCredentials()); server.AddListeningPort(host, port, TestCredentials.CreateTestServerCredentials());
} }
else 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.Start();
server.ShutdownTask.Wait(); server.ShutdownTask.Wait();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment