handle same vh protocol reinsert
[platform/upstream/libwebsockets.git] / lib / pollfd.c
index f829a8f..25167dd 100644 (file)
@@ -33,13 +33,33 @@ _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa)
        if (!wsi || wsi->position_in_fds_table < 0)
                return 0;
 
+       if (wsi->handling_pollout && !_and && _or == LWS_POLLOUT) {
+               /*
+                * Happening alongside service thread handling POLLOUT.
+                * The danger is when he is finished, he will disable POLLOUT,
+                * countermanding what we changed here.
+                *
+                * Instead of changing the fds, inform the service thread
+                * what happened, and ask it to leave POLLOUT active on exit
+                */
+               wsi->leave_pollout_active = 1;
+               /*
+                * by definition service thread is not in poll wait, so no need
+                * to cancel service
+                */
+
+               lwsl_debug("%s: using leave_pollout_active\n", __func__);
+
+               return 0;
+       }
+
        context = wsi->context;
        pt = &context->pt[(int)wsi->tsi];
        assert(wsi->position_in_fds_table >= 0 &&
               wsi->position_in_fds_table < pt->fds_count);
 
        pfd = &pt->fds[wsi->position_in_fds_table];
-       pa->fd = wsi->sock;
+       pa->fd = wsi->desc.sockfd;
        pa->prev_events = pfd->events;
        pa->events = pfd->events = (pfd->events & ~_and) | _or;
 
@@ -58,18 +78,22 @@ _lws_change_pollfd(struct lws *wsi, int _and, int _or, struct lws_pollargs *pa)
        if (_and & LWS_POLLIN) {
                lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ);
                lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ);
+               lws_libevent_io(wsi, LWS_EV_STOP | LWS_EV_READ);
        }
        if (_or & LWS_POLLIN) {
                lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
                lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
+               lws_libevent_io(wsi, LWS_EV_START | LWS_EV_READ);
        }
        if (_and & LWS_POLLOUT) {
                lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
                lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
+               lws_libevent_io(wsi, LWS_EV_STOP | LWS_EV_WRITE);
        }
        if (_or & LWS_POLLOUT) {
                lws_libev_io(wsi, LWS_EV_START | LWS_EV_WRITE);
                lws_libuv_io(wsi, LWS_EV_START | LWS_EV_WRITE);
+               lws_libevent_io(wsi, LWS_EV_START | LWS_EV_WRITE);
        }
 
        /*
@@ -132,13 +156,13 @@ lws_accept_modulation(struct lws_context_per_thread *pt, int allow)
 int
 insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi)
 {
-       struct lws_pollargs pa = { wsi->sock, LWS_POLLIN, 0 };
+       struct lws_pollargs pa = { wsi->desc.sockfd, LWS_POLLIN, 0 };
        struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
        int ret = 0;
 
 
        lwsl_debug("%s: %p: tsi=%d, sock=%d, pos-in-fds=%d\n",
-                 __func__, wsi, wsi->tsi, wsi->sock, pt->fds_count);
+                 __func__, wsi, wsi->tsi, wsi->desc.sockfd, pt->fds_count);
 
        if ((unsigned int)pt->fds_count >= context->fd_limit_per_thread) {
                lwsl_err("Too many fds (%d vs %d)\n", context->max_fds,
@@ -146,17 +170,17 @@ insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi)
                return 1;
        }
 
-#if !defined(_WIN32) && !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
-       if (wsi->sock >= context->max_fds) {
+#if !defined(_WIN32) && !defined(LWS_WITH_ESP8266)
+       if (wsi->desc.sockfd >= context->max_fds) {
                lwsl_err("Socket fd %d is too high (%d)\n",
-                        wsi->sock, context->max_fds);
+                        wsi->desc.sockfd, context->max_fds);
                return 1;
        }
 #endif
 
        assert(wsi);
        assert(wsi->vhost);
-       assert(lws_socket_is_valid(wsi->sock));
+       assert(lws_socket_is_valid(wsi->desc.sockfd));
 
        if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_LOCK_POLL,
                                           wsi->user_space, (void *) &pa, 1))
@@ -172,7 +196,7 @@ insert_wsi_socket_into_fds(struct lws_context *context, struct lws *wsi)
 
        // lwsl_notice("%s: %p: setting posinfds %d\n", __func__, wsi, wsi->position_in_fds_table);
 
-       pt->fds[wsi->position_in_fds_table].fd = wsi->sock;
+       pt->fds[wsi->position_in_fds_table].fd = wsi->desc.sockfd;
 #if LWS_POSIX
        pt->fds[wsi->position_in_fds_table].events = LWS_POLLIN;
 #else
@@ -204,7 +228,7 @@ int
 remove_wsi_socket_from_fds(struct lws *wsi)
 {
        struct lws_context *context = wsi->context;
-       struct lws_pollargs pa = { wsi->sock, 0, 0 };
+       struct lws_pollargs pa = { wsi->desc.sockfd, 0, 0 };
 #if !defined(LWS_WITH_ESP8266)
        struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
        struct lws *end_wsi;
@@ -212,9 +236,14 @@ remove_wsi_socket_from_fds(struct lws *wsi)
 #endif
        int m, ret = 0;
 
-#if !defined(_WIN32) && !defined(MBED_OPERATORS) && !defined(LWS_WITH_ESP8266)
-       if (wsi->sock > context->max_fds) {
-               lwsl_err("fd %d too high (%d)\n", wsi->sock, context->max_fds);
+       if (wsi->parent_carries_io) {
+               lws_same_vh_protocol_remove(wsi);
+               return 0;
+       }
+
+#if !defined(_WIN32) && !defined(LWS_WITH_ESP8266)
+       if (wsi->desc.sockfd > context->max_fds) {
+               lwsl_err("fd %d too high (%d)\n", wsi->desc.sockfd, context->max_fds);
                return 1;
        }
 #endif
@@ -223,31 +252,7 @@ remove_wsi_socket_from_fds(struct lws *wsi)
                                           wsi->user_space, (void *)&pa, 1))
                return -1;
 
-       /*
-        * detach ourselves from vh protocol list if we're on one
-        * A -> B -> C
-        * A -> C , or, B -> C, or A -> B
-        */
-       lwsl_info("%s: removing same prot wsi %p\n", __func__, wsi);
-       if (wsi->same_vh_protocol_prev) {
-               assert (*(wsi->same_vh_protocol_prev) == wsi);
-               lwsl_info("have prev %p, setting him to our next %p\n",
-                        wsi->same_vh_protocol_prev,
-                        wsi->same_vh_protocol_next);
-
-               /* guy who pointed to us should point to our next */
-               *(wsi->same_vh_protocol_prev) = wsi->same_vh_protocol_next;
-       } //else
-               //lwsl_err("null wsi->prev\n");
-       /* our next should point back to our prev */
-       if (wsi->same_vh_protocol_next) {
-               wsi->same_vh_protocol_next->same_vh_protocol_prev =
-                               wsi->same_vh_protocol_prev;
-       } //else
-               //lwsl_err("null wsi->next\n");
-
-       wsi->same_vh_protocol_prev = NULL;
-       wsi->same_vh_protocol_next = NULL;
+       lws_same_vh_protocol_remove(wsi);
 
        /* the guy who is to be deleted's slot index in pt->fds */
        m = wsi->position_in_fds_table;
@@ -259,7 +264,7 @@ remove_wsi_socket_from_fds(struct lws *wsi)
        lws_pt_lock(pt);
 
        lwsl_debug("%s: wsi=%p, sock=%d, fds pos=%d, end guy pos=%d, endfd=%d\n",
-                 __func__, wsi, wsi->sock, wsi->position_in_fds_table,
+                 __func__, wsi, wsi->desc.sockfd, wsi->position_in_fds_table,
                  pt->fds_count, pt->fds[pt->fds_count].fd);
 
        /* have the last guy take up the now vacant slot */
@@ -278,12 +283,12 @@ remove_wsi_socket_from_fds(struct lws *wsi)
                end_wsi->position_in_fds_table = m;
 
        /* deletion guy's lws_lookup entry needs nuking */
-       delete_from_fd(context, wsi->sock);
+       delete_from_fd(context, wsi->desc.sockfd);
        /* removed wsi has no position any more */
        wsi->position_in_fds_table = -1;
 
        /* remove also from external POLL support via protocol 0 */
-       if (lws_socket_is_valid(wsi->sock))
+       if (lws_socket_is_valid(wsi->desc.sockfd))
                if (wsi->vhost->protocols[0].callback(wsi, LWS_CALLBACK_DEL_POLL_FD,
                                                   wsi->user_space, (void *) &pa, 0))
                        ret = -1;
@@ -336,6 +341,7 @@ lws_change_pollfd(struct lws *wsi, int _and, int _or)
 LWS_VISIBLE int
 lws_callback_on_writable(struct lws *wsi)
 {
+       struct lws_context_per_thread *pt;
 #ifdef LWS_USE_HTTP2
        struct lws *network_wsi, *wsi2;
        int already;
@@ -347,6 +353,25 @@ lws_callback_on_writable(struct lws *wsi)
        if (wsi->socket_is_permanently_unusable)
                return 0;
 
+       if (wsi->parent_carries_io) {
+               int n = lws_callback_on_writable(wsi->parent);
+
+               if (n < 0)
+                       return n;
+
+               wsi->parent_pending_cb_on_writable = 1;
+               return 1;
+       }
+
+       pt = &wsi->context->pt[(int)wsi->tsi];
+       lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB_REQ, 1);
+#if defined(LWS_WITH_STATS)
+       if (!wsi->active_writable_req_us) {
+               wsi->active_writable_req_us = time_in_microseconds();
+               lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITEABLE_CB_EFF_REQ, 1);
+       }
+#endif
+
 #ifdef LWS_USE_HTTP2
        lwsl_info("%s: %p\n", __func__, wsi);
 
@@ -396,7 +421,7 @@ network_sock:
                return 1;
 
        if (wsi->position_in_fds_table < 0) {
-               lwsl_err("%s: failed to find socket %d\n", __func__, wsi->sock);
+               lwsl_err("%s: failed to find socket %d\n", __func__, wsi->desc.sockfd);
                return -1;
        }
 
@@ -406,6 +431,74 @@ network_sock:
        return 1;
 }
 
+/*
+ * stitch protocol choice into the vh protocol linked list
+ * We always insert ourselves at the start of the list
+ *
+ * X <-> B
+ * X <-> pAn <-> pB
+ *
+ * Illegal to attach more than once without detach inbetween
+ */
+void
+lws_same_vh_protocol_insert(struct lws *wsi, int n)
+{
+       //lwsl_err("%s: pre insert vhost start wsi %p, that wsi prev == %p\n",
+       //              __func__,
+       //              wsi->vhost->same_vh_protocol_list[n],
+       //              wsi->same_vh_protocol_prev);
+
+       if (wsi->same_vh_protocol_prev || wsi->same_vh_protocol_next) {
+               lws_same_vh_protocol_remove(wsi);
+               lwsl_notice("Attempted to attach wsi twice to same vh prot\n");
+       }
+
+       wsi->same_vh_protocol_prev = /* guy who points to us */
+               &wsi->vhost->same_vh_protocol_list[n];
+       wsi->same_vh_protocol_next = /* old first guy is our next */
+                       wsi->vhost->same_vh_protocol_list[n];
+       /* we become the new first guy */
+       wsi->vhost->same_vh_protocol_list[n] = wsi;
+
+       if (wsi->same_vh_protocol_next)
+               /* old first guy points back to us now */
+               wsi->same_vh_protocol_next->same_vh_protocol_prev =
+                               &wsi->same_vh_protocol_next;
+}
+
+void
+lws_same_vh_protocol_remove(struct lws *wsi)
+{
+       /*
+        * detach ourselves from vh protocol list if we're on one
+        * A -> B -> C
+        * A -> C , or, B -> C, or A -> B
+        *
+        * OK to call on already-detached wsi
+        */
+       lwsl_info("%s: removing same prot wsi %p\n", __func__, wsi);
+
+       if (wsi->same_vh_protocol_prev) {
+               assert (*(wsi->same_vh_protocol_prev) == wsi);
+               lwsl_info("have prev %p, setting him to our next %p\n",
+                        wsi->same_vh_protocol_prev,
+                        wsi->same_vh_protocol_next);
+
+               /* guy who pointed to us should point to our next */
+               *(wsi->same_vh_protocol_prev) = wsi->same_vh_protocol_next;
+       }
+
+       /* our next should point back to our prev */
+       if (wsi->same_vh_protocol_next) {
+               wsi->same_vh_protocol_next->same_vh_protocol_prev =
+                               wsi->same_vh_protocol_prev;
+       }
+
+       wsi->same_vh_protocol_prev = NULL;
+       wsi->same_vh_protocol_next = NULL;
+}
+
+
 LWS_VISIBLE int
 lws_callback_on_writable_all_protocol_vhost(const struct lws_vhost *vhost,
                                      const struct lws_protocols *protocol)