Split address passed to AddListeningPort into host and port

This commit is contained in:
Jan Tattermusch 2015-05-06 16:37:12 -07:00
parent 8ab1f7ed3d
commit 03e82e2cdf
7 changed files with 47 additions and 25 deletions

View File

@ -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);
} }

View File

@ -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();

View File

@ -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
/// 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
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>

View File

@ -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);

View File

@ -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);

View File

@ -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()

View File

@ -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();