diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2017-03-18 09:29:19 +0200 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2017-03-18 09:29:19 +0200 |
| commit | 7aeef53b089272f4633cc40512296bfd884a58d4 (patch) | |
| tree | 894753ced0495f725ad8362859f88d5b61e29eb7 /src/stream_mqtt.c | |
| parent | e9958e8a0f5aa5fbe0a4a03be42b8bf640add6f7 (diff) | |
| parent | 2c76b0da9e0aba2211d5b4a8e51c79e47ad9b6c8 (diff) | |
| download | mqtt-7aeef53b089272f4633cc40512296bfd884a58d4.tar.gz mqtt-7aeef53b089272f4633cc40512296bfd884a58d4.zip | |
Merge branch 'the-great-refactor'v0.5
* the-great-refactor:
Add big_message_test
Fix publish message serialization
Modify the code to use nonblocking sockets
Fix indentation
Free userName and password in MqttClientFree()
Add forgotten files
Massive refactoring of the internals
Diffstat (limited to 'src/stream_mqtt.c')
| -rw-r--r-- | src/stream_mqtt.c | 30 |
1 files changed, 16 insertions, 14 deletions
diff --git a/src/stream_mqtt.c b/src/stream_mqtt.c index 3864ef3..f2bd9cd 100644 --- a/src/stream_mqtt.c +++ b/src/stream_mqtt.c @@ -42,37 +42,39 @@ int64_t StreamWriteMqttString(const_bstring buf, Stream *stream) return 2 + blength(buf); } -int64_t StreamReadRemainingLength(size_t *remainingLength, Stream *stream) +int64_t StreamReadRemainingLength(size_t *remainingLength, size_t *mul, + Stream *stream) { - size_t multiplier = 1; unsigned char encodedByte; - *remainingLength = 0; do { if (StreamRead(&encodedByte, 1, stream) != 1) return -1; - *remainingLength += (encodedByte & 127) * multiplier; - if (multiplier > 128*128*128) + *remainingLength += (encodedByte & 127) * (*mul); + if ((*mul) > 128*128*128) return -1; - multiplier *= 128; + (*mul) *= 128; } while ((encodedByte & 128) != 0); + *mul = 0; return 0; } -int64_t StreamWriteRemainingLength(size_t remainingLength, Stream *stream) +int64_t StreamWriteRemainingLength(size_t *remainingLength, Stream *stream) { - size_t nbytes = 0; do { - unsigned char encodedByte = remainingLength % 128; - remainingLength /= 128; - if (remainingLength > 0) + size_t tmp = *remainingLength; + unsigned char encodedByte = tmp % 128; + tmp /= 128; + if (tmp > 0) encodedByte |= 128; if (StreamWrite(&encodedByte, 1, stream) != 1) + { return -1; - ++nbytes; + } + *remainingLength = tmp; } - while (remainingLength > 0); - return nbytes; + while (*remainingLength > 0); + return 0; } |
