Skip to content

Commit 29b62eb

Browse files
committed
Close socket on error in NetCOnnect
1 parent 53d541f commit 29b62eb

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

examples/mqttnet.c

+34-33
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,20 @@ static int NetDisconnect(void *context)
219219
/* -------------------------------------------------------------------------- */
220220
#elif defined(MICROCHIP_MPLAB_HARMONY)
221221

222+
static int NetDisconnect(void *context)
223+
{
224+
SocketContext *sock = (SocketContext*)context;
225+
if (sock) {
226+
if (sock->fd != SOCKET_INVALID) {
227+
closesocket(sock->fd);
228+
sock->fd = SOCKET_INVALID;
229+
}
230+
231+
sock->stat = SOCK_BEGIN;
232+
}
233+
return 0;
234+
}
235+
222236
static int NetConnect(void *context, const char* host, word16 port,
223237
int timeout_ms)
224238
{
@@ -287,6 +301,7 @@ static int NetConnect(void *context, const char* host, word16 port,
287301
if (errno == EINPROGRESS || errno == EWOULDBLOCK) {
288302
return MQTT_CODE_CONTINUE;
289303
}
304+
NetDisconnect(context);
290305

291306
/* Show error */
292307
PRINTF("NetConnect: Rc=%d, ErrNo=%d", rc, errno);
@@ -358,20 +373,6 @@ static int NetRead(void *context, byte* buf, int buf_len,
358373
return rc;
359374
}
360375

361-
static int NetDisconnect(void *context)
362-
{
363-
SocketContext *sock = (SocketContext*)context;
364-
if (sock) {
365-
if (sock->fd != SOCKET_INVALID) {
366-
closesocket(sock->fd);
367-
sock->fd = SOCKET_INVALID;
368-
}
369-
370-
sock->stat = SOCK_BEGIN;
371-
}
372-
return 0;
373-
}
374-
375376
/* -------------------------------------------------------------------------- */
376377
/* GENERIC BSD SOCKET TCP NETWORK CALLBACK EXAMPLE */
377378
/* -------------------------------------------------------------------------- */
@@ -410,6 +411,20 @@ static void tcp_set_nonblocking(SOCKET_T* sockfd)
410411
#endif /* WOLFMQTT_NONBLOCK */
411412
#endif /* !WOLFMQTT_NO_TIMEOUT */
412413

414+
static int NetDisconnect(void *context)
415+
{
416+
SocketContext *sock = (SocketContext*)context;
417+
if (sock) {
418+
if (sock->fd != SOCKET_INVALID) {
419+
SOCK_CLOSE(sock->fd);
420+
sock->fd = -1;
421+
}
422+
423+
sock->stat = SOCK_BEGIN;
424+
}
425+
return 0;
426+
}
427+
413428
static int NetConnect(void *context, const char* host, word16 port,
414429
int timeout_ms)
415430
{
@@ -539,9 +554,9 @@ static int NetConnect(void *context, const char* host, word16 port,
539554
(void)timeout_ms;
540555

541556
exit:
542-
/* Show error */
543-
if (rc != 0) {
544-
PRINTF("NetConnect: Rc=%d, SoErr=%d", rc, so_error);
557+
if ((rc != 0) && (rc != MQTT_CODE_CONTINUE)) {
558+
NetDisconnect(context);
559+
PRINTF("NetConnect: Rc=%d, SoErr=%d", rc, so_error); /* Show error */
545560
}
546561

547562
return rc;
@@ -630,8 +645,8 @@ static int SN_NetConnect(void *context, const char* host, word16 port,
630645

631646
exit:
632647
/* Show error */
633-
if (rc != 0) {
634-
SOCK_CLOSE(sock->fd);
648+
if ((rc != 0) && (rc != MQTT_CODE_CONTINUE)) {
649+
NetDisconnect(context);
635650
PRINTF("NetConnect: Rc=%d, SoErr=%d", rc, so_error);
636651
}
637652

@@ -873,20 +888,6 @@ static int NetPeek(void *context, byte* buf, int buf_len, int timeout_ms)
873888
}
874889
#endif
875890

876-
static int NetDisconnect(void *context)
877-
{
878-
SocketContext *sock = (SocketContext*)context;
879-
if (sock) {
880-
if (sock->fd != SOCKET_INVALID) {
881-
SOCK_CLOSE(sock->fd);
882-
sock->fd = -1;
883-
}
884-
885-
sock->stat = SOCK_BEGIN;
886-
}
887-
return 0;
888-
}
889-
890891
#endif
891892

892893

0 commit comments

Comments
 (0)