Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mosquitto client C++ wrapper to support more MQTT v5 features. #3019

Open
wants to merge 2 commits into
base: fixes
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Add asynchronous versions of connect functions for mqtt V5.
    Signed-off-by: Guy-SE <[email protected]>
  • Loading branch information
Guy-SE committed Mar 12, 2024
commit 587fc536bf2ad2c5fec630aec8dd5d88a50706a0
4 changes: 4 additions & 0 deletions include/mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ libmosq_EXPORT int mosquitto_connect_bind_v5(struct mosquitto *mosq, const char
*/
libmosq_EXPORT int mosquitto_connect_async(struct mosquitto *mosq, const char *host, int port, int keepalive);

libmosq_EXPORT int mosquitto_connect_async_v5(struct mosquitto *mosq, const char *host, int port, int keepalive, const mosquitto_property *properties);

/*
* Function: mosquitto_connect_bind_async
*
Expand Down Expand Up @@ -673,6 +675,8 @@ libmosq_EXPORT int mosquitto_connect_async(struct mosquitto *mosq, const char *h
*/
libmosq_EXPORT int mosquitto_connect_bind_async(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address);

libmosq_EXPORT int mosquitto_connect_bind_async_v5(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address, const mosquitto_property *properties);

/*
* Function: mosquitto_connect_srv
*
Expand Down
30 changes: 30 additions & 0 deletions lib/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ int mosquitto_connect_async(struct mosquitto *mosq, const char *host, int port,
return mosquitto_connect_bind_async(mosq, host, port, keepalive, NULL);
}

int mosquitto_connect_async_v5(struct mosquitto *mosq, const char *host, int port, int keepalive, const mosquitto_property *properties)
{
return mosquitto_connect_bind_async_v5(mosq, host, port, keepalive, NULL, properties);
}

int mosquitto_connect_bind_async(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address)
{
Expand All @@ -142,6 +146,32 @@ int mosquitto_connect_bind_async(struct mosquitto *mosq, const char *host, int p
return mosquitto__reconnect(mosq, false);
}

int mosquitto_connect_bind_async_v5(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address, const mosquitto_property *properties)
{
int rc;

if(bind_address){
rc = mosquitto_string_option(mosq, MOSQ_OPT_BIND_ADDRESS, bind_address);
if(rc) return rc;
}

mosquitto_property_free_all(&mosq->connect_properties);
if(properties){
rc = mosquitto_property_check_all(CMD_CONNECT, properties);
if(rc) return rc;

rc = mosquitto_property_copy_all(&mosq->connect_properties, properties);
if(rc) return rc;
mosq->connect_properties->client_generated = true;
}

rc = mosquitto__connect_init(mosq, host, port, keepalive);
if(rc) return rc;

mosquitto__set_state(mosq, mosq_cs_new);

return mosquitto__reconnect(mosq, false);
}

int mosquitto_reconnect_async(struct mosquitto *mosq)
{
Expand Down
10 changes: 10 additions & 0 deletions lib/cpp/mosquittopp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,21 @@ int mosquittopp::connect_async(const char *host, int port, int keepalive)
return mosquitto_connect_async(m_mosq, host, port, keepalive);
}

int mosquittopp_v5::connect_async(const char *host, int port, int keepalive, const mosquitto_property* properties)
{
return mosquitto_connect_async_v5(m_mosq, host, port, keepalive, properties);
}

int mosquittopp::connect_async(const char *host, int port, int keepalive, const char *bind_address)
{
return mosquitto_connect_bind_async(m_mosq, host, port, keepalive, bind_address);
}

int mosquittopp_v5::connect_async(const char *host, int port, int keepalive, const char *bind_address, const mosquitto_property* properties)
{
return mosquitto_connect_bind_async_v5(m_mosq, host, port, keepalive, bind_address, properties);
}

int mosquittopp_base::reconnect()
{
return mosquitto_reconnect(m_mosq);
Expand Down
2 changes: 2 additions & 0 deletions lib/cpp/mosquittopp.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ class mosqpp_EXPORT mosquittopp_v5 : public mosquittopp_base {
mosquitto_property* properties=nullptr);
int connect(const char* host, int port=1883, int keepalive=60, const char* bind_address=nullptr,
const mosquitto_property* properties=nullptr);
int connect_async(const char* host, int port, int keepalive=60, const mosquitto_property* properties=nullptr);
int connect_async(const char *host, int port, int keepalive, const char *bind_address=nullptr, const mosquitto_property* properties=nullptr);
int disconnect(int reason_code=0, const mosquitto_property* properties=nullptr);
int publish(int* mid, const char* topic, int payloadlen=0, const void* payload=nullptr, int qos=0, bool retain=false,
const mosquitto_property* properties=nullptr);
Expand Down
6 changes: 6 additions & 0 deletions lib/linker.version
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,9 @@ MOSQ_1.7 {
mosquitto_property_next;
mosquitto_ssl_get;
} MOSQ_1.6;

MOSQ_2.0 {
global:
mosquitto_connect_async_v5;
mosquitto_connect_bind_async_v5;
} MOSQ_1.7;