Home | History | Annotate | Line # | Download | only in libevent
buffer_iocp.c revision 1.1.1.6
      1  1.1  christos /*
      2  1.1  christos  * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
      3  1.1  christos  *
      4  1.1  christos  * Redistribution and use in source and binary forms, with or without
      5  1.1  christos  * modification, are permitted provided that the following conditions
      6  1.1  christos  * are met:
      7  1.1  christos  * 1. Redistributions of source code must retain the above copyright
      8  1.1  christos  *    notice, this list of conditions and the following disclaimer.
      9  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     10  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     11  1.1  christos  *    documentation and/or other materials provided with the distribution.
     12  1.1  christos  * 3. The name of the author may not be used to endorse or promote products
     13  1.1  christos  *    derived from this software without specific prior written permission.
     14  1.1  christos  *
     15  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  1.1  christos  */
     26  1.1  christos 
     27  1.1  christos /**
     28  1.1  christos    @file buffer_iocp.c
     29  1.1  christos 
     30  1.1  christos    This module implements overlapped read and write functions for evbuffer
     31  1.1  christos    objects on Windows.
     32  1.1  christos */
     33  1.1  christos #include "event2/event-config.h"
     34  1.1  christos #include "evconfig-private.h"
     35  1.1  christos 
     36  1.1  christos #include "event2/buffer.h"
     37  1.1  christos #include "event2/buffer_compat.h"
     38  1.1  christos #include "event2/util.h"
     39  1.1  christos #include "event2/thread.h"
     40  1.1  christos #include "util-internal.h"
     41  1.1  christos #include "evthread-internal.h"
     42  1.1  christos #include "evbuffer-internal.h"
     43  1.1  christos #include "iocp-internal.h"
     44  1.1  christos #include "mm-internal.h"
     45  1.1  christos 
     46  1.1  christos #include <winsock2.h>
     47  1.1  christos #include <windows.h>
     48  1.1  christos #include <stdio.h>
     49  1.1  christos 
     50  1.1  christos #define MAX_WSABUFS 16
     51  1.1  christos 
     52  1.1  christos /** An evbuffer that can handle overlapped IO. */
     53  1.1  christos struct evbuffer_overlapped {
     54  1.1  christos 	struct evbuffer buffer;
     55  1.1  christos 	/** The socket that we're doing overlapped IO on. */
     56  1.1  christos 	evutil_socket_t fd;
     57  1.1  christos 
     58  1.1  christos 	/** pending I/O type */
     59  1.1  christos 	unsigned read_in_progress : 1;
     60  1.1  christos 	unsigned write_in_progress : 1;
     61  1.1  christos 
     62  1.1  christos 	/** The first pinned chain in the buffer. */
     63  1.1  christos 	struct evbuffer_chain *first_pinned;
     64  1.1  christos 
     65  1.1  christos 	/** How many chains are pinned; how many of the fields in buffers
     66  1.1  christos 	 * are we using. */
     67  1.1  christos 	int n_buffers;
     68  1.1  christos 	WSABUF buffers[MAX_WSABUFS];
     69  1.1  christos };
     70  1.1  christos 
     71  1.1  christos /** Given an evbuffer, return the correponding evbuffer structure, or NULL if
     72  1.1  christos  * the evbuffer isn't overlapped. */
     73  1.1  christos static inline struct evbuffer_overlapped *
     74  1.1  christos upcast_evbuffer(struct evbuffer *buf)
     75  1.1  christos {
     76  1.1  christos 	if (!buf || !buf->is_overlapped)
     77  1.1  christos 		return NULL;
     78  1.1  christos 	return EVUTIL_UPCAST(buf, struct evbuffer_overlapped, buffer);
     79  1.1  christos }
     80  1.1  christos 
     81  1.1  christos /** Unpin all the chains noted as pinned in 'eo'. */
     82  1.1  christos static void
     83  1.1  christos pin_release(struct evbuffer_overlapped *eo, unsigned flag)
     84  1.1  christos {
     85  1.1  christos 	int i;
     86  1.1  christos 	struct evbuffer_chain *next, *chain = eo->first_pinned;
     87  1.1  christos 
     88  1.1  christos 	for (i = 0; i < eo->n_buffers; ++i) {
     89  1.1  christos 		EVUTIL_ASSERT(chain);
     90  1.1  christos 		next = chain->next;
     91  1.1  christos 		evbuffer_chain_unpin_(chain, flag);
     92  1.1  christos 		chain = next;
     93  1.1  christos 	}
     94  1.1  christos }
     95  1.1  christos 
     96  1.1  christos void
     97  1.1  christos evbuffer_commit_read_(struct evbuffer *evbuf, ev_ssize_t nBytes)
     98  1.1  christos {
     99  1.1  christos 	struct evbuffer_overlapped *buf = upcast_evbuffer(evbuf);
    100  1.1  christos 	struct evbuffer_chain **chainp;
    101  1.1  christos 	size_t remaining, len;
    102  1.1  christos 	unsigned i;
    103  1.1  christos 
    104  1.1  christos 	EVBUFFER_LOCK(evbuf);
    105  1.1  christos 	EVUTIL_ASSERT(buf->read_in_progress && !buf->write_in_progress);
    106  1.1  christos 	EVUTIL_ASSERT(nBytes >= 0); /* XXXX Can this be false? */
    107  1.1  christos 
    108  1.1  christos 	evbuffer_unfreeze(evbuf, 0);
    109  1.1  christos 
    110  1.1  christos 	chainp = evbuf->last_with_datap;
    111  1.1  christos 	if (!((*chainp)->flags & EVBUFFER_MEM_PINNED_R))
    112  1.1  christos 		chainp = &(*chainp)->next;
    113  1.1  christos 	remaining = nBytes;
    114  1.1  christos 	for (i = 0; remaining > 0 && i < (unsigned)buf->n_buffers; ++i) {
    115  1.1  christos 		EVUTIL_ASSERT(*chainp);
    116  1.1  christos 		len = buf->buffers[i].len;
    117  1.1  christos 		if (remaining < len)
    118  1.1  christos 			len = remaining;
    119  1.1  christos 		(*chainp)->off += len;
    120  1.1  christos 		evbuf->last_with_datap = chainp;
    121  1.1  christos 		remaining -= len;
    122  1.1  christos 		chainp = &(*chainp)->next;
    123  1.1  christos 	}
    124  1.1  christos 
    125  1.1  christos 	pin_release(buf, EVBUFFER_MEM_PINNED_R);
    126  1.1  christos 
    127  1.1  christos 	buf->read_in_progress = 0;
    128  1.1  christos 
    129  1.1  christos 	evbuf->total_len += nBytes;
    130  1.1  christos 	evbuf->n_add_for_cb += nBytes;
    131  1.1  christos 
    132  1.1  christos 	evbuffer_invoke_callbacks_(evbuf);
    133  1.1  christos 
    134  1.1  christos 	evbuffer_decref_and_unlock_(evbuf);
    135  1.1  christos }
    136  1.1  christos 
    137  1.1  christos void
    138  1.1  christos evbuffer_commit_write_(struct evbuffer *evbuf, ev_ssize_t nBytes)
    139  1.1  christos {
    140  1.1  christos 	struct evbuffer_overlapped *buf = upcast_evbuffer(evbuf);
    141  1.1  christos 
    142  1.1  christos 	EVBUFFER_LOCK(evbuf);
    143  1.1  christos 	EVUTIL_ASSERT(buf->write_in_progress && !buf->read_in_progress);
    144  1.1  christos 	evbuffer_unfreeze(evbuf, 1);
    145  1.1  christos 	evbuffer_drain(evbuf, nBytes);
    146  1.1  christos 	pin_release(buf,EVBUFFER_MEM_PINNED_W);
    147  1.1  christos 	buf->write_in_progress = 0;
    148  1.1  christos 	evbuffer_decref_and_unlock_(evbuf);
    149  1.1  christos }
    150  1.1  christos 
    151  1.1  christos struct evbuffer *
    152  1.1  christos evbuffer_overlapped_new_(evutil_socket_t fd)
    153  1.1  christos {
    154  1.1  christos 	struct evbuffer_overlapped *evo;
    155  1.1  christos 
    156  1.1  christos 	evo = mm_calloc(1, sizeof(struct evbuffer_overlapped));
    157  1.1  christos 	if (!evo)
    158  1.1  christos 		return NULL;
    159  1.1  christos 
    160  1.1  christos 	LIST_INIT(&evo->buffer.callbacks);
    161  1.1  christos 	evo->buffer.refcnt = 1;
    162  1.1  christos 	evo->buffer.last_with_datap = &evo->buffer.first;
    163  1.1  christos 
    164  1.1  christos 	evo->buffer.is_overlapped = 1;
    165  1.1  christos 	evo->fd = fd;
    166  1.1  christos 
    167  1.1  christos 	return &evo->buffer;
    168  1.1  christos }
    169  1.1  christos 
    170  1.1  christos int
    171  1.1  christos evbuffer_launch_write_(struct evbuffer *buf, ev_ssize_t at_most,
    172  1.1  christos 		struct event_overlapped *ol)
    173  1.1  christos {
    174  1.1  christos 	struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
    175  1.1  christos 	int r = -1;
    176  1.1  christos 	int i;
    177  1.1  christos 	struct evbuffer_chain *chain;
    178  1.1  christos 	DWORD bytesSent;
    179  1.1  christos 
    180  1.1  christos 	if (!buf) {
    181  1.1  christos 		/* No buffer, or it isn't overlapped */
    182  1.1  christos 		return -1;
    183  1.1  christos 	}
    184  1.1  christos 
    185  1.1  christos 	EVBUFFER_LOCK(buf);
    186  1.1  christos 	EVUTIL_ASSERT(!buf_o->read_in_progress);
    187  1.1  christos 	if (buf->freeze_start || buf_o->write_in_progress)
    188  1.1  christos 		goto done;
    189  1.1  christos 	if (!buf->total_len) {
    190  1.1  christos 		/* Nothing to write */
    191  1.1  christos 		r = 0;
    192  1.1  christos 		goto done;
    193  1.1  christos 	} else if (at_most < 0 || (size_t)at_most > buf->total_len) {
    194  1.1  christos 		at_most = buf->total_len;
    195  1.1  christos 	}
    196  1.1  christos 	evbuffer_freeze(buf, 1);
    197  1.1  christos 
    198  1.1  christos 	buf_o->first_pinned = NULL;
    199  1.1  christos 	buf_o->n_buffers = 0;
    200  1.1  christos 	memset(buf_o->buffers, 0, sizeof(buf_o->buffers));
    201  1.1  christos 
    202  1.1  christos 	chain = buf_o->first_pinned = buf->first;
    203  1.1  christos 
    204  1.1  christos 	for (i=0; i < MAX_WSABUFS && chain; ++i, chain=chain->next) {
    205  1.1  christos 		WSABUF *b = &buf_o->buffers[i];
    206  1.1  christos 		b->buf = (char*)( chain->buffer + chain->misalign );
    207  1.1  christos 		evbuffer_chain_pin_(chain, EVBUFFER_MEM_PINNED_W);
    208  1.1  christos 
    209  1.1  christos 		if ((size_t)at_most > chain->off) {
    210  1.1  christos 			/* XXXX Cast is safe for now, since win32 has no
    211  1.1  christos 			   mmaped chains.  But later, we need to have this
    212  1.1  christos 			   add more WSAbufs if chain->off is greater than
    213  1.1  christos 			   ULONG_MAX */
    214  1.1  christos 			b->len = (unsigned long)chain->off;
    215  1.1  christos 			at_most -= chain->off;
    216  1.1  christos 		} else {
    217  1.1  christos 			b->len = (unsigned long)at_most;
    218  1.1  christos 			++i;
    219  1.1  christos 			break;
    220  1.1  christos 		}
    221  1.1  christos 	}
    222  1.1  christos 
    223  1.1  christos 	buf_o->n_buffers = i;
    224  1.1  christos 	evbuffer_incref_(buf);
    225  1.1  christos 	if (WSASend(buf_o->fd, buf_o->buffers, i, &bytesSent, 0,
    226  1.1  christos 		&ol->overlapped, NULL)) {
    227  1.1  christos 		int error = WSAGetLastError();
    228  1.1  christos 		if (error != WSA_IO_PENDING) {
    229  1.1  christos 			/* An actual error. */
    230  1.1  christos 			pin_release(buf_o, EVBUFFER_MEM_PINNED_W);
    231  1.1  christos 			evbuffer_unfreeze(buf, 1);
    232  1.1  christos 			evbuffer_free(buf); /* decref */
    233  1.1  christos 			goto done;
    234  1.1  christos 		}
    235  1.1  christos 	}
    236  1.1  christos 
    237  1.1  christos 	buf_o->write_in_progress = 1;
    238  1.1  christos 	r = 0;
    239  1.1  christos done:
    240  1.1  christos 	EVBUFFER_UNLOCK(buf);
    241  1.1  christos 	return r;
    242  1.1  christos }
    243  1.1  christos 
    244  1.1  christos int
    245  1.1  christos evbuffer_launch_read_(struct evbuffer *buf, size_t at_most,
    246  1.1  christos 		struct event_overlapped *ol)
    247  1.1  christos {
    248  1.1  christos 	struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
    249  1.1  christos 	int r = -1, i;
    250  1.1  christos 	int nvecs;
    251  1.1  christos 	int npin=0;
    252  1.1  christos 	struct evbuffer_chain *chain=NULL, **chainp;
    253  1.1  christos 	DWORD bytesRead;
    254  1.1  christos 	DWORD flags = 0;
    255  1.1  christos 	struct evbuffer_iovec vecs[MAX_WSABUFS];
    256  1.1  christos 
    257  1.1  christos 	if (!buf_o)
    258  1.1  christos 		return -1;
    259  1.1  christos 	EVBUFFER_LOCK(buf);
    260  1.1  christos 	EVUTIL_ASSERT(!buf_o->write_in_progress);
    261  1.1  christos 	if (buf->freeze_end || buf_o->read_in_progress)
    262  1.1  christos 		goto done;
    263  1.1  christos 
    264  1.1  christos 	buf_o->first_pinned = NULL;
    265  1.1  christos 	buf_o->n_buffers = 0;
    266  1.1  christos 	memset(buf_o->buffers, 0, sizeof(buf_o->buffers));
    267  1.1  christos 
    268  1.1  christos 	if (evbuffer_expand_fast_(buf, at_most, MAX_WSABUFS) == -1)
    269  1.1  christos 		goto done;
    270  1.1  christos 	evbuffer_freeze(buf, 0);
    271  1.1  christos 
    272  1.1  christos 	nvecs = evbuffer_read_setup_vecs_(buf, at_most,
    273  1.1  christos 	    vecs, MAX_WSABUFS, &chainp, 1);
    274  1.1  christos 	for (i=0;i<nvecs;++i) {
    275  1.1  christos 		WSABUF_FROM_EVBUFFER_IOV(
    276  1.1  christos 			&buf_o->buffers[i],
    277  1.1  christos 			&vecs[i]);
    278  1.1  christos 	}
    279  1.1  christos 
    280  1.1  christos 	buf_o->n_buffers = nvecs;
    281  1.1  christos 	buf_o->first_pinned = chain = *chainp;
    282  1.1  christos 
    283  1.1  christos 	npin=0;
    284  1.1  christos 	for ( ; chain; chain = chain->next) {
    285  1.1  christos 		evbuffer_chain_pin_(chain, EVBUFFER_MEM_PINNED_R);
    286  1.1  christos 		++npin;
    287  1.1  christos 	}
    288  1.1  christos 	EVUTIL_ASSERT(npin == nvecs);
    289  1.1  christos 
    290  1.1  christos 	evbuffer_incref_(buf);
    291  1.1  christos 	if (WSARecv(buf_o->fd, buf_o->buffers, nvecs, &bytesRead, &flags,
    292  1.1  christos 		    &ol->overlapped, NULL)) {
    293  1.1  christos 		int error = WSAGetLastError();
    294  1.1  christos 		if (error != WSA_IO_PENDING) {
    295  1.1  christos 			/* An actual error. */
    296  1.1  christos 			pin_release(buf_o, EVBUFFER_MEM_PINNED_R);
    297  1.1  christos 			evbuffer_unfreeze(buf, 0);
    298  1.1  christos 			evbuffer_free(buf); /* decref */
    299  1.1  christos 			goto done;
    300  1.1  christos 		}
    301  1.1  christos 	}
    302  1.1  christos 
    303  1.1  christos 	buf_o->read_in_progress = 1;
    304  1.1  christos 	r = 0;
    305  1.1  christos done:
    306  1.1  christos 	EVBUFFER_UNLOCK(buf);
    307  1.1  christos 	return r;
    308  1.1  christos }
    309  1.1  christos 
    310  1.1  christos evutil_socket_t
    311  1.1  christos evbuffer_overlapped_get_fd_(struct evbuffer *buf)
    312  1.1  christos {
    313  1.1  christos 	struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
    314  1.1  christos 	return buf_o ? buf_o->fd : -1;
    315  1.1  christos }
    316  1.1  christos 
    317  1.1  christos void
    318  1.1  christos evbuffer_overlapped_set_fd_(struct evbuffer *buf, evutil_socket_t fd)
    319  1.1  christos {
    320  1.1  christos 	struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf);
    321  1.1  christos 	EVBUFFER_LOCK(buf);
    322  1.1  christos 	/* XXX is this right?, should it cancel current I/O operations? */
    323  1.1  christos 	if (buf_o)
    324  1.1  christos 		buf_o->fd = fd;
    325  1.1  christos 	EVBUFFER_UNLOCK(buf);
    326  1.1  christos }
    327