Annotation of libaitmqtt/src/cmds.c, revision 1.2.6.1
1.2 misho 1: #include "global.h"
2:
3:
4: #pragma GCC visibility push(hidden)
5:
6: inline int
7: mqtt_wait4data(int sock, u_short ka, short events)
8: {
9: int ret = 0;
10: struct pollfd pfd;
11:
12: if (sock < 3)
13: return -1; /* error */
14:
15: pfd.fd = sock;
16: pfd.events = POLLOUT;
17: if ((ret = poll(&pfd, 1, ka * 1000)) == -1 ||
18: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
19: LOGERR;
20: return -1; /* error */
21: } else if (!ret)
22: return 1; /* timeout */
23:
24: return 0; /* ready */
25: }
26:
27: #pragma GCC visibility pop
28:
29:
30: /*
31: * mqtt_KeepAlive() - Keep Alive check routine
32: *
33: * @sock = connected socket
34: * @ka = keep alive timeout
35: * @tries = tries for receive correct ping response, usually ==1
36: * return: -1 error, 0 host is alive, 1 timeout session or 2 broken session
37: */
38: int
39: mqtt_KeepAlive(int sock, u_short ka, u_char tries)
40: {
41: int ret = 0;
42: mqtt_msg_t msg = { NULL, 0 };
43:
44: if (sock < 3)
45: return -1; /* error */
46:
47: if ((ret = mqtt_wait4data(sock, ka, POLLOUT)))
48: return ret;
49: /* ping request */
50: if ((ret = mqtt_msgPINGREQ(&msg)) == -1)
51: return -1; /* error */
52: if ((ret = send(sock, msg.msg_base, ret, MSG_NOSIGNAL)) == -1) {
53: LOGERR;
54: goto end;
55: }
56:
57: while (tries--) {
58: if ((ret = mqtt_wait4data(sock, ka, POLLIN | POLLPRI))) {
59: if (ret == -1)
60: break;
61: else
62: continue;
63: }
64: /* receive & decode packet */
65: if ((ret = recv(sock, msg.msg_base, msg.msg_len, 0)) == -1) {
66: LOGERR;
67: break;
68: }
69: if (!mqtt_readPINGRESP(&msg)) {
70: ret = 0; /* Host is alive */
71: break;
72: } else
73: ret = 2; /* Session is broken ... must be disconnect! */
74: }
75: end:
76: free(msg.msg_base);
77: return ret;
78: }
1.2.6.1 ! misho 79:
! 80: /*
! 81: * mqtt_WillMessage() - Publish WILL message
! 82: *
! 83: * @sock = connected socket
! 84: * @ka = keep alive timeout
! 85: * @topic = will topic
! 86: * @data = will message
! 87: * return: -1 error, 1 timeout, 2 not ack or 0 ok
! 88: */
! 89: int
! 90: mqtt_WillMessage(int sock, u_short ka, const char *topic, const char *data)
! 91: {
! 92: int ret = 0;
! 93: mqtt_msg_t msg = { NULL, 0 };
! 94:
! 95: if (!topic)
! 96: return -1; /* error */
! 97:
! 98: /* will message */
! 99: if ((ret = mqtt_wait4data(sock, ka, POLLOUT)))
! 100: return ret;
! 101: ret = mqtt_msgPUBLISH(&msg, topic, 0xDEAD, 0, 1, 0, data, data ? strlen(data) : 0);
! 102: if (ret == -1)
! 103: return -1; /* error */
! 104: if ((ret = send(sock, msg.msg_base, ret, MSG_NOSIGNAL)) == -1) {
! 105: LOGERR;
! 106: free(msg.msg_base);
! 107: return -1; /* error */
! 108: } else
! 109: memset(msg.msg_base, 0, msg.msg_len);
! 110:
! 111: /* will ack */
! 112: if ((ret = mqtt_wait4data(sock, ka, POLLIN | POLLPRI))) {
! 113: free(msg.msg_base);
! 114: return ret;
! 115: }
! 116: /* receive & decode packet */
! 117: if ((ret = recv(sock, msg.msg_base, msg.msg_len, 0)) == -1) {
! 118: LOGERR;
! 119: free(msg.msg_base);
! 120: return -1; /* error */
! 121: }
! 122: if (mqtt_readPUBACK(&msg))
! 123: ret = 0; /* ok */
! 124: else
! 125: ret = 2; /* semi-error */
! 126:
! 127: free(msg.msg_base);
! 128: return ret;
! 129: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>