michael@0: /* michael@0: * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: /** michael@0: @file buffer_iocp.c michael@0: michael@0: This module implements overlapped read and write functions for evbuffer michael@0: objects on Windows. michael@0: */ michael@0: michael@0: #include "event2/buffer.h" michael@0: #include "event2/buffer_compat.h" michael@0: #include "event2/util.h" michael@0: #include "event2/thread.h" michael@0: #include "event2/event-config.h" michael@0: #include "util-internal.h" michael@0: #include "evthread-internal.h" michael@0: #include "evbuffer-internal.h" michael@0: #include "iocp-internal.h" michael@0: #include "mm-internal.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define MAX_WSABUFS 16 michael@0: michael@0: /** An evbuffer that can handle overlapped IO. */ michael@0: struct evbuffer_overlapped { michael@0: struct evbuffer buffer; michael@0: /** The socket that we're doing overlapped IO on. */ michael@0: evutil_socket_t fd; michael@0: michael@0: /** pending I/O type */ michael@0: unsigned read_in_progress : 1; michael@0: unsigned write_in_progress : 1; michael@0: michael@0: /** The first pinned chain in the buffer. */ michael@0: struct evbuffer_chain *first_pinned; michael@0: michael@0: /** How many chains are pinned; how many of the fields in buffers michael@0: * are we using. */ michael@0: int n_buffers; michael@0: WSABUF buffers[MAX_WSABUFS]; michael@0: }; michael@0: michael@0: /** Given an evbuffer, return the correponding evbuffer structure, or NULL if michael@0: * the evbuffer isn't overlapped. */ michael@0: static inline struct evbuffer_overlapped * michael@0: upcast_evbuffer(struct evbuffer *buf) michael@0: { michael@0: if (!buf || !buf->is_overlapped) michael@0: return NULL; michael@0: return EVUTIL_UPCAST(buf, struct evbuffer_overlapped, buffer); michael@0: } michael@0: michael@0: /** Unpin all the chains noted as pinned in 'eo'. */ michael@0: static void michael@0: pin_release(struct evbuffer_overlapped *eo, unsigned flag) michael@0: { michael@0: int i; michael@0: struct evbuffer_chain *next, *chain = eo->first_pinned; michael@0: michael@0: for (i = 0; i < eo->n_buffers; ++i) { michael@0: EVUTIL_ASSERT(chain); michael@0: next = chain->next; michael@0: _evbuffer_chain_unpin(chain, flag); michael@0: chain = next; michael@0: } michael@0: } michael@0: michael@0: void michael@0: evbuffer_commit_read(struct evbuffer *evbuf, ev_ssize_t nBytes) michael@0: { michael@0: struct evbuffer_overlapped *buf = upcast_evbuffer(evbuf); michael@0: struct evbuffer_chain **chainp; michael@0: size_t remaining, len; michael@0: unsigned i; michael@0: michael@0: EVBUFFER_LOCK(evbuf); michael@0: EVUTIL_ASSERT(buf->read_in_progress && !buf->write_in_progress); michael@0: EVUTIL_ASSERT(nBytes >= 0); /* XXXX Can this be false? */ michael@0: michael@0: evbuffer_unfreeze(evbuf, 0); michael@0: michael@0: chainp = evbuf->last_with_datap; michael@0: if (!((*chainp)->flags & EVBUFFER_MEM_PINNED_R)) michael@0: chainp = &(*chainp)->next; michael@0: remaining = nBytes; michael@0: for (i = 0; remaining > 0 && i < (unsigned)buf->n_buffers; ++i) { michael@0: EVUTIL_ASSERT(*chainp); michael@0: len = buf->buffers[i].len; michael@0: if (remaining < len) michael@0: len = remaining; michael@0: (*chainp)->off += len; michael@0: evbuf->last_with_datap = chainp; michael@0: remaining -= len; michael@0: chainp = &(*chainp)->next; michael@0: } michael@0: michael@0: pin_release(buf, EVBUFFER_MEM_PINNED_R); michael@0: michael@0: buf->read_in_progress = 0; michael@0: michael@0: evbuf->total_len += nBytes; michael@0: evbuf->n_add_for_cb += nBytes; michael@0: michael@0: evbuffer_invoke_callbacks(evbuf); michael@0: michael@0: _evbuffer_decref_and_unlock(evbuf); michael@0: } michael@0: michael@0: void michael@0: evbuffer_commit_write(struct evbuffer *evbuf, ev_ssize_t nBytes) michael@0: { michael@0: struct evbuffer_overlapped *buf = upcast_evbuffer(evbuf); michael@0: michael@0: EVBUFFER_LOCK(evbuf); michael@0: EVUTIL_ASSERT(buf->write_in_progress && !buf->read_in_progress); michael@0: evbuffer_unfreeze(evbuf, 1); michael@0: evbuffer_drain(evbuf, nBytes); michael@0: pin_release(buf,EVBUFFER_MEM_PINNED_W); michael@0: buf->write_in_progress = 0; michael@0: _evbuffer_decref_and_unlock(evbuf); michael@0: } michael@0: michael@0: struct evbuffer * michael@0: evbuffer_overlapped_new(evutil_socket_t fd) michael@0: { michael@0: struct evbuffer_overlapped *evo; michael@0: michael@0: evo = mm_calloc(1, sizeof(struct evbuffer_overlapped)); michael@0: if (!evo) michael@0: return NULL; michael@0: michael@0: TAILQ_INIT(&evo->buffer.callbacks); michael@0: evo->buffer.refcnt = 1; michael@0: evo->buffer.last_with_datap = &evo->buffer.first; michael@0: michael@0: evo->buffer.is_overlapped = 1; michael@0: evo->fd = fd; michael@0: michael@0: return &evo->buffer; michael@0: } michael@0: michael@0: int michael@0: evbuffer_launch_write(struct evbuffer *buf, ev_ssize_t at_most, michael@0: struct event_overlapped *ol) michael@0: { michael@0: struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf); michael@0: int r = -1; michael@0: int i; michael@0: struct evbuffer_chain *chain; michael@0: DWORD bytesSent; michael@0: michael@0: if (!buf) { michael@0: /* No buffer, or it isn't overlapped */ michael@0: return -1; michael@0: } michael@0: michael@0: EVBUFFER_LOCK(buf); michael@0: EVUTIL_ASSERT(!buf_o->read_in_progress); michael@0: if (buf->freeze_start || buf_o->write_in_progress) michael@0: goto done; michael@0: if (!buf->total_len) { michael@0: /* Nothing to write */ michael@0: r = 0; michael@0: goto done; michael@0: } else if (at_most < 0 || (size_t)at_most > buf->total_len) { michael@0: at_most = buf->total_len; michael@0: } michael@0: evbuffer_freeze(buf, 1); michael@0: michael@0: buf_o->first_pinned = NULL; michael@0: buf_o->n_buffers = 0; michael@0: memset(buf_o->buffers, 0, sizeof(buf_o->buffers)); michael@0: michael@0: chain = buf_o->first_pinned = buf->first; michael@0: michael@0: for (i=0; i < MAX_WSABUFS && chain; ++i, chain=chain->next) { michael@0: WSABUF *b = &buf_o->buffers[i]; michael@0: b->buf = (char*)( chain->buffer + chain->misalign ); michael@0: _evbuffer_chain_pin(chain, EVBUFFER_MEM_PINNED_W); michael@0: michael@0: if ((size_t)at_most > chain->off) { michael@0: /* XXXX Cast is safe for now, since win32 has no michael@0: mmaped chains. But later, we need to have this michael@0: add more WSAbufs if chain->off is greater than michael@0: ULONG_MAX */ michael@0: b->len = (unsigned long)chain->off; michael@0: at_most -= chain->off; michael@0: } else { michael@0: b->len = (unsigned long)at_most; michael@0: ++i; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: buf_o->n_buffers = i; michael@0: _evbuffer_incref(buf); michael@0: if (WSASend(buf_o->fd, buf_o->buffers, i, &bytesSent, 0, michael@0: &ol->overlapped, NULL)) { michael@0: int error = WSAGetLastError(); michael@0: if (error != WSA_IO_PENDING) { michael@0: /* An actual error. */ michael@0: pin_release(buf_o, EVBUFFER_MEM_PINNED_W); michael@0: evbuffer_unfreeze(buf, 1); michael@0: evbuffer_free(buf); /* decref */ michael@0: goto done; michael@0: } michael@0: } michael@0: michael@0: buf_o->write_in_progress = 1; michael@0: r = 0; michael@0: done: michael@0: EVBUFFER_UNLOCK(buf); michael@0: return r; michael@0: } michael@0: michael@0: int michael@0: evbuffer_launch_read(struct evbuffer *buf, size_t at_most, michael@0: struct event_overlapped *ol) michael@0: { michael@0: struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf); michael@0: int r = -1, i; michael@0: int nvecs; michael@0: int npin=0; michael@0: struct evbuffer_chain *chain=NULL, **chainp; michael@0: DWORD bytesRead; michael@0: DWORD flags = 0; michael@0: struct evbuffer_iovec vecs[MAX_WSABUFS]; michael@0: michael@0: if (!buf_o) michael@0: return -1; michael@0: EVBUFFER_LOCK(buf); michael@0: EVUTIL_ASSERT(!buf_o->write_in_progress); michael@0: if (buf->freeze_end || buf_o->read_in_progress) michael@0: goto done; michael@0: michael@0: buf_o->first_pinned = NULL; michael@0: buf_o->n_buffers = 0; michael@0: memset(buf_o->buffers, 0, sizeof(buf_o->buffers)); michael@0: michael@0: if (_evbuffer_expand_fast(buf, at_most, MAX_WSABUFS) == -1) michael@0: goto done; michael@0: evbuffer_freeze(buf, 0); michael@0: michael@0: nvecs = _evbuffer_read_setup_vecs(buf, at_most, michael@0: vecs, MAX_WSABUFS, &chainp, 1); michael@0: for (i=0;ibuffers[i], michael@0: &vecs[i]); michael@0: } michael@0: michael@0: buf_o->n_buffers = nvecs; michael@0: buf_o->first_pinned = chain = *chainp; michael@0: michael@0: npin=0; michael@0: for ( ; chain; chain = chain->next) { michael@0: _evbuffer_chain_pin(chain, EVBUFFER_MEM_PINNED_R); michael@0: ++npin; michael@0: } michael@0: EVUTIL_ASSERT(npin == nvecs); michael@0: michael@0: _evbuffer_incref(buf); michael@0: if (WSARecv(buf_o->fd, buf_o->buffers, nvecs, &bytesRead, &flags, michael@0: &ol->overlapped, NULL)) { michael@0: int error = WSAGetLastError(); michael@0: if (error != WSA_IO_PENDING) { michael@0: /* An actual error. */ michael@0: pin_release(buf_o, EVBUFFER_MEM_PINNED_R); michael@0: evbuffer_unfreeze(buf, 0); michael@0: evbuffer_free(buf); /* decref */ michael@0: goto done; michael@0: } michael@0: } michael@0: michael@0: buf_o->read_in_progress = 1; michael@0: r = 0; michael@0: done: michael@0: EVBUFFER_UNLOCK(buf); michael@0: return r; michael@0: } michael@0: michael@0: evutil_socket_t michael@0: _evbuffer_overlapped_get_fd(struct evbuffer *buf) michael@0: { michael@0: struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf); michael@0: return buf_o ? buf_o->fd : -1; michael@0: } michael@0: michael@0: void michael@0: _evbuffer_overlapped_set_fd(struct evbuffer *buf, evutil_socket_t fd) michael@0: { michael@0: struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf); michael@0: EVBUFFER_LOCK(buf); michael@0: /* XXX is this right?, should it cancel current I/O operations? */ michael@0: if (buf_o) michael@0: buf_o->fd = fd; michael@0: EVBUFFER_UNLOCK(buf); michael@0: }