| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "event_stream.h" |
| 6 |
|
| 7 |
#include "io.h" |
| 8 |
|
| 9 |
#include <aws/event-stream/event_stream_rpc_client.h> |
| 10 |
#include <aws/io/socket.h> |
| 11 |
|
| 12 |
struct connection_binding; |
| 13 |
static void s_capsule_destructor(PyObject *capsule); |
| 14 |
static void s_on_connection_setup( |
| 15 |
struct aws_event_stream_rpc_client_connection *native, |
| 16 |
int error_code, |
| 17 |
void *user_data); |
| 18 |
static void s_on_connection_shutdown( |
| 19 |
struct aws_event_stream_rpc_client_connection *native, |
| 20 |
int error_code, |
| 21 |
void *user_data); |
| 22 |
static void s_on_protocol_message( |
| 23 |
struct aws_event_stream_rpc_client_connection *native, |
| 24 |
const struct aws_event_stream_rpc_message_args *message_args, |
| 25 |
void *user_data); |
| 26 |
|
| 27 |
static const char *s_capsule_name = "aws_event_stream_rpc_client_connection"; |
| 28 |
|
| 29 |
struct connection_binding { |
| 30 |
struct aws_event_stream_rpc_client_connection *native; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
PyObject *self_py; |
| 37 |
}; |
| 38 |
|
| 39 |
struct aws_event_stream_rpc_client_connection *aws_py_get_event_stream_rpc_client_connection(PyObject *connection) { |
| 40 |
AWS_PY_RETURN_NATIVE_FROM_BINDING(connection, s_capsule_name, "ClientConnection", connection_binding); |
| 41 |
} |
| 42 |
|
| 43 |
PyObject *aws_py_event_stream_rpc_client_connection_connect(PyObject *self, PyObject *args) { |
| 44 |
(void)self; |
| 45 |
|
| 46 |
struct aws_allocator *alloc = aws_py_get_allocator(); |
| 47 |
|
| 48 |
const char *host_name; |
| 49 |
uint32_t port; |
| 50 |
PyObject *bootstrap_py; |
| 51 |
PyObject *socket_options_py; |
| 52 |
PyObject *tls_options_py; |
| 53 |
PyObject *connection_py; |
| 54 |
if (!PyArg_ParseTuple( |
| 55 |
args, "sIOOOO", &host_name, &port, &bootstrap_py, &socket_options_py, &tls_options_py, &connection_py)) { |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
struct aws_client_bootstrap *bootstrap = aws_py_get_client_bootstrap(bootstrap_py); |
| 60 |
if (!bootstrap) { |
| 61 |
return NULL; |
| 62 |
} |
| 63 |
|
| 64 |
struct aws_socket_options socket_options; |
| 65 |
if (!aws_py_socket_options_init(&socket_options, socket_options_py)) { |
| 66 |
return NULL; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
struct aws_tls_connection_options *tls_options = NULL; |
| 71 |
if (tls_options_py != Py_None) { |
| 72 |
tls_options = aws_py_get_tls_connection_options(tls_options_py); |
| 73 |
if (!tls_options) { |
| 74 |
return NULL; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
struct connection_binding *connection = aws_mem_calloc(alloc, 1, sizeof(struct connection_binding)); |
| 79 |
PyObject *capsule = PyCapsule_New(connection, s_capsule_name, s_capsule_destructor); |
| 80 |
if (!capsule) { |
| 81 |
aws_mem_release(alloc, connection); |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
connection->self_py = connection_py; |
| 88 |
Py_INCREF(connection->self_py); |
| 89 |
|
| 90 |
|
| 91 |
if (PyObject_SetAttrString(connection_py, "_binding", capsule) != 0) { |
| 92 |
goto error; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
Py_CLEAR(capsule); |
| 97 |
|
| 98 |
struct aws_event_stream_rpc_client_connection_options conn_options = { |
| 99 |
.host_name = host_name, |
| 100 |
.port = port, |
| 101 |
.socket_options = &socket_options, |
| 102 |
.tls_options = tls_options, |
| 103 |
.bootstrap = bootstrap, |
| 104 |
.on_connection_setup = s_on_connection_setup, |
| 105 |
.on_connection_shutdown = s_on_connection_shutdown, |
| 106 |
.on_connection_protocol_message = s_on_protocol_message, |
| 107 |
.user_data = connection, |
| 108 |
}; |
| 109 |
|
| 110 |
if (aws_event_stream_rpc_client_connection_connect(alloc, &conn_options)) { |
| 111 |
PyErr_SetAwsLastError(); |
| 112 |
goto error; |
| 113 |
} |
| 114 |
|
| 115 |
Py_RETURN_NONE; |
| 116 |
|
| 117 |
error: |
| 118 |
|
| 119 |
Py_CLEAR(connection->self_py); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
Py_CLEAR(capsule); |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
static void s_on_connection_setup( |
| 128 |
struct aws_event_stream_rpc_client_connection *native, |
| 129 |
int error_code, |
| 130 |
void *user_data) { |
| 131 |
struct connection_binding *connection = user_data; |
| 132 |
|
| 133 |
AWS_FATAL_ASSERT(((bool)native != (bool)error_code) && "illegal event-stream connection args"); |
| 134 |
|
| 135 |
if (native) { |
| 136 |
connection->native = native; |
| 137 |
aws_event_stream_rpc_client_connection_acquire(connection->native); |
| 138 |
} |
| 139 |
|
| 140 |
PyGILState_STATE state; |
| 141 |
if (aws_py_gilstate_ensure(&state)) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
PyObject *result = PyObject_CallMethod(connection->self_py, "_on_connection_setup", "(i)", error_code); |
| 146 |
if (result) { |
| 147 |
Py_DECREF(result); |
| 148 |
} else { |
| 149 |
|
| 150 |
|
| 151 |
AWS_LOGF_ERROR( |
| 152 |
AWS_LS_EVENT_STREAM_RPC_CLIENT, |
| 153 |
"id=%p: Exception in on_connection_setup() callback, closing connection.", |
| 154 |
(void *)connection->native); |
| 155 |
|
| 156 |
PyErr_WriteUnraisable(connection->self_py); |
| 157 |
if (native) { |
| 158 |
aws_event_stream_rpc_client_connection_close(connection->native, AWS_ERROR_CRT_CALLBACK_EXCEPTION); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if (!native) { |
| 163 |
|
| 164 |
|
| 165 |
Py_CLEAR(connection->self_py); |
| 166 |
} |
| 167 |
|
| 168 |
PyGILState_Release(state); |
| 169 |
} |
| 170 |
|
| 171 |
static void s_on_connection_shutdown( |
| 172 |
struct aws_event_stream_rpc_client_connection *native, |
| 173 |
int error_code, |
| 174 |
void *user_data) { |
| 175 |
|
| 176 |
(void)native; |
| 177 |
struct connection_binding *connection = user_data; |
| 178 |
|
| 179 |
PyGILState_STATE state; |
| 180 |
if (aws_py_gilstate_ensure(&state)) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
PyObject *result = PyObject_CallMethod(connection->self_py, "_on_connection_shutdown", "(i)", error_code); |
| 185 |
if (result) { |
| 186 |
Py_DECREF(result); |
| 187 |
} else { |
| 188 |
|
| 189 |
PyErr_WriteUnraisable(connection->self_py); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
Py_CLEAR(connection->self_py); |
| 195 |
|
| 196 |
PyGILState_Release(state); |
| 197 |
} |
| 198 |
|
| 199 |
static void s_capsule_destructor(PyObject *capsule) { |
| 200 |
struct connection_binding *connection = PyCapsule_GetPointer(capsule, s_capsule_name); |
| 201 |
aws_event_stream_rpc_client_connection_release(connection->native); |
| 202 |
aws_mem_release(aws_py_get_allocator(), connection); |
| 203 |
} |
| 204 |
|
| 205 |
static void s_on_protocol_message( |
| 206 |
struct aws_event_stream_rpc_client_connection *native, |
| 207 |
const struct aws_event_stream_rpc_message_args *message_args, |
| 208 |
void *user_data) { |
| 209 |
|
| 210 |
(void)native; |
| 211 |
struct connection_binding *connection = user_data; |
| 212 |
|
| 213 |
PyGILState_STATE state; |
| 214 |
if (aws_py_gilstate_ensure(&state)) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
PyObject *headers = NULL; |
| 219 |
PyObject *result = NULL; |
| 220 |
|
| 221 |
headers = aws_py_event_stream_python_headers_create(message_args->headers, message_args->headers_count); |
| 222 |
if (!headers) { |
| 223 |
PyErr_WriteUnraisable(connection->self_py); |
| 224 |
goto done; |
| 225 |
} |
| 226 |
|
| 227 |
result = PyObject_CallMethod( |
| 228 |
connection->self_py, |
| 229 |
"_on_protocol_message", |
| 230 |
"(Oy#iI)", |
| 231 |
headers, |
| 232 |
message_args->payload->buffer, |
| 233 |
message_args->payload->len, |
| 234 |
message_args->message_type, |
| 235 |
message_args->message_flags); |
| 236 |
if (!result) { |
| 237 |
|
| 238 |
PyErr_WriteUnraisable(connection->self_py); |
| 239 |
goto done; |
| 240 |
} |
| 241 |
|
| 242 |
done: |
| 243 |
Py_XDECREF(headers); |
| 244 |
Py_XDECREF(result); |
| 245 |
PyGILState_Release(state); |
| 246 |
} |
| 247 |
|
| 248 |
PyObject *aws_py_event_stream_rpc_client_connection_close(PyObject *self, PyObject *args) { |
| 249 |
(void)self; |
| 250 |
PyObject *capsule; |
| 251 |
if (!PyArg_ParseTuple(args, "O", &capsule)) { |
| 252 |
return NULL; |
| 253 |
} |
| 254 |
|
| 255 |
struct connection_binding *connection = PyCapsule_GetPointer(capsule, s_capsule_name); |
| 256 |
if (!connection) { |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
aws_event_stream_rpc_client_connection_close(connection->native, AWS_OP_SUCCESS); |
| 261 |
Py_RETURN_NONE; |
| 262 |
} |
| 263 |
|
| 264 |
PyObject *aws_py_event_stream_rpc_client_connection_is_open(PyObject *self, PyObject *args) { |
| 265 |
(void)self; |
| 266 |
PyObject *capsule; |
| 267 |
if (!PyArg_ParseTuple(args, "O", &capsule)) { |
| 268 |
return NULL; |
| 269 |
} |
| 270 |
|
| 271 |
struct connection_binding *connection = PyCapsule_GetPointer(capsule, s_capsule_name); |
| 272 |
if (!connection) { |
| 273 |
return NULL; |
| 274 |
} |
| 275 |
|
| 276 |
if (aws_event_stream_rpc_client_connection_is_open(connection->native)) { |
| 277 |
Py_RETURN_TRUE; |
| 278 |
} |
| 279 |
Py_RETURN_FALSE; |
| 280 |
} |
| 281 |
|
| 282 |
void aws_py_event_stream_rpc_client_on_message_flush(int error_code, void *user_data) { |
| 283 |
PyObject *on_flush_py = user_data; |
| 284 |
|
| 285 |
PyGILState_STATE state; |
| 286 |
if (aws_py_gilstate_ensure(&state)) { |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
PyObject *result = PyObject_CallFunction(on_flush_py, "(i)", error_code); |
| 291 |
if (result) { |
| 292 |
Py_DECREF(result); |
| 293 |
} else { |
| 294 |
|
| 295 |
PyErr_WriteUnraisable(on_flush_py); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
Py_DECREF(on_flush_py); |
| 300 |
|
| 301 |
PyGILState_Release(state); |
| 302 |
} |
| 303 |
|
| 304 |
PyObject *aws_py_event_stream_rpc_client_connection_send_protocol_message(PyObject *self, PyObject *args) { |
| 305 |
(void)self; |
| 306 |
|
| 307 |
PyObject *capsule_py; |
| 308 |
PyObject *headers_py; |
| 309 |
Py_buffer payload_buf; |
| 310 |
int message_type; |
| 311 |
uint32_t message_flags; |
| 312 |
PyObject *on_flush_py; |
| 313 |
if (!PyArg_ParseTuple( |
| 314 |
args, "OOs*iIO", &capsule_py, &headers_py, &payload_buf, &message_type, &message_flags, &on_flush_py)) { |
| 315 |
return NULL; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
bool success = false; |
| 321 |
struct aws_array_list headers; |
| 322 |
AWS_ZERO_STRUCT(headers); |
| 323 |
|
| 324 |
|
| 325 |
Py_INCREF(on_flush_py); |
| 326 |
|
| 327 |
struct connection_binding *connection = PyCapsule_GetPointer(capsule_py, s_capsule_name); |
| 328 |
if (!connection) { |
| 329 |
goto done; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
if (!aws_py_event_stream_native_headers_init(&headers, headers_py)) { |
| 339 |
goto done; |
| 340 |
} |
| 341 |
|
| 342 |
struct aws_byte_buf payload = aws_byte_buf_from_array(payload_buf.buf, payload_buf.len); |
| 343 |
struct aws_event_stream_rpc_message_args msg_args = { |
| 344 |
.headers = headers.data, |
| 345 |
.headers_count = aws_array_list_length(&headers), |
| 346 |
.payload = &payload, |
| 347 |
.message_type = message_type, |
| 348 |
.message_flags = message_flags, |
| 349 |
}; |
| 350 |
if (aws_event_stream_rpc_client_connection_send_protocol_message( |
| 351 |
connection->native, &msg_args, aws_py_event_stream_rpc_client_on_message_flush, on_flush_py)) { |
| 352 |
PyErr_SetAwsLastError(); |
| 353 |
goto done; |
| 354 |
} |
| 355 |
|
| 356 |
success = true; |
| 357 |
|
| 358 |
done: |
| 359 |
PyBuffer_Release(&payload_buf); |
| 360 |
if (aws_array_list_is_valid(&headers)) { |
| 361 |
aws_event_stream_headers_list_cleanup(&headers); |
| 362 |
} |
| 363 |
|
| 364 |
if (success) { |
| 365 |
Py_RETURN_NONE; |
| 366 |
} |
| 367 |
|
| 368 |
Py_DECREF(on_flush_py); |
| 369 |
return NULL; |
| 370 |
} |