File: event_stream_rpc_client_connection.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (370 lines) | stat: -rw-r--r-- 11,874 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include "event_stream.h"

#include "io.h"

#include <aws/event-stream/event_stream_rpc_client.h>
#include <aws/io/socket.h>

struct connection_binding;
static void s_capsule_destructor(PyObject *capsule);
static void s_on_connection_setup(
    struct aws_event_stream_rpc_client_connection *native,
    int error_code,
    void *user_data);
static void s_on_connection_shutdown(
    struct aws_event_stream_rpc_client_connection *native,
    int error_code,
    void *user_data);
static void s_on_protocol_message(
    struct aws_event_stream_rpc_client_connection *native,
    const struct aws_event_stream_rpc_message_args *message_args,
    void *user_data);

static const char *s_capsule_name = "aws_event_stream_rpc_client_connection";

struct connection_binding {
    struct aws_event_stream_rpc_client_connection *native;

    /* This reference is solely used for invoking callbacks,
     * and is cleared after the final callback is invoked.
     * If it were not cleared, circular references between the python object
     * and its binding would prevent the GC from ever cleaning things up */
    PyObject *self_py;
};

struct aws_event_stream_rpc_client_connection *aws_py_get_event_stream_rpc_client_connection(PyObject *connection) {
    AWS_PY_RETURN_NATIVE_FROM_BINDING(connection, s_capsule_name, "ClientConnection", connection_binding);
}

PyObject *aws_py_event_stream_rpc_client_connection_connect(PyObject *self, PyObject *args) {
    (void)self;

    struct aws_allocator *alloc = aws_py_get_allocator();

    const char *host_name;
    uint32_t port;
    PyObject *bootstrap_py;
    PyObject *socket_options_py;
    PyObject *tls_options_py;
    PyObject *connection_py;
    if (!PyArg_ParseTuple(
            args, "sIOOOO", &host_name, &port, &bootstrap_py, &socket_options_py, &tls_options_py, &connection_py)) {
        return NULL;
    }

    struct aws_client_bootstrap *bootstrap = aws_py_get_client_bootstrap(bootstrap_py);
    if (!bootstrap) {
        return NULL;
    }

    struct aws_socket_options socket_options;
    if (!aws_py_socket_options_init(&socket_options, socket_options_py)) {
        return NULL;
    }

    /* TLS options are optional */
    struct aws_tls_connection_options *tls_options = NULL;
    if (tls_options_py != Py_None) {
        tls_options = aws_py_get_tls_connection_options(tls_options_py);
        if (!tls_options) {
            return NULL;
        }
    }

    struct connection_binding *connection = aws_mem_calloc(alloc, 1, sizeof(struct connection_binding));
    PyObject *capsule = PyCapsule_New(connection, s_capsule_name, s_capsule_destructor);
    if (!capsule) {
        aws_mem_release(alloc, connection);
        return NULL;
    }

    /* From hereon, we need to clean up if errors occur */

    connection->self_py = connection_py;
    Py_INCREF(connection->self_py);

    /* Set _binding before calling connect() */
    if (PyObject_SetAttrString(connection_py, "_binding", capsule) != 0) {
        goto error;
    }

    /* Now that connection._binding holds reference to capsule, we can decref the creation reference */
    Py_CLEAR(capsule);

    struct aws_event_stream_rpc_client_connection_options conn_options = {
        .host_name = host_name,
        .port = port,
        .socket_options = &socket_options,
        .tls_options = tls_options,
        .bootstrap = bootstrap,
        .on_connection_setup = s_on_connection_setup,
        .on_connection_shutdown = s_on_connection_shutdown,
        .on_connection_protocol_message = s_on_protocol_message,
        .user_data = connection,
    };

    if (aws_event_stream_rpc_client_connection_connect(alloc, &conn_options)) {
        PyErr_SetAwsLastError();
        goto error;
    }

    Py_RETURN_NONE;

error:
    /* clear circular reference */
    Py_CLEAR(connection->self_py);

    /* if capsule pointer still valid, this will invoke its destructor,
     * which will clean up anything inside of it */
    Py_CLEAR(capsule);
    return NULL;
}

static void s_on_connection_setup(
    struct aws_event_stream_rpc_client_connection *native,
    int error_code,
    void *user_data) {
    struct connection_binding *connection = user_data;

    AWS_FATAL_ASSERT(((bool)native != (bool)error_code) && "illegal event-stream connection args");

    if (native) {
        connection->native = native;
        aws_event_stream_rpc_client_connection_acquire(connection->native);
    }

    PyGILState_STATE state;
    if (aws_py_gilstate_ensure(&state)) {
        return; /* Python has shut down. Nothing matters anymore, but don't crash */
    }

    PyObject *result = PyObject_CallMethod(connection->self_py, "_on_connection_setup", "(i)", error_code);
    if (result) {
        Py_DECREF(result);
    } else {
        /* Close connection if unhandled exception occurs.
         * Note that callback might fail during application shutdown */
        AWS_LOGF_ERROR(
            AWS_LS_EVENT_STREAM_RPC_CLIENT,
            "id=%p: Exception in on_connection_setup() callback, closing connection.",
            (void *)connection->native);

        PyErr_WriteUnraisable(connection->self_py);
        if (native) {
            aws_event_stream_rpc_client_connection_close(connection->native, AWS_ERROR_CRT_CALLBACK_EXCEPTION);
        }
    }

    if (!native) {
        /* There will be no further callbacks, clear circular reference
         * so that python connection object can ever be GC'd */
        Py_CLEAR(connection->self_py);
    }

    PyGILState_Release(state);
}

static void s_on_connection_shutdown(
    struct aws_event_stream_rpc_client_connection *native,
    int error_code,
    void *user_data) {

    (void)native;
    struct connection_binding *connection = user_data;

    PyGILState_STATE state;
    if (aws_py_gilstate_ensure(&state)) {
        return; /* Python has shut down. Nothing matters anymore, but don't crash */
    }

    PyObject *result = PyObject_CallMethod(connection->self_py, "_on_connection_shutdown", "(i)", error_code);
    if (result) {
        Py_DECREF(result);
    } else {
        /* Callback might fail during application shutdown */
        PyErr_WriteUnraisable(connection->self_py);
    }

    /* There will be no further callbacks, clear circular reference
     * so that python connection object can ever be GC'd */
    Py_CLEAR(connection->self_py);

    PyGILState_Release(state);
}

static void s_capsule_destructor(PyObject *capsule) {
    struct connection_binding *connection = PyCapsule_GetPointer(capsule, s_capsule_name);
    aws_event_stream_rpc_client_connection_release(connection->native);
    aws_mem_release(aws_py_get_allocator(), connection);
}

static void s_on_protocol_message(
    struct aws_event_stream_rpc_client_connection *native,
    const struct aws_event_stream_rpc_message_args *message_args,
    void *user_data) {

    (void)native;
    struct connection_binding *connection = user_data;

    PyGILState_STATE state;
    if (aws_py_gilstate_ensure(&state)) {
        return; /* Python has shut down. Nothing matters anymore, but don't crash */
    }

    PyObject *headers = NULL;
    PyObject *result = NULL;

    headers = aws_py_event_stream_python_headers_create(message_args->headers, message_args->headers_count);
    if (!headers) {
        PyErr_WriteUnraisable(connection->self_py);
        goto done;
    }

    result = PyObject_CallMethod(
        connection->self_py,
        "_on_protocol_message",
        "(Oy#iI)",
        headers,
        message_args->payload->buffer,
        message_args->payload->len,
        message_args->message_type,
        message_args->message_flags);
    if (!result) {
        /* Callback might fail during application shutdown */
        PyErr_WriteUnraisable(connection->self_py);
        goto done;
    }

done:
    Py_XDECREF(headers);
    Py_XDECREF(result);
    PyGILState_Release(state);
}

PyObject *aws_py_event_stream_rpc_client_connection_close(PyObject *self, PyObject *args) {
    (void)self;
    PyObject *capsule;
    if (!PyArg_ParseTuple(args, "O", &capsule)) {
        return NULL;
    }

    struct connection_binding *connection = PyCapsule_GetPointer(capsule, s_capsule_name);
    if (!connection) {
        return NULL;
    }

    aws_event_stream_rpc_client_connection_close(connection->native, AWS_OP_SUCCESS);
    Py_RETURN_NONE;
}

PyObject *aws_py_event_stream_rpc_client_connection_is_open(PyObject *self, PyObject *args) {
    (void)self;
    PyObject *capsule;
    if (!PyArg_ParseTuple(args, "O", &capsule)) {
        return NULL;
    }

    struct connection_binding *connection = PyCapsule_GetPointer(capsule, s_capsule_name);
    if (!connection) {
        return NULL;
    }

    if (aws_event_stream_rpc_client_connection_is_open(connection->native)) {
        Py_RETURN_TRUE;
    }
    Py_RETURN_FALSE;
}

void aws_py_event_stream_rpc_client_on_message_flush(int error_code, void *user_data) {
    PyObject *on_flush_py = user_data;

    PyGILState_STATE state;
    if (aws_py_gilstate_ensure(&state)) {
        return; /* Python has shut down. Nothing matters anymore, but don't crash */
    }

    PyObject *result = PyObject_CallFunction(on_flush_py, "(i)", error_code);
    if (result) {
        Py_DECREF(result);
    } else {
        /* Callback might fail during application shutdown */
        PyErr_WriteUnraisable(on_flush_py);
    }

    /* Release reference to completion callback */
    Py_DECREF(on_flush_py);

    PyGILState_Release(state);
}

PyObject *aws_py_event_stream_rpc_client_connection_send_protocol_message(PyObject *self, PyObject *args) {
    (void)self;

    PyObject *capsule_py;
    PyObject *headers_py;
    Py_buffer payload_buf; /* Py_buffers must be released after successful PyArg_ParseTuple() calls */
    int message_type;
    uint32_t message_flags;
    PyObject *on_flush_py;
    if (!PyArg_ParseTuple(
            args, "OOs*iIO", &capsule_py, &headers_py, &payload_buf, &message_type, &message_flags, &on_flush_py)) {
        return NULL;
    }

    /* From hereon, we need to clean up if errors occur */

    bool success = false;
    struct aws_array_list headers;
    AWS_ZERO_STRUCT(headers);

    /* Keep completion callback alive until it fires */
    Py_INCREF(on_flush_py);

    struct connection_binding *connection = PyCapsule_GetPointer(capsule_py, s_capsule_name);
    if (!connection) {
        goto done;
    }

    /* OPTIMIZATION IDEA: Currently, we're deep-copying byte_bufs and strings
     * into the C headers. We did this because it was simple (and headers
     * shouldn't be gigantic). Since send_protocol_message() ALSO copies
     * everything we could, instead, create non-owning C headers here.
     * It would be more complex because we'd need to track a
     * list of Py_buffer to release afterwards. */
    if (!aws_py_event_stream_native_headers_init(&headers, headers_py)) {
        goto done;
    }

    struct aws_byte_buf payload = aws_byte_buf_from_array(payload_buf.buf, payload_buf.len);
    struct aws_event_stream_rpc_message_args msg_args = {
        .headers = headers.data,
        .headers_count = aws_array_list_length(&headers),
        .payload = &payload,
        .message_type = message_type,
        .message_flags = message_flags,
    };
    if (aws_event_stream_rpc_client_connection_send_protocol_message(
            connection->native, &msg_args, aws_py_event_stream_rpc_client_on_message_flush, on_flush_py)) {
        PyErr_SetAwsLastError();
        goto done;
    }

    success = true;

done:
    PyBuffer_Release(&payload_buf);
    if (aws_array_list_is_valid(&headers)) {
        aws_event_stream_headers_list_cleanup(&headers);
    }

    if (success) {
        Py_RETURN_NONE;
    }

    Py_DECREF(on_flush_py);
    return NULL;
}