|
|
@ -33,203 +33,159 @@ using System; |
|
|
|
using System.Diagnostics; |
|
|
|
using System.Diagnostics; |
|
|
|
using System.Runtime.InteropServices; |
|
|
|
using System.Runtime.InteropServices; |
|
|
|
using Grpc.Core; |
|
|
|
using Grpc.Core; |
|
|
|
|
|
|
|
using Grpc.Core.Utils; |
|
|
|
|
|
|
|
|
|
|
|
namespace Grpc.Core.Internal { |
|
|
|
namespace Grpc.Core.Internal |
|
|
|
internal delegate void CompletionCallbackDelegate(GRPCOpError error, |
|
|
|
{ |
|
|
|
IntPtr batchContextPtr); |
|
|
|
internal delegate void CompletionCallbackDelegate(GRPCOpError error, IntPtr batchContextPtr); |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// grpc_call from <grpc/grpc.h> |
|
|
|
/// grpc_call from <grpc/grpc.h> |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
internal class CallSafeHandle : SafeHandleZeroIsInvalid { |
|
|
|
internal class CallSafeHandle : SafeHandleZeroIsInvalid |
|
|
|
|
|
|
|
{ |
|
|
|
const uint GRPC_WRITE_BUFFER_HINT = 1; |
|
|
|
const uint GRPC_WRITE_BUFFER_HINT = 1; |
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern CallSafeHandle |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
grpcsharp_channel_create_call(ChannelSafeHandle channel, |
|
|
|
static extern CallSafeHandle grpcsharp_channel_create_call(ChannelSafeHandle channel, CompletionQueueSafeHandle cq, string method, string host, Timespec deadline); |
|
|
|
CompletionQueueSafeHandle cq, string method, |
|
|
|
|
|
|
|
string host, Timespec deadline); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
grpcsharp_call_cancel(CallSafeHandle call); |
|
|
|
static extern GRPCCallError grpcsharp_call_cancel(CallSafeHandle call); |
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
grpcsharp_call_cancel_with_status(CallSafeHandle call, StatusCode status, |
|
|
|
static extern GRPCCallError grpcsharp_call_cancel_with_status(CallSafeHandle call, StatusCode status, string description); |
|
|
|
string description); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
grpcsharp_call_start_unary( |
|
|
|
static extern GRPCCallError grpcsharp_call_start_unary(CallSafeHandle call, |
|
|
|
CallSafeHandle call, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate |
|
|
|
byte[] send_buffer, UIntPtr send_buffer_len, MetadataArraySafeHandle metadataArray); |
|
|
|
callback, |
|
|
|
|
|
|
|
byte[] send_buffer, UIntPtr send_buffer_len, |
|
|
|
|
|
|
|
MetadataArraySafeHandle metadataArray); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern void |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
grpcsharp_call_blocking_unary( |
|
|
|
static extern void grpcsharp_call_blocking_unary(CallSafeHandle call, CompletionQueueSafeHandle dedicatedCq, |
|
|
|
CallSafeHandle call, CompletionQueueSafeHandle dedicatedCq, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate |
|
|
|
byte[] send_buffer, UIntPtr send_buffer_len, MetadataArraySafeHandle metadataArray); |
|
|
|
callback, |
|
|
|
|
|
|
|
byte[] send_buffer, UIntPtr send_buffer_len, |
|
|
|
|
|
|
|
MetadataArraySafeHandle metadataArray); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
grpcsharp_call_start_client_streaming( |
|
|
|
static extern GRPCCallError grpcsharp_call_start_client_streaming(CallSafeHandle call, |
|
|
|
CallSafeHandle call, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate |
|
|
|
|
|
|
|
callback, |
|
|
|
|
|
|
|
MetadataArraySafeHandle metadataArray); |
|
|
|
MetadataArraySafeHandle metadataArray); |
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
grpcsharp_call_start_server_streaming( |
|
|
|
static extern GRPCCallError grpcsharp_call_start_server_streaming(CallSafeHandle call, |
|
|
|
CallSafeHandle call, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate |
|
|
|
|
|
|
|
callback, |
|
|
|
|
|
|
|
byte[] send_buffer, UIntPtr send_buffer_len, |
|
|
|
byte[] send_buffer, UIntPtr send_buffer_len, |
|
|
|
MetadataArraySafeHandle metadataArray); |
|
|
|
MetadataArraySafeHandle metadataArray); |
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
grpcsharp_call_start_duplex_streaming( |
|
|
|
static extern GRPCCallError grpcsharp_call_start_duplex_streaming(CallSafeHandle call, |
|
|
|
CallSafeHandle call, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate |
|
|
|
|
|
|
|
callback, |
|
|
|
|
|
|
|
MetadataArraySafeHandle metadataArray); |
|
|
|
MetadataArraySafeHandle metadataArray); |
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
grpcsharp_call_send_message( |
|
|
|
static extern GRPCCallError grpcsharp_call_send_message(CallSafeHandle call, |
|
|
|
CallSafeHandle call, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate |
|
|
|
|
|
|
|
callback, |
|
|
|
|
|
|
|
byte[] send_buffer, UIntPtr send_buffer_len); |
|
|
|
byte[] send_buffer, UIntPtr send_buffer_len); |
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
grpcsharp_call_send_close_from_client( |
|
|
|
static extern GRPCCallError grpcsharp_call_send_close_from_client(CallSafeHandle call, |
|
|
|
CallSafeHandle call, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback); |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate |
|
|
|
|
|
|
|
callback); |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
|
|
|
|
static extern GRPCCallError grpcsharp_call_send_status_from_server(CallSafeHandle call, [MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback, StatusCode statusCode, string statusMessage); |
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
|
|
|
|
grpcsharp_call_send_status_from_server( |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
CallSafeHandle call, |
|
|
|
static extern GRPCCallError grpcsharp_call_recv_message(CallSafeHandle call, |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback); |
|
|
|
callback, |
|
|
|
|
|
|
|
StatusCode statusCode, string statusMessage); |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
|
|
|
|
static extern GRPCCallError grpcsharp_call_start_serverside(CallSafeHandle call, |
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate callback); |
|
|
|
grpcsharp_call_recv_message( |
|
|
|
|
|
|
|
CallSafeHandle call, |
|
|
|
[DllImport("grpc_csharp_ext.dll")] |
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate |
|
|
|
static extern void grpcsharp_call_destroy(IntPtr call); |
|
|
|
callback); |
|
|
|
|
|
|
|
|
|
|
|
private CallSafeHandle() |
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError |
|
|
|
{ |
|
|
|
grpcsharp_call_start_serverside( |
|
|
|
} |
|
|
|
CallSafeHandle call, |
|
|
|
|
|
|
|
[MarshalAs(UnmanagedType.FunctionPtr)] CompletionCallbackDelegate |
|
|
|
public static CallSafeHandle Create(ChannelSafeHandle channel, CompletionQueueSafeHandle cq, string method, string host, Timespec deadline) |
|
|
|
callback); |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
[DllImport("grpc_csharp_ext.dll")] static extern void |
|
|
|
|
|
|
|
grpcsharp_call_destroy(IntPtr call); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
CallSafeHandle() {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
|
|
|
|
static CallSafeHandle Create(ChannelSafeHandle channel, |
|
|
|
|
|
|
|
CompletionQueueSafeHandle cq, string method, |
|
|
|
|
|
|
|
string host, Timespec deadline) { |
|
|
|
|
|
|
|
return grpcsharp_channel_create_call(channel, cq, method, host, deadline); |
|
|
|
return grpcsharp_channel_create_call(channel, cq, method, host, deadline); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void StartUnary(byte[] payload, CompletionCallbackDelegate callback, MetadataArraySafeHandle metadataArray) |
|
|
|
void StartUnary(byte[] payload, CompletionCallbackDelegate callback, |
|
|
|
{ |
|
|
|
MetadataArraySafeHandle metadataArray) { |
|
|
|
AssertCallOk(grpcsharp_call_start_unary(this, callback, payload, new UIntPtr((ulong)payload.Length), metadataArray)); |
|
|
|
AssertCallOk(grpcsharp_call_start_unary( |
|
|
|
|
|
|
|
this, callback, payload, new UIntPtr((ulong)payload.Length), |
|
|
|
|
|
|
|
metadataArray)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void BlockingUnary(CompletionQueueSafeHandle dedicatedCq, byte[] payload, CompletionCallbackDelegate callback, MetadataArraySafeHandle metadataArray) |
|
|
|
void BlockingUnary(CompletionQueueSafeHandle dedicatedCq, byte[] payload, |
|
|
|
{ |
|
|
|
CompletionCallbackDelegate callback, |
|
|
|
grpcsharp_call_blocking_unary(this, dedicatedCq, callback, payload, new UIntPtr((ulong)payload.Length), metadataArray); |
|
|
|
MetadataArraySafeHandle metadataArray) { |
|
|
|
|
|
|
|
grpcsharp_call_blocking_unary(this, dedicatedCq, callback, payload, |
|
|
|
|
|
|
|
new UIntPtr((ulong)payload.Length), |
|
|
|
|
|
|
|
metadataArray); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void StartClientStreaming(CompletionCallbackDelegate callback, MetadataArraySafeHandle metadataArray) |
|
|
|
void StartClientStreaming(CompletionCallbackDelegate callback, |
|
|
|
{ |
|
|
|
MetadataArraySafeHandle metadataArray) { |
|
|
|
AssertCallOk(grpcsharp_call_start_client_streaming(this, callback, metadataArray)); |
|
|
|
AssertCallOk( |
|
|
|
|
|
|
|
grpcsharp_call_start_client_streaming(this, callback, metadataArray)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void StartServerStreaming(byte[] payload, CompletionCallbackDelegate callback, MetadataArraySafeHandle metadataArray) |
|
|
|
void StartServerStreaming(byte[] payload, |
|
|
|
{ |
|
|
|
CompletionCallbackDelegate callback, |
|
|
|
AssertCallOk(grpcsharp_call_start_server_streaming(this, callback, payload, new UIntPtr((ulong)payload.Length), metadataArray)); |
|
|
|
MetadataArraySafeHandle metadataArray) { |
|
|
|
|
|
|
|
AssertCallOk(grpcsharp_call_start_server_streaming( |
|
|
|
|
|
|
|
this, callback, payload, new UIntPtr((ulong)payload.Length), |
|
|
|
|
|
|
|
metadataArray)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void StartDuplexStreaming(CompletionCallbackDelegate callback, MetadataArraySafeHandle metadataArray) |
|
|
|
void StartDuplexStreaming(CompletionCallbackDelegate callback, |
|
|
|
{ |
|
|
|
MetadataArraySafeHandle metadataArray) { |
|
|
|
AssertCallOk(grpcsharp_call_start_duplex_streaming(this, callback, metadataArray)); |
|
|
|
AssertCallOk( |
|
|
|
|
|
|
|
grpcsharp_call_start_duplex_streaming(this, callback, metadataArray)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void StartSendMessage(byte[] payload, CompletionCallbackDelegate callback) |
|
|
|
void StartSendMessage(byte[] payload, CompletionCallbackDelegate callback) { |
|
|
|
{ |
|
|
|
AssertCallOk(grpcsharp_call_send_message( |
|
|
|
AssertCallOk(grpcsharp_call_send_message(this, callback, payload, new UIntPtr((ulong)payload.Length))); |
|
|
|
this, callback, payload, new UIntPtr((ulong)payload.Length))); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void StartSendCloseFromClient(CompletionCallbackDelegate callback) |
|
|
|
void StartSendCloseFromClient(CompletionCallbackDelegate callback) { |
|
|
|
{ |
|
|
|
AssertCallOk(grpcsharp_call_send_close_from_client(this, callback)); |
|
|
|
AssertCallOk(grpcsharp_call_send_close_from_client(this, callback)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void StartSendStatusFromServer(Status status, CompletionCallbackDelegate callback) |
|
|
|
void StartSendStatusFromServer(Status status, |
|
|
|
{ |
|
|
|
CompletionCallbackDelegate callback) { |
|
|
|
AssertCallOk(grpcsharp_call_send_status_from_server(this, callback, status.StatusCode, status.Detail)); |
|
|
|
AssertCallOk(grpcsharp_call_send_status_from_server( |
|
|
|
|
|
|
|
this, callback, status.StatusCode, status.Detail)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void StartReceiveMessage(CompletionCallbackDelegate callback) |
|
|
|
void StartReceiveMessage(CompletionCallbackDelegate callback) { |
|
|
|
{ |
|
|
|
AssertCallOk(grpcsharp_call_recv_message(this, callback)); |
|
|
|
AssertCallOk(grpcsharp_call_recv_message(this, callback)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void StartServerSide(CompletionCallbackDelegate callback) |
|
|
|
void StartServerSide(CompletionCallbackDelegate callback) { |
|
|
|
{ |
|
|
|
AssertCallOk(grpcsharp_call_start_serverside(this, callback)); |
|
|
|
AssertCallOk(grpcsharp_call_start_serverside(this, callback)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void Cancel() |
|
|
|
void Cancel() { AssertCallOk(grpcsharp_call_cancel(this)); } |
|
|
|
{ |
|
|
|
|
|
|
|
AssertCallOk(grpcsharp_call_cancel(this)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public |
|
|
|
public void CancelWithStatus(Status status) |
|
|
|
void CancelWithStatus(Status status) { |
|
|
|
{ |
|
|
|
AssertCallOk(grpcsharp_call_cancel_with_status(this, status.StatusCode, |
|
|
|
AssertCallOk(grpcsharp_call_cancel_with_status(this, status.StatusCode, status.Detail)); |
|
|
|
status.Detail)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected |
|
|
|
protected override bool ReleaseHandle() |
|
|
|
override bool ReleaseHandle() { |
|
|
|
{ |
|
|
|
grpcsharp_call_destroy(handle); |
|
|
|
grpcsharp_call_destroy(handle); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
private static void AssertCallOk(GRPCCallError callError) |
|
|
|
static void AssertCallOk(GRPCCallError callError) { |
|
|
|
{ |
|
|
|
Trace.Assert(callError == GRPCCallError.GRPC_CALL_OK, |
|
|
|
Preconditions.CheckState(callError == GRPCCallError.GRPC_CALL_OK, "Status not GRPC_CALL_OK"); |
|
|
|
"Status not GRPC_CALL_OK"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
private static uint GetFlags(bool buffered) |
|
|
|
static uint GetFlags(bool buffered) { |
|
|
|
{ |
|
|
|
return buffered ? 0 : GRPC_WRITE_BUFFER_HINT; |
|
|
|
return buffered ? 0 : GRPC_WRITE_BUFFER_HINT; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|