--- libaitmqtt/src/aitmqtt.c 2012/04/26 15:38:32 1.1.1.1.2.8 +++ libaitmqtt/src/aitmqtt.c 2012/05/05 13:10:24 1.1.1.1.2.14 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitmqtt.c,v 1.1.1.1.2.8 2012/04/26 15:38:32 misho Exp $ +* $Id: aitmqtt.c,v 1.1.1.1.2.14 2012/05/05 13:10:24 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -367,20 +367,87 @@ mqtt_subAlloc(u_short num) * return: NULL error or subscribe array, after use must call mqtt_subFree() */ inline mqtt_subscr_t * -mqtt_subRealloc(mqtt_subscr_t * __restrict subs, u_short num) +mqtt_subRealloc(mqtt_subscr_t ** __restrict subs, u_short num) { mqtt_subscr_t *s = NULL; - s = realloc(subs, (num + 1) * sizeof(mqtt_subscr_t)); + if (!subs) + return NULL; + + s = realloc(*subs, (num + 1) * sizeof(mqtt_subscr_t)); if (!s) { LOGERR; return NULL; + } else { + memset(s + num, 0, sizeof(mqtt_subscr_t)); + *subs = s; } - return s; + return *subs; } /* + * mqtt_subCopy() - Copy subscription structure to another one + * + * @dst = destination subscription + * @src = source subscription + * return: =NULL error or !=NULL successful copied a structure + */ +inline mqtt_subscr_t * +mqtt_subCopy(mqtt_subscr_t * __restrict dst, mqtt_subscr_t * __restrict src) +{ + if (!dst || !src) + return NULL; + + if (src->sub_topic.msg_base) { + dst->sub_topic.msg_base = malloc(src->sub_topic.msg_len + 1); + if (!dst->sub_topic.msg_base) { + LOGERR; + memset(dst, 0, sizeof(mqtt_subscr_t)); + return NULL; + } else { + dst->sub_topic.msg_len = src->sub_topic.msg_len; + ((char*) dst->sub_topic.msg_base)[dst->sub_topic.msg_len] = 0; + memcpy(dst->sub_topic.msg_base, src->sub_topic.msg_base, + dst->sub_topic.msg_len); + } + } else { + /* + if (dst->sub_topic.msg_base) + free(dst->sub_topic.msg_base); + */ + dst->sub_topic.msg_base = NULL; + dst->sub_topic.msg_len = 0; + } + if (src->sub_value.msg_base) { + dst->sub_value.msg_base = malloc(src->sub_value.msg_len + 1); + if (!dst->sub_value.msg_base) { + LOGERR; + if (dst->sub_topic.msg_base) + free(dst->sub_topic.msg_base); + memset(dst, 0, sizeof(mqtt_subscr_t)); + return NULL; + } else { + dst->sub_value.msg_len = src->sub_value.msg_len; + ((char*) dst->sub_value.msg_base)[dst->sub_value.msg_len] = 0; + memcpy(dst->sub_value.msg_base, src->sub_value.msg_base, + dst->sub_value.msg_len); + } + } else { + /* + if (dst->sub_value.msg_base) + free(dst->sub_value.msg_base); + */ + dst->sub_value.msg_base = NULL; + dst->sub_value.msg_len = 0; + } + + dst->sub_ret = src->sub_ret; + return dst; +} + + +/* * mqtt_expandTopic() - Expanding topic to regular expression * * @csInput = Input topic @@ -537,66 +604,5 @@ mqtt_sqlTopic(const char *csInput, char * __restrict p *s = *pos; } - return ret; -} - - -/* - * mqtt_KeepAlive() - Keep Alive check routine - * - * @sock = connected socket - * @ka = keep alive timeout - * @tries = tries for receive correct ping response, usually ==1 - * return: -1 error, 0 host is alive, 1 timeout session or 2 broken session - */ -int -mqtt_KeepAlive(int sock, u_short ka, u_char tries) -{ - int ret = 0; - struct pollfd pfd; - mqtt_msg_t msg = { NULL, 0 }; - - if (sock < 3) - return -1; /* error */ - - pfd.fd = sock; - pfd.events = POLLOUT; - if ((ret = poll(&pfd, 1, ka * 1000)) == -1 || - pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { - LOGERR; - return -1; /* error */ - } else if (!ret) - return 1; /* session is abandoned ... must be disconnect! */ - /* ping request */ - if ((ret = mqtt_msgPINGREQ(&msg)) == -1) - return -1; /* error */ - if ((ret = send(sock, msg.msg_base, ret, MSG_NOSIGNAL)) == -1) { - LOGERR; - goto end; - } - - pfd.events = POLLIN | POLLPRI; - while (tries--) { - if ((ret = poll(&pfd, 1, ka * 1000)) == -1 || - pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) { - LOGERR; - break; - } else if (!ret) { - ret = 1; /* session is abandoned ... must be disconnect! */ - continue; - } - /* receive & decode packet */ - if ((ret = recv(sock, msg.msg_base, msg.msg_len, 0)) == -1) { - LOGERR; - break; - } - if (!mqtt_readPINGRESP(&msg)) { - ret = 0; /* Host is alive */ - break; - } else - ret = 2; /* Session is broken ... must be disconnect! */ - } -end: - free(msg.msg_base); return ret; }