src/os/win32/ngx_wsasend_chain.c - nginx-1.31.9 nginx/ @ 2a3a19c6f

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_event.h>


  8. ‌#define NGX_WSABUFS  64


  9. ngx_chain_t *
  10. ‌ngx_wsasend_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
  11. {
  12.     int           rc;
  13.     u_char       *prev;
  14.     u_long        size, sent, send, prev_send;
  15.     ngx_err_t     err;
  16.     ngx_event_t  *wev;
  17.     ngx_array_t   vec;
  18.     ngx_chain_t  *cl;
  19.     LPWSABUF      wsabuf;
  20.     WSABUF        wsabufs[NGX_WSABUFS];

  21.     wev = c->write;

  22.     if (!wev->ready) {
  23.         return in;
  24.     }

  25.     /* the maximum limit size is the maximum u_long value - the page size */

  26.     if (limit == 0 || limit > (off_t) (NGX_MAX_UINT32_VALUE - ngx_pagesize)) {
  27.         limit = NGX_MAX_UINT32_VALUE - ngx_pagesize;
  28.     }

  29.     send = 0;

  30.     /*
  31.      * WSABUFs must be 4-byte aligned otherwise
  32.      * WSASend() will return undocumented WSAEINVAL error.
  33.      */

  34.     vec.elts = wsabufs;
  35.     vec.size = sizeof(WSABUF);
  36.     vec.nalloc = ngx_min(NGX_WSABUFS, ngx_max_wsabufs);
  37.     vec.pool = c->pool;

  38.     for ( ;; ) {
  39.         prev = NULL;
  40.         wsabuf = NULL;
  41.         prev_send = send;

  42.         vec.nelts = 0;

  43.         /* create the WSABUF and coalesce the neighbouring bufs */

  44.         for (cl = in; cl && send < limit; cl = cl->next) {

  45.             if (ngx_buf_special(cl->buf)) {
  46.                 continue;
  47.             }

  48.             size = cl->buf->last - cl->buf->pos;

  49.             if (send + size > limit) {
  50.                 size = (u_long) (limit - send);
  51.             }

  52.             if (prev == cl->buf->pos) {
  53.                 wsabuf->len += cl->buf->last - cl->buf->pos;

  54.             } else {
  55.                 if (vec.nelts == vec.nalloc) {
  56.                     break;
  57.                 }

  58.                 wsabuf = ngx_array_push(&vec);
  59.                 if (wsabuf == NULL) {
  60.                     return NGX_CHAIN_ERROR;
  61.                 }

  62.                 wsabuf->buf = (char *) cl->buf->pos;
  63.                 wsabuf->len = cl->buf->last - cl->buf->pos;
  64.             }

  65.             prev = cl->buf->last;
  66.             send += size;
  67.         }

  68.         sent = 0;

  69.         rc = WSASend(c->fd, vec.elts, vec.nelts, &sent, 0, NULL, NULL);

  70.         if (rc == -1) {
  71.             err = ngx_errno;

  72.             if (err == WSAEWOULDBLOCK) {
  73.                 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
  74.                                "WSASend() not ready");

  75.             } else {
  76.                 wev->error = 1;
  77.                 ngx_connection_error(c, err, "WSASend() failed");
  78.                 return NGX_CHAIN_ERROR;
  79.             }
  80.         }

  81.         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
  82.                        "WSASend: fd:%d, s:%ul", c->fd, sent);

  83.         c->sent += sent;

  84.         in = ngx_chain_update_sent(in, sent);

  85.         if (send - prev_send != sent) {
  86.             wev->ready = 0;
  87.             return in;
  88.         }

  89.         if (send >= limit || in == NULL) {
  90.             return in;
  91.         }
  92.     }
  93. }