Skip to content

Commit

Permalink
Add on_termination_callbcak for mqtt311 connection
Browse files Browse the repository at this point in the history
  • Loading branch information
sfodagain committed Aug 11, 2023
1 parent a2ee9a3 commit 0c17792
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/aws/mqtt/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ typedef void(aws_mqtt_client_publish_received_fn)(
/** Called when a connection is closed, right before any resources are deleted */
typedef void(aws_mqtt_client_on_disconnect_fn)(struct aws_mqtt_client_connection *connection, void *userdata);

typedef void(aws_mqtt_client_on_termination_fn)(struct aws_mqtt_client_connection *connection, void *userdata);

/**
* Function to invoke when the websocket handshake request transformation completes.
* This function MUST be invoked or the application will soft-lock.
Expand Down Expand Up @@ -506,6 +508,12 @@ int aws_mqtt_client_connection_set_on_any_publish_handler(
aws_mqtt_client_publish_received_fn *on_any_publish,
void *on_any_publish_ud);

AWS_MQTT_API
int aws_mqtt_client_connection_set_termination_handler(
struct aws_mqtt_client_connection *connection,
aws_mqtt_client_on_termination_fn *on_termination,
void *on_termination_ud);

/**
* Opens the actual connection defined by aws_mqtt_client_connection_new.
* Once the connection is opened, on_connack will be called. Only called when connection is disconnected.
Expand Down Expand Up @@ -560,6 +568,13 @@ int aws_mqtt_client_connection_disconnect(
aws_mqtt_client_on_disconnect_fn *on_disconnect,
void *userdata);


AWS_MQTT_API
int aws_mqtt_client_connection_termination(
struct aws_mqtt_client_connection *connection,
aws_mqtt_client_on_termination_fn *on_termination,
void *userdata);

/**
* Subscribe to topic filters. on_publish will be called when a PUBLISH matching each topic_filter is received.
*
Expand Down
2 changes: 2 additions & 0 deletions include/aws/mqtt/private/client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ struct aws_mqtt_client_connection_311_impl {
void *on_any_publish_ud;
aws_mqtt_client_on_disconnect_fn *on_disconnect;
void *on_disconnect_ud;
aws_mqtt_client_on_termination_fn *on_termination;
void *on_termination_ud;
aws_mqtt_on_operation_statistics_fn *on_any_operation_statistics;
void *on_any_operation_statistics_ud;

Expand Down
5 changes: 5 additions & 0 deletions include/aws/mqtt/private/client_impl_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ struct aws_mqtt_client_connection_vtable {
aws_mqtt_client_publish_received_fn *on_any_publish,
void *on_any_publish_ud);

int (*set_termination_handler_fn)(
void *impl,
aws_mqtt_client_on_termination_fn *on_termination,
void *on_termination_ud);

int (*connect_fn)(void *impl, const struct aws_mqtt_connection_options *connection_options);

int (*reconnect_fn)(void *impl, aws_mqtt_client_on_connection_complete_fn *on_connection_complete, void *userdata);
Expand Down
25 changes: 25 additions & 0 deletions source/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ static void s_mqtt_client_shutdown(
default:
break;
}

/* TODO Call termination callback */

/* The connection can die now. Release the refcount */
aws_mqtt_client_connection_release(&connection->base);
}
Expand Down Expand Up @@ -859,6 +862,8 @@ static void s_on_final_disconnect(struct aws_mqtt_client_connection *connection,
static void s_mqtt_client_connection_start_destroy(struct aws_mqtt_client_connection_311_impl *connection) {
bool call_destroy_final = false;

MQTT_CLIENT_CALL_CALLBACK(connection, on_termination);

AWS_LOGF_DEBUG(
AWS_LS_MQTT_CLIENT,
"id=%p: Last refcount on connection has been released, start destroying the connection.",
Expand Down Expand Up @@ -1159,6 +1164,25 @@ static int s_aws_mqtt_client_connection_311_set_on_any_publish_handler(
return AWS_OP_SUCCESS;
}

static int s_aws_mqtt_client_connection_311_set_termination_handler(
void *impl,
aws_mqtt_client_on_termination_fn *on_termination,
void *on_termination_ud) {

struct aws_mqtt_client_connection_311_impl *connection = impl;

AWS_PRECONDITION(connection);
if (s_check_connection_state_for_configuration(connection)) {
return aws_raise_error(AWS_ERROR_INVALID_STATE);
}
AWS_LOGF_TRACE(AWS_LS_MQTT_CLIENT, "id=%p: Setting connection closed handler", (void *)connection);

connection->on_termination = on_termination;
connection->on_termination_ud = on_termination_ud;

return AWS_OP_SUCCESS;
}

/*******************************************************************************
* Websockets
******************************************************************************/
Expand Down Expand Up @@ -3163,6 +3187,7 @@ static struct aws_mqtt_client_connection_vtable s_aws_mqtt_client_connection_311
.set_connection_interruption_handlers_fn = s_aws_mqtt_client_connection_311_set_connection_interruption_handlers,
.set_connection_closed_handler_fn = s_aws_mqtt_client_connection_311_set_connection_closed_handler,
.set_on_any_publish_handler_fn = s_aws_mqtt_client_connection_311_set_on_any_publish_handler,
.set_termination_handler_fn = s_aws_mqtt_client_connection_311_set_termination_handler,
.connect_fn = s_aws_mqtt_client_connection_311_connect,
.reconnect_fn = s_aws_mqtt_client_connection_311_reconnect,
.disconnect_fn = s_aws_mqtt_client_connection_311_disconnect,
Expand Down
8 changes: 8 additions & 0 deletions source/client_impl_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ int aws_mqtt_client_connection_set_on_any_publish_handler(
return (*connection->vtable->set_on_any_publish_handler_fn)(connection->impl, on_any_publish, on_any_publish_ud);
}

int aws_mqtt_client_connection_set_termination_handler(
struct aws_mqtt_client_connection *connection,
aws_mqtt_client_on_termination_fn *on_termination,
void *on_termination_ud) {

return (*connection->vtable->set_termination_handler_fn)(connection->impl, on_termination, on_termination_ud);
}

int aws_mqtt_client_connection_connect(
struct aws_mqtt_client_connection *connection,
const struct aws_mqtt_connection_options *connection_options) {
Expand Down

0 comments on commit 0c17792

Please sign in to comment.