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: #ifndef _EVENT_IOCP_INTERNAL_H michael@0: #define _EVENT_IOCP_INTERNAL_H michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: struct event_overlapped; michael@0: struct event_iocp_port; michael@0: struct evbuffer; michael@0: typedef void (*iocp_callback)(struct event_overlapped *, ev_uintptr_t, ev_ssize_t, int success); michael@0: michael@0: /* This whole file is actually win32 only. We wrap the structures in a win32 michael@0: * ifdef so that we can test-compile code that uses these interfaces on michael@0: * non-win32 platforms. */ michael@0: #ifdef WIN32 michael@0: michael@0: /** michael@0: Internal use only. Wraps an OVERLAPPED that we're using for libevent michael@0: functionality. Whenever an event_iocp_port gets an event for a given michael@0: OVERLAPPED*, it upcasts the pointer to an event_overlapped, and calls the michael@0: iocp_callback function with the event_overlapped, the iocp key, and the michael@0: number of bytes transferred as arguments. michael@0: */ michael@0: struct event_overlapped { michael@0: OVERLAPPED overlapped; michael@0: iocp_callback cb; michael@0: }; michael@0: michael@0: /* Mingw's headers don't define LPFN_ACCEPTEX. */ michael@0: michael@0: typedef BOOL (WINAPI *AcceptExPtr)(SOCKET, SOCKET, PVOID, DWORD, DWORD, DWORD, LPDWORD, LPOVERLAPPED); michael@0: typedef BOOL (WINAPI *ConnectExPtr)(SOCKET, const struct sockaddr *, int, PVOID, DWORD, LPDWORD, LPOVERLAPPED); michael@0: typedef void (WINAPI *GetAcceptExSockaddrsPtr)(PVOID, DWORD, DWORD, DWORD, LPSOCKADDR *, LPINT, LPSOCKADDR *, LPINT); michael@0: michael@0: /** Internal use only. Holds pointers to functions that only some versions of michael@0: Windows provide. michael@0: */ michael@0: struct win32_extension_fns { michael@0: AcceptExPtr AcceptEx; michael@0: ConnectExPtr ConnectEx; michael@0: GetAcceptExSockaddrsPtr GetAcceptExSockaddrs; michael@0: }; michael@0: michael@0: /** michael@0: Internal use only. Stores a Windows IO Completion port, along with michael@0: related data. michael@0: */ michael@0: struct event_iocp_port { michael@0: /** The port itself */ michael@0: HANDLE port; michael@0: /* A lock to cover internal structures. */ michael@0: CRITICAL_SECTION lock; michael@0: /** Number of threads ever open on the port. */ michael@0: short n_threads; michael@0: /** True iff we're shutting down all the threads on this port */ michael@0: short shutdown; michael@0: /** How often the threads on this port check for shutdown and other michael@0: * conditions */ michael@0: long ms; michael@0: /* The threads that are waiting for events. */ michael@0: HANDLE *threads; michael@0: /** Number of threads currently open on this port. */ michael@0: short n_live_threads; michael@0: /** A semaphore to signal when we are done shutting down. */ michael@0: HANDLE *shutdownSemaphore; michael@0: }; michael@0: michael@0: const struct win32_extension_fns *event_get_win32_extension_fns(void); michael@0: #else michael@0: /* Dummy definition so we can test-compile more things on unix. */ michael@0: struct event_overlapped { michael@0: iocp_callback cb; michael@0: }; michael@0: #endif michael@0: michael@0: /** Initialize the fields in an event_overlapped. michael@0: michael@0: @param overlapped The struct event_overlapped to initialize michael@0: @param cb The callback that should be invoked once the IO operation has michael@0: finished. michael@0: */ michael@0: void event_overlapped_init(struct event_overlapped *, iocp_callback cb); michael@0: michael@0: /** Allocate and return a new evbuffer that supports overlapped IO on a given michael@0: socket. The socket must be associated with an IO completion port using michael@0: event_iocp_port_associate. michael@0: */ michael@0: struct evbuffer *evbuffer_overlapped_new(evutil_socket_t fd); michael@0: michael@0: /** XXXX Document (nickm) */ michael@0: evutil_socket_t _evbuffer_overlapped_get_fd(struct evbuffer *buf); michael@0: michael@0: void _evbuffer_overlapped_set_fd(struct evbuffer *buf, evutil_socket_t fd); michael@0: michael@0: /** Start reading data onto the end of an overlapped evbuffer. michael@0: michael@0: An evbuffer can only have one read pending at a time. While the read michael@0: is in progress, no other data may be added to the end of the buffer. michael@0: The buffer must be created with event_overlapped_init(). michael@0: evbuffer_commit_read() must be called in the completion callback. michael@0: michael@0: @param buf The buffer to read onto michael@0: @param n The number of bytes to try to read. michael@0: @param ol Overlapped object with associated completion callback. michael@0: @return 0 on success, -1 on error. michael@0: */ michael@0: int evbuffer_launch_read(struct evbuffer *buf, size_t n, struct event_overlapped *ol); michael@0: michael@0: /** Start writing data from the start of an evbuffer. michael@0: michael@0: An evbuffer can only have one write pending at a time. While the write is michael@0: in progress, no other data may be removed from the front of the buffer. michael@0: The buffer must be created with event_overlapped_init(). michael@0: evbuffer_commit_write() must be called in the completion callback. michael@0: michael@0: @param buf The buffer to read onto michael@0: @param n The number of bytes to try to read. michael@0: @param ol Overlapped object with associated completion callback. michael@0: @return 0 on success, -1 on error. michael@0: */ michael@0: int evbuffer_launch_write(struct evbuffer *buf, ev_ssize_t n, struct event_overlapped *ol); michael@0: michael@0: /** XXX document */ michael@0: void evbuffer_commit_read(struct evbuffer *, ev_ssize_t); michael@0: void evbuffer_commit_write(struct evbuffer *, ev_ssize_t); michael@0: michael@0: /** Create an IOCP, and launch its worker threads. Internal use only. michael@0: michael@0: This interface is unstable, and will change. michael@0: */ michael@0: struct event_iocp_port *event_iocp_port_launch(int n_cpus); michael@0: michael@0: /** Associate a file descriptor with an iocp, such that overlapped IO on the michael@0: fd will happen on one of the iocp's worker threads. michael@0: */ michael@0: int event_iocp_port_associate(struct event_iocp_port *port, evutil_socket_t fd, michael@0: ev_uintptr_t key); michael@0: michael@0: /** Tell all threads serving an iocp to stop. Wait for up to waitMsec for all michael@0: the threads to finish whatever they're doing. If waitMsec is -1, wait michael@0: as long as required. If all the threads are done, free the port and return michael@0: 0. Otherwise, return -1. If you get a -1 return value, it is safe to call michael@0: this function again. michael@0: */ michael@0: int event_iocp_shutdown(struct event_iocp_port *port, long waitMsec); michael@0: michael@0: /* FIXME document. */ michael@0: int event_iocp_activate_overlapped(struct event_iocp_port *port, michael@0: struct event_overlapped *o, michael@0: ev_uintptr_t key, ev_uint32_t n_bytes); michael@0: michael@0: struct event_base; michael@0: /* FIXME document. */ michael@0: struct event_iocp_port *event_base_get_iocp(struct event_base *base); michael@0: michael@0: /* FIXME document. */ michael@0: int event_base_start_iocp(struct event_base *base, int n_cpus); michael@0: void event_base_stop_iocp(struct event_base *base); michael@0: michael@0: /* FIXME document. */ michael@0: struct bufferevent *bufferevent_async_new(struct event_base *base, michael@0: evutil_socket_t fd, int options); michael@0: michael@0: /* FIXME document. */ michael@0: void bufferevent_async_set_connected(struct bufferevent *bev); michael@0: int bufferevent_async_can_connect(struct bufferevent *bev); michael@0: int bufferevent_async_connect(struct bufferevent *bev, evutil_socket_t fd, michael@0: const struct sockaddr *sa, int socklen); michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: #endif