Split address passed to AddListeningPort into host and port
This commit is contained in:
parent
8ab1f7ed3d
commit
03e82e2cdf
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue