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

add host and port overload for new channel

parent 93b0f630
No related branches found
No related tags found
No related merge requests found
...@@ -86,7 +86,7 @@ namespace Grpc.Core.Tests ...@@ -86,7 +86,7 @@ namespace Grpc.Core.Tests
server.AddServiceDefinition(ServiceDefinition); server.AddServiceDefinition(ServiceDefinition);
int port = server.AddListeningPort(Host, Server.PickUnusedPort); int port = server.AddListeningPort(Host, Server.PickUnusedPort);
server.Start(); server.Start();
channel = new Channel(Host + ":" + port); channel = new Channel(Host, port);
} }
[TearDown] [TearDown]
......
...@@ -45,9 +45,13 @@ namespace Grpc.Core ...@@ -45,9 +45,13 @@ namespace Grpc.Core
readonly string target; readonly string target;
/// <summary> /// <summary>
/// Creates a channel. /// Creates a channel that connects to a specific host.
/// Port will default to 80 for an unsecure channel and to 443 a secure channel.
/// </summary> /// </summary>
public Channel(string target, Credentials credentials = null, ChannelArgs channelArgs = null) /// <param name="host">The DNS name of IP address of the host.</param>
/// <param name="credentials">Optional credentials to create a secure channel.</param>
/// <param name="channelArgs">Optional channel arguments.</param>
public Channel(string host, Credentials credentials = null, ChannelArgs channelArgs = null)
{ {
using (ChannelArgsSafeHandle nativeChannelArgs = CreateNativeChannelArgs(channelArgs)) using (ChannelArgsSafeHandle nativeChannelArgs = CreateNativeChannelArgs(channelArgs))
{ {
...@@ -55,23 +59,27 @@ namespace Grpc.Core ...@@ -55,23 +59,27 @@ namespace Grpc.Core
{ {
using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials()) using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials())
{ {
this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs); this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs);
} }
} }
else else
{ {
this.handle = ChannelSafeHandle.Create(target, nativeChannelArgs); this.handle = ChannelSafeHandle.Create(host, nativeChannelArgs);
} }
} }
this.target = GetOverridenTarget(target, channelArgs); this.target = GetOverridenTarget(host, channelArgs);
} }
public string Target /// <summary>
/// Creates a channel that connects to a specific host and port.
/// </summary>
/// <param name="host">DNS name or IP address</param>
/// <param name="port">the port</param>
/// <param name="credentials">Optional credentials to create a secure channel.</param>
/// <param name="channelArgs">Optional channel arguments.</param>
public Channel(string host, int port, Credentials credentials = null, ChannelArgs channelArgs = null) :
this(string.Format("{0}:{1}", host, port), credentials, channelArgs)
{ {
get
{
return this.target;
}
} }
public void Dispose() public void Dispose()
...@@ -80,6 +88,14 @@ namespace Grpc.Core ...@@ -80,6 +88,14 @@ namespace Grpc.Core
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
internal string Target
{
get
{
return target;
}
}
internal ChannelSafeHandle Handle internal ChannelSafeHandle Handle
{ {
get get
......
...@@ -108,7 +108,7 @@ namespace Grpc.Core ...@@ -108,7 +108,7 @@ namespace Grpc.Core
/// </summary> /// </summary>
/// <returns>The port on which server will be listening.</returns> /// <returns>The port on which server will be listening.</returns>
/// <param name="host">the host</param> /// <param name="host">the host</param>
/// <param name="port">the port. If zero, , an unused port is chosen automatically.</param> /// <param name="port">the port. If zero, an unused port is chosen automatically.</param>
public int AddListeningPort(string host, int port, ServerCredentials credentials) public int AddListeningPort(string host, int port, ServerCredentials credentials)
{ {
Preconditions.CheckNotNull(credentials); Preconditions.CheckNotNull(credentials);
......
...@@ -41,7 +41,7 @@ namespace math ...@@ -41,7 +41,7 @@ namespace math
{ {
GrpcEnvironment.Initialize(); GrpcEnvironment.Initialize();
using (Channel channel = new Channel("127.0.0.1:23456")) using (Channel channel = new Channel("127.0.0.1", 23456))
{ {
Math.IMathClient stub = new Math.MathClient(channel); Math.IMathClient stub = new Math.MathClient(channel);
MathExamples.DivExample(stub); MathExamples.DivExample(stub);
......
...@@ -60,7 +60,7 @@ namespace math.Tests ...@@ -60,7 +60,7 @@ namespace math.Tests
server.AddServiceDefinition(Math.BindService(new MathServiceImpl())); server.AddServiceDefinition(Math.BindService(new MathServiceImpl()));
int port = server.AddListeningPort(host, Server.PickUnusedPort); int port = server.AddListeningPort(host, Server.PickUnusedPort);
server.Start(); server.Start();
channel = new Channel(host + ":" + port); channel = new Channel(host, port);
// TODO(jtattermusch): get rid of the custom header here once we have dedicated tests // TODO(jtattermusch): get rid of the custom header here once we have dedicated tests
// for header support. // for header support.
......
...@@ -104,8 +104,6 @@ namespace Grpc.IntegrationTesting ...@@ -104,8 +104,6 @@ namespace Grpc.IntegrationTesting
{ {
GrpcEnvironment.Initialize(); GrpcEnvironment.Initialize();
string addr = string.Format("{0}:{1}", options.serverHost, options.serverPort);
Credentials credentials = null; Credentials credentials = null;
if (options.useTls) if (options.useTls)
{ {
...@@ -119,7 +117,7 @@ namespace Grpc.IntegrationTesting ...@@ -119,7 +117,7 @@ namespace Grpc.IntegrationTesting
.AddString(ChannelArgs.SslTargetNameOverrideKey, options.serverHostOverride).Build(); .AddString(ChannelArgs.SslTargetNameOverrideKey, options.serverHostOverride).Build();
} }
using (Channel channel = new Channel(addr, credentials, channelArgs)) using (Channel channel = new Channel(options.serverHost, options.serverPort.Value, credentials, channelArgs))
{ {
var stubConfig = StubConfiguration.Default; var stubConfig = StubConfiguration.Default;
if (options.testCase == "service_account_creds" || options.testCase == "compute_engine_creds") if (options.testCase == "service_account_creds" || options.testCase == "compute_engine_creds")
......
...@@ -65,7 +65,7 @@ namespace Grpc.IntegrationTesting ...@@ -65,7 +65,7 @@ namespace Grpc.IntegrationTesting
var channelArgs = ChannelArgs.CreateBuilder() var channelArgs = ChannelArgs.CreateBuilder()
.AddString(ChannelArgs.SslTargetNameOverrideKey, TestCredentials.DefaultHostOverride).Build(); .AddString(ChannelArgs.SslTargetNameOverrideKey, TestCredentials.DefaultHostOverride).Build();
channel = new Channel(host + ":" + port, TestCredentials.CreateTestClientCredentials(true), channelArgs); channel = new Channel(host, port, TestCredentials.CreateTestClientCredentials(true), channelArgs);
client = TestService.NewStub(channel); client = TestService.NewStub(channel);
} }
......
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