Skip to content

Commit

Permalink
various networking fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Apr 7, 2022
1 parent ff3d209 commit e05c361
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
24 changes: 10 additions & 14 deletions esp_lwmqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ lwmqtt_err_t esp_lwmqtt_network_wait(esp_lwmqtt_network_t *n, bool *connected, u
FD_ZERO(&set);
FD_ZERO(&ex_set);
FD_SET(n->socket, &set);
FD_SET(n->socket, &ex_set);

// wait for data
struct timeval t = {.tv_sec = timeout / 1000, .tv_usec = (timeout % 1000) * 1000};
Expand Down Expand Up @@ -118,6 +119,7 @@ lwmqtt_err_t esp_lwmqtt_network_select(esp_lwmqtt_network_t *n, bool *available,
FD_ZERO(&set);
FD_ZERO(&ex_set);
FD_SET(n->socket, &set);
FD_SET(n->socket, &ex_set);

// wait for data
struct timeval t = {.tv_sec = timeout / 1000, .tv_usec = (timeout % 1000) * 1000};
Expand All @@ -132,22 +134,16 @@ lwmqtt_err_t esp_lwmqtt_network_select(esp_lwmqtt_network_t *n, bool *available,
return LWMQTT_SUCCESS;
}

lwmqtt_err_t esp_lwmqtt_network_peek(esp_lwmqtt_network_t *n, size_t *available, uint32_t timeout) {
// set timeout
struct timeval t = {.tv_sec = timeout / 1000, .tv_usec = (timeout % 1000) * 1000};
int rc = setsockopt(n->socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&t, sizeof(t));
if (rc < 0) {
return LWMQTT_NETWORK_FAILED_READ;
}

lwmqtt_err_t esp_lwmqtt_network_peek(esp_lwmqtt_network_t *n, size_t *available) {
// check if socket is valid
int ret = read(n->socket, NULL, 0);
if (ret < 0 && errno != EAGAIN) {
char buf;
int ret = recv(n->socket, &buf, 1, MSG_PEEK | MSG_DONTWAIT);
if (ret <= 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
return LWMQTT_NETWORK_FAILED_READ;
}

// get the available bytes on the socket
rc = ioctl(n->socket, FIONREAD, available);
int rc = ioctl(n->socket, FIONREAD, available);
if (rc < 0) {
return LWMQTT_NETWORK_FAILED_READ;
}
Expand All @@ -168,9 +164,9 @@ lwmqtt_err_t esp_lwmqtt_network_read(void *ref, uint8_t *buffer, size_t len, siz

// read from socket
int bytes = read(n->socket, buffer, len);
if (bytes < 0 && errno == EAGAIN) {
if (bytes < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
return LWMQTT_SUCCESS;
} else if (bytes < 0) {
} else if (bytes <= 0) {
return LWMQTT_NETWORK_FAILED_READ;
}

Expand All @@ -193,7 +189,7 @@ lwmqtt_err_t esp_lwmqtt_network_write(void *ref, uint8_t *buffer, size_t len, si

// write to socket
int bytes = write(n->socket, buffer, len);
if (bytes < 0 && errno == EAGAIN) {
if (bytes < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
return LWMQTT_SUCCESS;
} else if (bytes < 0) {
return LWMQTT_NETWORK_FAILED_WRITE;
Expand Down
2 changes: 1 addition & 1 deletion esp_lwmqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void esp_lwmqtt_network_disconnect(esp_lwmqtt_network_t *n);
/**
* Will set available to the available amount of data in the underlying network buffer.
*/
lwmqtt_err_t esp_lwmqtt_network_peek(esp_lwmqtt_network_t *n, size_t *available, uint32_t timeout);
lwmqtt_err_t esp_lwmqtt_network_peek(esp_lwmqtt_network_t *n, size_t *available);

/**
* Will wait for a socket until data is available or the timeout has been reached.
Expand Down
2 changes: 1 addition & 1 deletion esp_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ static void esp_mqtt_process() {
if (esp_mqtt_use_tls) {
err = esp_tls_lwmqtt_network_peek(&esp_mqtt_tls_network, &available_bytes, esp_mqtt_command_timeout);
} else {
err = esp_lwmqtt_network_peek(&esp_mqtt_network, &available_bytes, esp_mqtt_command_timeout);
err = esp_lwmqtt_network_peek(&esp_mqtt_network, &available_bytes);
}
#else
err = esp_lwmqtt_network_peek(&esp_mqtt_network, &available_bytes);
Expand Down
4 changes: 4 additions & 0 deletions esp_tls_lwmqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ lwmqtt_err_t esp_tls_lwmqtt_network_wait(esp_tls_lwmqtt_network_t *n, bool *conn
FD_ZERO(&set);
FD_ZERO(&ex_set);
FD_SET(n->socket.fd, &set);
FD_SET(n->socket.fd, &ex_set);

// wait for data
struct timeval t = {.tv_sec = timeout / 1000, .tv_usec = (timeout % 1000) * 1000};
Expand Down Expand Up @@ -153,6 +154,7 @@ lwmqtt_err_t esp_tls_lwmqtt_network_select(esp_tls_lwmqtt_network_t *n, bool *av
FD_ZERO(&set);
FD_ZERO(&ex_set);
FD_SET(n->socket.fd, &set);
FD_SET(n->socket.fd, &ex_set);

// wait for data
struct timeval t = {.tv_sec = timeout / 1000, .tv_usec = (timeout % 1000) * 1000};
Expand Down Expand Up @@ -182,6 +184,8 @@ lwmqtt_err_t esp_tls_lwmqtt_network_peek(esp_tls_lwmqtt_network_t *n, size_t *av
return LWMQTT_NETWORK_FAILED_READ;
}

// TODO: Directly peek on underlying socket?

// check if socket is valid
ret = mbedtls_ssl_read(&n->ssl, NULL, 0);
if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Expand Down

0 comments on commit e05c361

Please sign in to comment.