--- libaitmqtt/src/pub.c 2012/06/19 15:41:15 1.1.1.1.2.6 +++ libaitmqtt/src/pub.c 2012/06/28 09:01:42 1.2.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: pub.c,v 1.1.1.1.2.6 2012/06/19 15:41:15 misho Exp $ +* $Id: pub.c,v 1.2.2.2 2012/06/28 09:01:42 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -64,6 +64,7 @@ mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const cha u_char Dup, u_char QOS, u_char Retain, const void *pData, int datlen) { int len, siz; + u_int n, *l; struct mqtthdr *hdr; mqtthdr_var_t *topic; mqtt_len_t *mid; @@ -71,8 +72,6 @@ mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const cha if (!buf || !csTopic) return -1; - else - data = buf->msg_base; if (QOS > MQTT_QOS_EXACTLY) { mqtt_SetErr(EINVAL, "Invalid QoS parameter"); return -1; @@ -89,12 +88,15 @@ mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const cha /* calculate header size */ siz = sizeof(struct mqtthdr); /* mqtt fixed header */ - siz += mqtt_sizeLen(mqtt_encodeLen(len)) - 1; /* length size */ + n = mqtt_encodeLen(len); /* message size */ + siz += mqtt_sizeLen(n) - 1; /* length size */ if (mqtt_msgRealloc(buf, siz + len) == -1) return -1; - else + else { + data = buf->msg_base; hdr = (struct mqtthdr *) data; + } /* fixed header */ MQTTHDR_MSGINIT(hdr); @@ -102,7 +104,8 @@ mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const cha hdr->mqtt_msg.qos = QOS; hdr->mqtt_msg.dup = Dup ? 1 : 0; hdr->mqtt_msg.retain = Retain ? 1 : 0; - *hdr->mqtt_len = mqtt_encodeLen(len); + l = (u_int*) hdr->mqtt_len; + *l = n; data += siz; /* variable header */ @@ -125,7 +128,7 @@ mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const cha static int _mqtt_msgPUB_(mqtt_msg_t * __restrict buf, u_char cmd, u_short msgID) { - int siz = 0; + int siz = sizeof(struct mqtthdr); struct mqtthdr *hdr; mqtt_len_t *v; @@ -134,12 +137,8 @@ _mqtt_msgPUB_(mqtt_msg_t * __restrict buf, u_char cmd, if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr) + sizeof(mqtt_len_t)) == -1) return -1; - else { - hdr = (struct mqtthdr *) (buf->msg_base + siz); - siz += sizeof(struct mqtthdr); - v = (mqtt_len_t*) (buf->msg_base + siz); - siz += sizeof(mqtt_len_t); - } + else + hdr = (struct mqtthdr *) buf->msg_base; /* fixed header */ MQTTHDR_MSGINIT(hdr); @@ -147,7 +146,9 @@ _mqtt_msgPUB_(mqtt_msg_t * __restrict buf, u_char cmd, *hdr->mqtt_len = sizeof(mqtt_len_t); /* MessageID */ + v = (mqtt_len_t*) (buf->msg_base + siz); v->val = htons(msgID); + siz += sizeof(mqtt_len_t); return siz; }