Skip to content

Commit b409fa0

Browse files
committed
Reuse socket on wolfMQTT async connect retry
1 parent 856818c commit b409fa0

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/port/wolfmqtt_io.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static struct wolfmqtt_io_desc *io_desc_alloc(void)
4545
if (!io_descs[i].in_use) {
4646
io_descs[i].in_use = 1;
4747
io_descs[i].connected = 0;
48+
io_descs[i].fd = -1;
4849
return &io_descs[i];
4950
}
5051
}
@@ -75,10 +76,12 @@ static int wolfmqtt_net_connect(void *context, const char *host, word16 port,
7576
return MQTT_CODE_ERROR_BAD_ARG;
7677
}
7778

78-
/* Create TCP socket */
79-
desc->fd = wolfIP_sock_socket(desc->stack, AF_INET, IPSTACK_SOCK_STREAM, 0);
79+
/* Create the socket only on the first attempt; async retries reuse it. */
8080
if (desc->fd < 0) {
81-
return MQTT_CODE_ERROR_NETWORK;
81+
desc->fd = wolfIP_sock_socket(desc->stack, AF_INET, IPSTACK_SOCK_STREAM, 0);
82+
if (desc->fd < 0) {
83+
return MQTT_CODE_ERROR_NETWORK;
84+
}
8285
}
8386

8487
/* Set up address */

0 commit comments

Comments
 (0)