Annotation of libaitmqtt/src/cmds.c, revision 1.1.2.2
1.1.2.1 misho 1: #include "global.h"
2:
3:
1.1.2.2 ! misho 4: static inline int
! 5: _wait4data(int sock, u_short ka, short events)
! 6: {
! 7: int ret = 0;
! 8: struct pollfd pfd;
! 9:
! 10: if (sock < 3)
! 11: return -1; /* error */
! 12:
! 13: pfd.fd = sock;
! 14: pfd.events = POLLOUT;
! 15: if ((ret = poll(&pfd, 1, ka * 1000)) == -1 ||
! 16: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
! 17: LOGERR;
! 18: return -1; /* error */
! 19: } else if (!ret)
! 20: return 1; /* timeout */
! 21:
! 22: return 0; /* ready */
! 23: }
! 24:
! 25:
! 26: /*
! 27: * mqtt_KeepAlive() - Keep Alive check routine
! 28: *
! 29: * @sock = connected socket
! 30: * @ka = keep alive timeout
! 31: * @tries = tries for receive correct ping response, usually ==1
! 32: * return: -1 error, 0 host is alive, 1 timeout session or 2 broken session
! 33: */
! 34: int
! 35: mqtt_KeepAlive(int sock, u_short ka, u_char tries)
! 36: {
! 37: int ret = 0;
! 38: mqtt_msg_t msg = { NULL, 0 };
! 39:
! 40: if (sock < 3)
! 41: return -1; /* error */
! 42:
! 43: if ((ret = _wait4data(sock, ka, POLLOUT)))
! 44: return ret;
! 45: /* ping request */
! 46: if ((ret = mqtt_msgPINGREQ(&msg)) == -1)
! 47: return -1; /* error */
! 48: if ((ret = send(sock, msg.msg_base, ret, MSG_NOSIGNAL)) == -1) {
! 49: LOGERR;
! 50: goto end;
! 51: }
! 52:
! 53: while (tries--) {
! 54: if ((ret = _wait4data(sock, ka, POLLIN | POLLPRI))) {
! 55: if (ret == -1)
! 56: break;
! 57: else
! 58: continue;
! 59: }
! 60: /* receive & decode packet */
! 61: if ((ret = recv(sock, msg.msg_base, msg.msg_len, 0)) == -1) {
! 62: LOGERR;
! 63: break;
! 64: }
! 65: if (!mqtt_readPINGRESP(&msg)) {
! 66: ret = 0; /* Host is alive */
! 67: break;
! 68: } else
! 69: ret = 2; /* Session is broken ... must be disconnect! */
! 70: }
! 71: end:
! 72: free(msg.msg_base);
! 73: return ret;
! 74: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>