aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@users.noreply.github.com>2015-08-05 13:02:48 -0700
committerGravatar Jan Tattermusch <jtattermusch@users.noreply.github.com>2015-08-05 13:02:48 -0700
commitc6f2d4f3cdca7eeb040d659e1ef9f24d750ceffa (patch)
treec5ba1b20c7bde2cbf1fe911afa9765e007bbbe70 /src/csharp
parentc08a4d6975e0abc112800c4cd7ae6612d2aa5197 (diff)
parent31ba0632247993b02b552294691a354ee5d0ef08 (diff)
Merge pull request #2797 from jtattermusch/polishing_api
C# API improvements
Diffstat (limited to 'src/csharp')
-rw-r--r--src/csharp/Grpc.Core.Tests/ClientServerTest.cs10
-rw-r--r--src/csharp/Grpc.Core.Tests/ServerTest.cs39
-rw-r--r--src/csharp/Grpc.Core.Tests/TimeoutsTest.cs10
-rw-r--r--src/csharp/Grpc.Core/ChannelOptions.cs1
-rw-r--r--src/csharp/Grpc.Core/Grpc.Core.csproj8
-rw-r--r--src/csharp/Grpc.Core/Grpc.Core.nuspec1
-rw-r--r--src/csharp/Grpc.Core/Internal/ServerCredentialsSafeHandle.cs7
-rw-r--r--src/csharp/Grpc.Core/KeyCertificatePair.cs1
-rw-r--r--src/csharp/Grpc.Core/Metadata.cs1
-rw-r--r--src/csharp/Grpc.Core/Server.cs175
-rw-r--r--src/csharp/Grpc.Core/ServerCredentials.cs28
-rw-r--r--src/csharp/Grpc.Core/ServerPort.cs120
-rw-r--r--src/csharp/Grpc.Core/ServerServiceDefinition.cs12
-rw-r--r--src/csharp/Grpc.Core/packages.config1
-rw-r--r--src/csharp/Grpc.Examples.MathServer/MathServer.cs15
-rw-r--r--src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs20
-rw-r--r--src/csharp/Grpc.HealthCheck.Tests/HealthClientServerTest.cs10
-rw-r--r--src/csharp/Grpc.IntegrationTesting/Grpc.IntegrationTesting.csproj3
-rw-r--r--src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs14
-rw-r--r--src/csharp/Grpc.IntegrationTesting/InteropServer.cs10
-rw-r--r--src/csharp/Grpc.IntegrationTesting/SslCredentialsTest.cs15
-rw-r--r--src/csharp/Grpc.IntegrationTesting/TestCredentials.cs1
-rw-r--r--src/csharp/Grpc.IntegrationTesting/packages.config1
-rw-r--r--src/csharp/ext/grpc_csharp_ext.c7
24 files changed, 393 insertions, 117 deletions
diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs
index 35924868ca..bf7cc3fbf3 100644
--- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs
+++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs
@@ -77,11 +77,13 @@ namespace Grpc.Core.Tests
[SetUp]
public void Init()
{
- server = new Server();
- server.AddServiceDefinition(ServiceDefinition);
- int port = server.AddPort(Host, Server.PickUnusedPort, ServerCredentials.Insecure);
+ server = new Server
+ {
+ Services = { ServiceDefinition },
+ Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
+ };
server.Start();
- channel = new Channel(Host, port, Credentials.Insecure);
+ channel = new Channel(Host, server.Ports.Single().BoundPort, Credentials.Insecure);
}
[TearDown]
diff --git a/src/csharp/Grpc.Core.Tests/ServerTest.cs b/src/csharp/Grpc.Core.Tests/ServerTest.cs
index ba9efae871..485006ebac 100644
--- a/src/csharp/Grpc.Core.Tests/ServerTest.cs
+++ b/src/csharp/Grpc.Core.Tests/ServerTest.cs
@@ -32,6 +32,7 @@
#endregion
using System;
+using System.Linq;
using Grpc.Core;
using Grpc.Core.Internal;
using Grpc.Core.Utils;
@@ -44,11 +45,45 @@ namespace Grpc.Core.Tests
[Test]
public void StartAndShutdownServer()
{
- Server server = new Server();
- server.AddPort("localhost", Server.PickUnusedPort, ServerCredentials.Insecure);
+ Server server = new Server
+ {
+ Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
+ };
server.Start();
server.ShutdownAsync().Wait();
GrpcEnvironment.Shutdown();
}
+
+ [Test]
+ public void PickUnusedPort()
+ {
+ Server server = new Server
+ {
+ Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
+ };
+
+ var boundPort = server.Ports.Single();
+ Assert.AreEqual(0, boundPort.Port);
+ Assert.Greater(boundPort.BoundPort, 0);
+
+ server.Start();
+ server.ShutdownAsync();
+ GrpcEnvironment.Shutdown();
+ }
+
+ [Test]
+ public void CannotModifyAfterStarted()
+ {
+ Server server = new Server
+ {
+ Ports = { new ServerPort("localhost", ServerPort.PickUnused, ServerCredentials.Insecure) }
+ };
+ server.Start();
+ Assert.Throws(typeof(InvalidOperationException), () => server.Ports.Add("localhost", 9999, ServerCredentials.Insecure));
+ Assert.Throws(typeof(InvalidOperationException), () => server.Services.Add(ServerServiceDefinition.CreateBuilder("serviceName").Build()));
+
+ server.ShutdownAsync().Wait();
+ GrpcEnvironment.Shutdown();
+ }
}
}
diff --git a/src/csharp/Grpc.Core.Tests/TimeoutsTest.cs b/src/csharp/Grpc.Core.Tests/TimeoutsTest.cs
index a09273b846..d84801fbac 100644
--- a/src/csharp/Grpc.Core.Tests/TimeoutsTest.cs
+++ b/src/csharp/Grpc.Core.Tests/TimeoutsTest.cs
@@ -70,11 +70,13 @@ namespace Grpc.Core.Tests
[SetUp]
public void Init()
{
- server = new Server();
- server.AddServiceDefinition(ServiceDefinition);
- int port = server.AddPort(Host, Server.PickUnusedPort, ServerCredentials.Insecure);
+ server = new Server
+ {
+ Services = { ServiceDefinition },
+ Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
+ };
server.Start();
- channel = new Channel(Host, port, Credentials.Insecure);
+ channel = new Channel(Host, server.Ports.Single().BoundPort, Credentials.Insecure);
stringFromServerHandlerTcs = new TaskCompletionSource<string>();
}
diff --git a/src/csharp/Grpc.Core/ChannelOptions.cs b/src/csharp/Grpc.Core/ChannelOptions.cs
index f70408dae7..1e0f90287a 100644
--- a/src/csharp/Grpc.Core/ChannelOptions.cs
+++ b/src/csharp/Grpc.Core/ChannelOptions.cs
@@ -30,7 +30,6 @@
#endregion
using System;
using System.Collections.Generic;
-using System.Collections.Immutable;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
diff --git a/src/csharp/Grpc.Core/Grpc.Core.csproj b/src/csharp/Grpc.Core/Grpc.Core.csproj
index 641b54baba..17add77164 100644
--- a/src/csharp/Grpc.Core/Grpc.Core.csproj
+++ b/src/csharp/Grpc.Core/Grpc.Core.csproj
@@ -44,9 +44,6 @@
<Reference Include="System.Interactive.Async">
<HintPath>..\packages\Ix-Async.1.2.3\lib\net45\System.Interactive.Async.dll</HintPath>
</Reference>
- <Reference Include="System.Collections.Immutable">
- <HintPath>..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
- </Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AsyncDuplexStreamingCall.cs" />
@@ -55,6 +52,7 @@
<Compile Include="IServerStreamWriter.cs" />
<Compile Include="IAsyncStreamWriter.cs" />
<Compile Include="IAsyncStreamReader.cs" />
+ <Compile Include="ServerPort.cs" />
<Compile Include="Version.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RpcException.cs" />
@@ -146,7 +144,5 @@
</Target>
<Import Project="..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.targets" Condition="Exists('..\packages\grpc.dependencies.openssl.redist.1.0.2.2\build\portable-net45\grpc.dependencies.openssl.redist.targets')" />
<Import Project="..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.targets" Condition="Exists('..\packages\grpc.dependencies.zlib.redist.1.2.8.9\build\portable-net45\grpc.dependencies.zlib.redist.targets')" />
- <ItemGroup>
- <Folder Include="Logging\" />
- </ItemGroup>
+ <ItemGroup />
</Project> \ No newline at end of file
diff --git a/src/csharp/Grpc.Core/Grpc.Core.nuspec b/src/csharp/Grpc.Core/Grpc.Core.nuspec
index 086776f69d..fe49efc7ec 100644
--- a/src/csharp/Grpc.Core/Grpc.Core.nuspec
+++ b/src/csharp/Grpc.Core/Grpc.Core.nuspec
@@ -15,7 +15,6 @@
<copyright>Copyright 2015, Google Inc.</copyright>
<tags>gRPC RPC Protocol HTTP/2</tags>
<dependencies>
- <dependency id="System.Collections.Immutable" version="1.1.36" />
<dependency id="Ix-Async" version="1.2.3" />
<dependency id="grpc.native.csharp_ext" version="$GrpcNativeCsharpExtVersion$" />
</dependencies>
diff --git a/src/csharp/Grpc.Core/Internal/ServerCredentialsSafeHandle.cs b/src/csharp/Grpc.Core/Internal/ServerCredentialsSafeHandle.cs
index 59238a452c..37a4f5256b 100644
--- a/src/csharp/Grpc.Core/Internal/ServerCredentialsSafeHandle.cs
+++ b/src/csharp/Grpc.Core/Internal/ServerCredentialsSafeHandle.cs
@@ -42,7 +42,7 @@ namespace Grpc.Core.Internal
internal class ServerCredentialsSafeHandle : SafeHandleZeroIsInvalid
{
[DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)]
- static extern ServerCredentialsSafeHandle grpcsharp_ssl_server_credentials_create(string pemRootCerts, string[] keyCertPairCertChainArray, string[] keyCertPairPrivateKeyArray, UIntPtr numKeyCertPairs);
+ static extern ServerCredentialsSafeHandle grpcsharp_ssl_server_credentials_create(string pemRootCerts, string[] keyCertPairCertChainArray, string[] keyCertPairPrivateKeyArray, UIntPtr numKeyCertPairs, bool forceClientAuth);
[DllImport("grpc_csharp_ext.dll")]
static extern void grpcsharp_server_credentials_release(IntPtr credentials);
@@ -51,12 +51,13 @@ namespace Grpc.Core.Internal
{
}
- public static ServerCredentialsSafeHandle CreateSslCredentials(string pemRootCerts, string[] keyCertPairCertChainArray, string[] keyCertPairPrivateKeyArray)
+ public static ServerCredentialsSafeHandle CreateSslCredentials(string pemRootCerts, string[] keyCertPairCertChainArray, string[] keyCertPairPrivateKeyArray, bool forceClientAuth)
{
Preconditions.CheckArgument(keyCertPairCertChainArray.Length == keyCertPairPrivateKeyArray.Length);
return grpcsharp_ssl_server_credentials_create(pemRootCerts,
keyCertPairCertChainArray, keyCertPairPrivateKeyArray,
- new UIntPtr((ulong)keyCertPairCertChainArray.Length));
+ new UIntPtr((ulong)keyCertPairCertChainArray.Length),
+ forceClientAuth);
}
protected override bool ReleaseHandle()
diff --git a/src/csharp/Grpc.Core/KeyCertificatePair.cs b/src/csharp/Grpc.Core/KeyCertificatePair.cs
index 7cea18618e..5def15a656 100644
--- a/src/csharp/Grpc.Core/KeyCertificatePair.cs
+++ b/src/csharp/Grpc.Core/KeyCertificatePair.cs
@@ -33,7 +33,6 @@
using System;
using System.Collections.Generic;
-using System.Collections.Immutable;
using Grpc.Core.Internal;
using Grpc.Core.Utils;
diff --git a/src/csharp/Grpc.Core/Metadata.cs b/src/csharp/Grpc.Core/Metadata.cs
index 2f308cbb11..6fd0a7109d 100644
--- a/src/csharp/Grpc.Core/Metadata.cs
+++ b/src/csharp/Grpc.Core/Metadata.cs
@@ -32,7 +32,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
-using System.Collections.Immutable;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Text;
diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs
index 3217547cc4..eb5b043d1c 100644
--- a/src/csharp/Grpc.Core/Server.cs
+++ b/src/csharp/Grpc.Core/Server.cs
@@ -32,7 +32,7 @@
#endregion
using System;
-using System.Collections.Concurrent;
+using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
@@ -48,18 +48,17 @@ 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;
-
static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<Server>();
+ readonly ServiceDefinitionCollection serviceDefinitions;
+ readonly ServerPortCollection ports;
readonly GrpcEnvironment environment;
readonly List<ChannelOption> options;
readonly ServerSafeHandle handle;
readonly object myLock = new object();
+ readonly List<ServerServiceDefinition> serviceDefinitionsList = new List<ServerServiceDefinition>();
+ readonly List<ServerPort> serverPortList = new List<ServerPort>();
readonly Dictionary<string, IServerCallHandler> callHandlers = new Dictionary<string, IServerCallHandler>();
readonly TaskCompletionSource<object> shutdownTcs = new TaskCompletionSource<object>();
@@ -72,6 +71,8 @@ namespace Grpc.Core
/// <param name="options">Channel options.</param>
public Server(IEnumerable<ChannelOption> options = null)
{
+ this.serviceDefinitions = new ServiceDefinitionCollection(this);
+ this.ports = new ServerPortCollection(this);
this.environment = GrpcEnvironment.GetInstance();
this.options = options != null ? new List<ChannelOption>(options) : new List<ChannelOption>();
using (var channelArgs = ChannelOptions.CreateChannelArgs(this.options))
@@ -81,47 +82,26 @@ namespace Grpc.Core
}
/// <summary>
- /// Adds a service definition to the server. This is how you register
- /// handlers for a service with the server.
- /// Only call this before Start().
+ /// Services that will be exported by the server once started. Register a service with this
+ /// server by adding its definition to this collection.
/// </summary>
- public void AddServiceDefinition(ServerServiceDefinition serviceDefinition)
+ public ServiceDefinitionCollection Services
{
- lock (myLock)
+ get
{
- Preconditions.CheckState(!startRequested);
- foreach (var entry in serviceDefinition.CallHandlers)
- {
- callHandlers.Add(entry.Key, entry.Value);
- }
+ return serviceDefinitions;
}
}
/// <summary>
- /// Add a port on which server should listen.
- /// Only call this before Start().
+ /// Ports on which the server will listen once started. Register a port with this
+ /// server by adding its definition to this collection.
/// </summary>
- /// <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 AddPort(string host, int port, ServerCredentials credentials)
+ public ServerPortCollection Ports
{
- lock (myLock)
+ get
{
- Preconditions.CheckNotNull(credentials);
- Preconditions.CheckState(!startRequested);
- var address = string.Format("{0}:{1}", host, port);
- using (var nativeCredentials = credentials.ToNativeCredentials())
- {
- if (nativeCredentials != null)
- {
- return handle.AddSecurePort(address, nativeCredentials);
- }
- else
- {
- return handle.AddInsecurePort(address);
- }
- }
+ return ports;
}
}
@@ -190,6 +170,50 @@ namespace Grpc.Core
}
/// <summary>
+ /// Adds a service definition.
+ /// </summary>
+ private void AddServiceDefinitionInternal(ServerServiceDefinition serviceDefinition)
+ {
+ lock (myLock)
+ {
+ Preconditions.CheckState(!startRequested);
+ foreach (var entry in serviceDefinition.CallHandlers)
+ {
+ callHandlers.Add(entry.Key, entry.Value);
+ }
+ serviceDefinitionsList.Add(serviceDefinition);
+ }
+ }
+
+ /// <summary>
+ /// Adds a listening port.
+ /// </summary>
+ private int AddPortInternal(ServerPort serverPort)
+ {
+ lock (myLock)
+ {
+ Preconditions.CheckNotNull(serverPort.Credentials);
+ Preconditions.CheckState(!startRequested);
+ var address = string.Format("{0}:{1}", serverPort.Host, serverPort.Port);
+ int boundPort;
+ using (var nativeCredentials = serverPort.Credentials.ToNativeCredentials())
+ {
+ if (nativeCredentials != null)
+ {
+ boundPort = handle.AddSecurePort(address, nativeCredentials);
+ }
+ else
+ {
+ boundPort = handle.AddInsecurePort(address);
+ }
+ }
+ var newServerPort = new ServerPort(serverPort, boundPort);
+ this.serverPortList.Add(newServerPort);
+ return boundPort;
+ }
+ }
+
+ /// <summary>
/// Allows one new RPC call to be received by server.
/// </summary>
private void AllowOneRpc()
@@ -249,5 +273,82 @@ namespace Grpc.Core
{
shutdownTcs.SetResult(null);
}
+
+ /// <summary>
+ /// Collection of service definitions.
+ /// </summary>
+ public class ServiceDefinitionCollection : IEnumerable<ServerServiceDefinition>
+ {
+ readonly Server server;
+
+ internal ServiceDefinitionCollection(Server server)
+ {
+ this.server = server;
+ }
+
+ /// <summary>
+ /// Adds a service definition to the server. This is how you register
+ /// handlers for a service with the server. Only call this before Start().
+ /// </summary>
+ public void Add(ServerServiceDefinition serviceDefinition)
+ {
+ server.AddServiceDefinitionInternal(serviceDefinition);
+ }
+
+ public IEnumerator<ServerServiceDefinition> GetEnumerator()
+ {
+ return server.serviceDefinitionsList.GetEnumerator();
+ }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return server.serviceDefinitionsList.GetEnumerator();
+ }
+ }
+
+ /// <summary>
+ /// Collection of server ports.
+ /// </summary>
+ public class ServerPortCollection : IEnumerable<ServerPort>
+ {
+ readonly Server server;
+
+ internal ServerPortCollection(Server server)
+ {
+ this.server = server;
+ }
+
+ /// <summary>
+ /// Adds a new port on which server should listen.
+ /// Only call this before Start().
+ /// <returns>The port on which server will be listening.</returns>
+ /// </summary>
+ public int Add(ServerPort serverPort)
+ {
+ return server.AddPortInternal(serverPort);
+ }
+
+ /// <summary>
+ /// Adds a new port on which server should listen.
+ /// <returns>The port on which server will be listening.</returns>
+ /// </summary>
+ /// <param name="host">the host</param>
+ /// <param name="port">the port. If zero, an unused port is chosen automatically.</param>
+ /// <param name="credentials">credentials to use to secure this port.</param>
+ public int Add(string host, int port, ServerCredentials credentials)
+ {
+ return Add(new ServerPort(host, port, credentials));
+ }
+
+ public IEnumerator<ServerPort> GetEnumerator()
+ {
+ return server.serverPortList.GetEnumerator();
+ }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return server.serverPortList.GetEnumerator();
+ }
+ }
}
}
diff --git a/src/csharp/Grpc.Core/ServerCredentials.cs b/src/csharp/Grpc.Core/ServerCredentials.cs
index 32ed4b78a1..c11a1ede08 100644
--- a/src/csharp/Grpc.Core/ServerCredentials.cs
+++ b/src/csharp/Grpc.Core/ServerCredentials.cs
@@ -33,7 +33,6 @@
using System;
using System.Collections.Generic;
-using System.Collections.Immutable;
using Grpc.Core.Internal;
using Grpc.Core.Utils;
@@ -80,18 +79,26 @@ namespace Grpc.Core
{
readonly IList<KeyCertificatePair> keyCertificatePairs;
readonly string rootCertificates;
+ readonly bool forceClientAuth;
/// <summary>
/// Creates server-side SSL credentials.
/// </summary>
- /// <param name="rootCertificates">PEM encoded client root certificates used to authenticate client.</param>
/// <param name="keyCertificatePairs">Key-certificates to use.</param>
- public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertificatePairs, string rootCertificates)
+ /// <param name="rootCertificates">PEM encoded client root certificates used to authenticate client.</param>
+ /// <param name="forceClientAuth">If true, client will be rejected unless it proves its unthenticity using against rootCertificates.</param>
+ public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertificatePairs, string rootCertificates, bool forceClientAuth)
{
this.keyCertificatePairs = new List<KeyCertificatePair>(keyCertificatePairs).AsReadOnly();
Preconditions.CheckArgument(this.keyCertificatePairs.Count > 0,
"At least one KeyCertificatePair needs to be provided");
+ if (forceClientAuth)
+ {
+ Preconditions.CheckNotNull(rootCertificates,
+ "Cannot force client authentication unless you provide rootCertificates.");
+ }
this.rootCertificates = rootCertificates;
+ this.forceClientAuth = forceClientAuth;
}
/// <summary>
@@ -100,7 +107,7 @@ namespace Grpc.Core
/// using client root certificates.
/// </summary>
/// <param name="keyCertificatePairs">Key-certificates to use.</param>
- public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertificatePairs) : this(keyCertificatePairs, null)
+ public SslServerCredentials(IEnumerable<KeyCertificatePair> keyCertificatePairs) : this(keyCertificatePairs, null, false)
{
}
@@ -126,6 +133,17 @@ namespace Grpc.Core
}
}
+ /// <summary>
+ /// If true, the authenticity of client check will be enforced.
+ /// </summary>
+ public bool ForceClientAuthentication
+ {
+ get
+ {
+ return this.forceClientAuth;
+ }
+ }
+
internal override ServerCredentialsSafeHandle ToNativeCredentials()
{
int count = keyCertificatePairs.Count;
@@ -136,7 +154,7 @@ namespace Grpc.Core
certChains[i] = keyCertificatePairs[i].CertificateChain;
keys[i] = keyCertificatePairs[i].PrivateKey;
}
- return ServerCredentialsSafeHandle.CreateSslCredentials(rootCertificates, certChains, keys);
+ return ServerCredentialsSafeHandle.CreateSslCredentials(rootCertificates, certChains, keys, forceClientAuth);
}
}
}
diff --git a/src/csharp/Grpc.Core/ServerPort.cs b/src/csharp/Grpc.Core/ServerPort.cs
new file mode 100644
index 0000000000..55e4bd0062
--- /dev/null
+++ b/src/csharp/Grpc.Core/ServerPort.cs
@@ -0,0 +1,120 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+
+using Grpc.Core.Utils;
+
+namespace Grpc.Core
+{
+ /// <summary>
+ /// A port exposed by a server.
+ /// </summary>
+ public class ServerPort
+ {
+ /// <summary>
+ /// Pass this value as port to have the server choose an unused listening port for you.
+ /// Ports added to a server will contain the bound port in their <see cref="BoundPort"/> property.
+ /// </summary>
+ public const int PickUnused = 0;
+
+ readonly string host;
+ readonly int port;
+ readonly ServerCredentials credentials;
+ readonly int boundPort;
+
+ /// <summary>
+ /// Creates a new port on which server should listen.
+ /// </summary>
+ /// <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>
+ /// <param name="credentials">credentials to use to secure this port.</param>
+ public ServerPort(string host, int port, ServerCredentials credentials)
+ {
+ this.host = Preconditions.CheckNotNull(host);
+ this.port = port;
+ this.credentials = Preconditions.CheckNotNull(credentials);
+ }
+
+ /// <summary>
+ /// Creates a port from an existing <c>ServerPort</c> instance and boundPort value.
+ /// </summary>
+ internal ServerPort(ServerPort serverPort, int boundPort)
+ {
+ this.host = serverPort.host;
+ this.port = serverPort.port;
+ this.credentials = serverPort.credentials;
+ this.boundPort = boundPort;
+ }
+
+ /// <value>The host.</value>
+ public string Host
+ {
+ get
+ {
+ return host;
+ }
+ }
+
+ /// <value>The port.</value>
+ public int Port
+ {
+ get
+ {
+ return port;
+ }
+ }
+
+ /// <value>The server credentials.</value>
+ public ServerCredentials Credentials
+ {
+ get
+ {
+ return credentials;
+ }
+ }
+
+ /// <value>
+ /// The port actually bound by the server. This is useful if you let server
+ /// pick port automatically. <see cref="PickUnused"/>
+ /// </value>
+ public int BoundPort
+ {
+ get
+ {
+ return boundPort;
+ }
+ }
+ }
+}
diff --git a/src/csharp/Grpc.Core/ServerServiceDefinition.cs b/src/csharp/Grpc.Core/ServerServiceDefinition.cs
index b180186c12..a00d156e52 100644
--- a/src/csharp/Grpc.Core/ServerServiceDefinition.cs
+++ b/src/csharp/Grpc.Core/ServerServiceDefinition.cs
@@ -33,7 +33,7 @@
using System;
using System.Collections.Generic;
-using System.Collections.Immutable;
+using System.Collections.ObjectModel;
using Grpc.Core.Internal;
namespace Grpc.Core
@@ -43,14 +43,14 @@ namespace Grpc.Core
/// </summary>
public class ServerServiceDefinition
{
- readonly ImmutableDictionary<string, IServerCallHandler> callHandlers;
+ readonly ReadOnlyDictionary<string, IServerCallHandler> callHandlers;
- private ServerServiceDefinition(ImmutableDictionary<string, IServerCallHandler> callHandlers)
+ private ServerServiceDefinition(Dictionary<string, IServerCallHandler> callHandlers)
{
- this.callHandlers = callHandlers;
+ this.callHandlers = new ReadOnlyDictionary<string, IServerCallHandler>(callHandlers);
}
- internal ImmutableDictionary<string, IServerCallHandler> CallHandlers
+ internal IDictionary<string, IServerCallHandler> CallHandlers
{
get
{
@@ -115,7 +115,7 @@ namespace Grpc.Core
public ServerServiceDefinition Build()
{
- return new ServerServiceDefinition(callHandlers.ToImmutableDictionary());
+ return new ServerServiceDefinition(callHandlers);
}
}
}
diff --git a/src/csharp/Grpc.Core/packages.config b/src/csharp/Grpc.Core/packages.config
index 6cdcdf2656..9b12b9b096 100644
--- a/src/csharp/Grpc.Core/packages.config
+++ b/src/csharp/Grpc.Core/packages.config
@@ -3,5 +3,4 @@
<package id="grpc.dependencies.openssl.redist" version="1.0.2.2" targetFramework="net45" />
<package id="grpc.dependencies.zlib.redist" version="1.2.8.9" targetFramework="net45" />
<package id="Ix-Async" version="1.2.3" targetFramework="net45" />
- <package id="System.Collections.Immutable" version="1.1.36" targetFramework="net45" />
</packages> \ No newline at end of file
diff --git a/src/csharp/Grpc.Examples.MathServer/MathServer.cs b/src/csharp/Grpc.Examples.MathServer/MathServer.cs
index 468eefbe3e..5f7e717b0c 100644
--- a/src/csharp/Grpc.Examples.MathServer/MathServer.cs
+++ b/src/csharp/Grpc.Examples.MathServer/MathServer.cs
@@ -38,16 +38,19 @@ namespace math
{
class MainClass
{
+ const string Host = "0.0.0.0";
+ const int Port = 23456;
+
public static void Main(string[] args)
{
- string host = "0.0.0.0";
-
- Server server = new Server();
- server.AddServiceDefinition(Math.BindService(new MathServiceImpl()));
- int port = server.AddPort(host, 23456, ServerCredentials.Insecure);
+ Server server = new Server
+ {
+ Services = { Math.BindService(new MathServiceImpl()) },
+ Ports = { { Host, Port, ServerCredentials.Insecure } }
+ };
server.Start();
- Console.WriteLine("MathServer listening on port " + port);
+ Console.WriteLine("MathServer listening on port " + Port);
Console.WriteLine("Press any key to stop the server...");
Console.ReadKey();
diff --git a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
index 242d29a9a5..08aece7ef2 100644
--- a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
+++ b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
@@ -33,6 +33,7 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
@@ -46,7 +47,7 @@ namespace math.Tests
/// </summary>
public class MathClientServerTest
{
- string host = "localhost";
+ const string Host = "localhost";
Server server;
Channel channel;
Math.MathClient client;
@@ -54,19 +55,14 @@ namespace math.Tests
[TestFixtureSetUp]
public void Init()
{
- server = new Server();
- server.AddServiceDefinition(Math.BindService(new MathServiceImpl()));
- int port = server.AddPort(host, Server.PickUnusedPort, ServerCredentials.Insecure);
- server.Start();
- channel = new Channel(host, port, Credentials.Insecure);
- client = Math.NewClient(channel);
-
- // TODO(jtattermusch): get rid of the custom header here once we have dedicated tests
- // for header support.
- client.HeaderInterceptor = (metadata) =>
+ server = new Server
{
- metadata.Add(new Metadata.Entry("custom-header", "abcdef"));
+ Services = { Math.BindService(new MathServiceImpl()) },
+ Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
};
+ server.Start();
+ channel = new Channel(Host, server.Ports.Single().BoundPort, Credentials.Insecure);
+ client = Math.NewClient(channel);
}
[TestFixtureTearDown]
diff --git a/src/csharp/Grpc.HealthCheck.Tests/HealthClientServerTest.cs b/src/csharp/Grpc.HealthCheck.Tests/HealthClientServerTest.cs
index 9d89698a8f..024377e216 100644
--- a/src/csharp/Grpc.HealthCheck.Tests/HealthClientServerTest.cs
+++ b/src/csharp/Grpc.HealthCheck.Tests/HealthClientServerTest.cs
@@ -57,11 +57,13 @@ namespace Grpc.HealthCheck.Tests
{
serviceImpl = new HealthServiceImpl();
- server = new Server();
- server.AddServiceDefinition(Grpc.Health.V1Alpha.Health.BindService(serviceImpl));
- int port = server.AddPort(Host, Server.PickUnusedPort, ServerCredentials.Insecure);
+ server = new Server
+ {
+ Services = { Grpc.Health.V1Alpha.Health.BindService(serviceImpl) },
+ Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
+ };
server.Start();
- channel = new Channel(Host, port, Credentials.Insecure);
+ channel = new Channel(Host, server.Ports.Single().BoundPort, Credentials.Insecure);
client = Grpc.Health.V1Alpha.Health.NewClient(channel);
}
diff --git a/src/csharp/Grpc.IntegrationTesting/Grpc.IntegrationTesting.csproj b/src/csharp/Grpc.IntegrationTesting/Grpc.IntegrationTesting.csproj
index abc27f811e..06a75a3351 100644
--- a/src/csharp/Grpc.IntegrationTesting/Grpc.IntegrationTesting.csproj
+++ b/src/csharp/Grpc.IntegrationTesting/Grpc.IntegrationTesting.csproj
@@ -87,9 +87,6 @@
<Reference Include="Microsoft.Threading.Tasks.Extensions.Desktop">
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath>
</Reference>
- <Reference Include="System.Collections.Immutable">
- <HintPath>..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
- </Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\Grpc.Core\Version.cs">
diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs b/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs
index 2756ce97aa..6fa721bc1c 100644
--- a/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs
+++ b/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs
@@ -33,6 +33,7 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using grpc.testing;
@@ -47,7 +48,7 @@ namespace Grpc.IntegrationTesting
/// </summary>
public class InteropClientServerTest
{
- string host = "localhost";
+ const string Host = "localhost";
Server server;
Channel channel;
TestService.ITestServiceClient client;
@@ -55,16 +56,19 @@ namespace Grpc.IntegrationTesting
[TestFixtureSetUp]
public void Init()
{
- server = new Server();
- server.AddServiceDefinition(TestService.BindService(new TestServiceImpl()));
- int port = server.AddPort(host, Server.PickUnusedPort, TestCredentials.CreateTestServerCredentials());
+ server = new Server
+ {
+ Services = { TestService.BindService(new TestServiceImpl()) },
+ Ports = { { Host, ServerPort.PickUnused, TestCredentials.CreateTestServerCredentials() } }
+ };
server.Start();
var options = new List<ChannelOption>
{
new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride)
};
- channel = new Channel(host, port, TestCredentials.CreateTestClientCredentials(true), options);
+ int port = server.Ports.Single().BoundPort;
+ channel = new Channel(Host, port, TestCredentials.CreateTestClientCredentials(true), options);
client = TestService.NewClient(channel);
}
diff --git a/src/csharp/Grpc.IntegrationTesting/InteropServer.cs b/src/csharp/Grpc.IntegrationTesting/InteropServer.cs
index bf6947e09d..504fd11857 100644
--- a/src/csharp/Grpc.IntegrationTesting/InteropServer.cs
+++ b/src/csharp/Grpc.IntegrationTesting/InteropServer.cs
@@ -88,18 +88,20 @@ namespace Grpc.IntegrationTesting
private void Run()
{
- var server = new Server();
- server.AddServiceDefinition(TestService.BindService(new TestServiceImpl()));
+ var server = new Server
+ {
+ Services = { TestService.BindService(new TestServiceImpl()) }
+ };
string host = "0.0.0.0";
int port = options.port.Value;
if (options.useTls)
{
- server.AddPort(host, port, TestCredentials.CreateTestServerCredentials());
+ server.Ports.Add(host, port, TestCredentials.CreateTestServerCredentials());
}
else
{
- server.AddPort(host, options.port.Value, ServerCredentials.Insecure);
+ server.Ports.Add(host, options.port.Value, ServerCredentials.Insecure);
}
Console.WriteLine("Running server on " + string.Format("{0}:{1}", host, port));
server.Start();
diff --git a/src/csharp/Grpc.IntegrationTesting/SslCredentialsTest.cs b/src/csharp/Grpc.IntegrationTesting/SslCredentialsTest.cs
index 1baf40eea2..1c398eb84e 100644
--- a/src/csharp/Grpc.IntegrationTesting/SslCredentialsTest.cs
+++ b/src/csharp/Grpc.IntegrationTesting/SslCredentialsTest.cs
@@ -34,6 +34,7 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using grpc.testing;
@@ -49,7 +50,7 @@ namespace Grpc.IntegrationTesting
/// </summary>
public class SslCredentialsTest
{
- string host = "localhost";
+ const string Host = "localhost";
Server server;
Channel channel;
TestService.ITestServiceClient client;
@@ -62,12 +63,14 @@ namespace Grpc.IntegrationTesting
File.ReadAllText(TestCredentials.ServerCertChainPath),
File.ReadAllText(TestCredentials.ServerPrivateKeyPath));
- var serverCredentials = new SslServerCredentials(new[] { keyCertPair }, rootCert);
+ var serverCredentials = new SslServerCredentials(new[] { keyCertPair }, rootCert, true);
var clientCredentials = new SslCredentials(rootCert, keyCertPair);
- server = new Server();
- server.AddServiceDefinition(TestService.BindService(new TestServiceImpl()));
- int port = server.AddPort(host, Server.PickUnusedPort, serverCredentials);
+ server = new Server
+ {
+ Services = { TestService.BindService(new TestServiceImpl()) },
+ Ports = { { Host, ServerPort.PickUnused, serverCredentials } }
+ };
server.Start();
var options = new List<ChannelOption>
@@ -75,7 +78,7 @@ namespace Grpc.IntegrationTesting
new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride)
};
- channel = new Channel(host, port, clientCredentials, options);
+ channel = new Channel(Host, server.Ports.Single().BoundPort, clientCredentials, options);
client = TestService.NewClient(channel);
}
diff --git a/src/csharp/Grpc.IntegrationTesting/TestCredentials.cs b/src/csharp/Grpc.IntegrationTesting/TestCredentials.cs
index 54d8587713..da0b7fb910 100644
--- a/src/csharp/Grpc.IntegrationTesting/TestCredentials.cs
+++ b/src/csharp/Grpc.IntegrationTesting/TestCredentials.cs
@@ -33,7 +33,6 @@
using System;
using System.Collections.Generic;
-using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
diff --git a/src/csharp/Grpc.IntegrationTesting/packages.config b/src/csharp/Grpc.IntegrationTesting/packages.config
index 746133a7a5..7d1f84f303 100644
--- a/src/csharp/Grpc.IntegrationTesting/packages.config
+++ b/src/csharp/Grpc.IntegrationTesting/packages.config
@@ -11,5 +11,4 @@
<package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net45" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" />
- <package id="System.Collections.Immutable" version="1.1.36" targetFramework="net45" />
</packages> \ No newline at end of file
diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c
index a7be4077b8..37864a62a5 100644
--- a/src/csharp/ext/grpc_csharp_ext.c
+++ b/src/csharp/ext/grpc_csharp_ext.c
@@ -792,7 +792,8 @@ grpcsharp_secure_channel_create(grpc_credentials *creds, const char *target,
GPR_EXPORT grpc_server_credentials *GPR_CALLTYPE
grpcsharp_ssl_server_credentials_create(
const char *pem_root_certs, const char **key_cert_pair_cert_chain_array,
- const char **key_cert_pair_private_key_array, size_t num_key_cert_pairs) {
+ const char **key_cert_pair_private_key_array, size_t num_key_cert_pairs,
+ int force_client_auth) {
size_t i;
grpc_server_credentials *creds;
grpc_ssl_pem_key_cert_pair *key_cert_pairs =
@@ -807,9 +808,9 @@ grpcsharp_ssl_server_credentials_create(
key_cert_pairs[i].private_key = key_cert_pair_private_key_array[i];
}
}
- /* TODO: Add a force_client_auth parameter and pass it here. */
creds = grpc_ssl_server_credentials_create(pem_root_certs, key_cert_pairs,
- num_key_cert_pairs, 0);
+ num_key_cert_pairs,
+ force_client_auth);
gpr_free(key_cert_pairs);
return creds;
}