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