Skip to content
Snippets Groups Projects
Commit 57e289c1 authored by Tim Emiola's avatar Tim Emiola
Browse files

Merge pull request #1504 from jtattermusch/csharp_api_fixes

Polishing the new C# API
parents eb697aa1 03e82e2c
No related branches found
No related tags found
No related merge requests found
Showing
with 100 additions and 31 deletions
......@@ -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);
}
......@@ -220,7 +220,7 @@ namespace Grpc.Core.Tests
}
}
private static async Task<string> EchoHandler(string request)
private static async Task<string> EchoHandler(ServerCallContext context, string request)
{
if (request == "THROW")
{
......@@ -229,7 +229,7 @@ namespace Grpc.Core.Tests
return request;
}
private static async Task<string> ConcatAndEchoHandler(IAsyncStreamReader<string> requestStream)
private static async Task<string> ConcatAndEchoHandler(ServerCallContext context, IAsyncStreamReader<string> requestStream)
{
string result = "";
await requestStream.ForEach(async (request) =>
......
......@@ -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();
......
......@@ -40,7 +40,9 @@ namespace Grpc.Core
/// <summary>
/// Return type for client streaming calls.
/// </summary>
public struct AsyncClientStreamingCall<TRequest, TResponse>
public sealed class AsyncClientStreamingCall<TRequest, TResponse>
where TRequest : class
where TResponse : class
{
readonly IClientStreamWriter<TRequest> requestStream;
readonly Task<TResponse> result;
......
......@@ -40,7 +40,9 @@ namespace Grpc.Core
/// <summary>
/// Return type for bidirectional streaming calls.
/// </summary>
public struct AsyncDuplexStreamingCall<TRequest, TResponse>
public sealed class AsyncDuplexStreamingCall<TRequest, TResponse>
where TRequest : class
where TResponse : class
{
readonly IClientStreamWriter<TRequest> requestStream;
readonly IAsyncStreamReader<TResponse> responseStream;
......
......@@ -40,7 +40,8 @@ namespace Grpc.Core
/// <summary>
/// Return type for server streaming calls.
/// </summary>
public struct AsyncServerStreamingCall<TResponse>
public sealed class AsyncServerStreamingCall<TResponse>
where TResponse : class
{
readonly IAsyncStreamReader<TResponse> responseStream;
......
......@@ -41,6 +41,8 @@ namespace Grpc.Core
/// Abstraction of a call to be invoked on a client.
/// </summary>
public class Call<TRequest, TResponse>
where TRequest : class
where TResponse : class
{
readonly string name;
readonly Marshaller<TRequest> requestMarshaller;
......
......@@ -44,6 +44,8 @@ namespace Grpc.Core
public static class Calls
{
public static TResponse BlockingUnaryCall<TRequest, TResponse>(Call<TRequest, TResponse> call, TRequest req, CancellationToken token)
where TRequest : class
where TResponse : class
{
var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
// TODO(jtattermusch): this gives a race that cancellation can be requested before the call even starts.
......@@ -52,6 +54,8 @@ namespace Grpc.Core
}
public static async Task<TResponse> AsyncUnaryCall<TRequest, TResponse>(Call<TRequest, TResponse> call, TRequest req, CancellationToken token)
where TRequest : class
where TResponse : class
{
var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
asyncCall.Initialize(call.Channel, GetCompletionQueue(), call.Name);
......@@ -61,6 +65,8 @@ namespace Grpc.Core
}
public static AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, TRequest req, CancellationToken token)
where TRequest : class
where TResponse : class
{
var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
asyncCall.Initialize(call.Channel, GetCompletionQueue(), call.Name);
......@@ -71,6 +77,8 @@ namespace Grpc.Core
}
public static AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, CancellationToken token)
where TRequest : class
where TResponse : class
{
var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
asyncCall.Initialize(call.Channel, GetCompletionQueue(), call.Name);
......@@ -81,6 +89,8 @@ namespace Grpc.Core
}
public static AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, CancellationToken token)
where TRequest : class
where TResponse : class
{
var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
asyncCall.Initialize(call.Channel, GetCompletionQueue(), call.Name);
......
......@@ -96,6 +96,7 @@
<Compile Include="Internal\ServerResponseStream.cs" />
<Compile Include="Internal\AtomicCounter.cs" />
<Compile Include="Internal\DebugStats.cs" />
<Compile Include="ServerCallContext.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......
......@@ -44,9 +44,10 @@ namespace Grpc.Core
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IAsyncStreamReader<T>
where T : class
{
/// <summary>
/// Reads a single message. Returns default(T) if the last message was already read.
/// Reads a single message. Returns null if the last message was already read.
/// A following read can only be started when the previous one finishes.
/// </summary>
Task<T> ReadNext();
......
......@@ -44,6 +44,7 @@ namespace Grpc.Core
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IAsyncStreamWriter<T>
where T : class
{
/// <summary>
/// Writes a single message. Only one write can be pending at a time.
......
......@@ -44,6 +44,7 @@ namespace Grpc.Core
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IClientStreamWriter<T> : IAsyncStreamWriter<T>
where T : class
{
/// <summary>
/// Closes the stream. Can only be called once there is no pending write. No writes should follow calling this.
......
......@@ -43,6 +43,7 @@ namespace Grpc.Core
/// A writable stream of messages that is used in server-side handlers.
/// </summary>
public interface IServerStreamWriter<T> : IAsyncStreamWriter<T>
where T : class
{
}
}
......@@ -138,9 +138,7 @@ namespace Grpc.Core.Internal
ReleaseResourcesIfPossible();
}
// TODO(jtattermusch): check if call was cancelled.
// TODO: handle error ...
// TODO(jtattermusch): handle error
finishedServersideTcs.SetResult(null);
}
......
......@@ -38,6 +38,8 @@ namespace Grpc.Core.Internal
/// Writes requests asynchronously to an underlying AsyncCall object.
/// </summary>
internal class ClientRequestStream<TRequest, TResponse> : IClientStreamWriter<TRequest>
where TRequest : class
where TResponse : class
{
readonly AsyncCall<TRequest, TResponse> call;
......
......@@ -38,6 +38,8 @@ using System.Threading.Tasks;
namespace Grpc.Core.Internal
{
internal class ClientResponseStream<TRequest, TResponse> : IAsyncStreamReader<TResponse>
where TRequest : class
where TResponse : class
{
readonly AsyncCall<TRequest, TResponse> call;
......
......@@ -45,6 +45,8 @@ namespace Grpc.Core.Internal
}
internal class UnaryServerCallHandler<TRequest, TResponse> : IServerCallHandler
where TRequest : class
where TResponse : class
{
readonly Method<TRequest, TResponse> method;
readonly UnaryServerMethod<TRequest, TResponse> handler;
......@@ -72,7 +74,8 @@ namespace Grpc.Core.Internal
var request = await requestStream.ReadNext();
// TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated.
Preconditions.CheckArgument(await requestStream.ReadNext() == null);
var result = await handler(request);
var context = new ServerCallContext(); // TODO(jtattermusch): initialize the context
var result = await handler(context, request);
await responseStream.Write(result);
}
catch (Exception e)
......@@ -93,6 +96,8 @@ namespace Grpc.Core.Internal
}
internal class ServerStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler
where TRequest : class
where TResponse : class
{
readonly Method<TRequest, TResponse> method;
readonly ServerStreamingServerMethod<TRequest, TResponse> handler;
......@@ -121,7 +126,8 @@ namespace Grpc.Core.Internal
// TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated.
Preconditions.CheckArgument(await requestStream.ReadNext() == null);
await handler(request, responseStream);
var context = new ServerCallContext(); // TODO(jtattermusch): initialize the context
await handler(context, request, responseStream);
}
catch (Exception e)
{
......@@ -142,6 +148,8 @@ namespace Grpc.Core.Internal
}
internal class ClientStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler
where TRequest : class
where TResponse : class
{
readonly Method<TRequest, TResponse> method;
readonly ClientStreamingServerMethod<TRequest, TResponse> handler;
......@@ -162,11 +170,12 @@ namespace Grpc.Core.Internal
var finishedTask = asyncCall.ServerSideCallAsync();
var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall);
var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall);
var context = new ServerCallContext(); // TODO(jtattermusch): initialize the context
Status status = Status.DefaultSuccess;
try
{
var result = await handler(requestStream);
var result = await handler(context, requestStream);
try
{
await responseStream.Write(result);
......@@ -195,6 +204,8 @@ namespace Grpc.Core.Internal
}
internal class DuplexStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler
where TRequest : class
where TResponse : class
{
readonly Method<TRequest, TResponse> method;
readonly DuplexStreamingServerMethod<TRequest, TResponse> handler;
......@@ -215,11 +226,12 @@ namespace Grpc.Core.Internal
var finishedTask = asyncCall.ServerSideCallAsync();
var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall);
var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall);
var context = new ServerCallContext(); // TODO(jtattermusch): initialize the context
Status status = Status.DefaultSuccess;
try
{
await handler(requestStream, responseStream);
await handler(context, requestStream, responseStream);
}
catch (Exception e)
{
......
......@@ -41,21 +41,29 @@ namespace Grpc.Core.Internal
internal static class ServerCalls
{
public static IServerCallHandler UnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, UnaryServerMethod<TRequest, TResponse> handler)
where TRequest : class
where TResponse : class
{
return new UnaryServerCallHandler<TRequest, TResponse>(method, handler);
}
public static IServerCallHandler ClientStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, ClientStreamingServerMethod<TRequest, TResponse> handler)
where TRequest : class
where TResponse : class
{
return new ClientStreamingServerCallHandler<TRequest, TResponse>(method, handler);
}
public static IServerCallHandler ServerStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, ServerStreamingServerMethod<TRequest, TResponse> handler)
where TRequest : class
where TResponse : class
{
return new ServerStreamingServerCallHandler<TRequest, TResponse>(method, handler);
}
public static IServerCallHandler DuplexStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, DuplexStreamingServerMethod<TRequest, TResponse> handler)
where TRequest : class
where TResponse : class
{
return new DuplexStreamingServerCallHandler<TRequest, TResponse>(method, handler);
}
......
......@@ -38,6 +38,8 @@ using System.Threading.Tasks;
namespace Grpc.Core.Internal
{
internal class ServerRequestStream<TRequest, TResponse> : IAsyncStreamReader<TRequest>
where TRequest : class
where TResponse : class
{
readonly AsyncCallServer<TRequest, TResponse> call;
......
......@@ -39,6 +39,8 @@ namespace Grpc.Core.Internal
/// Writes responses asynchronously to an underlying AsyncCallServer object.
/// </summary>
internal class ServerResponseStream<TRequest, TResponse> : IServerStreamWriter<TResponse>
where TRequest : class
where TResponse : class
{
readonly AsyncCallServer<TRequest, TResponse> call;
......
......@@ -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>
......
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