fix(transport): Fix a bug of the connection whether be active or not by timeout option when the select function return a correct value.

This commit is contained in:
Liu Han 2019-07-24 19:22:00 +08:00 committed by David Cermak
parent 8246bfa8fb
commit 67be0ae585
3 changed files with 7 additions and 4 deletions

View File

@ -117,6 +117,7 @@ static int esp_tcp_connect(const char *host, int hostlen, int port, int *sockfd,
struct timeval tv;
ms_to_timeval(cfg->timeout_ms, &tv);
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
}
if (cfg->non_block) {
int flags = fcntl(fd, F_GETFL, 0);

View File

@ -74,6 +74,7 @@ static int ssl_connect(esp_transport_handle_t t, const char *host, int port, int
ESP_LOGE(TAG, "Failed to open a new connection");
return -1;
}
return 0;
}
@ -95,7 +96,7 @@ static int ssl_poll_read(esp_transport_handle_t t, int timeout_ms)
int sock_errno = 0;
uint32_t optlen = sizeof(sock_errno);
getsockopt(ssl->tls->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
ESP_LOGE(TAG, "Poll read catch a error, errno = %s, fd = %d", strerror(sock_errno), ssl->tls->sockfd);
ESP_LOGE(TAG, "ssl_poll_read select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), ssl->tls->sockfd);
ret = -1;
}
return ret;
@ -118,7 +119,7 @@ static int ssl_poll_write(esp_transport_handle_t t, int timeout_ms)
int sock_errno = 0;
uint32_t optlen = sizeof(sock_errno);
getsockopt(ssl->tls->sockfd, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
ESP_LOGE(TAG, "Poll write catch a error, errno = %s, fd = %d", strerror(sock_errno), ssl->tls->sockfd);
ESP_LOGE(TAG, "ssl_poll_write select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), ssl->tls->sockfd);
ret = -1;
}
return ret;

View File

@ -77,6 +77,7 @@ static int tcp_connect(esp_transport_handle_t t, const char *host, int port, int
esp_transport_utils_ms_to_timeval(timeout_ms, &tv);
setsockopt(tcp->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(tcp->sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
ESP_LOGD(TAG, "[sock=%d],connecting to server IP:%s,Port:%d...",
tcp->sock, ipaddr_ntoa((const ip_addr_t*)&remote_ip.sin_addr.s_addr), port);
@ -129,7 +130,7 @@ static int tcp_poll_read(esp_transport_handle_t t, int timeout_ms)
int sock_errno = 0;
uint32_t optlen = sizeof(sock_errno);
getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
ESP_LOGE(TAG, "Poll read catch a error, errno = %s, fd = %d", strerror(sock_errno), tcp->sock);
ESP_LOGE(TAG, "tcp_poll_read select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), tcp->sock);
ret = -1;
}
return ret;
@ -152,7 +153,7 @@ static int tcp_poll_write(esp_transport_handle_t t, int timeout_ms)
int sock_errno = 0;
uint32_t optlen = sizeof(sock_errno);
getsockopt(tcp->sock, SOL_SOCKET, SO_ERROR, &sock_errno, &optlen);
ESP_LOGE(TAG, "Poll write catch a error, errno = %s, fd = %d", strerror(sock_errno), tcp->sock);
ESP_LOGE(TAG, "tcp_poll_write select error %d, errno = %s, fd = %d", sock_errno, strerror(sock_errno), tcp->sock);
ret = -1;
}
return ret;