operation.pyx.pxi 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # Copyright 2017 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. cdef class Operation:
  15. cdef void c(self) except *:
  16. raise NotImplementedError()
  17. cdef void un_c(self) except *:
  18. raise NotImplementedError()
  19. cdef class SendInitialMetadataOperation(Operation):
  20. def __cinit__(self, initial_metadata, flags):
  21. self._initial_metadata = initial_metadata
  22. self._flags = flags
  23. def type(self):
  24. return GRPC_OP_SEND_INITIAL_METADATA
  25. cdef void c(self) except *:
  26. self.c_op.type = GRPC_OP_SEND_INITIAL_METADATA
  27. self.c_op.flags = self._flags
  28. _store_c_metadata(
  29. self._initial_metadata, &self._c_initial_metadata,
  30. &self._c_initial_metadata_count)
  31. self.c_op.data.send_initial_metadata.metadata = self._c_initial_metadata
  32. self.c_op.data.send_initial_metadata.count = self._c_initial_metadata_count
  33. self.c_op.data.send_initial_metadata.maybe_compression_level.is_set = 0
  34. cdef void un_c(self) except *:
  35. _release_c_metadata(
  36. self._c_initial_metadata, self._c_initial_metadata_count)
  37. cdef class SendMessageOperation(Operation):
  38. def __cinit__(self, bytes message, int flags):
  39. if message is None:
  40. self._message = b''
  41. else:
  42. self._message = message
  43. self._flags = flags
  44. def type(self):
  45. return GRPC_OP_SEND_MESSAGE
  46. cdef void c(self) except *:
  47. self.c_op.type = GRPC_OP_SEND_MESSAGE
  48. self.c_op.flags = self._flags
  49. cdef grpc_slice message_slice = grpc_slice_from_copied_buffer(
  50. self._message, len(self._message))
  51. self._c_message_byte_buffer = grpc_raw_byte_buffer_create(
  52. &message_slice, 1)
  53. grpc_slice_unref(message_slice)
  54. self.c_op.data.send_message.send_message = self._c_message_byte_buffer
  55. cdef void un_c(self) except *:
  56. grpc_byte_buffer_destroy(self._c_message_byte_buffer)
  57. cdef class SendCloseFromClientOperation(Operation):
  58. def __cinit__(self, int flags):
  59. self._flags = flags
  60. def type(self):
  61. return GRPC_OP_SEND_CLOSE_FROM_CLIENT
  62. cdef void c(self) except *:
  63. self.c_op.type = GRPC_OP_SEND_CLOSE_FROM_CLIENT
  64. self.c_op.flags = self._flags
  65. cdef void un_c(self) except *:
  66. pass
  67. cdef class SendStatusFromServerOperation(Operation):
  68. def __cinit__(self, trailing_metadata, code, object details, int flags):
  69. self._trailing_metadata = trailing_metadata
  70. self._code = code
  71. self._details = details
  72. self._flags = flags
  73. def type(self):
  74. return GRPC_OP_SEND_STATUS_FROM_SERVER
  75. cdef void c(self) except *:
  76. self.c_op.type = GRPC_OP_SEND_STATUS_FROM_SERVER
  77. self.c_op.flags = self._flags
  78. _store_c_metadata(
  79. self._trailing_metadata, &self._c_trailing_metadata,
  80. &self._c_trailing_metadata_count)
  81. self.c_op.data.send_status_from_server.trailing_metadata = (
  82. self._c_trailing_metadata)
  83. self.c_op.data.send_status_from_server.trailing_metadata_count = (
  84. self._c_trailing_metadata_count)
  85. self.c_op.data.send_status_from_server.status = self._code
  86. self._c_details = _slice_from_bytes(_encode(self._details))
  87. self.c_op.data.send_status_from_server.status_details = &self._c_details
  88. cdef void un_c(self) except *:
  89. grpc_slice_unref(self._c_details)
  90. _release_c_metadata(
  91. self._c_trailing_metadata, self._c_trailing_metadata_count)
  92. cdef class ReceiveInitialMetadataOperation(Operation):
  93. def __cinit__(self, flags):
  94. self._flags = flags
  95. def type(self):
  96. return GRPC_OP_RECV_INITIAL_METADATA
  97. cdef void c(self) except *:
  98. self.c_op.type = GRPC_OP_RECV_INITIAL_METADATA
  99. self.c_op.flags = self._flags
  100. grpc_metadata_array_init(&self._c_initial_metadata)
  101. self.c_op.data.receive_initial_metadata.receive_initial_metadata = (
  102. &self._c_initial_metadata)
  103. cdef void un_c(self) except *:
  104. self._initial_metadata = _metadata(&self._c_initial_metadata)
  105. grpc_metadata_array_destroy(&self._c_initial_metadata)
  106. def initial_metadata(self):
  107. return self._initial_metadata
  108. cdef class ReceiveMessageOperation(Operation):
  109. def __cinit__(self, flags):
  110. self._flags = flags
  111. def type(self):
  112. return GRPC_OP_RECV_MESSAGE
  113. cdef void c(self) except *:
  114. self.c_op.type = GRPC_OP_RECV_MESSAGE
  115. self.c_op.flags = self._flags
  116. self.c_op.data.receive_message.receive_message = (
  117. &self._c_message_byte_buffer)
  118. cdef void un_c(self) except *:
  119. cdef grpc_byte_buffer_reader message_reader
  120. cdef bint message_reader_status
  121. cdef grpc_slice message_slice
  122. cdef size_t message_slice_length
  123. cdef void *message_slice_pointer
  124. if self._c_message_byte_buffer != NULL:
  125. message_reader_status = grpc_byte_buffer_reader_init(
  126. &message_reader, self._c_message_byte_buffer)
  127. if message_reader_status:
  128. message = bytearray()
  129. while grpc_byte_buffer_reader_next(&message_reader, &message_slice):
  130. message_slice_pointer = grpc_slice_start_ptr(message_slice)
  131. message_slice_length = grpc_slice_length(message_slice)
  132. message += (<char *>message_slice_pointer)[:message_slice_length]
  133. grpc_slice_unref(message_slice)
  134. grpc_byte_buffer_reader_destroy(&message_reader)
  135. self._message = bytes(message)
  136. else:
  137. self._message = None
  138. grpc_byte_buffer_destroy(self._c_message_byte_buffer)
  139. else:
  140. self._message = None
  141. def message(self):
  142. return self._message
  143. cdef class ReceiveStatusOnClientOperation(Operation):
  144. def __cinit__(self, flags):
  145. self._flags = flags
  146. def type(self):
  147. return GRPC_OP_RECV_STATUS_ON_CLIENT
  148. cdef void c(self) except *:
  149. self.c_op.type = GRPC_OP_RECV_STATUS_ON_CLIENT
  150. self.c_op.flags = self._flags
  151. grpc_metadata_array_init(&self._c_trailing_metadata)
  152. self.c_op.data.receive_status_on_client.trailing_metadata = (
  153. &self._c_trailing_metadata)
  154. self.c_op.data.receive_status_on_client.status = (
  155. &self._c_code)
  156. self.c_op.data.receive_status_on_client.status_details = (
  157. &self._c_details)
  158. self.c_op.data.receive_status_on_client.error_string = (
  159. &self._c_error_string)
  160. cdef void un_c(self) except *:
  161. self._trailing_metadata = _metadata(&self._c_trailing_metadata)
  162. grpc_metadata_array_destroy(&self._c_trailing_metadata)
  163. self._code = self._c_code
  164. self._details = _decode(_slice_bytes(self._c_details))
  165. grpc_slice_unref(self._c_details)
  166. if self._c_error_string != NULL:
  167. self._error_string = _decode(self._c_error_string)
  168. gpr_free(<void*>self._c_error_string)
  169. else:
  170. self._error_string = ""
  171. def trailing_metadata(self):
  172. return self._trailing_metadata
  173. def code(self):
  174. return self._code
  175. def details(self):
  176. return self._details
  177. def error_string(self):
  178. return self._error_string
  179. cdef class ReceiveCloseOnServerOperation(Operation):
  180. def __cinit__(self, flags):
  181. self._flags = flags
  182. def type(self):
  183. return GRPC_OP_RECV_CLOSE_ON_SERVER
  184. cdef void c(self) except *:
  185. self.c_op.type = GRPC_OP_RECV_CLOSE_ON_SERVER
  186. self.c_op.flags = self._flags
  187. self.c_op.data.receive_close_on_server.cancelled = &self._c_cancelled
  188. cdef void un_c(self) except *:
  189. self._cancelled = bool(self._c_cancelled)
  190. def cancelled(self):
  191. return self._cancelled