CallSafeHandle.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #region Copyright notice and license
  2. // Copyright 2015, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Diagnostics;
  33. using System.Runtime.InteropServices;
  34. using Grpc.Core;
  35. using Grpc.Core.Utils;
  36. namespace Grpc.Core.Internal
  37. {
  38. /// <summary>
  39. /// grpc_call from <grpc/grpc.h>
  40. /// </summary>
  41. internal class CallSafeHandle : SafeHandleZeroIsInvalid
  42. {
  43. const uint GRPC_WRITE_BUFFER_HINT = 1;
  44. CompletionRegistry completionRegistry;
  45. [DllImport("grpc_csharp_ext.dll")]
  46. static extern GRPCCallError grpcsharp_call_cancel(CallSafeHandle call);
  47. [DllImport("grpc_csharp_ext.dll")]
  48. static extern GRPCCallError grpcsharp_call_cancel_with_status(CallSafeHandle call, StatusCode status, string description);
  49. [DllImport("grpc_csharp_ext.dll")]
  50. static extern GRPCCallError grpcsharp_call_start_unary(CallSafeHandle call,
  51. BatchContextSafeHandle ctx, byte[] send_buffer, UIntPtr send_buffer_len, MetadataArraySafeHandle metadataArray);
  52. [DllImport("grpc_csharp_ext.dll")]
  53. static extern GRPCCallError grpcsharp_call_start_client_streaming(CallSafeHandle call,
  54. BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray);
  55. [DllImport("grpc_csharp_ext.dll")]
  56. static extern GRPCCallError grpcsharp_call_start_server_streaming(CallSafeHandle call,
  57. BatchContextSafeHandle ctx, byte[] send_buffer, UIntPtr send_buffer_len,
  58. MetadataArraySafeHandle metadataArray);
  59. [DllImport("grpc_csharp_ext.dll")]
  60. static extern GRPCCallError grpcsharp_call_start_duplex_streaming(CallSafeHandle call,
  61. BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray);
  62. [DllImport("grpc_csharp_ext.dll")]
  63. static extern GRPCCallError grpcsharp_call_send_message(CallSafeHandle call,
  64. BatchContextSafeHandle ctx, byte[] send_buffer, UIntPtr send_buffer_len);
  65. [DllImport("grpc_csharp_ext.dll")]
  66. static extern GRPCCallError grpcsharp_call_send_close_from_client(CallSafeHandle call,
  67. BatchContextSafeHandle ctx);
  68. [DllImport("grpc_csharp_ext.dll")]
  69. static extern GRPCCallError grpcsharp_call_send_status_from_server(CallSafeHandle call,
  70. BatchContextSafeHandle ctx, StatusCode statusCode, string statusMessage, MetadataArraySafeHandle metadataArray);
  71. [DllImport("grpc_csharp_ext.dll")]
  72. static extern GRPCCallError grpcsharp_call_recv_message(CallSafeHandle call,
  73. BatchContextSafeHandle ctx);
  74. [DllImport("grpc_csharp_ext.dll")]
  75. static extern GRPCCallError grpcsharp_call_start_serverside(CallSafeHandle call,
  76. BatchContextSafeHandle ctx);
  77. [DllImport("grpc_csharp_ext.dll")]
  78. static extern void grpcsharp_call_destroy(IntPtr call);
  79. private CallSafeHandle()
  80. {
  81. }
  82. public void SetCompletionRegistry(CompletionRegistry completionRegistry)
  83. {
  84. this.completionRegistry = completionRegistry;
  85. }
  86. public void StartUnary(byte[] payload, BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray)
  87. {
  88. var ctx = BatchContextSafeHandle.Create();
  89. completionRegistry.RegisterBatchCompletion(ctx, callback);
  90. grpcsharp_call_start_unary(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray)
  91. .CheckOk();
  92. }
  93. public void StartUnary(byte[] payload, BatchContextSafeHandle ctx, MetadataArraySafeHandle metadataArray)
  94. {
  95. grpcsharp_call_start_unary(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray)
  96. .CheckOk();
  97. }
  98. public void StartClientStreaming(BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray)
  99. {
  100. var ctx = BatchContextSafeHandle.Create();
  101. completionRegistry.RegisterBatchCompletion(ctx, callback);
  102. grpcsharp_call_start_client_streaming(this, ctx, metadataArray).CheckOk();
  103. }
  104. public void StartServerStreaming(byte[] payload, BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray)
  105. {
  106. var ctx = BatchContextSafeHandle.Create();
  107. completionRegistry.RegisterBatchCompletion(ctx, callback);
  108. grpcsharp_call_start_server_streaming(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray).CheckOk();
  109. }
  110. public void StartDuplexStreaming(BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray)
  111. {
  112. var ctx = BatchContextSafeHandle.Create();
  113. completionRegistry.RegisterBatchCompletion(ctx, callback);
  114. grpcsharp_call_start_duplex_streaming(this, ctx, metadataArray).CheckOk();
  115. }
  116. public void StartSendMessage(byte[] payload, BatchCompletionDelegate callback)
  117. {
  118. var ctx = BatchContextSafeHandle.Create();
  119. completionRegistry.RegisterBatchCompletion(ctx, callback);
  120. grpcsharp_call_send_message(this, ctx, payload, new UIntPtr((ulong)payload.Length)).CheckOk();
  121. }
  122. public void StartSendCloseFromClient(BatchCompletionDelegate callback)
  123. {
  124. var ctx = BatchContextSafeHandle.Create();
  125. completionRegistry.RegisterBatchCompletion(ctx, callback);
  126. grpcsharp_call_send_close_from_client(this, ctx).CheckOk();
  127. }
  128. public void StartSendStatusFromServer(Status status, BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray)
  129. {
  130. var ctx = BatchContextSafeHandle.Create();
  131. completionRegistry.RegisterBatchCompletion(ctx, callback);
  132. grpcsharp_call_send_status_from_server(this, ctx, status.StatusCode, status.Detail, metadataArray).CheckOk();
  133. }
  134. public void StartReceiveMessage(BatchCompletionDelegate callback)
  135. {
  136. var ctx = BatchContextSafeHandle.Create();
  137. completionRegistry.RegisterBatchCompletion(ctx, callback);
  138. grpcsharp_call_recv_message(this, ctx).CheckOk();
  139. }
  140. public void StartServerSide(BatchCompletionDelegate callback)
  141. {
  142. var ctx = BatchContextSafeHandle.Create();
  143. completionRegistry.RegisterBatchCompletion(ctx, callback);
  144. grpcsharp_call_start_serverside(this, ctx).CheckOk();
  145. }
  146. public void Cancel()
  147. {
  148. grpcsharp_call_cancel(this).CheckOk();
  149. }
  150. public void CancelWithStatus(Status status)
  151. {
  152. grpcsharp_call_cancel_with_status(this, status.StatusCode, status.Detail).CheckOk();
  153. }
  154. protected override bool ReleaseHandle()
  155. {
  156. grpcsharp_call_destroy(handle);
  157. return true;
  158. }
  159. private static uint GetFlags(bool buffered)
  160. {
  161. return buffered ? 0 : GRPC_WRITE_BUFFER_HINT;
  162. }
  163. }
  164. }