| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "mqtt_client_connection.h" |
| 6 |
#include "mqtt5_client.h" |
| 7 |
|
| 8 |
#include "http.h" |
| 9 |
#include "io.h" |
| 10 |
#include "mqtt_client.h" |
| 11 |
|
| 12 |
#include <aws/mqtt/client.h> |
| 13 |
|
| 14 |
#include <aws/http/connection.h> |
| 15 |
#include <aws/http/proxy.h> |
| 16 |
#include <aws/http/request_response.h> |
| 17 |
|
| 18 |
#include <aws/io/channel.h> |
| 19 |
#include <aws/io/channel_bootstrap.h> |
| 20 |
#include <aws/io/event_loop.h> |
| 21 |
#include <aws/io/host_resolver.h> |
| 22 |
#include <aws/io/socket.h> |
| 23 |
#include <aws/io/socket_channel_handler.h> |
| 24 |
#include <aws/io/tls_channel_handler.h> |
| 25 |
|
| 26 |
#include <aws/common/condition_variable.h> |
| 27 |
#include <aws/common/mutex.h> |
| 28 |
#include <aws/common/string.h> |
| 29 |
#include <aws/common/thread.h> |
| 30 |
|
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
static void s_ws_handshake_transform( |
| 34 |
struct aws_http_message *request, |
| 35 |
void *user_data, |
| 36 |
aws_mqtt_transform_websocket_handshake_complete_fn *complete_fn, |
| 37 |
void *complete_ctx); |
| 38 |
|
| 39 |
static const char *s_capsule_name_mqtt_client_connection = "aws_mqtt_client_connection"; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
struct mqtt_connection_binding { |
| 46 |
struct aws_mqtt_client_connection *native; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
PyObject *self_proxy; |
| 51 |
|
| 52 |
PyObject *on_connect; |
| 53 |
PyObject *on_any_publish; |
| 54 |
|
| 55 |
|
| 56 |
PyObject *client; |
| 57 |
}; |
| 58 |
|
| 59 |
static void s_mqtt_python_connection_finish_destruction(struct mqtt_connection_binding *py_connection) { |
| 60 |
|
| 61 |
Py_DECREF(py_connection->self_proxy); |
| 62 |
Py_DECREF(py_connection->client); |
| 63 |
Py_XDECREF(py_connection->on_any_publish); |
| 64 |
|
| 65 |
aws_mem_release(aws_py_get_allocator(), py_connection); |
| 66 |
} |
| 67 |
|
| 68 |
static void s_start_destroy_native(struct mqtt_connection_binding *py_connection) { |
| 69 |
if (py_connection == NULL || py_connection->native == NULL) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
aws_mqtt_client_connection_release(py_connection->native); |
| 74 |
} |
| 75 |
|
| 76 |
static void s_mqtt_python_connection_termination(void *userdata) { |
| 77 |
|
| 78 |
if (userdata == NULL) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
struct mqtt_connection_binding *py_connection = userdata; |
| 83 |
|
| 84 |
PyGILState_STATE state; |
| 85 |
if (aws_py_gilstate_ensure(&state)) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
s_mqtt_python_connection_finish_destruction(py_connection); |
| 90 |
PyGILState_Release(state); |
| 91 |
} |
| 92 |
|
| 93 |
static void s_mqtt_python_connection_destructor_on_disconnect( |
| 94 |
struct aws_mqtt_client_connection *connection, |
| 95 |
void *user_data) { |
| 96 |
if (connection == NULL || user_data == NULL) { |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
struct mqtt_connection_binding *py_connection = user_data; |
| 101 |
PyGILState_STATE state; |
| 102 |
if (aws_py_gilstate_ensure(&state)) { |
| 103 |
return; |
| 104 |
} |
| 105 |
s_start_destroy_native(py_connection); |
| 106 |
PyGILState_Release(state); |
| 107 |
} |
| 108 |
|
| 109 |
static void s_mqtt_python_connection_destructor(PyObject *connection_capsule) { |
| 110 |
|
| 111 |
struct mqtt_connection_binding *py_connection = |
| 112 |
PyCapsule_GetPointer(connection_capsule, s_capsule_name_mqtt_client_connection); |
| 113 |
AWS_FATAL_ASSERT(py_connection); |
| 114 |
AWS_FATAL_ASSERT(py_connection->native); |
| 115 |
|
| 116 |
|
| 117 |
aws_mqtt_client_connection_set_connection_closed_handler(py_connection->native, NULL, NULL); |
| 118 |
|
| 119 |
if (aws_mqtt_client_connection_disconnect( |
| 120 |
py_connection->native, s_mqtt_python_connection_destructor_on_disconnect, py_connection)) { |
| 121 |
|
| 122 |
s_start_destroy_native(py_connection); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
static void s_on_connection_success( |
| 127 |
struct aws_mqtt_client_connection *connection, |
| 128 |
enum aws_mqtt_connect_return_code return_code, |
| 129 |
bool session_present, |
| 130 |
void *user_data) { |
| 131 |
|
| 132 |
if (connection == NULL || user_data == NULL) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
struct mqtt_connection_binding *py_connection = user_data; |
| 137 |
|
| 138 |
PyGILState_STATE state; |
| 139 |
if (aws_py_gilstate_ensure(&state)) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
PyObject *self = aws_py_weakref_get_ref(py_connection->self_proxy); |
| 144 |
if (self != NULL) { |
| 145 |
PyObject *success_result = |
| 146 |
PyObject_CallMethod(self, "_on_connection_success", "(iN)", return_code, PyBool_FromLong(session_present)); |
| 147 |
if (success_result) { |
| 148 |
Py_DECREF(success_result); |
| 149 |
} else { |
| 150 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 151 |
} |
| 152 |
Py_DECREF(self); |
| 153 |
} |
| 154 |
|
| 155 |
PyGILState_Release(state); |
| 156 |
} |
| 157 |
|
| 158 |
static void s_on_connection_failure(struct aws_mqtt_client_connection *connection, int error_code, void *user_data) { |
| 159 |
|
| 160 |
if (connection == NULL || user_data == NULL) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
struct mqtt_connection_binding *py_connection = user_data; |
| 165 |
|
| 166 |
PyGILState_STATE state; |
| 167 |
if (aws_py_gilstate_ensure(&state)) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
PyObject *self = aws_py_weakref_get_ref(py_connection->self_proxy); |
| 172 |
if (self != NULL) { |
| 173 |
PyObject *success_result = PyObject_CallMethod(self, "_on_connection_failure", "(i)", error_code); |
| 174 |
if (success_result) { |
| 175 |
Py_DECREF(success_result); |
| 176 |
} else { |
| 177 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 178 |
} |
| 179 |
Py_DECREF(self); |
| 180 |
} |
| 181 |
|
| 182 |
PyGILState_Release(state); |
| 183 |
} |
| 184 |
|
| 185 |
static void s_on_connection_interrupted(struct aws_mqtt_client_connection *connection, int error_code, void *userdata) { |
| 186 |
|
| 187 |
if (connection == NULL || userdata == NULL) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
struct mqtt_connection_binding *py_connection = userdata; |
| 192 |
|
| 193 |
PyGILState_STATE state; |
| 194 |
if (aws_py_gilstate_ensure(&state)) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
PyObject *self = aws_py_weakref_get_ref(py_connection->self_proxy); |
| 200 |
if (self != NULL) { |
| 201 |
PyObject *result = PyObject_CallMethod(self, "_on_connection_interrupted", "(i)", error_code); |
| 202 |
if (result) { |
| 203 |
Py_DECREF(result); |
| 204 |
} else { |
| 205 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 206 |
} |
| 207 |
Py_DECREF(self); |
| 208 |
} |
| 209 |
|
| 210 |
PyGILState_Release(state); |
| 211 |
} |
| 212 |
|
| 213 |
static void s_on_connection_resumed( |
| 214 |
struct aws_mqtt_client_connection *connection, |
| 215 |
enum aws_mqtt_connect_return_code return_code, |
| 216 |
bool session_present, |
| 217 |
void *userdata) { |
| 218 |
|
| 219 |
if (connection == NULL || userdata == NULL) { |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
(void)connection; |
| 224 |
|
| 225 |
struct mqtt_connection_binding *py_connection = userdata; |
| 226 |
|
| 227 |
PyGILState_STATE state; |
| 228 |
if (aws_py_gilstate_ensure(&state)) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
PyObject *self = aws_py_weakref_get_ref(py_connection->self_proxy); |
| 234 |
if (self != NULL) { |
| 235 |
PyObject *result = |
| 236 |
PyObject_CallMethod(self, "_on_connection_resumed", "(iN)", return_code, PyBool_FromLong(session_present)); |
| 237 |
if (result) { |
| 238 |
Py_DECREF(result); |
| 239 |
} else { |
| 240 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 241 |
} |
| 242 |
Py_DECREF(self); |
| 243 |
} |
| 244 |
|
| 245 |
PyGILState_Release(state); |
| 246 |
} |
| 247 |
|
| 248 |
static void s_on_connection_closed( |
| 249 |
struct aws_mqtt_client_connection *connection, |
| 250 |
struct on_connection_closed_data *data, |
| 251 |
void *userdata) { |
| 252 |
|
| 253 |
if (connection == NULL || userdata == NULL) { |
| 254 |
return; |
| 255 |
} |
| 256 |
(void)data; |
| 257 |
|
| 258 |
PyGILState_STATE state; |
| 259 |
if (aws_py_gilstate_ensure(&state)) { |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
struct mqtt_connection_binding *py_connection = userdata; |
| 264 |
|
| 265 |
PyObject *self = aws_py_weakref_get_ref(py_connection->self_proxy); |
| 266 |
if (self != NULL) { |
| 267 |
PyObject *result = PyObject_CallMethod(self, "_on_connection_closed", "()"); |
| 268 |
if (result) { |
| 269 |
Py_DECREF(result); |
| 270 |
} else { |
| 271 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 272 |
} |
| 273 |
Py_DECREF(self); |
| 274 |
} |
| 275 |
|
| 276 |
PyGILState_Release(state); |
| 277 |
} |
| 278 |
|
| 279 |
PyObject *aws_py_mqtt_client_connection_new(PyObject *self, PyObject *args) { |
| 280 |
(void)self; |
| 281 |
|
| 282 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 283 |
|
| 284 |
PyObject *self_py; |
| 285 |
PyObject *client_py; |
| 286 |
PyObject *use_websocket_py; |
| 287 |
unsigned char client_version; |
| 288 |
if (!PyArg_ParseTuple(args, "OOOb", &self_py, &client_py, &use_websocket_py, &client_version)) { |
| 289 |
return NULL; |
| 290 |
} |
| 291 |
|
| 292 |
void *client = NULL; |
| 293 |
if (client_version == 3) { |
| 294 |
client = aws_py_get_mqtt_client(client_py); |
| 295 |
} else if (client_version == 5) { |
| 296 |
client = aws_py_get_mqtt5_client(client_py); |
| 297 |
} else { |
| 298 |
PyErr_SetString(PyExc_TypeError, "Mqtt Client version not supported. Failed to create connection."); |
| 299 |
return NULL; |
| 300 |
} |
| 301 |
|
| 302 |
if (!client) { |
| 303 |
return NULL; |
| 304 |
} |
| 305 |
|
| 306 |
struct mqtt_connection_binding *py_connection = |
| 307 |
aws_mem_calloc(allocator, 1, sizeof(struct mqtt_connection_binding)); |
| 308 |
if (!py_connection) { |
| 309 |
PyErr_SetAwsLastError(); |
| 310 |
return NULL; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
PyObject *self_proxy = NULL; |
| 315 |
|
| 316 |
if (client_version == 3) { |
| 317 |
py_connection->native = aws_mqtt_client_connection_new(client); |
| 318 |
} else if (client_version == 5) { |
| 319 |
py_connection->native = aws_mqtt_client_connection_new_from_mqtt5_client(client); |
| 320 |
} |
| 321 |
if (!py_connection->native) { |
| 322 |
PyErr_SetAwsLastError(); |
| 323 |
goto on_error; |
| 324 |
} |
| 325 |
|
| 326 |
if (aws_mqtt_client_connection_set_connection_termination_handler( |
| 327 |
py_connection->native, s_mqtt_python_connection_termination, py_connection)) { |
| 328 |
PyErr_SetAwsLastError(); |
| 329 |
goto on_error; |
| 330 |
} |
| 331 |
|
| 332 |
if (aws_mqtt_client_connection_set_connection_result_handlers( |
| 333 |
py_connection->native, s_on_connection_success, py_connection, s_on_connection_failure, py_connection)) { |
| 334 |
PyErr_SetAwsLastError(); |
| 335 |
goto on_error; |
| 336 |
} |
| 337 |
|
| 338 |
if (aws_mqtt_client_connection_set_connection_interruption_handlers( |
| 339 |
py_connection->native, |
| 340 |
s_on_connection_interrupted, |
| 341 |
py_connection, |
| 342 |
s_on_connection_resumed, |
| 343 |
py_connection)) { |
| 344 |
|
| 345 |
PyErr_SetAwsLastError(); |
| 346 |
goto on_error; |
| 347 |
} |
| 348 |
|
| 349 |
if (aws_mqtt_client_connection_set_connection_closed_handler( |
| 350 |
py_connection->native, s_on_connection_closed, py_connection)) { |
| 351 |
PyErr_SetAwsLastError(); |
| 352 |
goto on_error; |
| 353 |
} |
| 354 |
|
| 355 |
if (PyObject_IsTrue(use_websocket_py)) { |
| 356 |
if (aws_mqtt_client_connection_use_websockets( |
| 357 |
py_connection->native, |
| 358 |
s_ws_handshake_transform, |
| 359 |
py_connection , |
| 360 |
NULL , |
| 361 |
NULL )) { |
| 362 |
|
| 363 |
PyErr_SetAwsLastError(); |
| 364 |
goto on_error; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
self_proxy = PyWeakref_NewProxy(self_py, NULL); |
| 369 |
if (!self_proxy) { |
| 370 |
goto on_error; |
| 371 |
} |
| 372 |
|
| 373 |
PyObject *capsule = |
| 374 |
PyCapsule_New(py_connection, s_capsule_name_mqtt_client_connection, s_mqtt_python_connection_destructor); |
| 375 |
if (!capsule) { |
| 376 |
goto on_error; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
py_connection->self_proxy = self_proxy; |
| 381 |
|
| 382 |
py_connection->client = client_py; |
| 383 |
Py_INCREF(py_connection->client); |
| 384 |
|
| 385 |
return capsule; |
| 386 |
|
| 387 |
on_error: |
| 388 |
Py_XDECREF(self_proxy); |
| 389 |
aws_mqtt_client_connection_release(py_connection->native); |
| 390 |
aws_mem_release(allocator, py_connection); |
| 391 |
return NULL; |
| 392 |
} |
| 393 |
|
| 394 |
struct aws_mqtt_client_connection *aws_py_get_mqtt_client_connection(PyObject *mqtt_connection) { |
| 395 |
AWS_PY_RETURN_NATIVE_FROM_BINDING( |
| 396 |
mqtt_connection, s_capsule_name_mqtt_client_connection, "Connection", mqtt_connection_binding); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
static void s_on_connect( |
| 404 |
struct aws_mqtt_client_connection *connection, |
| 405 |
int error_code, |
| 406 |
enum aws_mqtt_connect_return_code return_code, |
| 407 |
bool session_present, |
| 408 |
void *user_data) { |
| 409 |
|
| 410 |
if (connection == NULL || user_data == NULL) { |
| 411 |
return; |
| 412 |
} |
| 413 |
|
| 414 |
struct mqtt_connection_binding *py_connection = user_data; |
| 415 |
|
| 416 |
PyGILState_STATE state; |
| 417 |
if (aws_py_gilstate_ensure(&state)) { |
| 418 |
return; |
| 419 |
} |
| 420 |
|
| 421 |
if (py_connection->on_connect) { |
| 422 |
|
| 423 |
PyObject *callback = py_connection->on_connect; |
| 424 |
py_connection->on_connect = NULL; |
| 425 |
|
| 426 |
PyObject *result = |
| 427 |
PyObject_CallFunction(callback, "(iiN)", error_code, return_code, PyBool_FromLong(session_present)); |
| 428 |
if (result) { |
| 429 |
Py_DECREF(result); |
| 430 |
} else { |
| 431 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 432 |
} |
| 433 |
|
| 434 |
Py_XDECREF(callback); |
| 435 |
} |
| 436 |
|
| 437 |
PyGILState_Release(state); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
static bool s_set_metrics(struct aws_mqtt_client_connection *connection, PyObject *metrics) { |
| 442 |
assert(metrics && (metrics != Py_None)); |
| 443 |
|
| 444 |
if (connection == NULL) { |
| 445 |
return false; |
| 446 |
} |
| 447 |
|
| 448 |
struct aws_mqtt_iot_metrics metrics_tmp; |
| 449 |
if (!aws_py_metrics_parse(metrics, &metrics_tmp)) { |
| 450 |
return false; |
| 451 |
} |
| 452 |
|
| 453 |
bool success = true; |
| 454 |
if (aws_mqtt_client_connection_set_metrics(connection, &metrics_tmp)) { |
| 455 |
PyErr_SetAwsLastError(); |
| 456 |
success = false; |
| 457 |
} |
| 458 |
|
| 459 |
aws_py_metrics_clean_up(&metrics_tmp); |
| 460 |
return success; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
bool s_set_will(struct aws_mqtt_client_connection *connection, PyObject *will) { |
| 465 |
assert(will && (will != Py_None)); |
| 466 |
|
| 467 |
if (connection == NULL) { |
| 468 |
return false; |
| 469 |
} |
| 470 |
|
| 471 |
bool success = false; |
| 472 |
|
| 473 |
|
| 474 |
PyObject *py_topic = NULL; |
| 475 |
PyObject *py_payload = NULL; |
| 476 |
|
| 477 |
py_topic = PyObject_GetAttrString(will, "topic"); |
| 478 |
struct aws_byte_cursor topic = aws_byte_cursor_from_pyunicode(py_topic); |
| 479 |
if (!topic.ptr) { |
| 480 |
PyErr_SetString(PyExc_TypeError, "Will.topic must be str type"); |
| 481 |
goto done; |
| 482 |
} |
| 483 |
|
| 484 |
enum aws_mqtt_qos qos = PyObject_GetAttrAsIntEnum(will, "Will", "qos"); |
| 485 |
if (PyErr_Occurred()) { |
| 486 |
goto done; |
| 487 |
} |
| 488 |
|
| 489 |
py_payload = PyObject_GetAttrString(will, "payload"); |
| 490 |
struct aws_byte_cursor payload = aws_byte_cursor_from_pybytes(py_payload); |
| 491 |
if (!payload.ptr) { |
| 492 |
PyErr_SetString(PyExc_TypeError, "Will.payload must be bytes type"); |
| 493 |
goto done; |
| 494 |
} |
| 495 |
|
| 496 |
bool retain = PyObject_GetAttrAsBool(will, "Will", "retain"); |
| 497 |
if (PyErr_Occurred()) { |
| 498 |
goto done; |
| 499 |
} |
| 500 |
|
| 501 |
if (aws_mqtt_client_connection_set_will(connection, &topic, qos, retain, &payload)) { |
| 502 |
PyErr_SetAwsLastError(); |
| 503 |
goto done; |
| 504 |
} |
| 505 |
|
| 506 |
success = true; |
| 507 |
|
| 508 |
done: |
| 509 |
Py_XDECREF(py_topic); |
| 510 |
Py_XDECREF(py_payload); |
| 511 |
return success; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
struct ws_handshake_transform_data { |
| 517 |
struct aws_http_message *request; |
| 518 |
aws_mqtt_transform_websocket_handshake_complete_fn *complete_fn; |
| 519 |
void *complete_ctx; |
| 520 |
|
| 521 |
|
| 522 |
PyObject *connection_py; |
| 523 |
|
| 524 |
|
| 525 |
PyObject *request_binding_py; |
| 526 |
PyObject *headers_binding_py; |
| 527 |
}; |
| 528 |
|
| 529 |
static const char *s_capsule_name_ws_handshake_transform_data = "aws_ws_handshake_transform_data"; |
| 530 |
|
| 531 |
void s_ws_handshake_transform_data_destructor(PyObject *capsule) { |
| 532 |
struct ws_handshake_transform_data *ws_data = |
| 533 |
PyCapsule_GetPointer(capsule, s_capsule_name_ws_handshake_transform_data); |
| 534 |
|
| 535 |
|
| 536 |
Py_XDECREF(ws_data->connection_py); |
| 537 |
Py_XDECREF(ws_data->request_binding_py); |
| 538 |
Py_XDECREF(ws_data->headers_binding_py); |
| 539 |
|
| 540 |
aws_mem_release(aws_py_get_allocator(), ws_data); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
static void s_ws_handshake_transform( |
| 545 |
struct aws_http_message *request, |
| 546 |
void *user_data, |
| 547 |
aws_mqtt_transform_websocket_handshake_complete_fn *complete_fn, |
| 548 |
void *complete_ctx) { |
| 549 |
|
| 550 |
struct mqtt_connection_binding *connection_binding = user_data; |
| 551 |
|
| 552 |
bool success = false; |
| 553 |
|
| 554 |
|
| 555 |
struct ws_handshake_transform_data *ws_transform_data = NULL; |
| 556 |
PyObject *ws_transform_capsule = NULL; |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
PyGILState_STATE state; |
| 561 |
if (aws_py_gilstate_ensure(&state)) { |
| 562 |
return; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
PyObject *connection_py = aws_py_weakref_get_ref(connection_binding->self_proxy); |
| 568 |
if (connection_py == NULL) { |
| 569 |
aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 570 |
goto done; |
| 571 |
} |
| 572 |
|
| 573 |
ws_transform_data = aws_mem_calloc(aws_py_get_allocator(), 1, sizeof(struct ws_handshake_transform_data)); |
| 574 |
if (!ws_transform_data) { |
| 575 |
goto done; |
| 576 |
} |
| 577 |
|
| 578 |
ws_transform_capsule = PyCapsule_New( |
| 579 |
ws_transform_data, s_capsule_name_ws_handshake_transform_data, s_ws_handshake_transform_data_destructor); |
| 580 |
if (!ws_transform_capsule) { |
| 581 |
aws_py_raise_error(); |
| 582 |
goto done; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
ws_transform_data->request = request; |
| 588 |
ws_transform_data->complete_fn = complete_fn; |
| 589 |
ws_transform_data->complete_ctx = complete_ctx; |
| 590 |
|
| 591 |
ws_transform_data->connection_py = connection_py; |
| 592 |
Py_INCREF(ws_transform_data->connection_py); |
| 593 |
|
| 594 |
ws_transform_data->request_binding_py = aws_py_http_message_new_request_from_native(request); |
| 595 |
if (!ws_transform_data->request_binding_py) { |
| 596 |
aws_py_raise_error(); |
| 597 |
goto done; |
| 598 |
} |
| 599 |
|
| 600 |
ws_transform_data->headers_binding_py = aws_py_http_headers_new_from_native(aws_http_message_get_headers(request)); |
| 601 |
if (!ws_transform_data->headers_binding_py) { |
| 602 |
aws_py_raise_error(); |
| 603 |
goto done; |
| 604 |
} |
| 605 |
|
| 606 |
PyObject *result = PyObject_CallMethod( |
| 607 |
connection_py, |
| 608 |
"_ws_handshake_transform", |
| 609 |
"(OOO)", |
| 610 |
ws_transform_data->request_binding_py, |
| 611 |
ws_transform_data->headers_binding_py, |
| 612 |
ws_transform_capsule); |
| 613 |
|
| 614 |
if (result) { |
| 615 |
Py_DECREF(result); |
| 616 |
} else { |
| 617 |
aws_py_raise_error(); |
| 618 |
goto done; |
| 619 |
} |
| 620 |
|
| 621 |
success = true; |
| 622 |
done:; |
| 623 |
|
| 624 |
int error_code = aws_last_error(); |
| 625 |
Py_XDECREF(connection_py); |
| 626 |
|
| 627 |
if (ws_transform_capsule) { |
| 628 |
Py_DECREF(ws_transform_capsule); |
| 629 |
} else if (ws_transform_data) { |
| 630 |
aws_mem_release(aws_py_get_allocator(), ws_transform_data); |
| 631 |
} |
| 632 |
|
| 633 |
PyGILState_Release(state); |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
if (!success) { |
| 638 |
complete_fn(request, error_code, complete_ctx); |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
PyObject *aws_py_mqtt_ws_handshake_transform_complete(PyObject *self, PyObject *args) { |
| 644 |
(void)self; |
| 645 |
|
| 646 |
PyObject *exception_py; |
| 647 |
PyObject *ws_transform_capsule; |
| 648 |
int error_code = AWS_ERROR_SUCCESS; |
| 649 |
if (!PyArg_ParseTuple(args, "OOi", &exception_py, &ws_transform_capsule, &error_code)) { |
| 650 |
return NULL; |
| 651 |
} |
| 652 |
|
| 653 |
if (exception_py != Py_None && error_code == AWS_ERROR_SUCCESS) { |
| 654 |
|
| 655 |
error_code = AWS_ERROR_HTTP_CALLBACK_FAILURE; |
| 656 |
} |
| 657 |
|
| 658 |
struct ws_handshake_transform_data *ws_transform_data = |
| 659 |
PyCapsule_GetPointer(ws_transform_capsule, s_capsule_name_ws_handshake_transform_data); |
| 660 |
if (!ws_transform_data) { |
| 661 |
return NULL; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
ws_transform_data->complete_fn(ws_transform_data->request, error_code, ws_transform_data->complete_ctx); |
| 666 |
|
| 667 |
Py_RETURN_NONE; |
| 668 |
} |
| 669 |
|
| 670 |
PyObject *aws_py_mqtt_client_connection_connect(PyObject *self, PyObject *args) { |
| 671 |
(void)self; |
| 672 |
|
| 673 |
PyObject *impl_capsule; |
| 674 |
const char *client_id; |
| 675 |
Py_ssize_t client_id_len; |
| 676 |
const char *server_name; |
| 677 |
Py_ssize_t server_name_len; |
| 678 |
uint32_t port_number; |
| 679 |
PyObject *socket_options_py; |
| 680 |
PyObject *tls_ctx_py; |
| 681 |
uint64_t reconnect_min_timeout_secs; |
| 682 |
uint64_t reconnect_max_timeout_secs; |
| 683 |
uint16_t keep_alive_time; |
| 684 |
uint32_t ping_timeout; |
| 685 |
uint32_t protocol_operation_timeout; |
| 686 |
PyObject *will; |
| 687 |
const char *username; |
| 688 |
Py_ssize_t username_len; |
| 689 |
const char *password; |
| 690 |
Py_ssize_t password_len; |
| 691 |
PyObject *is_clean_session; |
| 692 |
PyObject *on_connect; |
| 693 |
PyObject *proxy_options_py; |
| 694 |
PyObject *metrics_py; |
| 695 |
if (!PyArg_ParseTuple( |
| 696 |
args, |
| 697 |
"Os#s#IOOKKHIIOz#z#OOOO", |
| 698 |
&impl_capsule, |
| 699 |
&client_id, |
| 700 |
&client_id_len, |
| 701 |
&server_name, |
| 702 |
&server_name_len, |
| 703 |
&port_number, |
| 704 |
&socket_options_py, |
| 705 |
&tls_ctx_py, |
| 706 |
&reconnect_min_timeout_secs, |
| 707 |
&reconnect_max_timeout_secs, |
| 708 |
&keep_alive_time, |
| 709 |
&ping_timeout, |
| 710 |
&protocol_operation_timeout, |
| 711 |
&will, |
| 712 |
&username, |
| 713 |
&username_len, |
| 714 |
&password, |
| 715 |
&password_len, |
| 716 |
&is_clean_session, |
| 717 |
&on_connect, |
| 718 |
&proxy_options_py, |
| 719 |
&metrics_py)) { |
| 720 |
return NULL; |
| 721 |
} |
| 722 |
|
| 723 |
struct mqtt_connection_binding *py_connection = |
| 724 |
PyCapsule_GetPointer(impl_capsule, s_capsule_name_mqtt_client_connection); |
| 725 |
if (!py_connection) { |
| 726 |
return NULL; |
| 727 |
} |
| 728 |
|
| 729 |
if (py_connection->on_connect) { |
| 730 |
PyErr_SetString(PyExc_RuntimeError, "Connection already in progress"); |
| 731 |
return NULL; |
| 732 |
} |
| 733 |
|
| 734 |
struct aws_socket_options socket_options; |
| 735 |
if (!aws_py_socket_options_init(&socket_options, socket_options_py)) { |
| 736 |
return NULL; |
| 737 |
} |
| 738 |
|
| 739 |
struct aws_byte_cursor server_name_cur = aws_byte_cursor_from_array(server_name, server_name_len); |
| 740 |
|
| 741 |
if (aws_mqtt_client_connection_set_reconnect_timeout( |
| 742 |
py_connection->native, reconnect_min_timeout_secs, reconnect_max_timeout_secs)) { |
| 743 |
return PyErr_AwsLastError(); |
| 744 |
} |
| 745 |
|
| 746 |
if (will != Py_None) { |
| 747 |
if (!s_set_will(py_connection->native, will)) { |
| 748 |
return NULL; |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
if (username) { |
| 753 |
struct aws_byte_cursor username_cur = aws_byte_cursor_from_array(username, username_len); |
| 754 |
|
| 755 |
struct aws_byte_cursor password_cur; |
| 756 |
struct aws_byte_cursor *password_cur_ptr = NULL; |
| 757 |
if (password) { |
| 758 |
password_cur.ptr = (uint8_t *)password; |
| 759 |
password_cur.len = password_len; |
| 760 |
password_cur_ptr = &password_cur; |
| 761 |
} |
| 762 |
|
| 763 |
if (aws_mqtt_client_connection_set_login(py_connection->native, &username_cur, password_cur_ptr)) { |
| 764 |
return PyErr_AwsLastError(); |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
if (proxy_options_py != Py_None) { |
| 769 |
struct aws_http_proxy_options proxy_options; |
| 770 |
if (!aws_py_http_proxy_options_init(&proxy_options, proxy_options_py)) { |
| 771 |
return NULL; |
| 772 |
} |
| 773 |
|
| 774 |
if (aws_mqtt_client_connection_set_http_proxy_options(py_connection->native, &proxy_options)) { |
| 775 |
return PyErr_AwsLastError(); |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
struct aws_tls_ctx *tls_ctx = NULL; |
| 780 |
struct aws_tls_connection_options tls_options; |
| 781 |
AWS_ZERO_STRUCT(tls_options); |
| 782 |
|
| 783 |
|
| 784 |
bool success = false; |
| 785 |
|
| 786 |
if (tls_ctx_py != Py_None) { |
| 787 |
tls_ctx = aws_py_get_tls_ctx(tls_ctx_py); |
| 788 |
if (!tls_ctx) { |
| 789 |
goto done; |
| 790 |
} |
| 791 |
|
| 792 |
aws_tls_connection_options_init_from_ctx(&tls_options, tls_ctx); |
| 793 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 794 |
struct aws_byte_cursor server_name_cur = aws_byte_cursor_from_c_str(server_name); |
| 795 |
if (aws_tls_connection_options_set_server_name(&tls_options, allocator, &server_name_cur)) { |
| 796 |
PyErr_SetAwsLastError(); |
| 797 |
goto done; |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
if (metrics_py != Py_None) { |
| 803 |
if (!s_set_metrics(py_connection->native, metrics_py)) { |
| 804 |
AWS_LOGF_DEBUG(AWS_LS_MQTT_CLIENT, "MQTT connection failed to set AWS IoT metrics."); |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
if (on_connect != Py_None) { |
| 809 |
Py_INCREF(on_connect); |
| 810 |
py_connection->on_connect = on_connect; |
| 811 |
} |
| 812 |
|
| 813 |
struct aws_byte_cursor client_id_cur = aws_byte_cursor_from_array(client_id, client_id_len); |
| 814 |
struct aws_mqtt_connection_options options = { |
| 815 |
.host_name = server_name_cur, |
| 816 |
.port = port_number, |
| 817 |
.socket_options = &socket_options, |
| 818 |
.tls_options = tls_ctx ? &tls_options : NULL, |
| 819 |
.client_id = client_id_cur, |
| 820 |
.keep_alive_time_secs = keep_alive_time, |
| 821 |
.ping_timeout_ms = ping_timeout, |
| 822 |
.protocol_operation_timeout_ms = protocol_operation_timeout, |
| 823 |
.on_connection_complete = s_on_connect, |
| 824 |
.user_data = py_connection, |
| 825 |
.clean_session = PyObject_IsTrue(is_clean_session), |
| 826 |
}; |
| 827 |
if (aws_mqtt_client_connection_connect(py_connection->native, &options)) { |
| 828 |
PyErr_SetAwsLastError(); |
| 829 |
goto done; |
| 830 |
} |
| 831 |
|
| 832 |
success = true; |
| 833 |
done: |
| 834 |
aws_tls_connection_options_clean_up(&tls_options); |
| 835 |
if (success) { |
| 836 |
Py_RETURN_NONE; |
| 837 |
} |
| 838 |
|
| 839 |
Py_CLEAR(py_connection->on_connect); |
| 840 |
return NULL; |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
PyObject *aws_py_mqtt_client_connection_reconnect(PyObject *self, PyObject *args) { |
| 848 |
|
| 849 |
(void)self; |
| 850 |
|
| 851 |
PyObject *impl_capsule; |
| 852 |
PyObject *on_connect; |
| 853 |
if (!PyArg_ParseTuple(args, "OO", &impl_capsule, &on_connect)) { |
| 854 |
return NULL; |
| 855 |
} |
| 856 |
|
| 857 |
struct mqtt_connection_binding *py_connection = |
| 858 |
PyCapsule_GetPointer(impl_capsule, s_capsule_name_mqtt_client_connection); |
| 859 |
if (!py_connection) { |
| 860 |
return NULL; |
| 861 |
} |
| 862 |
|
| 863 |
if (py_connection->on_connect) { |
| 864 |
PyErr_SetString(PyExc_RuntimeError, "Connection already in progress"); |
| 865 |
return NULL; |
| 866 |
} |
| 867 |
|
| 868 |
if (on_connect != Py_None) { |
| 869 |
Py_INCREF(on_connect); |
| 870 |
py_connection->on_connect = on_connect; |
| 871 |
} |
| 872 |
|
| 873 |
if (aws_mqtt_client_connection_reconnect(py_connection->native, s_on_connect, py_connection)) { |
| 874 |
Py_CLEAR(py_connection->on_connect); |
| 875 |
PyErr_SetAwsLastError(); |
| 876 |
return NULL; |
| 877 |
} |
| 878 |
|
| 879 |
Py_RETURN_NONE; |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
struct publish_complete_userdata { |
| 887 |
PyObject *callback; |
| 888 |
}; |
| 889 |
|
| 890 |
static void s_publish_complete( |
| 891 |
struct aws_mqtt_client_connection *connection, |
| 892 |
uint16_t packet_id, |
| 893 |
int error_code, |
| 894 |
void *userdata) { |
| 895 |
|
| 896 |
if (connection == NULL || userdata == NULL) { |
| 897 |
return; |
| 898 |
} |
| 899 |
|
| 900 |
struct publish_complete_userdata *metadata = userdata; |
| 901 |
assert(metadata); |
| 902 |
|
| 903 |
PyGILState_STATE state; |
| 904 |
if (aws_py_gilstate_ensure(&state)) { |
| 905 |
return; |
| 906 |
} |
| 907 |
|
| 908 |
if (metadata->callback != Py_None) { |
| 909 |
PyObject *result = PyObject_CallFunction(metadata->callback, "(Hi)", packet_id, error_code); |
| 910 |
if (result) { |
| 911 |
Py_DECREF(result); |
| 912 |
} else { |
| 913 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
Py_DECREF(metadata->callback); |
| 918 |
|
| 919 |
PyGILState_Release(state); |
| 920 |
|
| 921 |
aws_mem_release(aws_py_get_allocator(), metadata); |
| 922 |
} |
| 923 |
|
| 924 |
PyObject *aws_py_mqtt_client_connection_publish(PyObject *self, PyObject *args) { |
| 925 |
(void)self; |
| 926 |
|
| 927 |
PyObject *impl_capsule; |
| 928 |
Py_buffer topic_stack; |
| 929 |
Py_buffer payload_stack; |
| 930 |
uint8_t qos_val; |
| 931 |
PyObject *retain; |
| 932 |
PyObject *puback_callback; |
| 933 |
if (!PyArg_ParseTuple( |
| 934 |
args, "Os*s*bOO", &impl_capsule, &topic_stack, &payload_stack, &qos_val, &retain, &puback_callback)) { |
| 935 |
return NULL; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
struct mqtt_connection_binding *connection = |
| 941 |
PyCapsule_GetPointer(impl_capsule, s_capsule_name_mqtt_client_connection); |
| 942 |
if (!connection) { |
| 943 |
goto arg_error; |
| 944 |
} |
| 945 |
|
| 946 |
if (qos_val >= AWS_MQTT_QOS_EXACTLY_ONCE) { |
| 947 |
PyErr_SetNone(PyExc_ValueError); |
| 948 |
goto arg_error; |
| 949 |
} |
| 950 |
|
| 951 |
struct publish_complete_userdata *metadata = NULL; |
| 952 |
|
| 953 |
|
| 954 |
metadata = aws_mem_calloc(aws_py_get_allocator(), 1, sizeof(struct publish_complete_userdata)); |
| 955 |
if (!metadata) { |
| 956 |
PyErr_SetAwsLastError(); |
| 957 |
goto metadata_alloc_failed; |
| 958 |
} |
| 959 |
|
| 960 |
metadata->callback = puback_callback; |
| 961 |
Py_INCREF(metadata->callback); |
| 962 |
|
| 963 |
struct aws_byte_cursor topic_cursor; |
| 964 |
topic_cursor = aws_byte_cursor_from_array(topic_stack.buf, topic_stack.len); |
| 965 |
|
| 966 |
struct aws_byte_cursor payload_cursor; |
| 967 |
payload_cursor = aws_byte_cursor_from_array(payload_stack.buf, payload_stack.len); |
| 968 |
|
| 969 |
enum aws_mqtt_qos qos = (enum aws_mqtt_qos)qos_val; |
| 970 |
|
| 971 |
uint16_t msg_id = aws_mqtt_client_connection_publish( |
| 972 |
connection->native, &topic_cursor, qos, retain == Py_True, &payload_cursor, s_publish_complete, metadata); |
| 973 |
|
| 974 |
if (msg_id == 0) { |
| 975 |
PyErr_SetAwsLastError(); |
| 976 |
goto publish_failed; |
| 977 |
} |
| 978 |
PyBuffer_Release(&topic_stack); |
| 979 |
PyBuffer_Release(&payload_stack); |
| 980 |
|
| 981 |
return PyLong_FromUnsignedLong(msg_id); |
| 982 |
|
| 983 |
publish_failed: |
| 984 |
Py_DECREF(metadata->callback); |
| 985 |
aws_mem_release(aws_py_get_allocator(), metadata); |
| 986 |
metadata_alloc_failed: |
| 987 |
arg_error: |
| 988 |
PyBuffer_Release(&topic_stack); |
| 989 |
PyBuffer_Release(&payload_stack); |
| 990 |
return NULL; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
static void s_subscribe_callback( |
| 998 |
struct aws_mqtt_client_connection *connection, |
| 999 |
const struct aws_byte_cursor *topic, |
| 1000 |
const struct aws_byte_cursor *payload, |
| 1001 |
bool dup, |
| 1002 |
enum aws_mqtt_qos qos, |
| 1003 |
bool retain, |
| 1004 |
void *user_data) { |
| 1005 |
|
| 1006 |
if (connection == NULL || user_data == NULL) { |
| 1007 |
return; |
| 1008 |
} |
| 1009 |
|
| 1010 |
PyObject *callback = user_data; |
| 1011 |
if (callback == Py_None) { |
| 1012 |
return; |
| 1013 |
} |
| 1014 |
|
| 1015 |
PyGILState_STATE state; |
| 1016 |
if (aws_py_gilstate_ensure(&state)) { |
| 1017 |
return; |
| 1018 |
} |
| 1019 |
|
| 1020 |
PyObject *result = PyObject_CallFunction( |
| 1021 |
callback, |
| 1022 |
"(s#y#OiO)", |
| 1023 |
topic->ptr, |
| 1024 |
topic->len, |
| 1025 |
payload->ptr, |
| 1026 |
payload->len, |
| 1027 |
dup ? Py_True : Py_False, |
| 1028 |
qos, |
| 1029 |
retain ? Py_True : Py_False); |
| 1030 |
|
| 1031 |
if (result) { |
| 1032 |
Py_DECREF(result); |
| 1033 |
} else { |
| 1034 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 1035 |
} |
| 1036 |
|
| 1037 |
PyGILState_Release(state); |
| 1038 |
} |
| 1039 |
|
| 1040 |
static void s_callback_cleanup(void *userdata) { |
| 1041 |
PyObject *callback = userdata; |
| 1042 |
|
| 1043 |
PyGILState_STATE state; |
| 1044 |
if (aws_py_gilstate_ensure(&state)) { |
| 1045 |
return; |
| 1046 |
} |
| 1047 |
|
| 1048 |
Py_DECREF(callback); |
| 1049 |
|
| 1050 |
PyGILState_Release(state); |
| 1051 |
} |
| 1052 |
|
| 1053 |
static void s_suback_callback( |
| 1054 |
struct aws_mqtt_client_connection *connection, |
| 1055 |
uint16_t packet_id, |
| 1056 |
const struct aws_byte_cursor *topic, |
| 1057 |
enum aws_mqtt_qos qos, |
| 1058 |
int error_code, |
| 1059 |
void *userdata) { |
| 1060 |
|
| 1061 |
if (connection == NULL || userdata == NULL) { |
| 1062 |
return; |
| 1063 |
} |
| 1064 |
|
| 1065 |
PyObject *callback = userdata; |
| 1066 |
AWS_FATAL_ASSERT(callback && callback != Py_None); |
| 1067 |
|
| 1068 |
PyGILState_STATE state; |
| 1069 |
if (aws_py_gilstate_ensure(&state)) { |
| 1070 |
return; |
| 1071 |
} |
| 1072 |
|
| 1073 |
const char *topic_str = (const char *)topic->ptr; |
| 1074 |
Py_ssize_t topic_len = topic->len; |
| 1075 |
PyObject *result = PyObject_CallFunction(callback, "(Hs#Bi)", packet_id, topic_str, topic_len, qos, error_code); |
| 1076 |
if (result) { |
| 1077 |
Py_DECREF(result); |
| 1078 |
} else { |
| 1079 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 1080 |
} |
| 1081 |
|
| 1082 |
Py_DECREF(callback); |
| 1083 |
|
| 1084 |
PyGILState_Release(state); |
| 1085 |
} |
| 1086 |
|
| 1087 |
PyObject *aws_py_mqtt_client_connection_subscribe(PyObject *self, PyObject *args) { |
| 1088 |
(void)self; |
| 1089 |
|
| 1090 |
PyObject *impl_capsule; |
| 1091 |
const char *topic; |
| 1092 |
Py_ssize_t topic_len; |
| 1093 |
uint8_t qos_val; |
| 1094 |
PyObject *callback; |
| 1095 |
PyObject *suback_callback; |
| 1096 |
if (!PyArg_ParseTuple(args, "Os#bOO", &impl_capsule, &topic, &topic_len, &qos_val, &callback, &suback_callback)) { |
| 1097 |
return NULL; |
| 1098 |
} |
| 1099 |
|
| 1100 |
struct mqtt_connection_binding *py_connection = |
| 1101 |
PyCapsule_GetPointer(impl_capsule, s_capsule_name_mqtt_client_connection); |
| 1102 |
if (!py_connection) { |
| 1103 |
return NULL; |
| 1104 |
} |
| 1105 |
|
| 1106 |
Py_INCREF(callback); |
| 1107 |
Py_INCREF(suback_callback); |
| 1108 |
|
| 1109 |
struct aws_byte_cursor topic_filter = aws_byte_cursor_from_array(topic, topic_len); |
| 1110 |
uint16_t msg_id = aws_mqtt_client_connection_subscribe( |
| 1111 |
py_connection->native, |
| 1112 |
&topic_filter, |
| 1113 |
qos_val, |
| 1114 |
s_subscribe_callback, |
| 1115 |
callback, |
| 1116 |
s_callback_cleanup, |
| 1117 |
s_suback_callback, |
| 1118 |
suback_callback); |
| 1119 |
|
| 1120 |
if (msg_id == 0) { |
| 1121 |
Py_DECREF(callback); |
| 1122 |
Py_DECREF(suback_callback); |
| 1123 |
return PyErr_AwsLastError(); |
| 1124 |
} |
| 1125 |
|
| 1126 |
return PyLong_FromUnsignedLong(msg_id); |
| 1127 |
} |
| 1128 |
|
| 1129 |
PyObject *aws_py_mqtt_client_connection_on_message(PyObject *self, PyObject *args) { |
| 1130 |
(void)self; |
| 1131 |
|
| 1132 |
PyObject *impl_capsule; |
| 1133 |
PyObject *callback; |
| 1134 |
if (!PyArg_ParseTuple(args, "OO", &impl_capsule, &callback)) { |
| 1135 |
return NULL; |
| 1136 |
} |
| 1137 |
|
| 1138 |
struct mqtt_connection_binding *py_connection = |
| 1139 |
PyCapsule_GetPointer(impl_capsule, s_capsule_name_mqtt_client_connection); |
| 1140 |
if (!py_connection) { |
| 1141 |
return NULL; |
| 1142 |
} |
| 1143 |
|
| 1144 |
if (callback == Py_None) { |
| 1145 |
if (aws_mqtt_client_connection_set_on_any_publish_handler(py_connection->native, NULL, NULL)) { |
| 1146 |
return PyErr_AwsLastError(); |
| 1147 |
} |
| 1148 |
} else { |
| 1149 |
if (aws_mqtt_client_connection_set_on_any_publish_handler( |
| 1150 |
py_connection->native, s_subscribe_callback, callback)) { |
| 1151 |
return PyErr_AwsLastError(); |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
Py_XDECREF(py_connection->on_any_publish); |
| 1156 |
|
| 1157 |
py_connection->on_any_publish = callback; |
| 1158 |
Py_INCREF(callback); |
| 1159 |
|
| 1160 |
Py_RETURN_NONE; |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
static void s_unsuback_callback( |
| 1168 |
struct aws_mqtt_client_connection *connection, |
| 1169 |
uint16_t packet_id, |
| 1170 |
int error_code, |
| 1171 |
void *userdata) { |
| 1172 |
|
| 1173 |
if (connection == NULL || userdata == NULL) { |
| 1174 |
return; |
| 1175 |
} |
| 1176 |
|
| 1177 |
PyObject *callback = userdata; |
| 1178 |
|
| 1179 |
PyGILState_STATE state; |
| 1180 |
if (aws_py_gilstate_ensure(&state)) { |
| 1181 |
return; |
| 1182 |
} |
| 1183 |
|
| 1184 |
PyObject *result = PyObject_CallFunction(callback, "(Hi)", packet_id, error_code); |
| 1185 |
if (result) { |
| 1186 |
Py_DECREF(result); |
| 1187 |
} else { |
| 1188 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 1189 |
} |
| 1190 |
|
| 1191 |
Py_DECREF(callback); |
| 1192 |
|
| 1193 |
PyGILState_Release(state); |
| 1194 |
} |
| 1195 |
|
| 1196 |
PyObject *aws_py_mqtt_client_connection_unsubscribe(PyObject *self, PyObject *args) { |
| 1197 |
(void)self; |
| 1198 |
|
| 1199 |
PyObject *impl_capsule; |
| 1200 |
const char *topic; |
| 1201 |
Py_ssize_t topic_len; |
| 1202 |
PyObject *unsuback_callback; |
| 1203 |
if (!PyArg_ParseTuple(args, "Os#O", &impl_capsule, &topic, &topic_len, &unsuback_callback)) { |
| 1204 |
return NULL; |
| 1205 |
} |
| 1206 |
|
| 1207 |
struct mqtt_connection_binding *connection = |
| 1208 |
PyCapsule_GetPointer(impl_capsule, s_capsule_name_mqtt_client_connection); |
| 1209 |
if (!connection) { |
| 1210 |
return NULL; |
| 1211 |
} |
| 1212 |
|
| 1213 |
struct aws_byte_cursor filter = aws_byte_cursor_from_array(topic, topic_len); |
| 1214 |
Py_INCREF(unsuback_callback); |
| 1215 |
uint16_t msg_id = |
| 1216 |
aws_mqtt_client_connection_unsubscribe(connection->native, &filter, s_unsuback_callback, unsuback_callback); |
| 1217 |
|
| 1218 |
if (msg_id == 0) { |
| 1219 |
Py_DECREF(unsuback_callback); |
| 1220 |
return PyErr_AwsLastError(); |
| 1221 |
} |
| 1222 |
|
| 1223 |
return PyLong_FromUnsignedLong(msg_id); |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
static void s_suback_multi_callback( |
| 1231 |
struct aws_mqtt_client_connection *connection, |
| 1232 |
uint16_t packet_id, |
| 1233 |
const struct aws_array_list *topic_subacks, |
| 1234 |
int error_code, |
| 1235 |
void *userdata) { |
| 1236 |
|
| 1237 |
if (connection == NULL || userdata == NULL) { |
| 1238 |
return; |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
PyObject *callback = userdata; |
| 1243 |
PyObject *callback_result = NULL; |
| 1244 |
PyObject *topic_qos_list = NULL; |
| 1245 |
|
| 1246 |
PyGILState_STATE state; |
| 1247 |
if (aws_py_gilstate_ensure(&state)) { |
| 1248 |
return; |
| 1249 |
} |
| 1250 |
|
| 1251 |
if (error_code) { |
| 1252 |
goto done_prepping_args; |
| 1253 |
} |
| 1254 |
|
| 1255 |
const size_t num_topics = aws_array_list_length(topic_subacks); |
| 1256 |
|
| 1257 |
|
| 1258 |
topic_qos_list = PyList_New(num_topics); |
| 1259 |
if (!topic_qos_list) { |
| 1260 |
error_code = aws_py_translate_py_error(); |
| 1261 |
goto done_prepping_args; |
| 1262 |
} |
| 1263 |
|
| 1264 |
for (size_t i = 0; i < num_topics; ++i) { |
| 1265 |
struct aws_mqtt_topic_subscription *sub_i = NULL; |
| 1266 |
aws_array_list_get_at(topic_subacks, &sub_i, i); |
| 1267 |
PyObject *tuple = Py_BuildValue("(s#i)", sub_i->topic.ptr, sub_i->topic.len, sub_i->qos); |
| 1268 |
if (!tuple) { |
| 1269 |
error_code = aws_py_translate_py_error(); |
| 1270 |
goto done_prepping_args; |
| 1271 |
} |
| 1272 |
|
| 1273 |
PyList_SetItem(topic_qos_list, i, tuple); |
| 1274 |
} |
| 1275 |
|
| 1276 |
done_prepping_args:; |
| 1277 |
|
| 1278 |
|
| 1279 |
callback_result = |
| 1280 |
PyObject_CallFunction(callback, "(HOi)", packet_id, (error_code ? Py_None : topic_qos_list), error_code); |
| 1281 |
if (!callback_result) { |
| 1282 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 1283 |
} |
| 1284 |
|
| 1285 |
Py_DECREF(callback); |
| 1286 |
Py_XDECREF(callback_result); |
| 1287 |
Py_XDECREF(topic_qos_list); |
| 1288 |
PyGILState_Release(state); |
| 1289 |
} |
| 1290 |
|
| 1291 |
PyObject *aws_py_mqtt_client_connection_resubscribe_existing_topics(PyObject *self, PyObject *args) { |
| 1292 |
(void)self; |
| 1293 |
|
| 1294 |
PyObject *impl_capsule; |
| 1295 |
PyObject *suback_callback; |
| 1296 |
if (!PyArg_ParseTuple(args, "OO", &impl_capsule, &suback_callback)) { |
| 1297 |
return NULL; |
| 1298 |
} |
| 1299 |
|
| 1300 |
struct mqtt_connection_binding *py_connection = |
| 1301 |
PyCapsule_GetPointer(impl_capsule, s_capsule_name_mqtt_client_connection); |
| 1302 |
if (!py_connection) { |
| 1303 |
return NULL; |
| 1304 |
} |
| 1305 |
|
| 1306 |
if (!PyCallable_Check(suback_callback)) { |
| 1307 |
PyErr_SetString(PyExc_TypeError, "suback_callback is not callable"); |
| 1308 |
return NULL; |
| 1309 |
} |
| 1310 |
|
| 1311 |
Py_INCREF(suback_callback); |
| 1312 |
uint16_t msg_id = |
| 1313 |
aws_mqtt_resubscribe_existing_topics(py_connection->native, s_suback_multi_callback, suback_callback); |
| 1314 |
if (msg_id == 0) { |
| 1315 |
|
| 1316 |
Py_DECREF(suback_callback); |
| 1317 |
|
| 1318 |
int aws_err = aws_last_error(); |
| 1319 |
if (aws_err) { |
| 1320 |
PyErr_SetAwsLastError(); |
| 1321 |
return NULL; |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
return PyLong_FromUnsignedLong(msg_id); |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
static void s_on_disconnect(struct aws_mqtt_client_connection *connection, void *user_data) { |
| 1333 |
|
| 1334 |
if (connection == NULL || user_data == NULL) { |
| 1335 |
return; |
| 1336 |
} |
| 1337 |
|
| 1338 |
PyObject *on_disconnect = user_data; |
| 1339 |
|
| 1340 |
if (on_disconnect) { |
| 1341 |
PyGILState_STATE state; |
| 1342 |
if (aws_py_gilstate_ensure(&state)) { |
| 1343 |
return; |
| 1344 |
} |
| 1345 |
|
| 1346 |
PyObject *result = PyObject_CallFunction(on_disconnect, "()"); |
| 1347 |
if (result) { |
| 1348 |
Py_DECREF(result); |
| 1349 |
} else { |
| 1350 |
PyErr_WriteUnraisable(PyErr_Occurred()); |
| 1351 |
} |
| 1352 |
|
| 1353 |
Py_DECREF(on_disconnect); |
| 1354 |
|
| 1355 |
PyGILState_Release(state); |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
PyObject *aws_py_mqtt_client_connection_disconnect(PyObject *self, PyObject *args) { |
| 1360 |
(void)self; |
| 1361 |
|
| 1362 |
PyObject *impl_capsule; |
| 1363 |
PyObject *on_disconnect; |
| 1364 |
if (!PyArg_ParseTuple(args, "OO", &impl_capsule, &on_disconnect)) { |
| 1365 |
return NULL; |
| 1366 |
} |
| 1367 |
|
| 1368 |
struct mqtt_connection_binding *connection = |
| 1369 |
PyCapsule_GetPointer(impl_capsule, s_capsule_name_mqtt_client_connection); |
| 1370 |
if (!connection) { |
| 1371 |
return NULL; |
| 1372 |
} |
| 1373 |
|
| 1374 |
Py_INCREF(on_disconnect); |
| 1375 |
|
| 1376 |
int err = aws_mqtt_client_connection_disconnect(connection->native, s_on_disconnect, on_disconnect); |
| 1377 |
if (err) { |
| 1378 |
Py_DECREF(on_disconnect); |
| 1379 |
return PyErr_AwsLastError(); |
| 1380 |
} |
| 1381 |
|
| 1382 |
Py_RETURN_NONE; |
| 1383 |
} |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
PyObject *aws_py_mqtt_client_connection_get_stats(PyObject *self, PyObject *args) { |
| 1390 |
(void)self; |
| 1391 |
bool success = false; |
| 1392 |
|
| 1393 |
PyObject *impl_capsule; |
| 1394 |
|
| 1395 |
if (!PyArg_ParseTuple(args, "O", &impl_capsule)) { |
| 1396 |
return NULL; |
| 1397 |
} |
| 1398 |
|
| 1399 |
struct mqtt_connection_binding *connection = |
| 1400 |
PyCapsule_GetPointer(impl_capsule, s_capsule_name_mqtt_client_connection); |
| 1401 |
if (!connection) { |
| 1402 |
return NULL; |
| 1403 |
} |
| 1404 |
|
| 1405 |
|
| 1406 |
PyObject *result = NULL; |
| 1407 |
|
| 1408 |
struct aws_mqtt_connection_operation_statistics stats; |
| 1409 |
AWS_ZERO_STRUCT(stats); |
| 1410 |
|
| 1411 |
aws_mqtt_client_connection_get_stats(connection->native, &stats); |
| 1412 |
|
| 1413 |
result = PyTuple_New(4); |
| 1414 |
if (!result) { |
| 1415 |
goto done; |
| 1416 |
} |
| 1417 |
|
| 1418 |
PyTuple_SetItem( |
| 1419 |
result, |
| 1420 |
0, |
| 1421 |
PyLong_FromUnsignedLongLong((unsigned long long)stats.incomplete_operation_count)); |
| 1422 |
if (PyTuple_GetItem(result, 0) == NULL) { |
| 1423 |
goto done; |
| 1424 |
} |
| 1425 |
|
| 1426 |
PyTuple_SetItem( |
| 1427 |
result, |
| 1428 |
1, |
| 1429 |
PyLong_FromUnsignedLongLong((unsigned long long)stats.incomplete_operation_size)); |
| 1430 |
if (PyTuple_GetItem(result, 1) == NULL) { |
| 1431 |
goto done; |
| 1432 |
} |
| 1433 |
|
| 1434 |
PyTuple_SetItem( |
| 1435 |
result, |
| 1436 |
2, |
| 1437 |
PyLong_FromUnsignedLongLong((unsigned long long)stats.unacked_operation_count)); |
| 1438 |
if (PyTuple_GetItem(result, 2) == NULL) { |
| 1439 |
goto done; |
| 1440 |
} |
| 1441 |
|
| 1442 |
PyTuple_SetItem( |
| 1443 |
result, |
| 1444 |
3, |
| 1445 |
PyLong_FromUnsignedLongLong((unsigned long long)stats.unacked_operation_size)); |
| 1446 |
if (PyTuple_GetItem(result, 3) == NULL) { |
| 1447 |
goto done; |
| 1448 |
} |
| 1449 |
|
| 1450 |
success = true; |
| 1451 |
|
| 1452 |
done: |
| 1453 |
if (success) { |
| 1454 |
return result; |
| 1455 |
} |
| 1456 |
Py_XDECREF(result); |
| 1457 |
return NULL; |
| 1458 |
} |