mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-01 03:17:37 +08:00
test mqtt length's encoding
This commit is contained in:
parent
7270b2eb71
commit
9a9dae1ab8
4
lib_acl_cpp/samples/mqtt/mqtt_length/Makefile
Normal file
4
lib_acl_cpp/samples/mqtt/mqtt_length/Makefile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
all:
|
||||||
|
g++ -o test main.cpp
|
||||||
|
clean:
|
||||||
|
rm -f test
|
71
lib_acl_cpp/samples/mqtt/mqtt_length/main.cpp
Normal file
71
lib_acl_cpp/samples/mqtt/mqtt_length/main.cpp
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (C) 2015-2018 IQIYI
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* AUTHOR(S)
|
||||||
|
* Zheng Shuxin
|
||||||
|
* E-mail: zhengshuxin@qiyi.com
|
||||||
|
*
|
||||||
|
* VERSION
|
||||||
|
* Wed 24 Mar 2021 06:49:23 PM CST
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static unsigned decode(const unsigned char* buf, unsigned len) {
|
||||||
|
unsigned n = 0;
|
||||||
|
for (unsigned i = 0; i < len; i++) {
|
||||||
|
n |= (unsigned) ((buf[i] & 0x7f) << (7 * i));
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int encode(unsigned n, unsigned char *buf) {
|
||||||
|
int len = 0;
|
||||||
|
if (n < 128) {
|
||||||
|
buf[len++] = n & 0x7f;
|
||||||
|
} else if (n < 16384) {
|
||||||
|
buf[len++] = ((unsigned char) n & 0x7f) | 0x80;
|
||||||
|
buf[len++] = ((unsigned char) (n >> 7) & 0x7f);
|
||||||
|
} else if (n < 2097152) {
|
||||||
|
buf[len++] = ((unsigned char) n & 0x7f) | 0x80;
|
||||||
|
buf[len++] = ((unsigned char) (n >> 7 ) & 0x7f) | 0x80;
|
||||||
|
buf[len++] = ((unsigned char) (n >> 14) & 0x7f);
|
||||||
|
} else if (n < 268435456) {
|
||||||
|
buf[len++] = ((unsigned char) n & 0x7f) | 0x80;
|
||||||
|
buf[len++] = ((unsigned char) (n >> 7 ) & 0x7f) | 0x80;
|
||||||
|
buf[len++] = ((unsigned char) (n >> 14) & 0x7f) | 0x80;
|
||||||
|
buf[len++] = ((unsigned char) (n >> 21) & 0x7f);
|
||||||
|
} else {
|
||||||
|
printf("invalid dlen_=%u\n", n);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
unsigned char buf[4];
|
||||||
|
|
||||||
|
unsigned i, max= 268435455;
|
||||||
|
for (i = 0; i < max; i++) {
|
||||||
|
int len = encode(i, buf);
|
||||||
|
if (len == -1) {
|
||||||
|
printf("encode i=%u error\r\n", i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned n = decode(buf, (unsigned) len);
|
||||||
|
if (n != i) {
|
||||||
|
printf("decode error, n=%u, i=%u\n", n, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (i < 10) {
|
||||||
|
printf("ok, i=%u, n=%u\n", i, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("i=%u, max=%u, status=%s\r\n", i, max, i == max ? "ok" : "error");
|
||||||
|
return 0;
|
||||||
|
}
|
@ -24,7 +24,7 @@ static bool test_publish(acl::mqtt_client& conn, unsigned short id) {
|
|||||||
publish.set_topic(topic);
|
publish.set_topic(topic);
|
||||||
|
|
||||||
acl::string payload;
|
acl::string payload;
|
||||||
payload.format("payload-%d", id);
|
payload.format("payload-%ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", id);
|
||||||
publish.set_payload((unsigned) payload.size(), payload);
|
publish.set_payload((unsigned) payload.size(), payload);
|
||||||
|
|
||||||
if (!conn.send(publish)) {
|
if (!conn.send(publish)) {
|
||||||
|
@ -151,17 +151,17 @@ bool mqtt_header::build_header(string& out) {
|
|||||||
if (dlen_ < 128) {
|
if (dlen_ < 128) {
|
||||||
header[len++] = dlen_ & 0x7f;
|
header[len++] = dlen_ & 0x7f;
|
||||||
} else if (dlen_ < 16384) {
|
} else if (dlen_ < 16384) {
|
||||||
header[len++] = (unsigned char) dlen_ & 0x7f;
|
header[len++] = ((unsigned char) dlen_ & 0x7f) | 0x80;
|
||||||
header[len++] = ((unsigned char) (dlen_ >> 7) & 0x7f) | 0x80;
|
header[len++] = ((unsigned char) (dlen_ >> 7) & 0x7f);
|
||||||
} else if (dlen_ < 2097152) {
|
} else if (dlen_ < 2097152) {
|
||||||
header[len++] = (unsigned char) dlen_ & 0x7f;
|
header[len++] = ((unsigned char) dlen_ & 0x7f) | 0x80;
|
||||||
header[len++] = ((unsigned char) (dlen_ >> 7 ) & 0x7f) | 0x80;
|
header[len++] = ((unsigned char) (dlen_ >> 7 ) & 0x7f) | 0x80;
|
||||||
header[len++] = ((unsigned char)(dlen_ >> 14) & 0x7f) | 0x80;
|
header[len++] = ((unsigned char) (dlen_ >> 14) & 0x7f);
|
||||||
} else if (dlen_ < 268435456) {
|
} else if (dlen_ < 268435456) {
|
||||||
header[len++] = (unsigned char) dlen_ & 0x7f;
|
header[len++] = ((unsigned char) dlen_ & 0x7f) | 0x80;
|
||||||
header[len++] = ((unsigned char) (dlen_ >> 7 ) & 0x7f) | 0x80;
|
header[len++] = ((unsigned char) (dlen_ >> 7 ) & 0x7f) | 0x80;
|
||||||
header[len++] = ((unsigned char) (dlen_ >> 14) & 0x7f) | 0x80;
|
header[len++] = ((unsigned char) (dlen_ >> 14) & 0x7f) | 0x80;
|
||||||
header[len++] = ((unsigned char)(dlen_ >> 21) & 0x7f) | 0x80;
|
header[len++] = ((unsigned char) (dlen_ >> 21) & 0x7f);
|
||||||
} else {
|
} else {
|
||||||
logger_error("invalid dlen_=%u", dlen_);
|
logger_error("invalid dlen_=%u", dlen_);
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
Reference in New Issue
Block a user