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