123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /////////////////////////////////////////////////////////////////////////////
- /// @file disconnect_options.h
- /// Implementation of the class 'disconnect_options'
- /// @date 26-Aug-2016
- /////////////////////////////////////////////////////////////////////////////
- /****************************************************************************
- * Copyright (c) 2016-2017 Frank Pagliughi <fpagliughi@mindspring.com>
- *
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * and Eclipse Distribution License v1.0 which accompany this distribution.
- *
- * The Eclipse Public License is available at
- * http://www.eclipse.org/legal/epl-v10.html
- * and the Eclipse Distribution License is available at
- * http://www.eclipse.org/org/documents/edl-v10.php.
- *
- * Contributors:
- * Frank Pagliughi - initial implementation and documentation
- ***************************************************************************/
- #ifndef __mqtt_disconnect_options_h
- #define __mqtt_disconnect_options_h
- #include "MQTTAsync.h"
- #include "mqtt/types.h"
- #include "mqtt/token.h"
- #include "mqtt/properties.h"
- #include <chrono>
- namespace mqtt {
- /////////////////////////////////////////////////////////////////////////////
- /**
- * Options for disconnecting from an MQTT broker.
- */
- class disconnect_options
- {
- /** The default C struct */
- static const MQTTAsync_disconnectOptions DFLT_C_STRUCT;
- /** The underlying C disconnect options */
- MQTTAsync_disconnectOptions opts_;
- /** Shared token pointer for context, if any */
- token_ptr tok_;
- /** Disconnect message properties */
- properties props_;
- /** The client has special access */
- friend class async_client;
- friend class disconnect_options_test;
- public:
- /**
- * Create an empty delivery response object.
- */
- disconnect_options();
- /**
- * Creates disconnect options tied to the specific token.
- * @param timeout The timeout (in milliseconds).
- */
- disconnect_options(int timeout) : disconnect_options() {
- set_timeout(timeout);
- }
- /**
- * Creates disconnect options tied to the specific token.
- * @param to The timeout.
- */
- template <class Rep, class Period>
- disconnect_options(const std::chrono::duration<Rep, Period>& to)
- : disconnect_options() {
- set_timeout(to);
- }
- /**
- * Copy constructor.
- * @param opt Another object to copy.
- */
- disconnect_options(const disconnect_options& opt);
- /**
- * Move constructor.
- * @param opt Another object to move into this new one.
- */
- disconnect_options(disconnect_options&& opt);
- /**
- * Copy assignment.
- * @param opt Another object to copy.
- */
- disconnect_options& operator=(const disconnect_options& opt);
- /**
- * Move assignment.
- * @param opt Another object to move into this new one.
- */
- disconnect_options& operator=(disconnect_options&& opt);
- /**
- * Gets the timeout used for disconnecting.
- * @return The timeout for disconnecting (in milliseconds).
- */
- std::chrono::milliseconds get_timeout() const {
- return std::chrono::milliseconds(opts_.timeout);
- }
- /**
- * Sets the timeout for disconnecting.
- * This allows for any remaining in-flight messages to be delivered.
- * @param timeout The timeout (in milliseconds).
- */
- void set_timeout(int timeout) { opts_.timeout = timeout; }
- /**
- * Sets the connect timeout with a chrono duration.
- * This is the maximum time that the underlying library will wait for a
- * connection before failing.
- * @param to The connect timeout in seconds.
- */
- template <class Rep, class Period>
- void set_timeout(const std::chrono::duration<Rep, Period>& to) {
- // TODO: check range
- set_timeout((int) to_milliseconds_count(to));
- }
- /**
- * Sets the callback context to a delivery token.
- * @param tok The delivery token to be used as the callback context.
- * @param mqttVersion The version of MQTT we're using for the
- * connection.
- */
- void set_token(const token_ptr& tok, int mqttVersion);
- /**
- * Gets the callback context to a delivery token.
- * @return The delivery token to be used as the callback context.
- */
- token_ptr get_token() const { return tok_; }
- /**
- * Gets the connect properties.
- * @return A const reference to the properties for the connect.
- */
- const properties& get_properties() const { return props_; }
- /**
- * Sets the properties for the connect.
- * @param props The properties to place into the message.
- */
- void set_properties(const properties& props) {
- props_ = props;
- opts_.properties = props_.c_struct();
- }
- /**
- * Moves the properties for the connect.
- * @param props The properties to move into the connect object.
- */
- void set_properties(properties&& props) {
- props_ = props;
- opts_.properties = props_.c_struct();
- }
- /**
- * Gets the reason code for the disconnect.
- * @return The reason code for the disconnect.
- */
- ReasonCode get_reason_code() const {
- return ReasonCode(opts_.reasonCode);
- }
- /**
- * Sets the reason code for the disconnect.
- * @param code The reason code for the disconnect.
- */
- void set_reason_code(ReasonCode code) {
- opts_.reasonCode = MQTTReasonCodes(code);
- }
- };
- /////////////////////////////////////////////////////////////////////////////
- // end namespace 'mqtt'
- }
- #endif // __mqtt_disconnect_options_h
|