Annotation of libaitmqtt/src/cmds.c, revision 1.1.2.3
1.1.2.1 misho 1: #include "global.h"
2:
3:
1.1.2.3 ! misho 4: #pragma GCC visibility push(hidden)
! 5:
! 6: inline int
! 7: mqtt_wait4data(int sock, u_short ka, short events)
1.1.2.2 misho 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:
1.1.2.3 ! misho 27: #pragma GCC visibility pop
! 28:
1.1.2.2 misho 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:
1.1.2.3 ! misho 47: if ((ret = mqtt_wait4data(sock, ka, POLLOUT)))
1.1.2.2 misho 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--) {
1.1.2.3 ! misho 58: if ((ret = mqtt_wait4data(sock, ka, POLLIN | POLLPRI))) {
1.1.2.2 misho 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: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>