1# Copyright 2015 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 15 16cdef bytes _slice_bytes(grpc_slice slice): 17 cdef void *start = grpc_slice_start_ptr(slice) 18 cdef size_t length = grpc_slice_length(slice) 19 return (<const char *>start)[:length] 20 21cdef grpc_slice _copy_slice(grpc_slice slice) nogil: 22 cdef void *start = grpc_slice_start_ptr(slice) 23 cdef size_t length = grpc_slice_length(slice) 24 return grpc_slice_from_copied_buffer(<const char *>start, length) 25 26cdef grpc_slice _slice_from_bytes(bytes value) nogil: 27 cdef const char *value_ptr 28 cdef size_t length 29 with gil: 30 value_ptr = <const char *>value 31 length = len(value) 32 return grpc_slice_from_copied_buffer(value_ptr, length) 33 34 35class ConnectivityState: 36 idle = GRPC_CHANNEL_IDLE 37 connecting = GRPC_CHANNEL_CONNECTING 38 ready = GRPC_CHANNEL_READY 39 transient_failure = GRPC_CHANNEL_TRANSIENT_FAILURE 40 shutdown = GRPC_CHANNEL_SHUTDOWN 41 42 43class ChannelArgKey: 44 enable_census = GRPC_ARG_ENABLE_CENSUS 45 max_concurrent_streams = GRPC_ARG_MAX_CONCURRENT_STREAMS 46 max_receive_message_length = GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH 47 max_send_message_length = GRPC_ARG_MAX_SEND_MESSAGE_LENGTH 48 http2_initial_sequence_number = GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER 49 default_authority = GRPC_ARG_DEFAULT_AUTHORITY 50 primary_user_agent_string = GRPC_ARG_PRIMARY_USER_AGENT_STRING 51 secondary_user_agent_string = GRPC_ARG_SECONDARY_USER_AGENT_STRING 52 ssl_session_cache = GRPC_SSL_SESSION_CACHE_ARG 53 ssl_target_name_override = GRPC_SSL_TARGET_NAME_OVERRIDE_ARG 54 55 56class WriteFlag: 57 buffer_hint = GRPC_WRITE_BUFFER_HINT 58 no_compress = GRPC_WRITE_NO_COMPRESS 59 60 61class StatusCode: 62 ok = GRPC_STATUS_OK 63 cancelled = GRPC_STATUS_CANCELLED 64 unknown = GRPC_STATUS_UNKNOWN 65 invalid_argument = GRPC_STATUS_INVALID_ARGUMENT 66 deadline_exceeded = GRPC_STATUS_DEADLINE_EXCEEDED 67 not_found = GRPC_STATUS_NOT_FOUND 68 already_exists = GRPC_STATUS_ALREADY_EXISTS 69 permission_denied = GRPC_STATUS_PERMISSION_DENIED 70 unauthenticated = GRPC_STATUS_UNAUTHENTICATED 71 resource_exhausted = GRPC_STATUS_RESOURCE_EXHAUSTED 72 failed_precondition = GRPC_STATUS_FAILED_PRECONDITION 73 aborted = GRPC_STATUS_ABORTED 74 out_of_range = GRPC_STATUS_OUT_OF_RANGE 75 unimplemented = GRPC_STATUS_UNIMPLEMENTED 76 internal = GRPC_STATUS_INTERNAL 77 unavailable = GRPC_STATUS_UNAVAILABLE 78 data_loss = GRPC_STATUS_DATA_LOSS 79 80 81class CallError: 82 ok = GRPC_CALL_OK 83 error = GRPC_CALL_ERROR 84 not_on_server = GRPC_CALL_ERROR_NOT_ON_SERVER 85 not_on_client = GRPC_CALL_ERROR_NOT_ON_CLIENT 86 already_accepted = GRPC_CALL_ERROR_ALREADY_ACCEPTED 87 already_invoked = GRPC_CALL_ERROR_ALREADY_INVOKED 88 not_invoked = GRPC_CALL_ERROR_NOT_INVOKED 89 already_finished = GRPC_CALL_ERROR_ALREADY_FINISHED 90 too_many_operations = GRPC_CALL_ERROR_TOO_MANY_OPERATIONS 91 invalid_flags = GRPC_CALL_ERROR_INVALID_FLAGS 92 invalid_metadata = GRPC_CALL_ERROR_INVALID_METADATA 93 94 95class CompletionType: 96 queue_shutdown = GRPC_QUEUE_SHUTDOWN 97 queue_timeout = GRPC_QUEUE_TIMEOUT 98 operation_complete = GRPC_OP_COMPLETE 99 100 101class OperationType: 102 send_initial_metadata = GRPC_OP_SEND_INITIAL_METADATA 103 send_message = GRPC_OP_SEND_MESSAGE 104 send_close_from_client = GRPC_OP_SEND_CLOSE_FROM_CLIENT 105 send_status_from_server = GRPC_OP_SEND_STATUS_FROM_SERVER 106 receive_initial_metadata = GRPC_OP_RECV_INITIAL_METADATA 107 receive_message = GRPC_OP_RECV_MESSAGE 108 receive_status_on_client = GRPC_OP_RECV_STATUS_ON_CLIENT 109 receive_close_on_server = GRPC_OP_RECV_CLOSE_ON_SERVER 110 111GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM= ( 112 _GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM) 113 114GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY = ( 115 _GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY) 116 117class CompressionAlgorithm: 118 none = GRPC_COMPRESS_NONE 119 deflate = GRPC_COMPRESS_DEFLATE 120 gzip = GRPC_COMPRESS_GZIP 121 122 123class CompressionLevel: 124 none = GRPC_COMPRESS_LEVEL_NONE 125 low = GRPC_COMPRESS_LEVEL_LOW 126 medium = GRPC_COMPRESS_LEVEL_MED 127 high = GRPC_COMPRESS_LEVEL_HIGH 128 129 130cdef class CallDetails: 131 132 def __cinit__(self): 133 fork_handlers_and_grpc_init() 134 with nogil: 135 grpc_call_details_init(&self.c_details) 136 137 def __dealloc__(self): 138 with nogil: 139 grpc_call_details_destroy(&self.c_details) 140 grpc_shutdown() 141 142 @property 143 def method(self): 144 return _slice_bytes(self.c_details.method) 145 146 @property 147 def host(self): 148 return _slice_bytes(self.c_details.host) 149 150 @property 151 def deadline(self): 152 return _time_from_timespec(self.c_details.deadline) 153 154 155cdef class SslPemKeyCertPair: 156 157 def __cinit__(self, bytes private_key, bytes certificate_chain): 158 self.private_key = private_key 159 self.certificate_chain = certificate_chain 160 self.c_pair.private_key = self.private_key 161 self.c_pair.certificate_chain = self.certificate_chain 162 163 164cdef class CompressionOptions: 165 166 def __cinit__(self): 167 with nogil: 168 grpc_compression_options_init(&self.c_options) 169 170 def enable_algorithm(self, grpc_compression_algorithm algorithm): 171 with nogil: 172 grpc_compression_options_enable_algorithm(&self.c_options, algorithm) 173 174 def disable_algorithm(self, grpc_compression_algorithm algorithm): 175 with nogil: 176 grpc_compression_options_disable_algorithm(&self.c_options, algorithm) 177 178 def is_algorithm_enabled(self, grpc_compression_algorithm algorithm): 179 cdef int result 180 with nogil: 181 result = grpc_compression_options_is_algorithm_enabled( 182 &self.c_options, algorithm) 183 return result 184 185 def to_channel_arg(self): 186 return ( 187 GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET, 188 self.c_options.enabled_algorithms_bitset, 189 ) 190 191 192def compression_algorithm_name(grpc_compression_algorithm algorithm): 193 cdef const char* name 194 with nogil: 195 grpc_compression_algorithm_name(algorithm, &name) 196 # Let Cython do the right thing with string casting 197 return name 198