152845Sphk/* 252845Sphk * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson 352845Sphk * 452845Sphk * Redistribution and use in source and binary forms, with or without 552845Sphk * modification, are permitted provided that the following conditions 652845Sphk * are met: 752845Sphk * 1. Redistributions of source code must retain the above copyright 852845Sphk * notice, this list of conditions and the following disclaimer. 952845Sphk * 2. Redistributions in binary form must reproduce the above copyright 1052845Sphk * notice, this list of conditions and the following disclaimer in the 1152845Sphk * documentation and/or other materials provided with the distribution. 1252845Sphk * 3. The name of the author may not be used to endorse or promote products 1352845Sphk * derived from this software without specific prior written permission. 1452845Sphk * 1552845Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1652845Sphk * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1752845Sphk * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1852845Sphk * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1952845Sphk * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2052845Sphk * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2152845Sphk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2252845Sphk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2352845Sphk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2452845Sphk * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2552845Sphk */ 2652845Sphk 2752845Sphk#ifndef IOCP_INTERNAL_H_INCLUDED_ 2852845Sphk#define IOCP_INTERNAL_H_INCLUDED_ 2952845Sphk 3052845Sphk#ifdef __cplusplus 3152845Sphkextern "C" { 3252845Sphk#endif 3352845Sphk 3452845Sphkstruct event_overlapped; 3552845Sphkstruct event_iocp_port; 3652845Sphkstruct evbuffer; 3752845Sphktypedef void (*iocp_callback)(struct event_overlapped *, ev_uintptr_t, ev_ssize_t, int success); 3852845Sphk 3952845Sphk/* This whole file is actually win32 only. We wrap the structures in a win32 4052845Sphk * ifdef so that we can test-compile code that uses these interfaces on 4152845Sphk * non-win32 platforms. */ 4252845Sphk#ifdef _WIN32 4352845Sphk 4452845Sphk/** 4552845Sphk Internal use only. Wraps an OVERLAPPED that we're using for libevent 4652845Sphk functionality. Whenever an event_iocp_port gets an event for a given 4752845Sphk OVERLAPPED*, it upcasts the pointer to an event_overlapped, and calls the 4852845Sphk iocp_callback function with the event_overlapped, the iocp key, and the 4952845Sphk number of bytes transferred as arguments. 5052845Sphk */ 5152845Sphkstruct event_overlapped { 5252845Sphk OVERLAPPED overlapped; 5352845Sphk iocp_callback cb; 5452845Sphk}; 5552845Sphk 5652845Sphk/* Mingw's headers don't define LPFN_ACCEPTEX. */ 5752845Sphk 5852845Sphktypedef BOOL (WINAPI *AcceptExPtr)(SOCKET, SOCKET, PVOID, DWORD, DWORD, DWORD, LPDWORD, LPOVERLAPPED); 5952845Sphktypedef BOOL (WINAPI *ConnectExPtr)(SOCKET, const struct sockaddr *, int, PVOID, DWORD, LPDWORD, LPOVERLAPPED); 6052845Sphktypedef void (WINAPI *GetAcceptExSockaddrsPtr)(PVOID, DWORD, DWORD, DWORD, LPSOCKADDR *, LPINT, LPSOCKADDR *, LPINT); 6152845Sphk 6252845Sphk/** Internal use only. Holds pointers to functions that only some versions of 6352845Sphk Windows provide. 6452845Sphk */ 6552845Sphkstruct win32_extension_fns { 6652845Sphk AcceptExPtr AcceptEx; 6752845Sphk ConnectExPtr ConnectEx; 6852845Sphk GetAcceptExSockaddrsPtr GetAcceptExSockaddrs; 6952845Sphk}; 7052845Sphk 7152845Sphk/** 7252845Sphk Internal use only. Stores a Windows IO Completion port, along with 7352845Sphk related data. 7452845Sphk */ 7552845Sphkstruct event_iocp_port { 7652845Sphk /** The port itself */ 7752845Sphk HANDLE port; 7852845Sphk /* A lock to cover internal structures. */ 7952845Sphk CRITICAL_SECTION lock; 8052845Sphk /** Number of threads ever open on the port. */ 8152845Sphk short n_threads; 8252845Sphk /** True iff we're shutting down all the threads on this port */ 8352845Sphk short shutdown; 8452845Sphk /** How often the threads on this port check for shutdown and other 8552845Sphk * conditions */ 8652845Sphk long ms; 8752845Sphk /* The threads that are waiting for events. */ 8852845Sphk HANDLE *threads; 8952845Sphk /** Number of threads currently open on this port. */ 9052845Sphk short n_live_threads; 9152845Sphk /** A semaphore to signal when we are done shutting down. */ 9252845Sphk HANDLE *shutdownSemaphore; 9352845Sphk}; 9452845Sphk 9552845Sphkconst struct win32_extension_fns *event_get_win32_extension_fns_(void); 9652845Sphk#else 9752845Sphk/* Dummy definition so we can test-compile more things on unix. */ 9852845Sphkstruct event_overlapped { 9952845Sphk iocp_callback cb; 10052845Sphk}; 10152845Sphk#endif 10252845Sphk 10352845Sphk/** Initialize the fields in an event_overlapped. 10452845Sphk 10552845Sphk @param overlapped The struct event_overlapped to initialize 10652845Sphk @param cb The callback that should be invoked once the IO operation has 10752845Sphk finished. 10852845Sphk */ 10952845Sphkvoid event_overlapped_init_(struct event_overlapped *, iocp_callback cb); 11052845Sphk 11152845Sphk/** Allocate and return a new evbuffer that supports overlapped IO on a given 11252845Sphk socket. The socket must be associated with an IO completion port using 11352845Sphk event_iocp_port_associate_. 11452845Sphk*/ 11552845Sphkstruct evbuffer *evbuffer_overlapped_new_(evutil_socket_t fd); 11652845Sphk 11752845Sphk/** XXXX Document (nickm) */ 11852845Sphkevutil_socket_t evbuffer_overlapped_get_fd_(struct evbuffer *buf); 11952845Sphk 12052845Sphkvoid evbuffer_overlapped_set_fd_(struct evbuffer *buf, evutil_socket_t fd); 12152845Sphk 12252845Sphk/** Start reading data onto the end of an overlapped evbuffer. 12352845Sphk 12452845Sphk An evbuffer can only have one read pending at a time. While the read 12552845Sphk is in progress, no other data may be added to the end of the buffer. 12652845Sphk The buffer must be created with event_overlapped_init_(). 12752845Sphk evbuffer_commit_read_() must be called in the completion callback. 12852845Sphk 12952845Sphk @param buf The buffer to read onto 13052845Sphk @param n The number of bytes to try to read. 13152845Sphk @param ol Overlapped object with associated completion callback. 13252845Sphk @return 0 on success, -1 on error. 133 */ 134int evbuffer_launch_read_(struct evbuffer *buf, size_t n, struct event_overlapped *ol); 135 136/** Start writing data from the start of an evbuffer. 137 138 An evbuffer can only have one write pending at a time. While the write is 139 in progress, no other data may be removed from the front of the buffer. 140 The buffer must be created with event_overlapped_init_(). 141 evbuffer_commit_write_() must be called in the completion callback. 142 143 @param buf The buffer to read onto 144 @param n The number of bytes to try to read. 145 @param ol Overlapped object with associated completion callback. 146 @return 0 on success, -1 on error. 147 */ 148int evbuffer_launch_write_(struct evbuffer *buf, ev_ssize_t n, struct event_overlapped *ol); 149 150/** XXX document */ 151void evbuffer_commit_read_(struct evbuffer *, ev_ssize_t); 152void evbuffer_commit_write_(struct evbuffer *, ev_ssize_t); 153 154/** Create an IOCP, and launch its worker threads. Internal use only. 155 156 This interface is unstable, and will change. 157 */ 158struct event_iocp_port *event_iocp_port_launch_(int n_cpus); 159 160/** Associate a file descriptor with an iocp, such that overlapped IO on the 161 fd will happen on one of the iocp's worker threads. 162*/ 163int event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd, 164 ev_uintptr_t key); 165 166/** Tell all threads serving an iocp to stop. Wait for up to waitMsec for all 167 the threads to finish whatever they're doing. If waitMsec is -1, wait 168 as long as required. If all the threads are done, free the port and return 169 0. Otherwise, return -1. If you get a -1 return value, it is safe to call 170 this function again. 171*/ 172int event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec); 173 174/* FIXME document. */ 175int event_iocp_activate_overlapped_(struct event_iocp_port *port, 176 struct event_overlapped *o, 177 ev_uintptr_t key, ev_uint32_t n_bytes); 178 179struct event_base; 180/* FIXME document. */ 181struct event_iocp_port *event_base_get_iocp_(struct event_base *base); 182 183/* FIXME document. */ 184int event_base_start_iocp_(struct event_base *base, int n_cpus); 185void event_base_stop_iocp_(struct event_base *base); 186 187/* FIXME document. */ 188struct bufferevent *bufferevent_async_new_(struct event_base *base, 189 evutil_socket_t fd, int options); 190 191/* FIXME document. */ 192void bufferevent_async_set_connected_(struct bufferevent *bev); 193int bufferevent_async_can_connect_(struct bufferevent *bev); 194int bufferevent_async_connect_(struct bufferevent *bev, evutil_socket_t fd, 195 const struct sockaddr *sa, int socklen); 196 197#ifdef __cplusplus 198} 199#endif 200 201#endif 202