aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/Grpc.Core/Server.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/csharp/Grpc.Core/Server.cs')
-rw-r--r--src/csharp/Grpc.Core/Server.cs88
1 files changed, 38 insertions, 50 deletions
diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs
index cbf77196cf..3217547cc4 100644
--- a/src/csharp/Grpc.Core/Server.cs
+++ b/src/csharp/Grpc.Core/Server.cs
@@ -38,6 +38,7 @@ using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Grpc.Core.Internal;
+using Grpc.Core.Logging;
using Grpc.Core.Utils;
namespace Grpc.Core
@@ -52,7 +53,10 @@ namespace Grpc.Core
/// </summary>
public const int PickUnusedPort = 0;
+ static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<Server>();
+
readonly GrpcEnvironment environment;
+ readonly List<ChannelOption> options;
readonly ServerSafeHandle handle;
readonly object myLock = new object();
@@ -69,7 +73,8 @@ namespace Grpc.Core
public Server(IEnumerable<ChannelOption> options = null)
{
this.environment = GrpcEnvironment.GetInstance();
- using (var channelArgs = ChannelOptions.CreateChannelArgs(options))
+ this.options = options != null ? new List<ChannelOption>(options) : new List<ChannelOption>();
+ using (var channelArgs = ChannelOptions.CreateChannelArgs(this.options))
{
this.handle = ServerSafeHandle.NewServer(environment.CompletionQueue, channelArgs);
}
@@ -93,28 +98,31 @@ namespace Grpc.Core
}
/// <summary>
- /// Add a non-secure port on which server should listen.
- /// Only call this before Start().
- /// </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 AddListeningPort(string host, int port)
- {
- return AddListeningPortInternal(host, port, null);
- }
-
- /// <summary>
- /// Add a non-secure port on which server should listen.
+ /// Add a port on which server should listen.
/// Only call this before Start().
/// </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 AddListeningPort(string host, int port, ServerCredentials credentials)
+ public int AddPort(string host, int port, ServerCredentials credentials)
{
- Preconditions.CheckNotNull(credentials);
- return AddListeningPortInternal(host, port, credentials);
+ lock (myLock)
+ {
+ 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);
+ }
+ }
+ }
}
/// <summary>
@@ -181,26 +189,6 @@ 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>
@@ -218,20 +206,20 @@ namespace Grpc.Core
/// <summary>
/// Selects corresponding handler for given call and handles the call.
/// </summary>
- private async Task InvokeCallHandler(CallSafeHandle call, string method)
+ private async Task HandleCallAsync(ServerRpcNew newRpc)
{
try
{
IServerCallHandler callHandler;
- if (!callHandlers.TryGetValue(method, out callHandler))
+ if (!callHandlers.TryGetValue(newRpc.Method, out callHandler))
{
- callHandler = new NoSuchMethodCallHandler();
+ callHandler = NoSuchMethodCallHandler.Instance;
}
- await callHandler.HandleCall(method, call, environment);
+ await callHandler.HandleCall(newRpc, environment);
}
catch (Exception e)
{
- Console.WriteLine("Exception while handling RPC: " + e);
+ Logger.Warning(e, "Exception while handling RPC.");
}
}
@@ -240,15 +228,15 @@ namespace Grpc.Core
/// </summary>
private void HandleNewServerRpc(bool success, BatchContextSafeHandle ctx)
{
- // TODO: handle error
-
- CallSafeHandle call = ctx.GetServerRpcNewCall();
- string method = ctx.GetServerRpcNewMethod();
-
- // after server shutdown, the callback returns with null call
- if (!call.IsInvalid)
+ if (success)
{
- Task.Run(async () => await InvokeCallHandler(call, method));
+ ServerRpcNew newRpc = ctx.GetServerRpcNew();
+
+ // after server shutdown, the callback returns with null call
+ if (!newRpc.Call.IsInvalid)
+ {
+ Task.Run(async () => await HandleCallAsync(newRpc));
+ }
}
AllowOneRpc();