win32 more build fixes
[platform/upstream/libwebsockets.git] / lib / lws-plat-win.c
index 6d731fc..842423b 100644 (file)
@@ -31,7 +31,10 @@ time_in_microseconds()
 time_t time(time_t *t)
 {
        time_t ret = time_in_microseconds() / 1000000;
-       *t = ret;
+
+       if(t != NULL)
+               *t = ret;
+
        return ret;
 }
 #endif
@@ -171,28 +174,33 @@ lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
                memset(&_lws, 0, sizeof(_lws));
                _lws.context = context;
 
-               context->service_tid_detected = context->protocols[0].callback(
-                       &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
+               context->service_tid_detected = context->vhost_list->
+                       protocols[0].callback(&_lws, LWS_CALLBACK_GET_THREAD_ID,
+                                             NULL, NULL, 0);
        }
        context->service_tid = context->service_tid_detected;
 
+       if (timeout_ms < 0)
+               goto faked_service;
+
        for (i = 0; i < pt->fds_count; ++i) {
                pfd = &pt->fds[i];
-               if (pfd->fd == pt->lserv_fd)
+
+               if (!(pfd->events & LWS_POLLOUT))
                        continue;
 
-               if (pfd->events & LWS_POLLOUT) {
-                       wsi = wsi_from_fd(context, pfd->fd);
-                       if (!wsi || wsi->sock_send_blocking)
-                               continue;
-                       pfd->revents = LWS_POLLOUT;
-                       n = lws_service_fd(context, pfd);
-                       if (n < 0)
-                               return -1;
-                       /* if something closed, retry this slot */
-                       if (n)
-                               i--;
-               }
+               wsi = wsi_from_fd(context, pfd->fd);
+               if (wsi->listener)
+                       continue;
+               if (!wsi || wsi->sock_send_blocking)
+                       continue;
+               pfd->revents = LWS_POLLOUT;
+               n = lws_service_fd(context, pfd);
+               if (n < 0)
+                       return -1;
+               /* if something closed, retry this slot */
+               if (n)
+                       i--;
        }
 
        /* if we know something needs service already, don't wait in poll */
@@ -234,6 +242,8 @@ lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
                        wsi->sock_send_blocking = 0;
        }
 
+faked_service:
+
        /* if someone faked their LWS_POLLIN, then go through all active fds */
 
        if (lws_service_flag_pending(context, tsi)) {
@@ -252,6 +262,9 @@ lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
                return 0;
        }
 
+       if (timeout_ms < 0)
+               return 0;
+
        /* otherwise just do the one... must be a way to improve that... */
 
        return lws_service_fd_tsi(context, pfd, tsi);
@@ -264,16 +277,19 @@ lws_plat_service(struct lws_context *context, int timeout_ms)
 }
 
 LWS_VISIBLE int
-lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd)
+lws_plat_set_socket_options(struct lws_vhost *vhost, lws_sockfd_type fd)
 {
        int optval = 1;
        int optlen = sizeof(optval);
        u_long optl = 1;
        DWORD dwBytesRet;
        struct tcp_keepalive alive;
+       int protonbr;
+#ifndef _WIN32_WCE
        struct protoent *tcp_proto;
+#endif
 
-       if (context->ka_time) {
+       if (vhost->ka_time) {
                /* enable keepalive on this socket */
                optval = 1;
                if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
@@ -281,8 +297,8 @@ lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd)
                        return 1;
 
                alive.onoff = TRUE;
-               alive.keepalivetime = context->ka_time;
-               alive.keepaliveinterval = context->ka_interval;
+               alive.keepalivetime = vhost->ka_time;
+               alive.keepaliveinterval = vhost->ka_interval;
 
                if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
                                              NULL, 0, &dwBytesRet, NULL, NULL))
@@ -291,13 +307,18 @@ lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd)
 
        /* Disable Nagle */
        optval = 1;
+#ifndef _WIN32_WCE
        tcp_proto = getprotobyname("TCP");
        if (!tcp_proto) {
                lwsl_err("getprotobyname() failed with error %d\n", LWS_ERRNO);
                return 1;
        }
+       protonbr = tcp_proto->p_proto;
+#else
+       protonbr = 6;
+#endif
 
-       setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, (const char *)&optval, optlen);
+       setsockopt(fd, protonbr, TCP_NODELAY, (const char *)&optval, optlen);
 
        /* We are nonblocking... */
        ioctlsocket(fd, FIONBIO, &optl);
@@ -523,7 +544,6 @@ _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
 {
        DWORD _amount;
 
-       (void *)wsi;
        if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) {
                *amount = 0;