From da71a4d391eebbf5d15a1e2aad9e32ec1c99f22b Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 8 Jun 2015 15:36:53 -0700 Subject: [PATCH] add host and port overload for new channel --- .../Grpc.Core.Tests/ClientServerTest.cs | 2 +- src/csharp/Grpc.Core/Channel.cs | 36 +++++++++++++------ src/csharp/Grpc.Core/Server.cs | 2 +- .../Grpc.Examples.MathClient/MathClient.cs | 2 +- .../MathClientServerTests.cs | 2 +- .../Grpc.IntegrationTesting/InteropClient.cs | 4 +-- .../InteropClientServerTest.cs | 2 +- 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs index b69b933aba6..82ded5cc7a6 100644 --- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs @@ -86,7 +86,7 @@ namespace Grpc.Core.Tests server.AddServiceDefinition(ServiceDefinition); int port = server.AddListeningPort(Host, Server.PickUnusedPort); server.Start(); - channel = new Channel(Host + ":" + port); + channel = new Channel(Host, port); } [TearDown] diff --git a/src/csharp/Grpc.Core/Channel.cs b/src/csharp/Grpc.Core/Channel.cs index b47d8106720..44b610f65b8 100644 --- a/src/csharp/Grpc.Core/Channel.cs +++ b/src/csharp/Grpc.Core/Channel.cs @@ -45,9 +45,13 @@ namespace Grpc.Core readonly string target; /// - /// 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. /// - public Channel(string target, Credentials credentials = null, ChannelArgs channelArgs = null) + /// The DNS name of IP address of the host. + /// Optional credentials to create a secure channel. + /// Optional channel arguments. + public Channel(string host, Credentials credentials = null, ChannelArgs channelArgs = null) { using (ChannelArgsSafeHandle nativeChannelArgs = CreateNativeChannelArgs(channelArgs)) { @@ -55,23 +59,27 @@ namespace Grpc.Core { using (CredentialsSafeHandle nativeCredentials = credentials.ToNativeCredentials()) { - this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs); + this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, host, nativeChannelArgs); } } 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 + /// + /// Creates a channel that connects to a specific host and port. + /// + /// DNS name or IP address + /// the port + /// Optional credentials to create a secure channel. + /// Optional channel arguments. + 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() @@ -80,6 +88,14 @@ namespace Grpc.Core GC.SuppressFinalize(this); } + internal string Target + { + get + { + return target; + } + } + internal ChannelSafeHandle Handle { get diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs index 4a7abbb33f7..521eb4be7ff 100644 --- a/src/csharp/Grpc.Core/Server.cs +++ b/src/csharp/Grpc.Core/Server.cs @@ -108,7 +108,7 @@ namespace Grpc.Core /// /// The port on which server will be listening. /// the host - /// the port. If zero, , an unused port is chosen automatically. + /// the port. If zero, an unused port is chosen automatically. public int AddListeningPort(string host, int port, ServerCredentials credentials) { Preconditions.CheckNotNull(credentials); diff --git a/src/csharp/Grpc.Examples.MathClient/MathClient.cs b/src/csharp/Grpc.Examples.MathClient/MathClient.cs index 85d9cdc7a61..360fe928dd8 100644 --- a/src/csharp/Grpc.Examples.MathClient/MathClient.cs +++ b/src/csharp/Grpc.Examples.MathClient/MathClient.cs @@ -41,7 +41,7 @@ namespace math { 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); MathExamples.DivExample(stub); diff --git a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs index 5aa6f4162d4..aadd49f795d 100644 --- a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs +++ b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs @@ -60,7 +60,7 @@ namespace math.Tests server.AddServiceDefinition(Math.BindService(new MathServiceImpl())); int port = server.AddListeningPort(host, Server.PickUnusedPort); 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 // for header support. diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs index dfaf18cae13..66171fae576 100644 --- a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs +++ b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs @@ -104,8 +104,6 @@ namespace Grpc.IntegrationTesting { GrpcEnvironment.Initialize(); - string addr = string.Format("{0}:{1}", options.serverHost, options.serverPort); - Credentials credentials = null; if (options.useTls) { @@ -119,7 +117,7 @@ namespace Grpc.IntegrationTesting .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; if (options.testCase == "service_account_creds" || options.testCase == "compute_engine_creds") diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs b/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs index ddbfc61a4ef..f756dfbc409 100644 --- a/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs +++ b/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs @@ -65,7 +65,7 @@ namespace Grpc.IntegrationTesting var channelArgs = ChannelArgs.CreateBuilder() .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); }