Minor fixes to socket support when used with LWIP

- LWIP UDP sockets are always writable and they are not reported as
   such by select() (bug? featuyre?). TCP sockets are ok.
 - Always deliver MG_EV_POLL, whether we have other fd_events or not.
   Otherwise MG_EV_POLL would not be delivered at all to perma-writable
   UDP sockets (as they are on LWIP).
 - Ignore EAGAIN/EWOULDBLOCK errors while connecting, this happens
   during SSL handshake.

PUBLISHED_FROM=3b1ee0d2b0c672512da5c9d6141aba1112983426
This commit is contained in:
Deomid Ryabkov 2016-12-21 12:49:11 +00:00 committed by Cesanta Bot
parent 7782500dbb
commit 54ebcc5bd8

View File

@ -3163,7 +3163,8 @@ void mg_socket_if_connect_tcp(struct mg_connection *nc,
#endif #endif
rc = connect(nc->sock, &sa->sa, sizeof(sa->sin)); rc = connect(nc->sock, &sa->sa, sizeof(sa->sin));
nc->err = mg_is_error(rc) ? mg_get_errno() : 0; nc->err = mg_is_error(rc) ? mg_get_errno() : 0;
LOG(LL_INFO, ("%p sock %d err %d", nc, nc->sock, nc->err)); DBG(("%p sock %d rc %d errno %d err %d", nc, nc->sock, rc, mg_get_errno(),
nc->err));
} }
void mg_socket_if_connect_udp(struct mg_connection *nc) { void mg_socket_if_connect_udp(struct mg_connection *nc) {
@ -3482,8 +3483,7 @@ static void mg_ssl_begin(struct mg_connection *nc) {
void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now) { void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now) {
int worth_logging = int worth_logging =
fd_flags != 0 || (nc->flags & (MG_F_WANT_READ | MG_F_SSL_HANDSHAKE_DONE | fd_flags != 0 || (nc->flags & (MG_F_WANT_READ | MG_F_WANT_WRITE));
MG_F_WANT_WRITE));
if (worth_logging) { if (worth_logging) {
DBG(("%p fd=%d fd_flags=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock, DBG(("%p fd=%d fd_flags=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock,
fd_flags, nc->flags, (int) nc->recv_mbuf.len, fd_flags, nc->flags, (int) nc->recv_mbuf.len,
@ -3498,7 +3498,11 @@ void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now) {
socklen_t len = sizeof(err); socklen_t len = sizeof(err);
int ret = int ret =
getsockopt(nc->sock, SOL_SOCKET, SO_ERROR, (char *) &err, &len); getsockopt(nc->sock, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
if (ret != 0) err = 1; if (ret != 0) {
err = 1;
} else if (err == EAGAIN || err == EWOULDBLOCK) {
err = 0;
}
} }
#else #else
/* /*
@ -3541,10 +3545,7 @@ void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now) {
if ((fd_flags & _MG_F_FD_CAN_WRITE) && nc->send_mbuf.len > 0) { if ((fd_flags & _MG_F_FD_CAN_WRITE) && nc->send_mbuf.len > 0) {
mg_write_to_socket(nc); mg_write_to_socket(nc);
} }
mg_if_poll(nc, (time_t) now);
if (!(fd_flags & (_MG_F_FD_CAN_READ | _MG_F_FD_CAN_WRITE))) {
mg_if_poll(nc, (time_t) now);
}
mg_if_timer(nc, now); mg_if_timer(nc, now);
} }
@ -3721,8 +3722,10 @@ time_t mg_socket_if_poll(struct mg_iface *iface, int timeout_ms) {
(FD_ISSET(nc->sock, &err_set) ? _MG_F_FD_ERROR : 0); (FD_ISSET(nc->sock, &err_set) ? _MG_F_FD_ERROR : 0);
} }
#if MG_LWIP #if MG_LWIP
/* With LWIP socket emulation layer, we don't get write events */ /* With LWIP socket emulation layer, we don't get write events for UDP */
fd_flags |= _MG_F_FD_CAN_WRITE; if ((nc->flags & MG_F_UDP) && nc->listener == NULL) {
fd_flags |= _MG_F_FD_CAN_WRITE;
}
#endif #endif
} }
tmp = nc->next; tmp = nc->next;