CallSafeHandle.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. using Grpc.Core.Profiling;
  37. namespace Grpc.Core.Internal
  38. {
  39. /// <summary>
  40. /// grpc_call from <c>grpc/grpc.h</c>
  41. /// </summary>
  42. internal class CallSafeHandle : SafeHandleZeroIsInvalid, INativeCall
  43. {
  44. public static readonly CallSafeHandle NullInstance = new CallSafeHandle();
  45. static readonly NativeMethods Native = NativeMethods.Get();
  46. const uint GRPC_WRITE_BUFFER_HINT = 1;
  47. CompletionRegistry completionRegistry;
  48. CompletionQueueSafeHandle completionQueue;
  49. private CallSafeHandle()
  50. {
  51. }
  52. public void Initialize(CompletionRegistry completionRegistry, CompletionQueueSafeHandle completionQueue)
  53. {
  54. this.completionRegistry = completionRegistry;
  55. this.completionQueue = completionQueue;
  56. }
  57. public void SetCredentials(CallCredentialsSafeHandle credentials)
  58. {
  59. Native.grpcsharp_call_set_credentials(this, credentials).CheckOk();
  60. }
  61. public void StartUnary(UnaryResponseClientHandler callback, byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags)
  62. {
  63. using (completionQueue.NewScope())
  64. {
  65. var ctx = BatchContextSafeHandle.Create();
  66. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success, context.GetReceivedStatusOnClient(), context.GetReceivedMessage(), context.GetReceivedInitialMetadata()));
  67. Native.grpcsharp_call_start_unary(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray, writeFlags)
  68. .CheckOk();
  69. }
  70. }
  71. public void StartUnary(BatchContextSafeHandle ctx, byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags)
  72. {
  73. using (Profilers.ForCurrentThread().NewScope("CallSafeHandle.StartUnary"))
  74. {
  75. Native.grpcsharp_call_start_unary(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray, writeFlags)
  76. .CheckOk();
  77. }
  78. }
  79. public void StartClientStreaming(UnaryResponseClientHandler callback, MetadataArraySafeHandle metadataArray)
  80. {
  81. using (completionQueue.NewScope())
  82. {
  83. var ctx = BatchContextSafeHandle.Create();
  84. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success, context.GetReceivedStatusOnClient(), context.GetReceivedMessage(), context.GetReceivedInitialMetadata()));
  85. Native.grpcsharp_call_start_client_streaming(this, ctx, metadataArray).CheckOk();
  86. }
  87. }
  88. public void StartServerStreaming(ReceivedStatusOnClientHandler callback, byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags)
  89. {
  90. using (completionQueue.NewScope())
  91. {
  92. var ctx = BatchContextSafeHandle.Create();
  93. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success, context.GetReceivedStatusOnClient()));
  94. Native.grpcsharp_call_start_server_streaming(this, ctx, payload, new UIntPtr((ulong)payload.Length), metadataArray, writeFlags).CheckOk();
  95. }
  96. }
  97. public void StartDuplexStreaming(ReceivedStatusOnClientHandler callback, MetadataArraySafeHandle metadataArray)
  98. {
  99. using (completionQueue.NewScope())
  100. {
  101. var ctx = BatchContextSafeHandle.Create();
  102. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success, context.GetReceivedStatusOnClient()));
  103. Native.grpcsharp_call_start_duplex_streaming(this, ctx, metadataArray).CheckOk();
  104. }
  105. }
  106. public void StartSendMessage(SendCompletionHandler callback, byte[] payload, WriteFlags writeFlags, bool sendEmptyInitialMetadata)
  107. {
  108. using (completionQueue.NewScope())
  109. {
  110. var ctx = BatchContextSafeHandle.Create();
  111. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success));
  112. Native.grpcsharp_call_send_message(this, ctx, payload, new UIntPtr((ulong)payload.Length), writeFlags, sendEmptyInitialMetadata).CheckOk();
  113. }
  114. }
  115. public void StartSendCloseFromClient(SendCompletionHandler callback)
  116. {
  117. using (completionQueue.NewScope())
  118. {
  119. var ctx = BatchContextSafeHandle.Create();
  120. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success));
  121. Native.grpcsharp_call_send_close_from_client(this, ctx).CheckOk();
  122. }
  123. }
  124. public void StartSendStatusFromServer(SendCompletionHandler callback, Status status, MetadataArraySafeHandle metadataArray, bool sendEmptyInitialMetadata,
  125. byte[] optionalPayload, WriteFlags writeFlags)
  126. {
  127. using (completionQueue.NewScope())
  128. {
  129. var ctx = BatchContextSafeHandle.Create();
  130. var optionalPayloadLength = optionalPayload != null ? new UIntPtr((ulong)optionalPayload.Length) : UIntPtr.Zero;
  131. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success));
  132. Native.grpcsharp_call_send_status_from_server(this, ctx, status.StatusCode, status.Detail, metadataArray, sendEmptyInitialMetadata,
  133. optionalPayload, optionalPayloadLength, writeFlags).CheckOk();
  134. }
  135. }
  136. public void StartReceiveMessage(ReceivedMessageHandler callback)
  137. {
  138. using (completionQueue.NewScope())
  139. {
  140. var ctx = BatchContextSafeHandle.Create();
  141. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success, context.GetReceivedMessage()));
  142. Native.grpcsharp_call_recv_message(this, ctx).CheckOk();
  143. }
  144. }
  145. public void StartReceiveInitialMetadata(ReceivedResponseHeadersHandler callback)
  146. {
  147. using (completionQueue.NewScope())
  148. {
  149. var ctx = BatchContextSafeHandle.Create();
  150. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success, context.GetReceivedInitialMetadata()));
  151. Native.grpcsharp_call_recv_initial_metadata(this, ctx).CheckOk();
  152. }
  153. }
  154. public void StartServerSide(ReceivedCloseOnServerHandler callback)
  155. {
  156. using (completionQueue.NewScope())
  157. {
  158. var ctx = BatchContextSafeHandle.Create();
  159. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success, context.GetReceivedCloseOnServerCancelled()));
  160. Native.grpcsharp_call_start_serverside(this, ctx).CheckOk();
  161. }
  162. }
  163. public void StartSendInitialMetadata(SendCompletionHandler callback, MetadataArraySafeHandle metadataArray)
  164. {
  165. using (completionQueue.NewScope())
  166. {
  167. var ctx = BatchContextSafeHandle.Create();
  168. completionRegistry.RegisterBatchCompletion(ctx, (success, context) => callback(success));
  169. Native.grpcsharp_call_send_initial_metadata(this, ctx, metadataArray).CheckOk();
  170. }
  171. }
  172. public void Cancel()
  173. {
  174. Native.grpcsharp_call_cancel(this).CheckOk();
  175. }
  176. public void CancelWithStatus(Status status)
  177. {
  178. Native.grpcsharp_call_cancel_with_status(this, status.StatusCode, status.Detail).CheckOk();
  179. }
  180. public string GetPeer()
  181. {
  182. using (var cstring = Native.grpcsharp_call_get_peer(this))
  183. {
  184. return cstring.GetValue();
  185. }
  186. }
  187. protected override bool ReleaseHandle()
  188. {
  189. Native.grpcsharp_call_destroy(handle);
  190. return true;
  191. }
  192. private static uint GetFlags(bool buffered)
  193. {
  194. return buffered ? 0 : GRPC_WRITE_BUFFER_HINT;
  195. }
  196. }
  197. }