File:  [ELWIX - Embedded LightWeight unIX -] / libaitmqtt / src / cmds.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Wed Jun 20 15:02:24 2012 UTC (12 years ago) by misho
Branches: MAIN
CVS tags: mqtt1_4, mqtt1_3, mqtt1_2, MQTT1_3, MQTT1_2, MQTT1_1, HEAD
version 1.1

    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: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>