mirror of
https://github.com/eclipse/mosquitto.git
synced 2025-05-09 01:01:11 +08:00
Client disconnect property support, plus disconnect packet fix.
This commit is contained in:
parent
383608613a
commit
741a8a9cc3
@ -94,7 +94,7 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result)
|
||||
break;
|
||||
}
|
||||
}
|
||||
mosquitto_disconnect(mosq);
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
}
|
||||
}else{
|
||||
if(result && !quiet){
|
||||
@ -113,11 +113,11 @@ void my_publish_callback(struct mosquitto *mosq, void *obj, int mid)
|
||||
last_mid_sent = mid;
|
||||
if(mode == MSGMODE_STDIN_LINE){
|
||||
if(mid == last_mid){
|
||||
mosquitto_disconnect(mosq);
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
disconnect_sent = true;
|
||||
}
|
||||
}else if(disconnect_sent == false){
|
||||
mosquitto_disconnect(mosq);
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
disconnect_sent = true;
|
||||
}
|
||||
}
|
||||
@ -419,7 +419,7 @@ int main(int argc, char *argv[])
|
||||
rc2 = mosquitto_publish_with_properties(mosq, &mid_sent, topic, buf_len_actual-1, buf, qos, retain, cfg.publish_props);
|
||||
if(rc2){
|
||||
if(!quiet) fprintf(stderr, "Error: Publish returned %d, disconnecting.\n", rc2);
|
||||
mosquitto_disconnect(mosq);
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
}
|
||||
break;
|
||||
}else{
|
||||
@ -438,7 +438,7 @@ int main(int argc, char *argv[])
|
||||
if(feof(stdin)){
|
||||
if(last_mid == -1){
|
||||
/* Empty file */
|
||||
mosquitto_disconnect(mosq);
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
disconnect_sent = true;
|
||||
status = STATUS_DISCONNECTING;
|
||||
}else{
|
||||
@ -448,7 +448,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
}else if(status == STATUS_WAITING){
|
||||
if(last_mid_sent == last_mid && disconnect_sent == false){
|
||||
mosquitto_disconnect(mosq);
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
disconnect_sent = true;
|
||||
}
|
||||
#ifdef WIN32
|
||||
|
@ -34,6 +34,7 @@ Contributors:
|
||||
#include <mosquitto.h>
|
||||
#include "client_shared.h"
|
||||
|
||||
static struct mosq_config cfg;
|
||||
bool process_messages = true;
|
||||
int msg_count = 0;
|
||||
struct mosquitto *mosq = NULL;
|
||||
@ -43,7 +44,7 @@ void my_signal_handler(int signum)
|
||||
{
|
||||
if(signum == SIGALRM){
|
||||
process_messages = false;
|
||||
mosquitto_disconnect(mosq);
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -53,36 +54,32 @@ void print_message(struct mosq_config *cfg, const struct mosquitto_message *mess
|
||||
|
||||
void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
|
||||
{
|
||||
struct mosq_config *cfg;
|
||||
int i;
|
||||
bool res;
|
||||
|
||||
if(process_messages == false) return;
|
||||
|
||||
assert(obj);
|
||||
cfg = (struct mosq_config *)obj;
|
||||
|
||||
if(cfg->retained_only && !message->retain && process_messages){
|
||||
if(cfg.retained_only && !message->retain && process_messages){
|
||||
process_messages = false;
|
||||
mosquitto_disconnect(mosq);
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
return;
|
||||
}
|
||||
|
||||
if(message->retain && cfg->no_retain) return;
|
||||
if(cfg->filter_outs){
|
||||
for(i=0; i<cfg->filter_out_count; i++){
|
||||
mosquitto_topic_matches_sub(cfg->filter_outs[i], message->topic, &res);
|
||||
if(message->retain && cfg.no_retain) return;
|
||||
if(cfg.filter_outs){
|
||||
for(i=0; i<cfg.filter_out_count; i++){
|
||||
mosquitto_topic_matches_sub(cfg.filter_outs[i], message->topic, &res);
|
||||
if(res) return;
|
||||
}
|
||||
}
|
||||
|
||||
print_message(cfg, message);
|
||||
print_message(&cfg, message);
|
||||
|
||||
if(cfg->msg_count>0){
|
||||
if(cfg.msg_count>0){
|
||||
msg_count++;
|
||||
if(cfg->msg_count == msg_count){
|
||||
if(cfg.msg_count == msg_count){
|
||||
process_messages = false;
|
||||
mosquitto_disconnect(mosq);
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -90,41 +87,33 @@ void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquit
|
||||
void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flags)
|
||||
{
|
||||
int i;
|
||||
struct mosq_config *cfg;
|
||||
|
||||
assert(obj);
|
||||
cfg = (struct mosq_config *)obj;
|
||||
|
||||
if(!result){
|
||||
mosquitto_subscribe_multiple(mosq, NULL, cfg->topic_count, cfg->topics, cfg->qos, cfg->subscribe_props);
|
||||
mosquitto_subscribe_multiple(mosq, NULL, cfg.topic_count, cfg.topics, cfg.qos, cfg.subscribe_props);
|
||||
|
||||
for(i=0; i<cfg->unsub_topic_count; i++){
|
||||
mosquitto_unsubscribe(mosq, NULL, cfg->unsub_topics[i]);
|
||||
for(i=0; i<cfg.unsub_topic_count; i++){
|
||||
mosquitto_unsubscribe(mosq, NULL, cfg.unsub_topics[i]);
|
||||
}
|
||||
}else{
|
||||
if(result && !cfg->quiet){
|
||||
if(result && !cfg.quiet){
|
||||
fprintf(stderr, "%s\n", mosquitto_connack_string(result));
|
||||
}
|
||||
mosquitto_disconnect(mosq);
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
}
|
||||
}
|
||||
|
||||
void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
|
||||
{
|
||||
int i;
|
||||
struct mosq_config *cfg;
|
||||
|
||||
assert(obj);
|
||||
cfg = (struct mosq_config *)obj;
|
||||
|
||||
if(!cfg->quiet) printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
|
||||
if(!cfg.quiet) printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
|
||||
for(i=1; i<qos_count; i++){
|
||||
if(!cfg->quiet) printf(", %d", granted_qos[i]);
|
||||
if(!cfg.quiet) printf(", %d", granted_qos[i]);
|
||||
}
|
||||
if(!cfg->quiet) printf("\n");
|
||||
if(!cfg.quiet) printf("\n");
|
||||
|
||||
if(cfg->exit_after_sub){
|
||||
mosquitto_disconnect(mosq);
|
||||
if(cfg.exit_after_sub){
|
||||
mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props);
|
||||
}
|
||||
}
|
||||
|
||||
@ -240,7 +229,6 @@ void print_usage(void)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct mosq_config cfg;
|
||||
int rc;
|
||||
#ifndef WIN32
|
||||
struct sigaction sigact;
|
||||
|
@ -95,6 +95,7 @@ MOSQ_1.5 {
|
||||
MOSQ_1.6 {
|
||||
global:
|
||||
mosquitto_connect_bind_with_properties;
|
||||
mosquitto_disconnect_with_properties;
|
||||
mosquitto_property_add_binary;
|
||||
mosquitto_property_add_byte;
|
||||
mosquitto_property_add_int16;
|
||||
|
@ -54,7 +54,8 @@ int send__disconnect(struct mosquitto *mosq, const struct mqtt5__property *prope
|
||||
if(mosq->protocol == mosq_p_mqtt5){
|
||||
proplen = property__get_length_all(properties);
|
||||
varbytes = packet__varint_bytes(proplen);
|
||||
packet->remaining_length = proplen+varbytes;
|
||||
/* 1 here is the reason code */
|
||||
packet->remaining_length = 1 + proplen + varbytes;
|
||||
}else{
|
||||
packet->remaining_length = 0;
|
||||
}
|
||||
@ -65,6 +66,7 @@ int send__disconnect(struct mosquitto *mosq, const struct mqtt5__property *prope
|
||||
return rc;
|
||||
}
|
||||
if(mosq->protocol == mosq_p_mqtt5){
|
||||
packet__write_byte(packet, 0);
|
||||
property__write_all(packet, properties);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user