Debian Codegraph @

source/event_stream_rpc_client_continuation.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include "event_stream.h"
6
7 #include <aws/event-stream/event_stream_rpc_client.h>
8
9 struct continuation_binding;
10 static void s_continuation_capsule_destructor(PyObject *capsule);
11 static void s_on_continuation_closed(struct aws_event_stream_rpc_client_continuation_token *native, void *user_data);
12 static void s_on_continuation_message(
13 struct aws_event_stream_rpc_client_continuation_token *native,
14 const struct aws_event_stream_rpc_message_args *message_args,
15 void *user_data);
16
17 static const char *s_capsule_name = "aws_event_stream_rpc_client_continuation_token";
18
19 struct continuation_binding {
20 struct aws_event_stream_rpc_client_continuation_token *native;
21 struct aws_event_stream_rpc_client_connection *native_connection;
22
23 /* This reference is solely used for invoking callbacks,
24 * It is not set until activate(), and cleared upon on_continuation_closed().
25 * If it were not cleared, circular references between the python object
26 * and its binding would prevent the GC from ever cleaning things up */
27 PyObject *self_py;
28 };
29
30 struct aws_event_stream_rpc_client_continuation_token *aws_py_get_event_stream_rpc_client_continuation(
31 PyObject *continuation) {
32 AWS_PY_RETURN_NATIVE_FROM_BINDING(continuation, s_capsule_name, "ClientContinuation", continuation_binding);
33 }
34
35 PyObject *aws_py_event_stream_rpc_client_connection_new_stream(PyObject *self, PyObject *args) {
36 (void)self;
37
38 PyObject *connection_py;
39 if (!PyArg_ParseTuple(args, "O", &connection_py)) {
40 return NULL;
41 }
42
43 struct aws_event_stream_rpc_client_connection *native_connection =
44 aws_py_get_event_stream_rpc_client_connection(connection_py);
45 if (!native_connection) {
46 return NULL;
47 }
48
49 struct continuation_binding *continuation =
50 aws_mem_calloc(aws_py_get_allocator(), 1, sizeof(struct continuation_binding));
51 continuation->native_connection = native_connection;
52
53 PyObject *capsule = PyCapsule_New(continuation, s_capsule_name, s_continuation_capsule_destructor);
54 if (!capsule) {
55 return NULL;
56 }
57
58 /* From hereon, we need to clean up if errors occur */
59
60 struct aws_event_stream_rpc_client_stream_continuation_options options = {
61 .on_continuation = s_on_continuation_message,
62 .on_continuation_closed = s_on_continuation_closed,
63 .user_data = continuation,
64 };
65 continuation->native = aws_event_stream_rpc_client_connection_new_stream(native_connection, &options);
66 if (!continuation->native) {
67 PyErr_SetAwsLastError();
68 goto error;
69 }
70
71 return capsule;
72 error:
73 /* capsule's destructor will clean up anything inside of it */
74 Py_DECREF(capsule);
75 return NULL;
76 }
77
78 static void s_on_continuation_closed(struct aws_event_stream_rpc_client_continuation_token *native, void *user_data) {
79 (void)native;
80 struct continuation_binding *continuation = user_data;
81
82 PyGILState_STATE state;
83 if (aws_py_gilstate_ensure(&state)) {
84 return; /* Python has shut down. Nothing matters anymore, but don't crash */
85 }
86
87 PyObject *result = PyObject_CallMethod(continuation->self_py, "_on_continuation_closed", "()");
88 if (result) {
89 Py_DECREF(result);
90 } else {
91 /* Callback might fail during application shutdown */
92 PyErr_WriteUnraisable(continuation->self_py);
93 }
94
95 /* There will be no further callbacks, clear circular reference
96 * so that python continuation object can ever be GC'd */
97 Py_CLEAR(continuation->self_py);
98
99 PyGILState_Release(state);
100 }
101
102 static void s_continuation_capsule_destructor(PyObject *capsule) {
103 struct continuation_binding *continuation = PyCapsule_GetPointer(capsule, s_capsule_name);
104 aws_event_stream_rpc_client_continuation_release(continuation->native);
105 aws_mem_release(aws_py_get_allocator(), continuation);
106 }
107
108 static void s_on_continuation_message(
109 struct aws_event_stream_rpc_client_continuation_token *native,
110 const struct aws_event_stream_rpc_message_args *message_args,
111 void *user_data) {
112
113 (void)native;
114 struct continuation_binding *continuation = user_data;
115
116 PyGILState_STATE state;
117 if (aws_py_gilstate_ensure(&state)) {
118 return; /* Python has shut down. Nothing matters anymore, but don't crash */
119 }
120
121 PyObject *headers = NULL;
122 PyObject *result = NULL;
123
124 headers = aws_py_event_stream_python_headers_create(message_args->headers, message_args->headers_count);
125 if (!headers) {
126 PyErr_WriteUnraisable(continuation->self_py);
127 goto done;
128 }
129
130 result = PyObject_CallMethod(
131 continuation->self_py,
132 "_on_continuation_message",
133 "(Oy#iI)",
134 headers,
135 message_args->payload->buffer,
136 message_args->payload->len,
137 message_args->message_type,
138 message_args->message_flags);
139 if (!result) {
140 /* Callback might fail during application shutdown */
141 PyErr_WriteUnraisable(continuation->self_py);
142 goto done;
143 }
144
145 done:
146 Py_XDECREF(headers);
147 Py_XDECREF(result);
148 PyGILState_Release(state);
149 }
150
151 PyObject *aws_py_event_stream_rpc_client_continuation_activate(PyObject *self, PyObject *args) {
152 (void)self;
153
154 PyObject *capsule_py;
155 PyObject *self_py;
156 const char *operation_name;
157 Py_ssize_t operation_name_len;
158 PyObject *headers_py;
159 Py_buffer payload_buf; /* Py_buffers must be released after successful PyArg_ParseTuple() calls */
160 int message_type;
161 uint32_t message_flags;
162 PyObject *on_flush_py;
163 if (!PyArg_ParseTuple(
164 args,
165 "OOs#Os*iIO",
166 &capsule_py,
167 &self_py,
168 &operation_name,
169 &operation_name_len,
170 &headers_py,
171 &payload_buf,
172 &message_type,
173 &message_flags,
174 &on_flush_py)) {
175 return NULL;
176 }
177
178 /* From hereon, we need to clean up if errors occur */
179
180 bool success = false;
181 struct aws_array_list headers;
182 AWS_ZERO_STRUCT(headers);
183 bool self_py_set = false;
184 Py_INCREF(on_flush_py); /* Keep completion callback alive until it fires */
185
186 struct continuation_binding *continuation = PyCapsule_GetPointer(capsule_py, s_capsule_name);
187 if (!continuation) {
188 goto done;
189 }
190
191 if (continuation->self_py != NULL) {
192 PyErr_SetString(PyExc_RuntimeError, "Continuation already activated");
193 goto done;
194 }
195
196 continuation->self_py = self_py;
197 Py_INCREF(continuation->self_py);
198 self_py_set = true;
199
200 if (!aws_py_event_stream_native_headers_init(&headers, headers_py)) {
201 goto done;
202 }
203
204 struct aws_byte_buf payload = aws_byte_buf_from_array(payload_buf.buf, payload_buf.len);
205 struct aws_event_stream_rpc_message_args msg_args = {
206 .headers = headers.data,
207 .headers_count = aws_array_list_length(&headers),
208 .payload = &payload,
209 .message_type = message_type,
210 .message_flags = message_flags,
211 };
212 if (aws_event_stream_rpc_client_continuation_activate(
213 continuation->native,
214 aws_byte_cursor_from_array(operation_name, (size_t)operation_name_len),
215 &msg_args,
216 aws_py_event_stream_rpc_client_on_message_flush,
217 on_flush_py)) {
218 PyErr_SetAwsLastError();
219 goto done;
220 }
221
222 success = true;
223
224 done:
225 PyBuffer_Release(&payload_buf);
226 if (aws_array_list_is_valid(&headers)) {
227 aws_event_stream_headers_list_cleanup(&headers);
228 }
229
230 if (success) {
231 Py_RETURN_NONE;
232 }
233
234 /* failed */
235 Py_DECREF(on_flush_py);
236 if (self_py_set) {
237 Py_CLEAR(continuation->self_py);
238 }
239
240 return NULL;
241 }
242
243 PyObject *aws_py_event_stream_rpc_client_continuation_send_message(PyObject *self, PyObject *args) {
244 (void)self;
245
246 PyObject *capsule_py;
247 PyObject *headers_py;
248 Py_buffer payload_buf; /* Py_buffers must be released after successful PyArg_ParseTuple() calls */
249 int message_type;
250 uint32_t message_flags;
251 PyObject *on_flush_py;
252 if (!PyArg_ParseTuple(
253 args, "OOs*iIO", &capsule_py, &headers_py, &payload_buf, &message_type, &message_flags, &on_flush_py)) {
254 return NULL;
255 }
256
257 /* From hereon, we need to clean up if errors occur */
258
259 bool success = false;
260 struct aws_array_list headers;
261 AWS_ZERO_STRUCT(headers);
262
263 /* Keep completion callback alive until it fires */
264 Py_INCREF(on_flush_py);
265
266 struct continuation_binding *continuation = PyCapsule_GetPointer(capsule_py, s_capsule_name);
267 if (!continuation) {
268 goto done;
269 }
270
271 if (!aws_py_event_stream_native_headers_init(&headers, headers_py)) {
272 goto done;
273 }
274
275 struct aws_byte_buf payload = aws_byte_buf_from_array(payload_buf.buf, payload_buf.len);
276 struct aws_event_stream_rpc_message_args msg_args = {
277 .headers = headers.data,
278 .headers_count = aws_array_list_length(&headers),
279 .payload = &payload,
280 .message_type = message_type,
281 .message_flags = message_flags,
282 };
283 if (aws_event_stream_rpc_client_continuation_send_message(
284 continuation->native, &msg_args, aws_py_event_stream_rpc_client_on_message_flush, on_flush_py)) {
285 PyErr_SetAwsLastError();
286 goto done;
287 }
288
289 success = true;
290
291 done:
292 PyBuffer_Release(&payload_buf);
293 if (aws_array_list_is_valid(&headers)) {
294 aws_event_stream_headers_list_cleanup(&headers);
295 }
296
297 if (success) {
298 Py_RETURN_NONE;
299 }
300
301 Py_DECREF(on_flush_py);
302 return NULL;
303 }
304
305 PyObject *aws_py_event_stream_rpc_client_continuation_is_closed(PyObject *self, PyObject *args) {
306 (void)self;
307 PyObject *capsule;
308 if (!PyArg_ParseTuple(args, "O", &capsule)) {
309 return NULL;
310 }
311
312 struct continuation_binding *continuation = PyCapsule_GetPointer(capsule, s_capsule_name);
313 if (!continuation) {
314 return NULL;
315 }
316
317 if (aws_event_stream_rpc_client_continuation_is_closed(continuation->native)) {
318 Py_RETURN_TRUE;
319 }
320 Py_RETURN_FALSE;
321 }