aboutsummaryrefslogtreecommitdiff
path: root/src/os/win32/ngx_sendfile.c
blob: 2dd779d19c5ef53dffa0304f9f53d20d2b1c5d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_types.h>
#include <ngx_files.h>
#include <ngx_socket.h>
#include <ngx_errno.h>
#include <ngx_log.h>
#include <ngx_connection.h>
#include <ngx_sendv.h>
#include <ngx_sendfile.h>

/*
  TODO:
       various flags
       TransmitPackets
*/

#if (HAVE_WIN32_TRANSMITFILE)

int ngx_sendfile(ngx_connection_t *c,
                 ngx_iovec_t *headers, int hdr_cnt,
                 ngx_fd_t fd, off_t offset, size_t nbytes,
                 ngx_iovec_t *trailers, int trl_cnt,
                 off_t *sent, u_int flags)
{
    int                    tfrc, rc;
    ngx_err_t              tf_err, err;
    OVERLAPPED             olp;
    TRANSMIT_FILE_BUFFERS  tfb, *ptfb;

#if 0
    ev = c->write;

    if (ev->timedout) {
        ngx_set_socket_errno(NGX_ETIMEDOUT);
        ngx_log_error(NGX_LOG_ERR, ev->log, 0, "TransmitFile() timed out");

        return NGX_ERROR;
    }

    if (ev->ready) {
        ev->ready = 0;

#if (HAVE_IOCP) /* iocp */

        if (ngx_event_flags & NGX_HAVE_IOCP_EVENT) {
            if (ev->ovlp.error) {
                ngx_log_error(NGX_LOG_ERR, ev->log, 0, "TransmitFile() failed");
                return NGX_ERROR;
            }

            return ev->available;
            }
        }

#endif

        /* TODO: WSAGetOverlappedResult stuff */

    }

#endif


    tf_err = 0;
    err = 0;

    olp.Internal = 0;
    olp.InternalHigh = 0;
    olp.Offset = (DWORD) offset;
    olp.OffsetHigh = (DWORD) (offset >> 32);
    olp.hEvent = NULL;

    if (headers || trailers) {
        tfb.Head = headers->ngx_iov_base;
        tfb.HeadLength = headers->ngx_iov_len;
        tfb.Tail = trailers->ngx_iov_base;
        tfb.TailLength = trailers->ngx_iov_len;
        ptfb = &tfb;

    } else {
        ptfb = NULL;
    }

#if 1
    flags = TF_DISCONNECT|TF_REUSE_SOCKET;
    tfrc = transmitfile(c->fd, NULL, 0, 0, &olp, NULL, flags);
#else
    tfrc = TransmitFile(c->fd, fd, nbytes, 0, &olp, ptfb, flags);
#endif

#if 0
#if 1
    tfrc = TransmitFile(c->fd, fd, nbytes, 0, &olp, ptfb, 0);
#else
    tfrc = TransmitFile(c->fd, fd, nbytes, 0, NULL, ptfb, 0);
#endif
#endif

    if (tfrc == 0) {
        tf_err = ngx_socket_errno;
        ngx_log_error(NGX_LOG_NOTICE, c->log, tf_err,
                      "ngx_sendfile: TransmitFile failed");
        if (tf_err == WSA_IO_PENDING) {
            return NGX_AGAIN;
        }
    }

    /* set sent */
#if 0
    rc = WSAGetOverlappedResult(c->fd, &olp, (unsigned long *) sent, 0, NULL);
#else
    *sent = olp.InternalHigh;
    rc = 1;
#endif

    ngx_log_debug(c->log, "TransmitFile: %d, @%I64d %I64d:%d" _
                  tfrc _ offset _ *sent _ nbytes);

    if (rc == 0) {
        err = ngx_socket_errno;
        ngx_log_error(NGX_LOG_ERR, c->log, err,
                     "ngx_sendfile: WSAGetOverlappedResult failed");
    }

    if (tfrc == 0) {
        if (tf_err != NGX_EAGAIN) {
            ngx_log_error(NGX_LOG_ERR, c->log, tf_err,
                          "ngx_sendfile: TransmitFile failed");
            return NGX_ERROR;
        }

        ngx_log_error(NGX_LOG_INFO, c->log, tf_err,
                      "ngx_sendfile: TransmitFile sent only %I64d bytes",
                      *sent);
    }

    if (rc == 0)
        return NGX_ERROR;

    return NGX_OK;
}

#endif