2013-12-17 11:05:27 |
Yun Wu |
description |
It seems that blocking mode is not really supported.
Topology: publisher -- broker -- subscriber
Publisher simply publish a message then exit.
Subscriber sometimes can receive message published by publisher, but sometimes not.
The root cause is, libmosquitto does not really support blocking mode.
mosquitto_connect_bind --> _mosquitto_reconnect --> _mosquitto_send_connect
That means mosquitto_connect_bind() does not wait until CONNACK arrives.
Logs can prove that:
----
1387273535: New connection from 172.18.111.243 on port 1883.
1387273535: New client connected from 172.18.111.243 as TestClientId (c1, k3600).
1387273535: Sending CONNACK to TestClientId (0)
1387273535: Socket error on client TestClientId, disconnecting.
----
We can see:
1) Broker has not received PUBLISH message from publisher.
2) Broker has not received DISCONNECT message from publisher.
All because blocking mode is not really supported.
In blocking mode,
mosquitto_connect_bind should wait until CONNACK arrives;
If QoS not 0, mosquitto_publish should wait unit ...
mosquitto_subscribe should wait unit ...
... |
It seems that blocking mode is not really supported.
Topology: publisher -- broker -- subscriber
Publisher simply publish a message then exit.
Subscriber sometimes can receive message published by publisher, but sometimes not.
The root cause is, libmosquitto does not really support blocking mode.
mosquitto_connect_bind --> _mosquitto_reconnect --> _mosquitto_send_connect
That means mosquitto_connect_bind() does not wait until CONNACK arrives.
Logs can prove that:
----
1387273535: New connection from 172.18.111.243 on port 1883.
1387273535: New client connected from 172.18.111.243 as TestClientId (c1, k3600).
1387273535: Sending CONNACK to TestClientId (0)
1387273535: Socket error on client TestClientId, disconnecting.
----
We can see:
1) Broker has not received PUBLISH message from publisher.
2) Broker has not received DISCONNECT message from publisher.
All because blocking mode is not really supported.
In blocking mode,
mosquitto_connect_bind should wait until CONNACK arrives;
If QoS not 0, mosquitto_publish should wait unit ...
mosquitto_subscribe should wait unit ...
...
Here is code of the publisher:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mosquitto.h"
#define dbg(fmt, args...) \
printf("%s(%u): "fmt"\n", __FUNCTION__, __LINE__, ##args)
int main(int argc, char *argv[])
{
struct mosquitto *mosq = NULL;
int rv;
const char topic[] = "TestTopic";
const char buf[] = "Hello, world";
if (MOSQ_ERR_SUCCESS != mosquitto_lib_init()) {
return -1;
}
if (NULL == (mosq = mosquitto_new("TestClientId", 1, NULL))) {
dbg("mosquitto_new() failed");
rv = -1;
goto quit;
}
rv = mosquitto_connect_bind(mosq, "172.18.111.245", 1883, 3600, "172.18.111.243");
if (MOSQ_ERR_SUCCESS != rv) {
dbg("mosquitto_connect_bind() failed: %s", mosquitto_strerror(rv));
if (MOSQ_ERR_ERRNO == rv) {
dbg("errno = %d: %s", errno, strerror(errno));
}
rv = -1;
goto quit;
}
if (MOSQ_ERR_SUCCESS != mosquitto_publish(mosq, NULL, topic, sizeof(buf), buf, 0, 0)) {
dbg("mosquitto_publish to %s failed", topic);
rv = -1;
goto quit;
}
quit:
if (NULL != mosq) {
mosquitto_disconnect(mosq);
mosquitto_destroy(mosq);
}
mosquitto_lib_cleanup();
return rv;
} |
|