|
version 1.1.2.2, 2012/05/05 13:10:24
|
version 1.3.6.1, 2013/07/18 15:02:52
|
|
Line 1
|
Line 1
|
| #include "global.h" |
#include "global.h" |
| |
|
| |
|
| static inline int | #pragma GCC visibility push(hidden) |
| _wait4data(int sock, u_short ka, short events) | |
| | int |
| | mqtt_wait4data(int sock, u_short ka, short events) |
| { |
{ |
| int ret = 0; |
int ret = 0; |
| struct pollfd pfd; |
struct pollfd pfd; |
|
Line 22 _wait4data(int sock, u_short ka, short events)
|
Line 24 _wait4data(int sock, u_short ka, short events)
|
| return 0; /* ready */ |
return 0; /* ready */ |
| } |
} |
| |
|
| |
#pragma GCC visibility pop |
| |
|
| |
|
| /* |
/* |
| * mqtt_KeepAlive() - Keep Alive check routine |
* mqtt_KeepAlive() - Keep Alive check routine |
| * |
* |
|
Line 40 mqtt_KeepAlive(int sock, u_short ka, u_char tries)
|
Line 44 mqtt_KeepAlive(int sock, u_short ka, u_char tries)
|
| if (sock < 3) |
if (sock < 3) |
| return -1; /* error */ |
return -1; /* error */ |
| |
|
| if ((ret = _wait4data(sock, ka, POLLOUT))) | if ((ret = mqtt_wait4data(sock, ka, POLLOUT))) |
| return ret; |
return ret; |
| /* ping request */ |
/* ping request */ |
| if ((ret = mqtt_msgPINGREQ(&msg)) == -1) |
if ((ret = mqtt_msgPINGREQ(&msg)) == -1) |
|
Line 51 mqtt_KeepAlive(int sock, u_short ka, u_char tries)
|
Line 55 mqtt_KeepAlive(int sock, u_short ka, u_char tries)
|
| } |
} |
| |
|
| while (tries--) { |
while (tries--) { |
| if ((ret = _wait4data(sock, ka, POLLIN | POLLPRI))) { | if ((ret = mqtt_wait4data(sock, ka, POLLIN | POLLPRI))) { |
| if (ret == -1) |
if (ret == -1) |
| break; |
break; |
| else |
else |
|
Line 69 mqtt_KeepAlive(int sock, u_short ka, u_char tries)
|
Line 73 mqtt_KeepAlive(int sock, u_short ka, u_char tries)
|
| ret = 2; /* Session is broken ... must be disconnect! */ |
ret = 2; /* Session is broken ... must be disconnect! */ |
| } |
} |
| end: |
end: |
| |
free(msg.msg_base); |
| |
return ret; |
| |
} |
| |
|
| |
/* |
| |
* mqtt_WillMessage() - Publish WILL message |
| |
* |
| |
* @sock = connected socket |
| |
* @ka = keep alive timeout |
| |
* @topic = will topic |
| |
* @data = will message |
| |
* return: -1 error, 1 timeout, 2 not ack or 0 ok |
| |
*/ |
| |
int |
| |
mqtt_WillMessage(int sock, u_short ka, const char *topic, const char *data) |
| |
{ |
| |
int ret = 0; |
| |
mqtt_msg_t msg = { NULL, 0 }; |
| |
|
| |
if (!topic) |
| |
return -1; /* error */ |
| |
|
| |
/* will message */ |
| |
if ((ret = mqtt_wait4data(sock, ka, POLLOUT))) |
| |
return ret; |
| |
ret = mqtt_msgPUBLISH(&msg, topic, 0xDEAD, 0, 1, 0, data, data ? strlen(data) : 0); |
| |
if (ret == -1) |
| |
return -1; /* error */ |
| |
if ((ret = send(sock, msg.msg_base, ret, MSG_NOSIGNAL)) == -1) { |
| |
LOGERR; |
| |
free(msg.msg_base); |
| |
return -1; /* error */ |
| |
} else |
| |
memset(msg.msg_base, 0, msg.msg_len); |
| |
|
| |
/* will ack */ |
| |
if ((ret = mqtt_wait4data(sock, ka, POLLIN | POLLPRI))) { |
| |
free(msg.msg_base); |
| |
return ret; |
| |
} |
| |
/* receive & decode packet */ |
| |
if ((ret = recv(sock, msg.msg_base, msg.msg_len, 0)) == -1) { |
| |
LOGERR; |
| |
free(msg.msg_base); |
| |
return -1; /* error */ |
| |
} |
| |
if (mqtt_readPUBACK(&msg)) |
| |
ret = 0; /* ok */ |
| |
else |
| |
ret = 2; /* semi-error */ |
| |
|
| free(msg.msg_base); |
free(msg.msg_base); |
| return ret; |
return ret; |
| } |
} |