Skip to content

Commit d275f90

Browse files
committed
add mosquitto_delay_puback option
Signed-off-by: Linus Basig <[email protected]> Signed-off-by: Fabrizio Lazzaretti <[email protected]> Signed-off-by: Linus Basig <[email protected]>
1 parent 122e6ec commit d275f90

File tree

10 files changed

+45
-1
lines changed

10 files changed

+45
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ test/unit/out/
8585

8686
www/cache/
8787
__pycache__
88+
.DS_Store

ChangeLog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Client library:
2+
- Add `mosquitto_delay_puback()` for delaying the PUBACK message
3+
until the on_message callback returns.
4+
15
2.0.0 - 2020-12-03
26
==================
37

include/mosquitto.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,18 @@ libmosq_EXPORT void mosquitto_user_data_set(struct mosquitto *mosq, void *obj);
16651665
*/
16661666
libmosq_EXPORT void *mosquitto_userdata(struct mosquitto *mosq);
16671667

1668+
/*
1669+
* Function: mosquitto_delay_puback
1670+
*
1671+
* Per Default, the library will acknowledge a QoS 1 message before it calls the
1672+
* on_message callback. After <mosquitto_delay_puback> is called, the library
1673+
* delays the acknowledgment untill after the on_message callback has returned.
1674+
*
1675+
* Parameters:
1676+
* mosq - a valid mosquitto instance.
1677+
*/
1678+
libmosq_EXPORT int mosquitto_delay_puback(struct mosquitto *mosq);
1679+
16681680

16691681
/* ======================================================================
16701682
*

lib/cpp/mosquittopp.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ void mosquittopp::user_data_set(void *userdata)
352352
mosquitto_user_data_set(m_mosq, userdata);
353353
}
354354

355+
int mosquittopp::delay_puback()
356+
{
357+
mosquitto_delay_puback(m_mosq);
358+
}
359+
355360
int mosquittopp::socks5_set(const char *host, int port, const char *username, const char *password)
356361
{
357362
return mosquitto_socks5_set(m_mosq, host, port, username, password);

lib/cpp/mosquittopp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class mosqpp_EXPORT mosquittopp {
107107
int max_inflight_messages_set(unsigned int max_inflight_messages);
108108
void message_retry_set(unsigned int message_retry);
109109
void user_data_set(void *userdata);
110+
int delay_puback();
110111
int tls_set(const char *cafile, const char *capath=NULL, const char *certfile=NULL, const char *keyfile=NULL, int (*pw_callback)(char *buf, int size, int rwflag, void *userdata)=NULL);
111112
int tls_opts_set(int cert_reqs, const char *tls_version=NULL, const char *ciphers=NULL);
112113
int tls_insecure_set(bool value);

lib/handle_publish.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ int handle__publish(struct mosquitto *mosq)
135135
return MOSQ_ERR_SUCCESS;
136136
case 1:
137137
util__decrement_receive_quota(mosq);
138-
rc = send__puback(mosq, mid, 0, NULL);
138+
if(mosq->delayed_puback){
139+
rc = send__puback(mosq, mid, 0, NULL);
140+
}
139141
pthread_mutex_lock(&mosq->callback_mutex);
140142
if(mosq->on_message){
141143
mosq->in_callback = true;
@@ -148,6 +150,9 @@ int handle__publish(struct mosquitto *mosq)
148150
mosq->in_callback = false;
149151
}
150152
pthread_mutex_unlock(&mosq->callback_mutex);
153+
if(!mosq->delayed_puback){
154+
rc = send__puback(mosq, mid, 0, NULL);
155+
}
151156
message__cleanup(&message);
152157
mosquitto_property_free_all(&properties);
153158
return rc;

lib/linker.version

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,8 @@ MOSQ_1.7 {
141141
mosquitto_property_next;
142142
mosquitto_ssl_get;
143143
} MOSQ_1.6;
144+
145+
MOSQ_2.1 {
146+
global:
147+
mosquitto_delay_puback;
148+
} MOSQ_2.0;

lib/mosquitto.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st
193193
mosq->reconnect_delay_max = 1;
194194
mosq->reconnect_exponential_backoff = false;
195195
mosq->threaded = mosq_ts_none;
196+
mosq->delayed_puback = false;
196197
#ifdef WITH_TLS
197198
mosq->ssl = NULL;
198199
mosq->ssl_ctx = NULL;

lib/mosquitto_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ struct mosquitto {
351351
#ifdef WITH_EPOLL
352352
uint32_t events;
353353
#endif
354+
bool delayed_puback;
354355
};
355356

356357
#define STREMPTY(str) (str[0] == '\0')

lib/options.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,12 @@ void *mosquitto_userdata(struct mosquitto *mosq)
530530
{
531531
return mosq->userdata;
532532
}
533+
534+
int mosquitto_delay_puback(struct mosquitto *mosq)
535+
{
536+
if(!mosq) return MOSQ_ERR_INVAL;
537+
538+
mosq->delayed_puback = true;
539+
540+
return MOSQ_ERR_SUCCESS;
541+
}

0 commit comments

Comments
 (0)