Skip to content

Commit

Permalink
311 adapter lifecycle foundations (#293)
Browse files Browse the repository at this point in the history
* mqtt5 connection reset support
* Rework adapter callback and ref counting 


Co-authored-by: Bret Ambrose <bambrose@amazon.com>
  • Loading branch information
bretambrose and Bret Ambrose committed Jun 5, 2023
1 parent d3377e4 commit 8e40355
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 155 deletions.
1 change: 1 addition & 0 deletions include/aws/mqtt/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ enum aws_mqtt_error {
AWS_ERROR_MQTT5_INVALID_INBOUND_TOPIC_ALIAS,
AWS_ERROR_MQTT5_INVALID_OUTBOUND_TOPIC_ALIAS,
AWS_ERROR_MQTT5_INVALID_UTF8_STRING,
AWS_ERROR_MQTT_CONNECTION_RESET_FOR_ADAPTER_CONNECT,

AWS_ERROR_END_MQTT_RANGE = AWS_ERROR_ENUM_END_RANGE(AWS_C_MQTT_PACKAGE_ID),
};
Expand Down
36 changes: 33 additions & 3 deletions include/aws/mqtt/private/v5/mqtt5_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,29 @@ struct aws_mqtt5_client {
* with clean start set to false.
*/
bool has_connected_successfully;

/*
* A flag that allows in-thread observers (currently the mqtt3_to_5 adapter) to signal that the connection
* should be torn down and re-established. Only relevant to the CONNECTING state which is not interruptible:
*
* If the mqtt5 client is in the CONNECTING state (ie waiting for bootstrap to complete) and the 3-adapter
* is asked to connect, then we *MUST* discard the in-progress connection attempt in order to guarantee the
* connection we establish uses all of the configuration parameters that are passed during the mqtt3 API's connect
* call (host, port, tls options, socket options, etc...). Since we can't interrupt the CONNECTING state, we
* instead set a flag that tells the mqtt5 client to tear down the connection as soon as the initial bootstrap
* completes. The reconnect will establish the requested connection using the parameters passed to
* the mqtt3 API.
*
* Rather than try and catch every escape path from CONNECTING, we lazily reset this flag to false when we
* enter the CONNECTING state. On a similar note, we only check this flag as we transition to MQTT_CONNECT.
*
* This flag is ultimately only needed when the 3 adapter and 5 client are used out-of-sync. If you use the
* 3 adapter exclusively after 5 client creation, it never comes into play.
*
* Even the adapter shouldn't manipulate this directly. Instead, use the aws_mqtt5_client_reset_connection private
* API to tear down an in-progress or established connection in response to a connect() request on the adapter.
*/
bool should_reset_connection;
};

AWS_EXTERN_C_BEGIN
Expand Down Expand Up @@ -638,10 +661,17 @@ AWS_MQTT_API void aws_mqtt5_client_statistics_change_operation_statistic_state(
*/
AWS_MQTT_API const char *aws_mqtt5_client_state_to_c_string(enum aws_mqtt5_client_state state);

/*
* Temporary, private API to turn on total incoming packet logging at the byte level.
/**
* An internal API used by the MQTT3 adapter to force any existing-or-in-progress connection to
* be torn down and re-established. Necessary because the MQTT3 interface allows overrides on a large number
* of configuration parameters through the connect() call. We must honor those parameters and the safest thing
* to do is to just throw away the current connection (if it exists) and make a new one. In the case that an MQTT5
* client is being driven entirely by the MQTT3 adapter, this case never actually happens.
*
* @param client client to reset an existing or in-progress connection for
* @return true if a connection reset was triggered, false if there was nothing to do
*/
AWS_MQTT_API void aws_mqtt5_client_enable_full_packet_logging(struct aws_mqtt5_client *client);
AWS_MQTT_API bool aws_mqtt5_client_reset_connection(struct aws_mqtt5_client *client);

AWS_EXTERN_C_END

Expand Down
6 changes: 6 additions & 0 deletions source/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ bool aws_mqtt_is_valid_topic_filter(const struct aws_byte_cursor *topic_filter)
AWS_DEFINE_ERROR_INFO_MQTT(
AWS_ERROR_MQTT5_INVALID_OUTBOUND_TOPIC_ALIAS,
"Outgoing publish contained an invalid (too large or unknown) topic alias"),
AWS_DEFINE_ERROR_INFO_MQTT(
AWS_ERROR_MQTT5_INVALID_UTF8_STRING,
"Outbound packet contains invalid utf-8 data in a field that must be utf-8"),
AWS_DEFINE_ERROR_INFO_MQTT(
AWS_ERROR_MQTT_CONNECTION_RESET_FOR_ADAPTER_CONNECT,
"Mqtt5 connection was reset by the Mqtt3 adapter in order to guarantee correct connection configuration"),
};
/* clang-format on */
#undef AWS_DEFINE_ERROR_INFO_MQTT
Expand Down

0 comments on commit 8e40355

Please sign in to comment.