优化实现

master
Matthew 10 months ago
parent 9b85d41dca
commit d231dd62b3

@ -30,8 +30,13 @@
#include <netdb.h> #include <netdb.h>
#include <sys/socket.h> #include <sys/socket.h>
#else #else
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <lwip/netdb.h> #include <lwip/netdb.h>
#include <lwip/sockets.h> #include <lwip/sockets.h>
#endif
#endif /* RT_USING_SAL */ #endif /* RT_USING_SAL */
#define DBG_ENABLE #define DBG_ENABLE
@ -42,7 +47,9 @@
#define DBG_LEVEL DBG_INFO #define DBG_LEVEL DBG_INFO
#endif /* WEBCLIENT_DEBUG */ #endif /* WEBCLIENT_DEBUG */
#define DBG_COLOR #define DBG_COLOR
#ifndef _WIN32
#include <rtdbg.h> #include <rtdbg.h>
#endif
/* default receive or send timeout */ /* default receive or send timeout */
#define WEBCLIENT_DEFAULT_TIMEO 6 #define WEBCLIENT_DEFAULT_TIMEO 6
@ -123,8 +130,26 @@ static int webclient_read_line(struct webclient_session *session, char *buffer,
continue; continue;
} }
#endif #endif
if (rc <= 0)
return rc; if (rc < 0)
{
#ifdef _WIN32
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK || err == WSATRY_AGAIN || err == WSAEINTR || err == WSAETIMEDOUT)
#else
int err = errno;
if (err == EWOULDBLOCK || err == EAGAIN || err == EINTR)
#endif
{
continue;
}
return rc;
}
else if (rc == 0)
{
return rc;
}
if (ch == '\n' && last_ch == '\r') if (ch == '\n' && last_ch == '\r')
break; break;
@ -377,7 +402,7 @@ static int webclient_open_tls(struct webclient_session *session, const char *URI
* @return <0: connect failed or other error * @return <0: connect failed or other error
* =0: connect success * =0: connect success
*/ */
static int webclient_connect(struct webclient_session *session, const char *URI) int webclient_connect(struct webclient_session *session, const char *URI)
{ {
int rc = WEBCLIENT_OK; int rc = WEBCLIENT_OK;
int socket_handle; int socket_handle;
@ -629,7 +654,7 @@ int webclient_content_length_get(struct webclient_session *session)
return session->content_length; return session->content_length;
} }
static int webclient_send_header(struct webclient_session *session, int method) int webclient_send_header(struct webclient_session *session, int method)
{ {
int rc = WEBCLIENT_OK; int rc = WEBCLIENT_OK;
char *header = RT_NULL; char *header = RT_NULL;
@ -1327,7 +1352,13 @@ int webclient_read(struct webclient_session *session, void *buffer, size_t lengt
bytes_read = webclient_recv(session, buffer, length, 0); bytes_read = webclient_recv(session, buffer, length, 0);
if (bytes_read <= 0) if (bytes_read <= 0)
{ {
if (errno == EWOULDBLOCK || errno == EAGAIN) #ifdef _WIN32
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK || err == WSATRY_AGAIN || err == WSAEINTR || err == WSAETIMEDOUT)
#else
int err = errno;
if (err == EWOULDBLOCK || err == EAGAIN || err == EINTR)
#endif
{ {
/* recv timeout */ /* recv timeout */
return -WEBCLIENT_TIMEOUT; return -WEBCLIENT_TIMEOUT;
@ -1382,19 +1413,25 @@ int webclient_read(struct webclient_session *session, void *buffer, size_t lengt
#endif #endif
LOG_D("receive data error(%d).", bytes_read); LOG_D("receive data error(%d).", bytes_read);
#ifdef _WIN32
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK || err == WSATRY_AGAIN || err == WSAEINTR || err == WSAETIMEDOUT)
#else
int err = errno;
if (err == EWOULDBLOCK || err == EAGAIN || err == EINTR)
#endif
{
/* recv timeout */
LOG_E("receive data timeout.");
continue;
}
if (total_read) if (total_read)
{ {
break; break;
} }
else else
{ {
if (errno == EWOULDBLOCK || errno == EAGAIN)
{
/* recv timeout */
LOG_E("receive data timeout.");
return -WEBCLIENT_TIMEOUT;
}
else
{ {
closesocket(session->socket); closesocket(session->socket);
session->socket = -1; session->socket = -1;
@ -1458,7 +1495,13 @@ int webclient_write(struct webclient_session *session, const void *buffer, size_
continue; continue;
} }
#endif #endif
if (errno == EWOULDBLOCK || errno == EAGAIN) #ifdef _WIN32
int err = WSAGetLastError();
if (err == WSAEWOULDBLOCK || err == WSATRY_AGAIN || err == WSAEINTR || err == WSAETIMEDOUT)
#else
int err = errno;
if (err == EWOULDBLOCK || err == EAGAIN || err == EINTR)
#endif
{ {
/* send timeout */ /* send timeout */
if (total_write) if (total_write)

Loading…
Cancel
Save