Lesson 35 - Get Compute Auth Token Working
This commit is contained in:
@@ -0,0 +1,842 @@
|
||||
#pragma once
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
#include <aws/crt/http/HttpConnection.h>
|
||||
#include <aws/crt/mqtt/Mqtt5Types.h>
|
||||
#include <aws/crt/mqtt/MqttClient.h>
|
||||
|
||||
namespace Aws
|
||||
{
|
||||
namespace Crt
|
||||
{
|
||||
namespace Mqtt5
|
||||
{
|
||||
class ConnectPacket;
|
||||
class ConnAckPacket;
|
||||
class DisconnectPacket;
|
||||
class Mqtt5Client;
|
||||
class Mqtt5ClientOptions;
|
||||
class NegotiatedSettings;
|
||||
class PublishResult;
|
||||
class PublishPacket;
|
||||
class PubAckPacket;
|
||||
class SubscribePacket;
|
||||
class SubAckPacket;
|
||||
class UnsubscribePacket;
|
||||
class UnSubAckPacket;
|
||||
class Mqtt5ClientCore;
|
||||
|
||||
class Mqtt5to3AdapterOptions;
|
||||
|
||||
/**
|
||||
* An enumeration that controls how the client applies topic aliasing to outbound publish packets.
|
||||
*
|
||||
* Topic alias behavior is described in
|
||||
* https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901113
|
||||
*/
|
||||
enum class OutboundTopicAliasBehaviorType
|
||||
{
|
||||
|
||||
/**
|
||||
* Maps to Disabled. This keeps the client from being broken (by default) if the broker
|
||||
* topic aliasing implementation has a problem.
|
||||
*/
|
||||
Default = AWS_MQTT5_COTABT_DEFAULT,
|
||||
|
||||
/**
|
||||
* Outbound aliasing is the user's responsibility. Client will cache and use
|
||||
* previously-established aliases if they fall within the negotiated limits of the connection.
|
||||
*
|
||||
* The user must still always submit a full topic in their publishes because disconnections disrupt
|
||||
* topic alias mappings unpredictably. The client will properly use a requested alias when the
|
||||
* most-recently-seen binding for a topic alias value matches the alias and topic in the publish packet.
|
||||
*/
|
||||
Manual = AWS_MQTT5_COTABT_MANUAL,
|
||||
|
||||
/**
|
||||
* (Recommended) The client will ignore any user-specified topic aliasing and instead use an LRU cache
|
||||
* to drive alias usage.
|
||||
*/
|
||||
LRU = AWS_MQTT5_COTABT_LRU,
|
||||
|
||||
/**
|
||||
* Completely disable outbound topic aliasing.
|
||||
*/
|
||||
Disabled = AWS_MQTT5_COTABT_DISABLED,
|
||||
};
|
||||
|
||||
/**
|
||||
* An enumeration that controls whether or not the client allows the broker to send publishes that use topic
|
||||
* aliasing.
|
||||
*
|
||||
* Topic alias behavior is described in
|
||||
* https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901113
|
||||
*/
|
||||
enum class InboundTopicAliasBehaviorType
|
||||
{
|
||||
|
||||
/**
|
||||
* Maps to Disabled. This keeps the client from being broken (by default) if the broker
|
||||
* topic aliasing implementation has a problem.
|
||||
*/
|
||||
Default = AWS_MQTT5_CITABT_DEFAULT,
|
||||
|
||||
/**
|
||||
* Allow the server to send PUBLISH packets to the client that use topic aliasing
|
||||
*/
|
||||
Enabled = AWS_MQTT5_CITABT_ENABLED,
|
||||
|
||||
/**
|
||||
* Forbid the server from sending PUBLISH packets to the client that use topic aliasing
|
||||
*/
|
||||
Disabled = AWS_MQTT5_CITABT_DISABLED,
|
||||
};
|
||||
|
||||
/**
|
||||
* Configuration for all client topic aliasing behavior.
|
||||
*/
|
||||
struct AWS_CRT_CPP_API TopicAliasingOptions
|
||||
{
|
||||
|
||||
/**
|
||||
* Controls what kind of outbound topic aliasing behavior the client should attempt to use.
|
||||
*
|
||||
* If topic aliasing is not supported by the server, this setting has no effect and any attempts to
|
||||
* directly manipulate the topic alias id in outbound publishes will be ignored.
|
||||
*
|
||||
* If left undefined, then outbound topic aliasing is disabled.
|
||||
*/
|
||||
Crt::Optional<OutboundTopicAliasBehaviorType> m_outboundBehavior;
|
||||
|
||||
/**
|
||||
* If outbound topic aliasing is set to LRU, this controls the maximum size of the cache. If outbound
|
||||
* topic aliasing is set to LRU and this is zero or undefined, a sensible default is used (25). If
|
||||
* outbound topic aliasing is not set to LRU, then this setting has no effect.
|
||||
*
|
||||
* The final size of the cache is determined by the minimum of this setting and the value of the
|
||||
* topic_alias_maximum property of the received CONNACK. If the received CONNACK does not have an
|
||||
* explicit positive value for that field, outbound topic aliasing is disabled for the duration of that
|
||||
* connection.
|
||||
*/
|
||||
Crt::Optional<uint16_t> m_outboundCacheMaxSize;
|
||||
|
||||
/**
|
||||
* Controls whether or not the client allows the broker to use topic aliasing when sending publishes.
|
||||
* Even if inbound topic aliasing is enabled, it is up to the server to choose whether or not to use it.
|
||||
*
|
||||
* If left undefined, then inbound topic aliasing is disabled.
|
||||
*/
|
||||
Crt::Optional<InboundTopicAliasBehaviorType> m_inboundBehavior;
|
||||
|
||||
/**
|
||||
* If inbound topic aliasing is enabled, this will control the size of the inbound alias cache. If
|
||||
* inbound aliases are enabled and this is zero or undefined, then a sensible default will be used (25).
|
||||
* If inbound aliases are disabled, this setting has no effect.
|
||||
*
|
||||
* Behaviorally, this value overrides anything present in the topic_alias_maximum field of
|
||||
* the CONNECT packet options.
|
||||
*/
|
||||
Crt::Optional<uint16_t> m_inboundCacheMaxSize;
|
||||
};
|
||||
|
||||
struct AWS_CRT_CPP_API ReconnectOptions
|
||||
{
|
||||
/**
|
||||
* Controls how the reconnect delay is modified in order to smooth out the distribution of reconnection
|
||||
* attempt timepoints for a large set of reconnecting clients.
|
||||
*/
|
||||
ExponentialBackoffJitterMode m_reconnectMode;
|
||||
|
||||
/**
|
||||
* Minimum amount of time to wait to reconnect after a disconnect. Exponential backoff is performed
|
||||
* with jitter after each connection failure.
|
||||
*/
|
||||
uint64_t m_minReconnectDelayMs;
|
||||
|
||||
/**
|
||||
* Maximum amount of time to wait to reconnect after a disconnect. Exponential backoff is performed
|
||||
* with jitter after each connection failure.
|
||||
*/
|
||||
uint64_t m_maxReconnectDelayMs;
|
||||
|
||||
/**
|
||||
* Amount of time that must elapse with an established connection before the reconnect delay is reset to
|
||||
* the minimum. This helps alleviate bandwidth-waste in fast reconnect cycles due to permission failures
|
||||
* on operations.
|
||||
*/
|
||||
uint64_t m_minConnectedTimeToResetReconnectDelayMs;
|
||||
};
|
||||
|
||||
/**
|
||||
* Simple statistics about the current state of the client's queue of operations
|
||||
*/
|
||||
struct AWS_CRT_CPP_API Mqtt5ClientOperationStatistics
|
||||
{
|
||||
/**
|
||||
* total number of operations submitted to the client that have not yet been completed. Unacked
|
||||
* operations are a subset of this.
|
||||
*/
|
||||
uint64_t incompleteOperationCount;
|
||||
|
||||
/**
|
||||
* total packet size of operations submitted to the client that have not yet been completed. Unacked
|
||||
* operations are a subset of this.
|
||||
*/
|
||||
uint64_t incompleteOperationSize;
|
||||
|
||||
/**
|
||||
* total number of operations that have been sent to the server and are waiting for a corresponding ACK
|
||||
* before they can be completed.
|
||||
*/
|
||||
uint64_t unackedOperationCount;
|
||||
|
||||
/**
|
||||
* total packet size of operations that have been sent to the server and are waiting for a corresponding
|
||||
* ACK before they can be completed.
|
||||
*/
|
||||
uint64_t unackedOperationSize;
|
||||
};
|
||||
|
||||
/**
|
||||
* The data returned when AttemptingConnect is invoked in the LifecycleEvents callback.
|
||||
* Currently empty, but may be used in the future for passing additional data.
|
||||
*/
|
||||
struct AWS_CRT_CPP_API OnAttemptingConnectEventData
|
||||
{
|
||||
OnAttemptingConnectEventData() {}
|
||||
};
|
||||
|
||||
/**
|
||||
* The data returned when OnConnectionFailure is invoked in the LifecycleEvents callback.
|
||||
*/
|
||||
struct AWS_CRT_CPP_API OnConnectionFailureEventData
|
||||
{
|
||||
OnConnectionFailureEventData() : errorCode(AWS_ERROR_SUCCESS), connAckPacket(nullptr) {}
|
||||
|
||||
int errorCode;
|
||||
std::shared_ptr<ConnAckPacket> connAckPacket;
|
||||
};
|
||||
|
||||
/**
|
||||
* The data returned when OnConnectionSuccess is invoked in the LifecycleEvents callback.
|
||||
*/
|
||||
struct AWS_CRT_CPP_API OnConnectionSuccessEventData
|
||||
{
|
||||
OnConnectionSuccessEventData() : connAckPacket(nullptr), negotiatedSettings(nullptr) {}
|
||||
|
||||
std::shared_ptr<ConnAckPacket> connAckPacket;
|
||||
std::shared_ptr<NegotiatedSettings> negotiatedSettings;
|
||||
};
|
||||
|
||||
/**
|
||||
* The data returned when OnDisconnect is invoked in the LifecycleEvents callback.
|
||||
*/
|
||||
struct AWS_CRT_CPP_API OnDisconnectionEventData
|
||||
{
|
||||
OnDisconnectionEventData() : errorCode(AWS_ERROR_SUCCESS), disconnectPacket(nullptr) {}
|
||||
|
||||
int errorCode;
|
||||
std::shared_ptr<DisconnectPacket> disconnectPacket;
|
||||
};
|
||||
|
||||
/**
|
||||
* The data returned when OnStopped is invoked in the LifecycleEvents callback.
|
||||
* Currently empty, but may be used in the future for passing additional data.
|
||||
*/
|
||||
struct AWS_CRT_CPP_API OnStoppedEventData
|
||||
{
|
||||
OnStoppedEventData() {}
|
||||
};
|
||||
|
||||
/**
|
||||
* The data returned when a publish is made to a topic the MQTT5 client is subscribed to.
|
||||
*/
|
||||
struct AWS_CRT_CPP_API PublishReceivedEventData
|
||||
{
|
||||
PublishReceivedEventData() : publishPacket(nullptr) {}
|
||||
std::shared_ptr<PublishPacket> publishPacket;
|
||||
};
|
||||
|
||||
/**
|
||||
* Type signature of the callback invoked when connection succeed
|
||||
* Mandatory event fields: client, connack_data, settings
|
||||
*/
|
||||
using OnConnectionSuccessHandler = std::function<void(const OnConnectionSuccessEventData &)>;
|
||||
|
||||
/**
|
||||
* Type signature of the callback invoked when connection failed
|
||||
*/
|
||||
using OnConnectionFailureHandler = std::function<void(const OnConnectionFailureEventData &)>;
|
||||
|
||||
/**
|
||||
* Type signature of the callback invoked when the internal connection is shutdown
|
||||
*/
|
||||
using OnDisconnectionHandler = std::function<void(const OnDisconnectionEventData &)>;
|
||||
|
||||
/**
|
||||
* Type signature of the callback invoked when attempting connect to client
|
||||
* Mandatory event fields: client
|
||||
*/
|
||||
using OnAttemptingConnectHandler = std::function<void(const OnAttemptingConnectEventData &)>;
|
||||
|
||||
/**
|
||||
* Type signature of the callback invoked when client connection stopped
|
||||
* Mandatory event fields: client
|
||||
*/
|
||||
using OnStoppedHandler = std::function<void(const OnStoppedEventData &)>;
|
||||
|
||||
/**
|
||||
* Type signature of the callback invoked when a Publish Complete
|
||||
*/
|
||||
using OnPublishCompletionHandler = std::function<void(int, std::shared_ptr<PublishResult>)>;
|
||||
|
||||
/**
|
||||
* Type signature of the callback invoked when a Subscribe Complete
|
||||
*/
|
||||
using OnSubscribeCompletionHandler = std::function<void(int, std::shared_ptr<SubAckPacket>)>;
|
||||
|
||||
/**
|
||||
* Type signature of the callback invoked when a Unsubscribe Complete
|
||||
*/
|
||||
using OnUnsubscribeCompletionHandler = std::function<void(int, std::shared_ptr<UnSubAckPacket>)>;
|
||||
|
||||
/**
|
||||
* Type signature of the callback invoked when a PacketPublish message received (OnMessageHandler)
|
||||
*/
|
||||
using OnPublishReceivedHandler = std::function<void(const PublishReceivedEventData &)>;
|
||||
|
||||
/**
|
||||
* Callback for users to invoke upon completion of, presumably asynchronous, OnWebSocketHandshakeIntercept
|
||||
* callback's initiated process.
|
||||
*/
|
||||
using OnWebSocketHandshakeInterceptComplete =
|
||||
std::function<void(const std::shared_ptr<Http::HttpRequest> &, int)>;
|
||||
|
||||
/**
|
||||
* Invoked during websocket handshake to give users opportunity to transform an http request for purposes
|
||||
* such as signing/authorization etc... Returning from this function does not continue the websocket
|
||||
* handshake since some work flows may be asynchronous. To accommodate that, onComplete must be invoked upon
|
||||
* completion of the signing process.
|
||||
*/
|
||||
using OnWebSocketHandshakeIntercept =
|
||||
std::function<void(std::shared_ptr<Http::HttpRequest>, const OnWebSocketHandshakeInterceptComplete &)>;
|
||||
|
||||
/**
|
||||
* An MQTT5 client. This is a move-only type. Unless otherwise specified,
|
||||
* all function arguments need only to live through the duration of the
|
||||
* function call.
|
||||
*/
|
||||
class AWS_CRT_CPP_API Mqtt5Client final : public std::enable_shared_from_this<Mqtt5Client>
|
||||
{
|
||||
friend class Mqtt::MqttConnection;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Factory function for mqtt5 client
|
||||
*
|
||||
* @param options: Mqtt5 Client Options
|
||||
* @param allocator allocator to use
|
||||
* @return a new mqtt5 client
|
||||
*/
|
||||
static std::shared_ptr<Mqtt5Client> NewMqtt5Client(
|
||||
const Mqtt5ClientOptions &options,
|
||||
Allocator *allocator = ApiAllocator()) noexcept;
|
||||
|
||||
/**
|
||||
* Get shared poitner of the Mqtt5Client. Mqtt5Client is inherited to enable_shared_from_this to help
|
||||
* with memory safety.
|
||||
*
|
||||
* @return shared_ptr for the Mqtt5Client
|
||||
*/
|
||||
std::shared_ptr<Mqtt5Client> getptr() { return shared_from_this(); }
|
||||
|
||||
/**
|
||||
* @return true if the instance is in a valid state, false otherwise.
|
||||
*/
|
||||
operator bool() const noexcept;
|
||||
|
||||
/**
|
||||
* @return the value of the last aws error encountered by operations on this instance.
|
||||
*/
|
||||
int LastError() const noexcept;
|
||||
|
||||
/**
|
||||
* Notifies the MQTT5 client that you want it to attempt to connect to the configured endpoint.
|
||||
* The client will attempt to stay connected using the properties of the reconnect-related parameters
|
||||
* from the client configuration.
|
||||
*
|
||||
* @return bool: true if operation succeed, otherwise false.
|
||||
*/
|
||||
bool Start() const noexcept;
|
||||
|
||||
/**
|
||||
* Notifies the MQTT5 client that you want it to transition to the stopped state, disconnecting any
|
||||
* existing connection and stopping subsequent reconnect attempts.
|
||||
*
|
||||
* @return bool: true if operation succeed, otherwise false
|
||||
*/
|
||||
bool Stop() noexcept;
|
||||
|
||||
/**
|
||||
* Notifies the MQTT5 client that you want it to transition to the stopped state, disconnecting any
|
||||
* existing connection and stopping subsequent reconnect attempts.
|
||||
*
|
||||
* @param disconnectPacket (optional) properties of a DISCONNECT packet to send as part of the shutdown
|
||||
* process
|
||||
*
|
||||
* @return bool: true if operation succeed, otherwise false
|
||||
*/
|
||||
bool Stop(std::shared_ptr<DisconnectPacket> disconnectPacket) noexcept;
|
||||
|
||||
/**
|
||||
* Tells the client to attempt to send a PUBLISH packet
|
||||
*
|
||||
* @param publishPacket: packet PUBLISH to send to the server
|
||||
* @param onPublishCompletionCallback: callback on publish complete, default to NULL
|
||||
*
|
||||
* @return true if the publish operation succeed otherwise false
|
||||
*/
|
||||
bool Publish(
|
||||
std::shared_ptr<PublishPacket> publishPacket,
|
||||
OnPublishCompletionHandler onPublishCompletionCallback = NULL) noexcept;
|
||||
|
||||
/**
|
||||
* Tells the client to attempt to subscribe to one or more topic filters.
|
||||
*
|
||||
* @param subscribePacket: SUBSCRIBE packet to send to the server
|
||||
* @param onSubscribeCompletionCallback: callback on subscribe complete, default to NULL
|
||||
*
|
||||
* @return true if the subscription operation succeed otherwise false
|
||||
*/
|
||||
bool Subscribe(
|
||||
std::shared_ptr<SubscribePacket> subscribePacket,
|
||||
OnSubscribeCompletionHandler onSubscribeCompletionCallback = NULL) noexcept;
|
||||
|
||||
/**
|
||||
* Tells the client to attempt to unsubscribe to one or more topic filters.
|
||||
*
|
||||
* @param unsubscribePacket: UNSUBSCRIBE packet to send to the server
|
||||
* @param onUnsubscribeCompletionCallback: callback on unsubscribe complete, default to NULL
|
||||
*
|
||||
* @return true if the unsubscription operation succeed otherwise false
|
||||
*/
|
||||
bool Unsubscribe(
|
||||
std::shared_ptr<UnsubscribePacket> unsubscribePacket,
|
||||
OnUnsubscribeCompletionHandler onUnsubscribeCompletionCallback = NULL) noexcept;
|
||||
|
||||
/**
|
||||
* Get the statistics about the current state of the client's queue of operations
|
||||
*
|
||||
* @return Mqtt5ClientOperationStatistics
|
||||
*/
|
||||
const Mqtt5ClientOperationStatistics &GetOperationStatistics() noexcept;
|
||||
|
||||
virtual ~Mqtt5Client();
|
||||
|
||||
private:
|
||||
Mqtt5Client(const Mqtt5ClientOptions &options, Allocator *allocator = ApiAllocator()) noexcept;
|
||||
|
||||
/* The client core to handle the user callbacks and c client termination */
|
||||
std::shared_ptr<Mqtt5ClientCore> m_client_core;
|
||||
|
||||
Mqtt5ClientOperationStatistics m_operationStatistics;
|
||||
};
|
||||
|
||||
/**
|
||||
* Configuration interface for mqtt5 clients
|
||||
*/
|
||||
class AWS_CRT_CPP_API Mqtt5ClientOptions final
|
||||
{
|
||||
friend class Mqtt5ClientCore;
|
||||
friend class Mqtt5to3AdapterOptions;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Default constructior of Mqtt5ClientOptions
|
||||
*/
|
||||
Mqtt5ClientOptions(Crt::Allocator *allocator = ApiAllocator()) noexcept;
|
||||
|
||||
/**
|
||||
* Sets host to connect to.
|
||||
*
|
||||
* @param hostname endpoint to connect to
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithHostName(Crt::String hostname);
|
||||
|
||||
/**
|
||||
* Set port to connect to
|
||||
*
|
||||
* @param port port to connect to
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithPort(uint32_t port) noexcept;
|
||||
|
||||
/**
|
||||
* Set booststrap for mqtt5 client
|
||||
*
|
||||
* @param bootStrap bootstrap used for mqtt5 client. The default ClientBootstrap see
|
||||
* Aws::Crt::ApiHandle::GetOrCreateStaticDefaultClientBootstrap.
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithBootstrap(Io::ClientBootstrap *bootStrap) noexcept;
|
||||
|
||||
/**
|
||||
* Sets the aws socket options
|
||||
*
|
||||
* @param socketOptions Io::SocketOptions used to setup socket
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithSocketOptions(Io::SocketOptions socketOptions) noexcept;
|
||||
|
||||
/**
|
||||
* Sets the tls connection options
|
||||
*
|
||||
* @param tslOptions Io::TlsConnectionOptions
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithTlsConnectionOptions(const Io::TlsConnectionOptions &tslOptions) noexcept;
|
||||
|
||||
/**
|
||||
* Sets http proxy options.
|
||||
*
|
||||
* @param proxyOptions http proxy configuration for connection establishment
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithHttpProxyOptions(
|
||||
const Crt::Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
|
||||
|
||||
/**
|
||||
* Sets mqtt5 connection options
|
||||
*
|
||||
* @param connectPacket package connection options
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithConnectOptions(std::shared_ptr<ConnectPacket> connectPacket) noexcept;
|
||||
|
||||
/**
|
||||
* Sets session behavior. Overrides how the MQTT5 client should behave with respect to MQTT sessions.
|
||||
*
|
||||
* @param sessionBehavior
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithSessionBehavior(ClientSessionBehaviorType sessionBehavior) noexcept;
|
||||
|
||||
/**
|
||||
* Sets client extended validation and flow control, additional controls for client behavior with
|
||||
* respect to operation validation and flow control; these checks go beyond the base MQTT5 spec to
|
||||
* respect limits of specific MQTT brokers.
|
||||
*
|
||||
* @param clientExtendedValidationAndFlowControl
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithClientExtendedValidationAndFlowControl(
|
||||
ClientExtendedValidationAndFlowControl clientExtendedValidationAndFlowControl) noexcept;
|
||||
|
||||
/**
|
||||
* Sets OfflineQueueBehavior, controls how disconnects affect the queued and in-progress operations
|
||||
* tracked by the client. Also controls how new operations are handled while the client is not
|
||||
* connected. In particular, if the client is not connected, then any operation that would be failed
|
||||
* on disconnect (according to these rules) will also be rejected.
|
||||
*
|
||||
* @param offlineQueueBehavior
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithOfflineQueueBehavior(
|
||||
ClientOperationQueueBehaviorType offlineQueueBehavior) noexcept;
|
||||
|
||||
/**
|
||||
* Sets ReconnectOptions. Reconnect options, includes retryJitterMode, min reconnect delay time and
|
||||
* max reconnect delay time and reset reconnect delay time
|
||||
*
|
||||
* @param reconnectOptions
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithReconnectOptions(ReconnectOptions reconnectOptions) noexcept;
|
||||
|
||||
/**
|
||||
* Sets the topic aliasing behavior for the client.
|
||||
*
|
||||
* @param topicAliasingOptions topic aliasing behavior options to use
|
||||
* @return this options object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithTopicAliasingOptions(TopicAliasingOptions topicAliasingOptions) noexcept;
|
||||
|
||||
/**
|
||||
* Sets ping timeout (ms). Time interval to wait after sending a PINGREQ for a PINGRESP to arrive.
|
||||
* If one does not arrive, the client will close the current connection.
|
||||
*
|
||||
* @param pingTimeoutMs
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithPingTimeoutMs(uint32_t pingTimeoutMs) noexcept;
|
||||
|
||||
/**
|
||||
* Sets Connack Timeout (ms). Time interval to wait after sending a CONNECT request for a CONNACK
|
||||
* to arrive. If one does not arrive, the connection will be shut down.
|
||||
*
|
||||
* @param connackTimeoutMs
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithConnackTimeoutMs(uint32_t connackTimeoutMs) noexcept;
|
||||
|
||||
/**
|
||||
* @deprecated The function is deprecated, please use `Mqtt5ClientOptions::WithAckTimeoutSec(uint32_t)`
|
||||
*
|
||||
* Sets Operation Timeout(Seconds). Time interval to wait for an ack after sending a QoS 1+ PUBLISH,
|
||||
* SUBSCRIBE, or UNSUBSCRIBE before failing the operation.
|
||||
*
|
||||
* @param ackTimeoutSec
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithAckTimeoutSeconds(uint32_t ackTimeoutSec) noexcept;
|
||||
|
||||
/**
|
||||
* Sets Operation Timeout(Seconds). Time interval to wait for an ack after sending a QoS 1+ PUBLISH,
|
||||
* SUBSCRIBE, or UNSUBSCRIBE before failing the operation.
|
||||
*
|
||||
* @param ackTimeoutSec
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithAckTimeoutSec(uint32_t ackTimeoutSec) noexcept;
|
||||
|
||||
/**
|
||||
* Sets callback for transform HTTP request.
|
||||
* This callback allows a custom transformation of the HTTP request that acts as the websocket
|
||||
* handshake. Websockets will be used if this is set to a valid transformation callback. To use
|
||||
* websockets but not perform a transformation, just set this as a trivial completion callback. If
|
||||
* undefined, the connection will be made with direct MQTT.
|
||||
*
|
||||
* @param callback
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithWebsocketHandshakeTransformCallback(
|
||||
OnWebSocketHandshakeIntercept callback) noexcept;
|
||||
|
||||
/**
|
||||
* Sets callback trigged when client successfully establishes an MQTT connection
|
||||
*
|
||||
* @param callback
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithClientConnectionSuccessCallback(OnConnectionSuccessHandler callback) noexcept;
|
||||
|
||||
/**
|
||||
* Sets callback trigged when client fails to establish an MQTT connection
|
||||
*
|
||||
* @param callback
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithClientConnectionFailureCallback(OnConnectionFailureHandler callback) noexcept;
|
||||
|
||||
/**
|
||||
* Sets callback trigged when client's current MQTT connection is closed
|
||||
*
|
||||
* @param callback
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithClientDisconnectionCallback(OnDisconnectionHandler callback) noexcept;
|
||||
|
||||
/**
|
||||
* Sets callback trigged when client reaches the "Stopped" state
|
||||
*
|
||||
* @param callback
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithClientStoppedCallback(OnStoppedHandler callback) noexcept;
|
||||
|
||||
/**
|
||||
* Sets callback trigged when client begins an attempt to connect to the remote endpoint.
|
||||
*
|
||||
* @param callback
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithClientAttemptingConnectCallback(OnAttemptingConnectHandler callback) noexcept;
|
||||
|
||||
/**
|
||||
* Sets callback trigged when a PUBLISH packet is received by the client
|
||||
*
|
||||
* @param callback
|
||||
*
|
||||
* @return this option object
|
||||
*/
|
||||
Mqtt5ClientOptions &WithPublishReceivedCallback(OnPublishReceivedHandler callback) noexcept;
|
||||
|
||||
/**
|
||||
* Initializes the C aws_mqtt5_client_options from Mqtt5ClientOptions. For internal use
|
||||
*
|
||||
* @param raw_options - output parameter containing low level client options to be passed to the C
|
||||
* interface
|
||||
*
|
||||
*/
|
||||
bool initializeRawOptions(aws_mqtt5_client_options &raw_options) const noexcept;
|
||||
|
||||
virtual ~Mqtt5ClientOptions();
|
||||
Mqtt5ClientOptions(const Mqtt5ClientOptions &) = delete;
|
||||
Mqtt5ClientOptions(Mqtt5ClientOptions &&) = delete;
|
||||
Mqtt5ClientOptions &operator=(const Mqtt5ClientOptions &) = delete;
|
||||
Mqtt5ClientOptions &operator=(Mqtt5ClientOptions &&) = delete;
|
||||
|
||||
private:
|
||||
/**
|
||||
* This callback allows a custom transformation of the HTTP request that acts as the websocket
|
||||
* handshake. Websockets will be used if this is set to a valid transformation callback. To use
|
||||
* websockets but not perform a transformation, just set this as a trivial completion callback. If
|
||||
* undefined, the connection will be made with direct MQTT.
|
||||
*/
|
||||
OnWebSocketHandshakeIntercept websocketHandshakeTransform;
|
||||
|
||||
/**
|
||||
* Callback handler trigged when client successfully establishes an MQTT connection
|
||||
*/
|
||||
OnConnectionSuccessHandler onConnectionSuccess;
|
||||
|
||||
/**
|
||||
* Callback handler trigged when client fails to establish an MQTT connection
|
||||
*/
|
||||
OnConnectionFailureHandler onConnectionFailure;
|
||||
|
||||
/**
|
||||
* Callback handler trigged when client's current MQTT connection is closed
|
||||
*/
|
||||
OnDisconnectionHandler onDisconnection;
|
||||
|
||||
/**
|
||||
* Callback handler trigged when client reaches the "Stopped" state
|
||||
*
|
||||
* @param Mqtt5Client: The shared client
|
||||
*/
|
||||
OnStoppedHandler onStopped;
|
||||
|
||||
/**
|
||||
* Callback handler trigged when client begins an attempt to connect to the remote endpoint.
|
||||
*
|
||||
* @param Mqtt5Client: The shared client
|
||||
*/
|
||||
OnAttemptingConnectHandler onAttemptingConnect;
|
||||
|
||||
/**
|
||||
* Callback handler trigged when an MQTT PUBLISH packet is received by the client
|
||||
*
|
||||
* @param Mqtt5Client: The shared client
|
||||
* @param PublishPacket: received Publish Packet
|
||||
*/
|
||||
OnPublishReceivedHandler onPublishReceived;
|
||||
|
||||
/**
|
||||
* Host name of the MQTT server to connect to.
|
||||
*/
|
||||
Crt::String m_hostName;
|
||||
|
||||
/**
|
||||
* Network port of the MQTT server to connect to.
|
||||
*/
|
||||
uint32_t m_port;
|
||||
|
||||
/**
|
||||
* Client bootstrap to use. In almost all cases, this can be left undefined.
|
||||
*/
|
||||
Io::ClientBootstrap *m_bootstrap;
|
||||
|
||||
/**
|
||||
* Controls socket properties of the underlying MQTT connections made by the client. Leave undefined to
|
||||
* use defaults (no TCP keep alive, 10 second socket timeout).
|
||||
*/
|
||||
Crt::Io::SocketOptions m_socketOptions;
|
||||
|
||||
/**
|
||||
* TLS context for secure socket connections.
|
||||
* If undefined, a plaintext connection will be used.
|
||||
*/
|
||||
Crt::Optional<Crt::Io::TlsConnectionOptions> m_tlsConnectionOptions;
|
||||
|
||||
/**
|
||||
* Configures (tunneling) HTTP proxy usage when establishing MQTT connections
|
||||
*/
|
||||
Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> m_proxyOptions;
|
||||
|
||||
/**
|
||||
* All configurable options with respect to the CONNECT packet sent by the client, including the will.
|
||||
* These connect properties will be used for every connection attempt made by the client.
|
||||
*/
|
||||
std::shared_ptr<ConnectPacket> m_connectOptions;
|
||||
|
||||
/**
|
||||
* Controls how the MQTT5 client should behave with respect to MQTT sessions.
|
||||
*/
|
||||
ClientSessionBehaviorType m_sessionBehavior;
|
||||
|
||||
/**
|
||||
* Additional controls for client behavior with respect to operation validation and flow control; these
|
||||
* checks go beyond the base MQTT5 spec to respect limits of specific MQTT brokers.
|
||||
*/
|
||||
ClientExtendedValidationAndFlowControl m_extendedValidationAndFlowControlOptions;
|
||||
|
||||
/**
|
||||
* Controls how disconnects affect the queued and in-progress operations tracked by the client. Also
|
||||
* controls how new operations are handled while the client is not connected. In particular, if the
|
||||
* client is not connected, then any operation that would be failed on disconnect (according to these
|
||||
* rules) will also be rejected.
|
||||
*/
|
||||
ClientOperationQueueBehaviorType m_offlineQueueBehavior;
|
||||
|
||||
/**
|
||||
* Reconnect options, includes retryJitterMode, min reconnect delay time and max reconnect delay time
|
||||
*/
|
||||
ReconnectOptions m_reconnectionOptions;
|
||||
|
||||
/**
|
||||
* Controls client topic aliasing behavior
|
||||
*/
|
||||
aws_mqtt5_client_topic_alias_options m_topicAliasingOptions;
|
||||
|
||||
/**
|
||||
* Time interval to wait after sending a PINGREQ for a PINGRESP to arrive. If one does not arrive, the
|
||||
* client will close the current connection.
|
||||
*/
|
||||
uint32_t m_pingTimeoutMs;
|
||||
|
||||
/**
|
||||
* Time interval to wait after sending a CONNECT request for a CONNACK to arrive. If one does not
|
||||
* arrive, the connection will be shut down.
|
||||
*/
|
||||
uint32_t m_connackTimeoutMs;
|
||||
|
||||
/**
|
||||
* Time interval to wait for an ack after sending a QoS 1+ PUBLISH, SUBSCRIBE, or UNSUBSCRIBE before
|
||||
* failing the operation.
|
||||
*/
|
||||
uint32_t m_ackTimeoutSec;
|
||||
|
||||
/* Underlying Parameters */
|
||||
Crt::Allocator *m_allocator;
|
||||
aws_http_proxy_options m_httpProxyOptionsStorage;
|
||||
aws_mqtt5_packet_connect_view m_packetConnectViewStorage;
|
||||
};
|
||||
|
||||
} // namespace Mqtt5
|
||||
} // namespace Crt
|
||||
} // namespace Aws
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,313 @@
|
||||
#pragma once
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
#include <aws/mqtt/v5/mqtt5_client.h>
|
||||
#include <aws/mqtt/v5/mqtt5_types.h>
|
||||
|
||||
namespace Aws
|
||||
{
|
||||
namespace Crt
|
||||
{
|
||||
namespace Mqtt5
|
||||
{
|
||||
/**
|
||||
* MQTT message delivery quality of service.
|
||||
*
|
||||
* Enum values match [MQTT5
|
||||
* spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901234) encoding values.
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_QOS_AT_MOST_ONCE</TD><TD>https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901235</TD>
|
||||
* <TR><TD>AWS_MQTT5_QOS_AT_LEAST_ONCE</TD><TD>https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901236</TD>
|
||||
* <TR><TD>AWS_MQTT5_QOS_EXACTLY_ONCE</TD><TD>https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901237</TD>
|
||||
* </TABLE>
|
||||
*
|
||||
*/
|
||||
using QOS = aws_mqtt5_qos;
|
||||
|
||||
/**
|
||||
* Server return code for connect attempts.
|
||||
*
|
||||
* Enum values match [MQTT5
|
||||
* spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901079) encoding values.
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_CRC_SUCCESS</TD><TD>0</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_UNSPECIFIED_ERROR</TD><TD>128</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_MALFORMED_PACKET</TD><TD>129</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_PROTOCOL_ERROR</TD><TD>130</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_IMPLEMENTATION_SPECIFIC_ERROR</TD><TD>131</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_UNSUPPORTED_PROTOCOL_VERSION</TD><TD>132</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_CLIENT_IDENTIFIER_NOT_VALID</TD><TD>133</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_BAD_USERNAME_OR_PASSWORD</TD><TD>134</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_NOT_AUTHORIZED</TD><TD>135</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_SERVER_UNAVAILABLE</TD><TD>136</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_SERVER_BUSY</TD><TD>137</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_BANNED</TD><TD>138</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_BAD_AUTHENTICATION_METHOD</TD><TD>140</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_TOPIC_NAME_INVALID</TD><TD>144</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_PACKET_TOO_LARGE</TD><TD>149</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_QUOTA_EXCEEDED</TD><TD>151</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_PAYLOAD_FORMAT_INVALID</TD><TD>153</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_RETAIN_NOT_SUPPORTED</TD><TD>154</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_QOS_NOT_SUPPORTED</TD><TD>155</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_USE_ANOTHER_SERVER</TD><TD>156</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_SERVER_MOVED</TD><TD>157</TD>
|
||||
* <TR><TD>AWS_MQTT5_CRC_CONNECTION_RATE_EXCEEDED</TD><TD>159</TD>
|
||||
* </TABLE>
|
||||
*
|
||||
*
|
||||
*/
|
||||
using ConnectReasonCode = aws_mqtt5_connect_reason_code;
|
||||
|
||||
/**
|
||||
* Reason code inside DISCONNECT packets. Helps determine why a connection was terminated.
|
||||
*
|
||||
* Enum values match [MQTT5
|
||||
* spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901208) encoding values.
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_DRC_NORMAL_DISCONNECTION</TD><TD>0</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_DISCONNECT_WITH_WILL_MESSAGE</TD><TD>4</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_UNSPECIFIED_ERROR</TD><TD>128</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_MALFORMED_PACKET</TD><TD>129</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_PROTOCOL_ERROR</TD><TD>130</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_IMPLEMENTATION_SPECIFIC_ERROR</TD><TD>131</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_NOT_AUTHORIZED</TD><TD>135</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_SERVER_BUSY</TD><TD>137</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_SERVER_SHUTTING_DOWN</TD><TD>139</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_KEEP_ALIVE_TIMEOUT</TD><TD>141</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_SESSION_TAKEN_OVER</TD><TD>142</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_TOPIC_FILTER_INVALID</TD><TD>143</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_TOPIC_NAME_INVALID</TD><TD>144</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_RECEIVE_MAXIMUM_EXCEEDED</TD><TD>147</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_TOPIC_ALIAS_INVALID</TD><TD>148</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_PACKET_TOO_LARGE</TD><TD>149</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_MESSAGE_RATE_TOO_HIGH</TD><TD>150</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_QUOTA_EXCEEDED</TD><TD>151</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_ADMINISTRATIVE_ACTION</TD><TD>152</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_PAYLOAD_FORMAT_INVALID</TD><TD>153</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_RETAIN_NOT_SUPPORTED</TD><TD>154</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_QOS_NOT_SUPPORTED</TD><TD>155</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_USE_ANOTHER_SERVER</TD><TD>156</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_SERVER_MOVED</TD><TD>157</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_SHARED_SUBSCRIPTIONS_NOT_SUPPORTED</TD><TD>158</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_CONNECTION_RATE_EXCEEDED</TD><TD>159</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_MAXIMUM_CONNECT_TIME</TD><TD>160</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED</TD><TD>161</TD>
|
||||
* <TR><TD>AWS_MQTT5_DRC_WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED</TD><TD>162</TD>
|
||||
* </TABLE>
|
||||
*
|
||||
*/
|
||||
using DisconnectReasonCode = aws_mqtt5_disconnect_reason_code;
|
||||
|
||||
/**
|
||||
* Reason code inside PUBACK packets
|
||||
*
|
||||
* Data model of an [MQTT5
|
||||
* PUBACK](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901121) packet
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_PARC_SUCCESS</TD><TD>0</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_NO_MATCHING_SUBSCRIBERS</TD><TD>16</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_UNSPECIFIED_ERROR</TD><TD>128</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_IMPLEMENTATION_SPECIFIC_ERROR</TD><TD>131</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_NOT_AUTHORIZED</TD><TD>135</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_TOPIC_NAME_INVALID</TD><TD>144</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_PACKET_IDENTIFIER_IN_USE</TD><TD>145</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_QUOTA_EXCEEDED</TD><TD>151</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_PAYLOAD_FORMAT_INVALID</TD><TD>153</TD>
|
||||
* </TABLE>
|
||||
*/
|
||||
using PubAckReasonCode = aws_mqtt5_puback_reason_code;
|
||||
|
||||
/**
|
||||
* Reason code inside PUBACK packets that indicates the result of the associated PUBLISH request.
|
||||
*
|
||||
* Enum values match [MQTT5
|
||||
* spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901124) encoding values.
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_PARC_SUCCESS</TD><TD>0</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_NO_MATCHING_SUBSCRIBERS</TD><TD>16</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_UNSPECIFIED_ERROR</TD><TD>128</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_IMPLEMENTATION_SPECIFIC_ERROR</TD><TD>131</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_NOT_AUTHORIZED</TD><TD>135</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_TOPIC_NAME_INVALID</TD><TD>144</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_PACKET_IDENTIFIER_IN_USE</TD><TD>145</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_QUOTA_EXCEEDED</TD><TD>151</TD>
|
||||
* <TR><TD>AWS_MQTT5_PARC_PAYLOAD_FORMAT_INVALID</TD><TD>153</TD>
|
||||
* </TABLE>
|
||||
*/
|
||||
using SubAckReasonCode = aws_mqtt5_suback_reason_code;
|
||||
|
||||
/**
|
||||
* Reason codes inside UNSUBACK packet payloads that specify the results for each topic filter in the
|
||||
* associated UNSUBSCRIBE packet.
|
||||
*
|
||||
* Enum values match [MQTT5
|
||||
* spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901194) encoding values.
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_UARC_SUCCESS</TD><TD>0</TD>
|
||||
* <TR><TD>AWS_MQTT5_UARC_NO_SUBSCRIPTION_EXISTED</TD><TD>17</TD>
|
||||
* <TR><TD>AWS_MQTT5_UARC_UNSPECIFIED_ERROR</TD><TD>128</TD>
|
||||
* <TR><TD>AWS_MQTT5_UARC_IMPLEMENTATION_SPECIFIC_ERROR</TD><TD>131</TD>
|
||||
* <TR><TD>AWS_MQTT5_UARC_NOT_AUTHORIZED</TD><TD>135</TD>
|
||||
* <TR><TD>AWS_MQTT5_UARC_TOPIC_FILTER_INVALID</TD><TD>143</TD>
|
||||
* <TR><TD>AWS_MQTT5_UARC_PACKET_IDENTIFIER_IN_USE</TD><TD>145</TD>
|
||||
* </TABLE>
|
||||
*
|
||||
*/
|
||||
using UnSubAckReasonCode = aws_mqtt5_unsuback_reason_code;
|
||||
|
||||
/**
|
||||
* Controls how the MQTT5 client should behave with respect to MQTT sessions.
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_CSBT_DEFAULT</TD><TD>Maps to AWS_MQTT5_CSBT_CLEAN</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_CSBT_CLEAN</TD><TD>Always join a new, clean session</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_CSBT_REJOIN_POST_SUCCESS</TD><TD>Always attempt to rejoin an existing session after an
|
||||
* initial connection success.</TD></TR> <TR><TD>AWS_MQTT5_CSBT_REJOIN_ALWAYS</TD><TD>Always attempt to
|
||||
* rejoin an existing session. Since the client does not support durable session persistence, this option is
|
||||
* not guaranteed to be spec compliant because any unacknowledged qos1 publishes (which are part of the
|
||||
* client session state) will not be present on the initial connection. Until we support durable session
|
||||
* resumption, this option is technically spec-breaking, but useful.</TD></TR>
|
||||
* </TABLE>
|
||||
*/
|
||||
using ClientSessionBehaviorType = aws_mqtt5_client_session_behavior_type;
|
||||
|
||||
/**
|
||||
* Additional controls for client behavior with respect to operation validation and flow control; these
|
||||
* checks go beyond the MQTT5 spec to respect limits of specific MQTT brokers.
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_EVAFCO_NONE</TD><TD>Do not do any additional validation or flow control outside of the
|
||||
* MQTT5 spec</TD></TR> <TR><TD>AWS_MQTT5_EVAFCO_AWS_IOT_CORE_DEFAULTS</TD><TD>Apply additional client-side
|
||||
* operational flow control that respects the default AWS IoT Core limits. Applies the following flow
|
||||
* control: (1) Outbound throughput throttled to 512KB/s (2) Outbound publish TPS throttled to 100</TD></TR>
|
||||
* </TABLE>
|
||||
*
|
||||
*/
|
||||
using ClientExtendedValidationAndFlowControl = aws_mqtt5_extended_validation_and_flow_control_options;
|
||||
|
||||
/**
|
||||
* Controls how disconnects affect the queued and in-progress operations tracked by the client. Also
|
||||
* controls how operations are handled while the client is not connected. In particular, if the client is
|
||||
* not connected, then any operation that would be failed on disconnect (according to these rules) will be
|
||||
* rejected.
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_COQBT_DEFAULT</TD><TD>Maps to AWS_MQTT5_COQBT_FAIL_QOS0_PUBLISH_ON_DISCONNECT</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_COQBT_FAIL_NON_QOS1_PUBLISH_ON_DISCONNECT</TD><TD>Requeues QoS 1+ publishes on
|
||||
* disconnect; unacked publishes go to the front, unprocessed publishes stay in place. All other operations
|
||||
* (QoS 0 publishes, subscribe, unsubscribe) are failed.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_COQBT_FAIL_QOS0_PUBLISH_ON_DISCONNECT</TD><TD>Qos 0 publishes that are not complete at
|
||||
* the time of disconnection are failed. Unacked QoS 1+ publishes are requeued at the head of the line for
|
||||
* immediate retransmission on a session resumption. All other operations are requeued in the original order
|
||||
* behind any retransmissions.</TD></TR> <TR><TD>AWS_MQTT5_COQBT_FAIL_ALL_ON_DISCONNECT</TD><TD>All
|
||||
* operations that are not complete at the time of disconnection are failed, except those operations that
|
||||
* the MQTT 5 spec requires to be retransmitted (unacked QoS 1+ publishes).</TD></TR>
|
||||
* </TABLE>
|
||||
*
|
||||
*/
|
||||
using ClientOperationQueueBehaviorType = aws_mqtt5_client_operation_queue_behavior_type;
|
||||
|
||||
/**
|
||||
* Controls how the reconnect delay is modified in order to smooth out the distribution of reconnection
|
||||
* attempt timepoints for a large set of reconnecting clients.
|
||||
*
|
||||
* See [Exponential Backoff and
|
||||
* Jitter](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/)
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_EXPONENTIAL_BACKOFF_JITTER_DEFAULT</TD><TD>Uses AWS_EXPONENTIAL_BACKOFF_JITTER_FULL</TD></TR>
|
||||
* <TR><TD>AWS_EXPONENTIAL_BACKOFF_JITTER_NONE</TD><TD>No jitter is applied to the exponential
|
||||
* backoff</TD></TR> <TR><TD>AWS_EXPONENTIAL_BACKOFF_JITTER_FULL</TD><TD>Full jitter is applied to the
|
||||
* exponential backoff</TD></TR> <TR><TD>AWS_EXPONENTIAL_BACKOFF_JITTER_DECORRELATED</TD><TD>Jitter is
|
||||
* decorrelated from the backoff sequence</TD></TR>
|
||||
* </TABLE>
|
||||
*
|
||||
*/
|
||||
using ExponentialBackoffJitterMode = aws_exponential_backoff_jitter_mode;
|
||||
|
||||
/** @deprecated JitterMode is deprecated, please use Aws::Crt::Mqtt5::ExponentialBackoffJitterMode */
|
||||
using JitterMode = ExponentialBackoffJitterMode;
|
||||
|
||||
/**
|
||||
* Optional property describing a PUBLISH payload's format.
|
||||
*
|
||||
* Enum values match [MQTT5
|
||||
* spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901111) encoding values.
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_PFI_BYTES</TD><TD>Bytes format.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PFI_UTF8</TD><TD>UTF-8 format.</TD></TR>
|
||||
* </TABLE>
|
||||
*/
|
||||
using PayloadFormatIndicator = aws_mqtt5_payload_format_indicator;
|
||||
|
||||
/**
|
||||
* Configures how retained messages should be handled when subscribing with a topic filter that matches
|
||||
* topics with associated retained messages.
|
||||
*
|
||||
* Enum values match [MQTT5
|
||||
* spec](https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901169) encoding values.
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE</TD><TD>Server should send all retained messages on topics that
|
||||
* match the subscription's filter.</TD></TR> <TR><TD>AWS_MQTT5_RHT_SEND_ON_SUBSCRIBE_IF_NEW</TD><TD>Server
|
||||
* should send all retained messages on topics that match the subscription's filter, where this is the first
|
||||
* (relative to connection) subscription filter that matches the topic with a retained message.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_RHT_DONT_SEND</TD><TD>Subscribe must not trigger any retained message publishes from
|
||||
* the server.</TD></TR>
|
||||
* </TABLE>
|
||||
*/
|
||||
using RetainHandlingType = aws_mqtt5_retain_handling_type;
|
||||
|
||||
/**
|
||||
* Type of mqtt packet.
|
||||
* Enum values match mqtt spec encoding values.
|
||||
*
|
||||
* https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901022
|
||||
*
|
||||
* <TABLE>
|
||||
* <TR><TH colspan="2">Enumerator</TH>
|
||||
* <TR><TD>AWS_MQTT5_PT_NONE</TD><TD>Internal indicator that the associated packet is null.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_RESERVED</TD><TD>Reserved.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_CONNECT</TD><TD>CONNECT packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_CONNACK</TD><TD>CONNACK packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_PUBLISH</TD><TD>PUBLISH packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_PUBACK</TD><TD>PUBACK packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_PUBREC</TD><TD>PUBREC packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_PUBREL</TD><TD>PUBREL packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_PUBCOMP</TD><TD>PUBCOMP packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_SUBSCRIBE</TD><TD>SUBSCRIBE packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_SUBACK</TD><TD>SUBACK packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_UNSUBSCRIBE</TD><TD>UNSUBSCRIBE packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_UNSUBACK</TD><TD>UNSUBACK packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_PINGREQ</TD><TD>PINGREQ packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_PINGRESP</TD><TD>PINGRESP packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_DISCONNECT</TD><TD>DISCONNECT packet.</TD></TR>
|
||||
* <TR><TD>AWS_MQTT5_PT_AUTH</TD><TD>AUTH packet.</TD></TR>
|
||||
* </TABLE>
|
||||
*
|
||||
*/
|
||||
using PacketType = aws_mqtt5_packet_type;
|
||||
|
||||
} // namespace Mqtt5
|
||||
|
||||
} // namespace Crt
|
||||
} // namespace Aws
|
||||
@@ -0,0 +1,121 @@
|
||||
#pragma once
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
#include <aws/crt/Exports.h>
|
||||
#include <aws/crt/StlAllocator.h>
|
||||
#include <aws/crt/Types.h>
|
||||
#include <aws/crt/http/HttpConnection.h>
|
||||
#include <aws/crt/io/SocketOptions.h>
|
||||
#include <aws/crt/io/TlsOptions.h>
|
||||
#include <aws/crt/mqtt/MqttConnection.h>
|
||||
|
||||
#include <aws/mqtt/client.h>
|
||||
#include <aws/mqtt/v5/mqtt5_client.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace Aws
|
||||
{
|
||||
namespace Crt
|
||||
{
|
||||
namespace Io
|
||||
{
|
||||
class ClientBootstrap;
|
||||
}
|
||||
|
||||
namespace Http
|
||||
{
|
||||
class HttpRequest;
|
||||
}
|
||||
|
||||
namespace Mqtt5
|
||||
{
|
||||
class Mqtt5ClientCore;
|
||||
}
|
||||
|
||||
namespace Mqtt
|
||||
{
|
||||
/**
|
||||
* An MQTT client. This is a move-only type. Unless otherwise specified,
|
||||
* all function arguments need only to live through the duration of the
|
||||
* function call.
|
||||
*/
|
||||
class AWS_CRT_CPP_API MqttClient final
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Initialize an MqttClient using bootstrap and allocator
|
||||
*/
|
||||
MqttClient(Io::ClientBootstrap &bootstrap, Allocator *allocator = ApiAllocator()) noexcept;
|
||||
|
||||
/**
|
||||
* Initialize an MqttClient using a allocator and the default ClientBootstrap
|
||||
*
|
||||
* For more information on the default ClientBootstrap see
|
||||
* Aws::Crt::ApiHandle::GetOrCreateStaticDefaultClientBootstrap
|
||||
*/
|
||||
MqttClient(Allocator *allocator = ApiAllocator()) noexcept;
|
||||
|
||||
~MqttClient();
|
||||
MqttClient(const MqttClient &) = delete;
|
||||
MqttClient(MqttClient &&) noexcept;
|
||||
MqttClient &operator=(const MqttClient &) = delete;
|
||||
MqttClient &operator=(MqttClient &&) noexcept;
|
||||
|
||||
/**
|
||||
* @return true if the instance is in a valid state, false otherwise.
|
||||
*/
|
||||
operator bool() const noexcept;
|
||||
|
||||
/**
|
||||
* @return the value of the last aws error encountered by operations on this instance.
|
||||
*/
|
||||
int LastError() const noexcept;
|
||||
|
||||
/**
|
||||
* Create a new connection object using TLS from the client. The client must outlive
|
||||
* all of its connection instances.
|
||||
*
|
||||
* @param hostName endpoint to connect to
|
||||
* @param port port to connect to
|
||||
* @param socketOptions socket options to use when establishing the connection
|
||||
* @param tlsContext tls context to use with the connection
|
||||
* @param useWebsocket should the connection use websockets or should it use direct mqtt?
|
||||
*
|
||||
* @return a new connection object. Connect() will still need to be called after all further
|
||||
* configuration is finished.
|
||||
*/
|
||||
std::shared_ptr<MqttConnection> NewConnection(
|
||||
const char *hostName,
|
||||
uint32_t port,
|
||||
const Io::SocketOptions &socketOptions,
|
||||
const Crt::Io::TlsContext &tlsContext,
|
||||
bool useWebsocket = false) noexcept;
|
||||
|
||||
/**
|
||||
* Create a new connection object over plain text from the client. The client must outlive
|
||||
* all of its connection instances.
|
||||
* @param hostName endpoint to connect to
|
||||
* @param port port to connect to
|
||||
* @param socketOptions socket options to use when establishing the connection
|
||||
* @param useWebsocket should the connection use websockets or should it use direct mqtt?
|
||||
*
|
||||
* @return a new connection object. Connect() will still need to be called after all further
|
||||
* configuration is finished.
|
||||
*/
|
||||
std::shared_ptr<MqttConnection> NewConnection(
|
||||
const char *hostName,
|
||||
uint32_t port,
|
||||
const Io::SocketOptions &socketOptions,
|
||||
bool useWebsocket = false) noexcept;
|
||||
|
||||
private:
|
||||
aws_mqtt_client *m_client;
|
||||
};
|
||||
} // namespace Mqtt
|
||||
} // namespace Crt
|
||||
} // namespace Aws
|
||||
@@ -0,0 +1,462 @@
|
||||
#pragma once
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
|
||||
#include <aws/crt/Exports.h>
|
||||
#include <aws/crt/StlAllocator.h>
|
||||
#include <aws/crt/Types.h>
|
||||
#include <aws/crt/http/HttpConnection.h>
|
||||
#include <aws/crt/io/SocketOptions.h>
|
||||
#include <aws/crt/io/TlsOptions.h>
|
||||
#include <aws/crt/mqtt/MqttTypes.h>
|
||||
|
||||
#include <aws/mqtt/client.h>
|
||||
#include <aws/mqtt/v5/mqtt5_client.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace Aws
|
||||
{
|
||||
namespace Crt
|
||||
{
|
||||
namespace Http
|
||||
{
|
||||
class HttpRequest;
|
||||
}
|
||||
|
||||
namespace Mqtt5
|
||||
{
|
||||
class Mqtt5Client;
|
||||
class Mqtt5ClientCore;
|
||||
} // namespace Mqtt5
|
||||
|
||||
namespace Mqtt
|
||||
{
|
||||
class MqttClient;
|
||||
class MqttConnectionCore;
|
||||
class MqttConnection;
|
||||
|
||||
/**
|
||||
* The data returned when the connection closed callback is invoked in a connection.
|
||||
* Note: This class is currently empty, but this may contain data in the future.
|
||||
*/
|
||||
struct OnConnectionClosedData
|
||||
{
|
||||
};
|
||||
|
||||
/**
|
||||
* The data returned when the connection success callback is invoked in a connection.
|
||||
*/
|
||||
struct OnConnectionSuccessData
|
||||
{
|
||||
/**
|
||||
* The Connect return code received from the server.
|
||||
*/
|
||||
ReturnCode returnCode;
|
||||
|
||||
/**
|
||||
* Returns whether a session was present and resumed for this successful connection.
|
||||
* Will be set to true if the connection resumed an already present MQTT connection session.
|
||||
*/
|
||||
bool sessionPresent;
|
||||
};
|
||||
|
||||
/**
|
||||
* The data returned when the connection failure callback is invoked in a connection.
|
||||
*/
|
||||
struct OnConnectionFailureData
|
||||
{
|
||||
/**
|
||||
* The AWS CRT error code for the connection failure.
|
||||
* Use Aws::Crt::ErrorDebugString to get a human readable string from the error code.
|
||||
*/
|
||||
int error;
|
||||
};
|
||||
|
||||
/**
|
||||
* Invoked Upon Connection loss.
|
||||
*/
|
||||
using OnConnectionInterruptedHandler = std::function<void(MqttConnection &connection, int error)>;
|
||||
|
||||
/**
|
||||
* Invoked Upon Connection resumed.
|
||||
*/
|
||||
using OnConnectionResumedHandler =
|
||||
std::function<void(MqttConnection &connection, ReturnCode connectCode, bool sessionPresent)>;
|
||||
|
||||
/**
|
||||
* Invoked when a connack message is received, or an error occurred.
|
||||
*/
|
||||
using OnConnectionCompletedHandler = std::function<
|
||||
void(MqttConnection &connection, int errorCode, ReturnCode returnCode, bool sessionPresent)>;
|
||||
|
||||
/**
|
||||
* Invoked when a connection is disconnected and shutdown successfully.
|
||||
*
|
||||
* Note: Currently callbackData will always be nullptr, but this may change in the future to send additional
|
||||
* data.
|
||||
*/
|
||||
using OnConnectionClosedHandler =
|
||||
std::function<void(MqttConnection &connection, OnConnectionClosedData *callbackData)>;
|
||||
|
||||
/**
|
||||
* Invoked whenever the connection successfully connects.
|
||||
*
|
||||
* This callback is invoked for every successful connect and every successful reconnect.
|
||||
*/
|
||||
using OnConnectionSuccessHandler =
|
||||
std::function<void(MqttConnection &connection, OnConnectionSuccessData *callbackData)>;
|
||||
|
||||
/**
|
||||
* Invoked whenever the connection fails to connect.
|
||||
*
|
||||
* This callback is invoked for every failed connect and every failed reconnect.
|
||||
*/
|
||||
using OnConnectionFailureHandler =
|
||||
std::function<void(MqttConnection &connection, OnConnectionFailureData *callbackData)>;
|
||||
|
||||
/**
|
||||
* Invoked when a disconnect message has been sent.
|
||||
*/
|
||||
using OnDisconnectHandler = std::function<void(MqttConnection &connection)>;
|
||||
|
||||
/**
|
||||
* @deprecated Use OnMessageReceivedHandler
|
||||
*/
|
||||
using OnPublishReceivedHandler =
|
||||
std::function<void(MqttConnection &connection, const String &topic, const ByteBuf &payload)>;
|
||||
|
||||
/**
|
||||
* Callback for users to invoke upon completion of, presumably asynchronous, OnWebSocketHandshakeIntercept
|
||||
* callback's initiated process.
|
||||
*/
|
||||
using OnWebSocketHandshakeInterceptComplete =
|
||||
std::function<void(const std::shared_ptr<Http::HttpRequest> &, int errorCode)>;
|
||||
|
||||
/**
|
||||
* Invoked during websocket handshake to give users opportunity to transform an http request for purposes
|
||||
* such as signing/authorization etc... Returning from this function does not continue the websocket
|
||||
* handshake since some work flows may be asynchronous. To accommodate that, onComplete must be invoked upon
|
||||
* completion of the signing process.
|
||||
*/
|
||||
using OnWebSocketHandshakeIntercept = std::function<
|
||||
void(std::shared_ptr<Http::HttpRequest> req, const OnWebSocketHandshakeInterceptComplete &onComplete)>;
|
||||
|
||||
/**
|
||||
* Represents a persistent Mqtt Connection. The memory is owned by MqttClient or Mqtt5Client.
|
||||
*
|
||||
* To get a new instance of this class, use MqttClient::NewConnection or Mqtt5Client::NewConnection. Unless
|
||||
* specified all function arguments need only to live through the duration of the function call.
|
||||
*
|
||||
* @sa MqttClient::NewConnection
|
||||
* @sa Mqtt5Client::NewConnection
|
||||
*/
|
||||
class AWS_CRT_CPP_API MqttConnection final : public std::enable_shared_from_this<MqttConnection>
|
||||
{
|
||||
friend class MqttClient;
|
||||
friend class Mqtt5::Mqtt5ClientCore;
|
||||
|
||||
public:
|
||||
~MqttConnection();
|
||||
MqttConnection(const MqttConnection &) = delete;
|
||||
MqttConnection(MqttConnection &&) = delete;
|
||||
MqttConnection &operator=(const MqttConnection &) = delete;
|
||||
MqttConnection &operator=(MqttConnection &&) = delete;
|
||||
|
||||
/**
|
||||
* Create a new MqttConnection object from the Mqtt5Client.
|
||||
* @param mqtt5client The shared ptr of Mqtt5Client
|
||||
*
|
||||
* @return std::shared_ptr<Crt::Mqtt::MqttConnection>
|
||||
*/
|
||||
static std::shared_ptr<Crt::Mqtt::MqttConnection> NewConnectionFromMqtt5Client(
|
||||
std::shared_ptr<Mqtt5::Mqtt5Client> mqtt5client) noexcept;
|
||||
|
||||
/**
|
||||
* @return true if the instance is in a valid state, false otherwise.
|
||||
*/
|
||||
operator bool() const noexcept;
|
||||
|
||||
/**
|
||||
* @return the value of the last aws error encountered by operations on this instance.
|
||||
*/
|
||||
int LastError() const noexcept;
|
||||
|
||||
/**
|
||||
* Sets LastWill for the connection.
|
||||
* @param topic topic the will message should be published to
|
||||
* @param qos QOS the will message should be published with
|
||||
* @param retain true if the will publish should be treated as a retained publish
|
||||
* @param payload payload of the will message
|
||||
* @return success/failure in setting the will
|
||||
*/
|
||||
bool SetWill(const char *topic, QOS qos, bool retain, const ByteBuf &payload) noexcept;
|
||||
|
||||
/**
|
||||
* Sets login credentials for the connection. The must get set before the Connect call
|
||||
* if it is to be used.
|
||||
* @param username user name to add to the MQTT CONNECT packet
|
||||
* @param password password to add to the MQTT CONNECT packet
|
||||
* @return success/failure
|
||||
*/
|
||||
bool SetLogin(const char *username, const char *password) noexcept;
|
||||
|
||||
/**
|
||||
* @deprecated Sets websocket proxy options. Replaced by SetHttpProxyOptions.
|
||||
*/
|
||||
bool SetWebsocketProxyOptions(const Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
|
||||
|
||||
/**
|
||||
* Sets http proxy options. In order to use an http proxy with mqtt either
|
||||
* (1) Websockets are used
|
||||
* (2) Mqtt-over-tls is used and the ALPN list of the tls context contains a tag that resolves to mqtt
|
||||
*
|
||||
* @param proxyOptions proxy configuration for making the mqtt connection
|
||||
*
|
||||
* @return success/failure
|
||||
*/
|
||||
bool SetHttpProxyOptions(const Http::HttpClientConnectionProxyOptions &proxyOptions) noexcept;
|
||||
|
||||
/**
|
||||
* Customize time to wait between reconnect attempts.
|
||||
* The time will start at min and multiply by 2 until max is reached.
|
||||
* The time resets back to min after a successful connection.
|
||||
* This function should only be called before Connect().
|
||||
*
|
||||
* @param min_seconds minimum time to wait before attempting a reconnect
|
||||
* @param max_seconds maximum time to wait before attempting a reconnect
|
||||
*
|
||||
* @return success/failure
|
||||
*/
|
||||
bool SetReconnectTimeout(uint64_t min_seconds, uint64_t max_seconds) noexcept;
|
||||
|
||||
/**
|
||||
* Initiates the connection, OnConnectionCompleted will
|
||||
* be invoked in an event-loop thread.
|
||||
*
|
||||
* @param clientId client identifier to use when establishing the mqtt connection
|
||||
* @param cleanSession false to attempt to rejoin an existing session for the client id, true to skip
|
||||
* and start with a new session
|
||||
* @param keepAliveTimeSecs time interval to space mqtt pings apart by
|
||||
* @param pingTimeoutMs timeout in milliseconds before the keep alive ping is considered to have failed
|
||||
* @param protocolOperationTimeoutMs timeout in milliseconds to give up waiting for a response packet
|
||||
* for an operation. Necessary due to throttling properties on certain server implementations that do
|
||||
* not return an ACK for throttled operations.
|
||||
*
|
||||
* @return true if the connection attempt was successfully started (implying a callback will be invoked
|
||||
* with the eventual result), false if it could not be started (no callback will happen)
|
||||
*/
|
||||
bool Connect(
|
||||
const char *clientId,
|
||||
bool cleanSession,
|
||||
uint16_t keepAliveTimeSecs = 0,
|
||||
uint32_t pingTimeoutMs = 0,
|
||||
uint32_t protocolOperationTimeoutMs = 0) noexcept;
|
||||
|
||||
/**
|
||||
* Initiates disconnect, OnDisconnectHandler will be invoked in an event-loop thread.
|
||||
* @return success/failure in initiating disconnect
|
||||
*/
|
||||
bool Disconnect() noexcept;
|
||||
|
||||
/// @private
|
||||
aws_mqtt_client_connection *GetUnderlyingConnection() noexcept;
|
||||
|
||||
/**
|
||||
* Subscribes to topicFilter. OnMessageReceivedHandler will be invoked from an event-loop
|
||||
* thread upon an incoming Publish message. OnSubAckHandler will be invoked
|
||||
* upon receipt of a suback message.
|
||||
*
|
||||
* @param topicFilter topic filter to subscribe to
|
||||
* @param qos maximum qos client is willing to receive matching messages on
|
||||
* @param onMessage callback to invoke when a message is received based on matching this filter
|
||||
* @param onSubAck callback to invoke with the server's response to the subscribe request
|
||||
*
|
||||
* @return packet id of the subscribe request, or 0 if the attempt failed synchronously
|
||||
*/
|
||||
uint16_t Subscribe(
|
||||
const char *topicFilter,
|
||||
QOS qos,
|
||||
OnMessageReceivedHandler &&onMessage,
|
||||
OnSubAckHandler &&onSubAck) noexcept;
|
||||
|
||||
/**
|
||||
* @deprecated Use alternate Subscribe()
|
||||
*/
|
||||
uint16_t Subscribe(
|
||||
const char *topicFilter,
|
||||
QOS qos,
|
||||
OnPublishReceivedHandler &&onPublish,
|
||||
OnSubAckHandler &&onSubAck) noexcept;
|
||||
|
||||
/**
|
||||
* Subscribes to multiple topicFilters. OnMessageReceivedHandler will be invoked from an event-loop
|
||||
* thread upon an incoming Publish message. OnMultiSubAckHandler will be invoked
|
||||
* upon receipt of a suback message.
|
||||
*
|
||||
* @param topicFilters list of pairs of topic filters and message callbacks to invoke on a matching
|
||||
* publish
|
||||
* @param qos maximum qos client is willing to receive matching messages on
|
||||
* @param onOpComplete callback to invoke with the server's response to the subscribe request
|
||||
*
|
||||
* @return packet id of the subscribe request, or 0 if the attempt failed synchronously
|
||||
*/
|
||||
uint16_t Subscribe(
|
||||
const Vector<std::pair<const char *, OnMessageReceivedHandler>> &topicFilters,
|
||||
QOS qos,
|
||||
OnMultiSubAckHandler &&onOpComplete) noexcept;
|
||||
|
||||
/**
|
||||
* @deprecated Use alternate Subscribe()
|
||||
*/
|
||||
uint16_t Subscribe(
|
||||
const Vector<std::pair<const char *, OnPublishReceivedHandler>> &topicFilters,
|
||||
QOS qos,
|
||||
OnMultiSubAckHandler &&onOpComplete) noexcept;
|
||||
|
||||
/**
|
||||
* Installs a handler for all incoming publish messages, regardless of if Subscribe has been
|
||||
* called on the topic.
|
||||
*
|
||||
* @param onMessage callback to invoke for all received messages
|
||||
* @return success/failure
|
||||
*/
|
||||
bool SetOnMessageHandler(OnMessageReceivedHandler &&onMessage) noexcept;
|
||||
|
||||
/**
|
||||
* @deprecated Use alternate SetOnMessageHandler()
|
||||
*/
|
||||
bool SetOnMessageHandler(OnPublishReceivedHandler &&onPublish) noexcept;
|
||||
|
||||
/**
|
||||
* Unsubscribes from topicFilter. OnOperationCompleteHandler will be invoked upon receipt of
|
||||
* an unsuback message.
|
||||
*
|
||||
* @param topicFilter topic filter to unsubscribe the session from
|
||||
* @param onOpComplete callback to invoke on receipt of the server's UNSUBACK message
|
||||
*
|
||||
* @return packet id of the unsubscribe request, or 0 if the attempt failed synchronously
|
||||
*/
|
||||
uint16_t Unsubscribe(const char *topicFilter, OnOperationCompleteHandler &&onOpComplete) noexcept;
|
||||
|
||||
/**
|
||||
* Publishes to a topic.
|
||||
*
|
||||
* @param topic topic to publish to
|
||||
* @param qos QOS to publish the message with
|
||||
* @param retain should this message replace the current retained message of the topic?
|
||||
* @param payload payload of the message
|
||||
* @param onOpComplete completion callback to invoke when the operation is complete. If QoS is 0, then
|
||||
* the callback is invoked when the message is passed to the tls handler, otherwise it's invoked
|
||||
* on receipt of the final response from the server.
|
||||
*
|
||||
* @return packet id of the publish request, or 0 if the attempt failed synchronously
|
||||
*/
|
||||
uint16_t Publish(
|
||||
const char *topic,
|
||||
QOS qos,
|
||||
bool retain,
|
||||
const ByteBuf &payload,
|
||||
OnOperationCompleteHandler &&onOpComplete) noexcept;
|
||||
|
||||
/**
|
||||
* Get the statistics about the current state of the connection's queue of operations
|
||||
*
|
||||
* @return MqttConnectionOperationStatistics
|
||||
*/
|
||||
const MqttConnectionOperationStatistics &GetOperationStatistics() noexcept;
|
||||
|
||||
/**
|
||||
* A callback invoked every time the connections is interrupted.
|
||||
*/
|
||||
OnConnectionInterruptedHandler OnConnectionInterrupted;
|
||||
|
||||
/**
|
||||
* A callback invoked every time the connection is resumed.
|
||||
*/
|
||||
OnConnectionResumedHandler OnConnectionResumed;
|
||||
|
||||
/**
|
||||
* Invoked when a connack message is received, or an error occurred.
|
||||
*/
|
||||
OnConnectionCompletedHandler OnConnectionCompleted;
|
||||
|
||||
/**
|
||||
* A callback invoked on disconnect.
|
||||
*/
|
||||
OnDisconnectHandler OnDisconnect;
|
||||
|
||||
/**
|
||||
* Invoked during websocket handshake to give users opportunity to transform an http request for
|
||||
* purposes such as signing/authorization etc... Returning from this function does not continue the
|
||||
* websocket handshake since some work flows may be asynchronous. To accommodate that, onComplete must
|
||||
* be invoked upon completion of the signing process.
|
||||
*/
|
||||
OnWebSocketHandshakeIntercept WebsocketInterceptor;
|
||||
|
||||
/**
|
||||
* Invoked when a connection is disconnected and shutdown successfully.
|
||||
*
|
||||
* @note Currently callbackData will always be nullptr, but this may change in the future to send
|
||||
* additional data.
|
||||
* @note From the user perspective, this callback is indistinguishable from OnDisconnect.
|
||||
*/
|
||||
OnConnectionClosedHandler OnConnectionClosed;
|
||||
|
||||
/**
|
||||
* Invoked whenever the connection successfully connects.
|
||||
*
|
||||
* This callback is invoked for every successful connect and every successful reconnect.
|
||||
*/
|
||||
OnConnectionSuccessHandler OnConnectionSuccess;
|
||||
|
||||
/**
|
||||
* Invoked whenever the connection fails to connect.
|
||||
*
|
||||
* This callback is invoked for every failed connect and every failed reconnect.
|
||||
*/
|
||||
OnConnectionFailureHandler OnConnectionFailure;
|
||||
|
||||
private:
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Make private to restrict ability to create MqttConnections objects to certain classes.
|
||||
*/
|
||||
MqttConnection() = default;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* Factory method for creating MqttConnection.
|
||||
*
|
||||
* @param client MQTT3 client.
|
||||
* @param options Options required for MqttConnection creation.
|
||||
* @return New instance of MqttConnection.
|
||||
*/
|
||||
static std::shared_ptr<MqttConnection> s_CreateMqttConnection(
|
||||
aws_mqtt_client *client,
|
||||
MqttConnectionOptions options) noexcept;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* Factory method for creating MqttConnection.
|
||||
*
|
||||
* @param mqtt5Client MQTT5 client.
|
||||
* @param options Options required for MqttConnection creation.
|
||||
* @return New instance of MqttConnection.
|
||||
*/
|
||||
static std::shared_ptr<MqttConnection> s_CreateMqttConnection(
|
||||
aws_mqtt5_client *mqtt5Client,
|
||||
MqttConnectionOptions options) noexcept;
|
||||
/**
|
||||
* @internal
|
||||
* Internal handler for the underlying connection.
|
||||
*/
|
||||
std::shared_ptr<MqttConnectionCore> m_connectionCore;
|
||||
};
|
||||
} // namespace Mqtt
|
||||
} // namespace Crt
|
||||
} // namespace Aws
|
||||
@@ -0,0 +1,130 @@
|
||||
#pragma once
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
|
||||
#include <aws/crt/Types.h>
|
||||
#include <aws/crt/io/SocketOptions.h>
|
||||
#include <aws/crt/io/TlsOptions.h>
|
||||
|
||||
#include <aws/mqtt/client.h>
|
||||
#include <aws/mqtt/v5/mqtt5_client.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace Aws
|
||||
{
|
||||
namespace Crt
|
||||
{
|
||||
namespace Mqtt
|
||||
{
|
||||
class MqttConnection;
|
||||
|
||||
/**
|
||||
* Options required to create an MqttConnection.
|
||||
*/
|
||||
struct MqttConnectionOptions
|
||||
{
|
||||
const char *hostName = nullptr;
|
||||
uint32_t port = 0;
|
||||
Io::SocketOptions socketOptions;
|
||||
Crt::Io::TlsContext tlsContext;
|
||||
Crt::Io::TlsConnectionOptions tlsConnectionOptions;
|
||||
bool useWebsocket = false;
|
||||
bool useTls = false;
|
||||
Allocator *allocator = nullptr;
|
||||
};
|
||||
|
||||
/**
|
||||
* Invoked upon receipt of a Publish message on a subscribed topic.
|
||||
*
|
||||
* @param connection The connection object.
|
||||
* @param topic The information channel to which the payload data was published.
|
||||
* @param payload The payload data.
|
||||
* @param dup DUP flag. If true, this might be re-delivery of an earlier attempt to send the message.
|
||||
* @param qos Quality of Service used to deliver the message.
|
||||
* @param retain Retain flag. If true, the message was sent as a result of a new subscription being made by
|
||||
* the client.
|
||||
*/
|
||||
using OnMessageReceivedHandler = std::function<void(
|
||||
MqttConnection &connection,
|
||||
const String &topic,
|
||||
const ByteBuf &payload,
|
||||
bool dup,
|
||||
QOS qos,
|
||||
bool retain)>;
|
||||
|
||||
/**
|
||||
* Invoked when a suback message is received.
|
||||
*
|
||||
* @param connection The connection object.
|
||||
* @param packetId Packet ID of the corresponding subscribe request.
|
||||
* @param topic The information channel to which the payload data was published.
|
||||
* @param qos Quality of Service used to deliver the message.
|
||||
* @param errorCode Indicating if an error occurred.
|
||||
*/
|
||||
using OnSubAckHandler = std::function<
|
||||
void(MqttConnection &connection, uint16_t packetId, const String &topic, QOS qos, int errorCode)>;
|
||||
|
||||
/**
|
||||
* Invoked when a suback message for multiple topics is received.
|
||||
*
|
||||
* @param connection The connection object.
|
||||
* @param packetId Packet ID of the corresponding subscribe request.
|
||||
* @param topics The information channels to which the payload data was published.
|
||||
* @param qos Quality of Service used to deliver the message.
|
||||
* @param errorCode Indicating if an error occurred.
|
||||
*/
|
||||
using OnMultiSubAckHandler = std::function<void(
|
||||
MqttConnection &connection,
|
||||
uint16_t packetId,
|
||||
const Vector<String> &topics,
|
||||
QOS qos,
|
||||
int errorCode)>;
|
||||
|
||||
/**
|
||||
* Invoked when an operation completes.
|
||||
*
|
||||
* For QoS 0, this is when the packet is passed to the tls layer. For QoS 1 (and 2, in theory) this is when
|
||||
* the final ACK packet is received from the server.
|
||||
*
|
||||
* @param connection The connection object.
|
||||
* @param packetId Packet ID of the corresponding subscribe request.
|
||||
* @param errorCode Indicating if an error occurred.
|
||||
*/
|
||||
using OnOperationCompleteHandler =
|
||||
std::function<void(MqttConnection &connection, uint16_t packetId, int errorCode)>;
|
||||
|
||||
/**
|
||||
* Simple statistics about the current state of the client's queue of operations.
|
||||
*/
|
||||
struct AWS_CRT_CPP_API MqttConnectionOperationStatistics
|
||||
{
|
||||
/*
|
||||
* Total number of operations submitted to the connection that have not yet been completed. Unacked
|
||||
* operations are a subset of this.
|
||||
*/
|
||||
uint64_t incompleteOperationCount;
|
||||
|
||||
/*
|
||||
* Total packet size of operations submitted to the connection that have not yet been completed. Unacked
|
||||
* operations are a subset of this.
|
||||
*/
|
||||
uint64_t incompleteOperationSize;
|
||||
|
||||
/*
|
||||
* Total number of operations that have been sent to the server and are waiting for a corresponding ACK
|
||||
* before they can be completed.
|
||||
*/
|
||||
uint64_t unackedOperationCount;
|
||||
|
||||
/*
|
||||
* Total packet size of operations that have been sent to the server and are waiting for a corresponding
|
||||
* ACK before they can be completed.
|
||||
*/
|
||||
uint64_t unackedOperationSize;
|
||||
};
|
||||
} // namespace Mqtt
|
||||
} // namespace Crt
|
||||
} // namespace Aws
|
||||
Reference in New Issue
Block a user