Skip to content

Commit

Permalink
Merge pull request #375 from embhorn/zd17036
Browse files Browse the repository at this point in the history
Close socket on error in NetConnect
  • Loading branch information
dgarske authored Nov 27, 2023
2 parents 53d541f + 4bdde13 commit fef107f
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions examples/mqttnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,20 @@ static int NetDisconnect(void *context)
/* -------------------------------------------------------------------------- */
#elif defined(MICROCHIP_MPLAB_HARMONY)

static int NetDisconnect(void *context)
{
SocketContext *sock = (SocketContext*)context;
if (sock) {
if (sock->fd != SOCKET_INVALID) {
closesocket(sock->fd);
sock->fd = SOCKET_INVALID;
}

sock->stat = SOCK_BEGIN;
}
return 0;
}

static int NetConnect(void *context, const char* host, word16 port,
int timeout_ms)
{
Expand Down Expand Up @@ -287,6 +301,7 @@ static int NetConnect(void *context, const char* host, word16 port,
if (errno == EINPROGRESS || errno == EWOULDBLOCK) {
return MQTT_CODE_CONTINUE;
}
NetDisconnect(context);

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

static int NetDisconnect(void *context)
{
SocketContext *sock = (SocketContext*)context;
if (sock) {
if (sock->fd != SOCKET_INVALID) {
closesocket(sock->fd);
sock->fd = SOCKET_INVALID;
}

sock->stat = SOCK_BEGIN;
}
return 0;
}

/* -------------------------------------------------------------------------- */
/* GENERIC BSD SOCKET TCP NETWORK CALLBACK EXAMPLE */
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -410,6 +411,20 @@ static void tcp_set_nonblocking(SOCKET_T* sockfd)
#endif /* WOLFMQTT_NONBLOCK */
#endif /* !WOLFMQTT_NO_TIMEOUT */

static int NetDisconnect(void *context)
{
SocketContext *sock = (SocketContext*)context;
if (sock) {
if (sock->fd != SOCKET_INVALID) {
SOCK_CLOSE(sock->fd);
sock->fd = SOCKET_INVALID;
}

sock->stat = SOCK_BEGIN;
}
return 0;
}

static int NetConnect(void *context, const char* host, word16 port,
int timeout_ms)
{
Expand Down Expand Up @@ -539,9 +554,9 @@ static int NetConnect(void *context, const char* host, word16 port,
(void)timeout_ms;

exit:
/* Show error */
if (rc != 0) {
PRINTF("NetConnect: Rc=%d, SoErr=%d", rc, so_error);
if ((rc != 0) && (rc != MQTT_CODE_CONTINUE)) {
NetDisconnect(context);
PRINTF("NetConnect: Rc=%d, SoErr=%d", rc, so_error); /* Show error */
}

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

exit:
/* Show error */
if (rc != 0) {
SOCK_CLOSE(sock->fd);
if ((rc != 0) && (rc != MQTT_CODE_CONTINUE)) {
NetDisconnect(context);
PRINTF("NetConnect: Rc=%d, SoErr=%d", rc, so_error);
}

Expand Down Expand Up @@ -873,20 +888,6 @@ static int NetPeek(void *context, byte* buf, int buf_len, int timeout_ms)
}
#endif

static int NetDisconnect(void *context)
{
SocketContext *sock = (SocketContext*)context;
if (sock) {
if (sock->fd != SOCKET_INVALID) {
SOCK_CLOSE(sock->fd);
sock->fd = -1;
}

sock->stat = SOCK_BEGIN;
}
return 0;
}

#endif


Expand Down

0 comments on commit fef107f

Please sign in to comment.