Skip to content

Commit 2c4f12c

Browse files
Jindrich Dolezy256dpi
authored andcommitted
Add early detection of closed stream in lwmqtt_arduino_network_read
1 parent 2ba5912 commit 2c4f12c

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/MQTTClient.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,24 @@ inline lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buffer, size
3030
auto n = (lwmqtt_arduino_network_t *)ref;
3131

3232
// set timeout
33-
n->client->setTimeout(timeout);
33+
uint32_t start = millis();
3434

3535
// read bytes
36-
*read = n->client->readBytes(buffer, len);
37-
if (*read <= 0) {
38-
return LWMQTT_NETWORK_FAILED_READ;
36+
*read = 0;
37+
while (len && (millis() - start < timeout)) {
38+
int r = n->client->read(buffer, len);
39+
if (r > 0) { // read some bytes, advance buffer
40+
buffer += r;
41+
*read += r;
42+
len -= r;
43+
44+
} else if (r < 0) { // negative result - network failure
45+
return LWMQTT_NETWORK_FAILED_READ;
46+
}
47+
}
48+
49+
if (*read == 0) {
50+
return LWMQTT_NETWORK_TIMEOUT;
3951
}
4052

4153
return LWMQTT_SUCCESS;

0 commit comments

Comments
 (0)