Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close socket on error in NetConnect #375

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
embhorn marked this conversation as resolved.
Show resolved Hide resolved
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
Loading