pmd: autobahn fixes
[platform/upstream/libwebsockets.git] / lib / output.c
index efc9573..465cb5f 100644 (file)
@@ -95,10 +95,13 @@ LWS_VISIBLE void lwsl_hexdump(void *vbuf, size_t len)
 int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
 {
        struct lws_context *context = lws_get_context(wsi);
+       struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
        size_t real_len = len;
        unsigned int n;
        int m;
 
+       lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_API_WRITE, 1);
+
        if (!len)
                return 0;
        /* just ignore sends after we cleared the truncation buffer */
@@ -137,9 +140,13 @@ int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
                lwsl_warn("** error invalid sock but expected to send\n");
 
        /* limit sending */
-       n = wsi->protocol->rx_buffer_size;
-       if (!n)
-               n = context->pt_serv_buf_size;
+       if (wsi->protocol->tx_packet_size)
+               n = wsi->protocol->tx_packet_size;
+       else {
+               n = wsi->protocol->rx_buffer_size;
+               if (!n)
+                       n = context->pt_serv_buf_size;
+       }
        n += LWS_PRE + 4;
        if (n > len)
                n = len;
@@ -155,6 +162,8 @@ int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
        n = lws_ssl_capable_write(wsi, buf, n);
        lws_latency(context, wsi, "send lws_issue_raw", n, n == len);
 
+       //lwsl_notice("lws_ssl_capable_write: %d\n", n);
+
        switch (n) {
        case LWS_SSL_CAPABLE_ERROR:
                /* we're going to close, let close know sends aren't possible */
@@ -198,9 +207,12 @@ handle_truncated_send:
         * Newly truncated send.  Buffer the remainder (it will get
         * first priority next time the socket is writable)
         */
-       lwsl_info("%p new partial sent %d from %lu total\n", wsi, n,
+       lwsl_debug("%p new partial sent %d from %lu total\n", wsi, n,
                    (unsigned long)real_len);
 
+       lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITE_PARTIALS, 1);
+       lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_B_PARTIALS_ACCEPTED_PARTS, n);
+
        /*
         *  - if we still have a suitable malloc lying around, use it
         *  - or, if too small, reallocate it
@@ -238,6 +250,33 @@ LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
        int pre = 0, n;
        size_t orig_len = len;
 
+       if (wsi->parent_carries_io) {
+               struct lws_write_passthru pas;
+
+               pas.buf = buf;
+               pas.len = len;
+               pas.wp = wp;
+               pas.wsi = wsi;
+
+               if (wsi->parent->protocol->callback(wsi->parent,
+                               LWS_CALLBACK_CHILD_WRITE_VIA_PARENT,
+                               wsi->parent->user_space,
+                               (void *)&pas, 0))
+                       return 1;
+
+               return len;
+       }
+
+       lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_API_LWS_WRITE, 1);
+
+       if ((int)len < 0) {
+               lwsl_err("%s: suspicious len int %d, ulong %lu\n", __func__,
+                               (int)len, (unsigned long)len);
+               return -1;
+       }
+
+       lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_B_WRITE, len);
+
 #ifdef LWS_WITH_ACCESS_LOG
        wsi->access_log.sent += len;
 #endif
@@ -247,7 +286,8 @@ LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
        if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
                /* remove us from the list */
                struct lws **w = &pt->tx_draining_ext_list;
-               lwsl_debug("%s: TX EXT DRAINING: Remove from list\n", __func__);
+
+       //      lwsl_notice("%s: TX EXT DRAINING: Remove from list\n", __func__);
                wsi->u.ws.tx_draining_ext = 0;
                /* remove us from context draining ext list */
                while (*w) {
@@ -310,11 +350,13 @@ LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
        case LWS_WRITE_CLOSE:
                break;
        default:
+               lwsl_debug("LWS_EXT_CB_PAYLOAD_TX\n");
                n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_TX, &eff_buf, wp);
                if (n < 0)
                        return -1;
 
                if (n && eff_buf.token_len) {
+                       lwsl_debug("drain len %d\n", (int)eff_buf.token_len);
                        /* extension requires further draining */
                        wsi->u.ws.tx_draining_ext = 1;
                        wsi->u.ws.tx_draining_ext_list = pt->tx_draining_ext_list;
@@ -348,8 +390,8 @@ LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
         */
        if ((char *)buf != eff_buf.token) {
                /*
-                * ext might eat it, but no have anything to issue yet
-                * in that case we have to follow his lead, but stash and
+                * ext might eat it, but not have anything to issue yet.
+                * In that case we have to follow his lead, but stash and
                 * replace the write type that was lost here the first time.
                 */
                if (len && !eff_buf.token_len) {
@@ -368,6 +410,13 @@ LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
        buf = (unsigned char *)eff_buf.token;
        len = eff_buf.token_len;
 
+       lwsl_debug("%p / %d\n", buf, (int)len);
+
+       if (!buf) {
+               lwsl_err("null buf (%d)\n", (int)len);
+               return -1;
+       }
+
        switch (wsi->ietf_spec_revision) {
        case 13:
                if (masked7) {
@@ -491,8 +540,8 @@ send_raw:
                             wp == LWS_WRITE_HTTP_FINAL) &&
                            wsi->u.http.content_length) {
                                wsi->u.http.content_remain -= len;
-                               lwsl_info("%s: content_remain = %lu\n", __func__,
-                                         wsi->u.http.content_remain);
+                               lwsl_info("%s: content_remain = %llu\n", __func__,
+                                         (unsigned long long)wsi->u.http.content_remain);
                                if (!wsi->u.http.content_remain) {
                                        lwsl_info("%s: selecting final write mode\n", __func__);
                                        wp = LWS_WRITE_HTTP_FINAL;
@@ -591,7 +640,7 @@ LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
 
                        lwsl_notice("%s: doing range start %llu\n", __func__, wsi->u.http.range.start);
 
-                       if ((long)lws_vfs_file_seek_cur(wsi->u.http.fop_fd,
+                       if ((long long)lws_vfs_file_seek_cur(wsi->u.http.fop_fd,
                                                   wsi->u.http.range.start -
                                                   wsi->u.http.filepos) < 0)
                                goto file_had_it;
@@ -618,6 +667,14 @@ LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
 #endif
 
                poss = context->pt_serv_buf_size - n;
+
+               /*
+                * if there is a hint about how much we will do well to send at one time,
+                * restrict ourselves to only trying to send that.
+                */
+               if (wsi->protocol->tx_packet_size && poss > wsi->protocol->tx_packet_size)
+                       poss = wsi->protocol->tx_packet_size;
+
 #if defined(LWS_WITH_RANGES)
                if (wsi->u.http.range.count_ranges) {
                        if (wsi->u.http.range.count_ranges > 1)
@@ -649,7 +706,7 @@ LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
                        if (wsi->sending_chunked) {
                                args.p = (char *)p;
                                args.len = n;
-                               args.max_len = poss + 128;
+                               args.max_len = (unsigned int)poss + 128;
                                args.final = wsi->u.http.filepos + n ==
                                             wsi->u.http.filelen;
                                if (user_callback_handle_rxflow(
@@ -746,12 +803,17 @@ file_had_it:
 LWS_VISIBLE int
 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
 {
+       struct lws_context *context = wsi->context;
+       struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
        int n;
 
+       lws_stats_atomic_bump(context, pt, LWSSTATS_C_API_READ, 1);
+
        n = recv(wsi->desc.sockfd, (char *)buf, len, 0);
        if (n >= 0) {
                if (wsi->vhost)
                        wsi->vhost->conn_stats.rx += n;
+               lws_stats_atomic_bump(context, pt, LWSSTATS_B_READ, n);
                lws_restart_ws_ping_pong_timer(wsi);
                return n;
        }
@@ -801,5 +863,9 @@ LWS_VISIBLE int
 lws_ssl_pending_no_ssl(struct lws *wsi)
 {
        (void)wsi;
-       return 1;
+#if defined(LWS_WITH_ESP32)
+       return 100;
+#else
+       return 0;
+#endif
 }