blob: 21e0e0af68070daf13c9d089b2bec754cd3a8c3e [file] [log] [blame]
Christopher Wileye8679812015-07-01 13:36:18 -07001/*
2 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
Narayan Kamathfc74cb42017-09-13 12:53:52 +010027#ifndef IOCP_INTERNAL_H_INCLUDED_
28#define IOCP_INTERNAL_H_INCLUDED_
Christopher Wileye8679812015-07-01 13:36:18 -070029
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34struct event_overlapped;
35struct event_iocp_port;
36struct evbuffer;
37typedef void (*iocp_callback)(struct event_overlapped *, ev_uintptr_t, ev_ssize_t, int success);
38
39/* This whole file is actually win32 only. We wrap the structures in a win32
40 * ifdef so that we can test-compile code that uses these interfaces on
41 * non-win32 platforms. */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010042#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -070043
44/**
45 Internal use only. Wraps an OVERLAPPED that we're using for libevent
46 functionality. Whenever an event_iocp_port gets an event for a given
47 OVERLAPPED*, it upcasts the pointer to an event_overlapped, and calls the
48 iocp_callback function with the event_overlapped, the iocp key, and the
49 number of bytes transferred as arguments.
50 */
51struct event_overlapped {
52 OVERLAPPED overlapped;
53 iocp_callback cb;
54};
55
56/* Mingw's headers don't define LPFN_ACCEPTEX. */
57
58typedef BOOL (WINAPI *AcceptExPtr)(SOCKET, SOCKET, PVOID, DWORD, DWORD, DWORD, LPDWORD, LPOVERLAPPED);
59typedef BOOL (WINAPI *ConnectExPtr)(SOCKET, const struct sockaddr *, int, PVOID, DWORD, LPDWORD, LPOVERLAPPED);
60typedef void (WINAPI *GetAcceptExSockaddrsPtr)(PVOID, DWORD, DWORD, DWORD, LPSOCKADDR *, LPINT, LPSOCKADDR *, LPINT);
61
62/** Internal use only. Holds pointers to functions that only some versions of
63 Windows provide.
64 */
65struct win32_extension_fns {
66 AcceptExPtr AcceptEx;
67 ConnectExPtr ConnectEx;
68 GetAcceptExSockaddrsPtr GetAcceptExSockaddrs;
69};
70
71/**
72 Internal use only. Stores a Windows IO Completion port, along with
73 related data.
74 */
75struct event_iocp_port {
76 /** The port itself */
77 HANDLE port;
78 /* A lock to cover internal structures. */
79 CRITICAL_SECTION lock;
80 /** Number of threads ever open on the port. */
81 short n_threads;
82 /** True iff we're shutting down all the threads on this port */
83 short shutdown;
84 /** How often the threads on this port check for shutdown and other
85 * conditions */
86 long ms;
87 /* The threads that are waiting for events. */
88 HANDLE *threads;
89 /** Number of threads currently open on this port. */
90 short n_live_threads;
91 /** A semaphore to signal when we are done shutting down. */
92 HANDLE *shutdownSemaphore;
93};
94
Haibo Huangb2279672019-05-31 16:12:39 -070095EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +010096const struct win32_extension_fns *event_get_win32_extension_fns_(void);
Christopher Wileye8679812015-07-01 13:36:18 -070097#else
98/* Dummy definition so we can test-compile more things on unix. */
99struct event_overlapped {
100 iocp_callback cb;
101};
102#endif
103
104/** Initialize the fields in an event_overlapped.
105
106 @param overlapped The struct event_overlapped to initialize
107 @param cb The callback that should be invoked once the IO operation has
108 finished.
109 */
Haibo Huangb2279672019-05-31 16:12:39 -0700110EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100111void event_overlapped_init_(struct event_overlapped *, iocp_callback cb);
Christopher Wileye8679812015-07-01 13:36:18 -0700112
113/** Allocate and return a new evbuffer that supports overlapped IO on a given
114 socket. The socket must be associated with an IO completion port using
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100115 event_iocp_port_associate_.
Christopher Wileye8679812015-07-01 13:36:18 -0700116*/
Haibo Huangb2279672019-05-31 16:12:39 -0700117EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100118struct evbuffer *evbuffer_overlapped_new_(evutil_socket_t fd);
Christopher Wileye8679812015-07-01 13:36:18 -0700119
120/** XXXX Document (nickm) */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100121evutil_socket_t evbuffer_overlapped_get_fd_(struct evbuffer *buf);
Christopher Wileye8679812015-07-01 13:36:18 -0700122
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100123void evbuffer_overlapped_set_fd_(struct evbuffer *buf, evutil_socket_t fd);
Christopher Wileye8679812015-07-01 13:36:18 -0700124
125/** Start reading data onto the end of an overlapped evbuffer.
126
127 An evbuffer can only have one read pending at a time. While the read
128 is in progress, no other data may be added to the end of the buffer.
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100129 The buffer must be created with event_overlapped_init_().
130 evbuffer_commit_read_() must be called in the completion callback.
Christopher Wileye8679812015-07-01 13:36:18 -0700131
132 @param buf The buffer to read onto
133 @param n The number of bytes to try to read.
134 @param ol Overlapped object with associated completion callback.
135 @return 0 on success, -1 on error.
136 */
Haibo Huangb2279672019-05-31 16:12:39 -0700137EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100138int evbuffer_launch_read_(struct evbuffer *buf, size_t n, struct event_overlapped *ol);
Christopher Wileye8679812015-07-01 13:36:18 -0700139
140/** Start writing data from the start of an evbuffer.
141
142 An evbuffer can only have one write pending at a time. While the write is
143 in progress, no other data may be removed from the front of the buffer.
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100144 The buffer must be created with event_overlapped_init_().
145 evbuffer_commit_write_() must be called in the completion callback.
Christopher Wileye8679812015-07-01 13:36:18 -0700146
147 @param buf The buffer to read onto
148 @param n The number of bytes to try to read.
149 @param ol Overlapped object with associated completion callback.
150 @return 0 on success, -1 on error.
151 */
Haibo Huangb2279672019-05-31 16:12:39 -0700152EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100153int evbuffer_launch_write_(struct evbuffer *buf, ev_ssize_t n, struct event_overlapped *ol);
Christopher Wileye8679812015-07-01 13:36:18 -0700154
155/** XXX document */
Haibo Huangb2279672019-05-31 16:12:39 -0700156EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100157void evbuffer_commit_read_(struct evbuffer *, ev_ssize_t);
Haibo Huangb2279672019-05-31 16:12:39 -0700158EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100159void evbuffer_commit_write_(struct evbuffer *, ev_ssize_t);
Christopher Wileye8679812015-07-01 13:36:18 -0700160
161/** Create an IOCP, and launch its worker threads. Internal use only.
162
163 This interface is unstable, and will change.
164 */
Haibo Huangb2279672019-05-31 16:12:39 -0700165EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100166struct event_iocp_port *event_iocp_port_launch_(int n_cpus);
Christopher Wileye8679812015-07-01 13:36:18 -0700167
168/** Associate a file descriptor with an iocp, such that overlapped IO on the
169 fd will happen on one of the iocp's worker threads.
170*/
Haibo Huangb2279672019-05-31 16:12:39 -0700171EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100172int event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd,
Christopher Wileye8679812015-07-01 13:36:18 -0700173 ev_uintptr_t key);
174
175/** Tell all threads serving an iocp to stop. Wait for up to waitMsec for all
176 the threads to finish whatever they're doing. If waitMsec is -1, wait
177 as long as required. If all the threads are done, free the port and return
178 0. Otherwise, return -1. If you get a -1 return value, it is safe to call
179 this function again.
180*/
Haibo Huangb2279672019-05-31 16:12:39 -0700181EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100182int event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec);
Christopher Wileye8679812015-07-01 13:36:18 -0700183
184/* FIXME document. */
Haibo Huangb2279672019-05-31 16:12:39 -0700185EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100186int event_iocp_activate_overlapped_(struct event_iocp_port *port,
Christopher Wileye8679812015-07-01 13:36:18 -0700187 struct event_overlapped *o,
188 ev_uintptr_t key, ev_uint32_t n_bytes);
189
190struct event_base;
191/* FIXME document. */
Haibo Huangb2279672019-05-31 16:12:39 -0700192EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100193struct event_iocp_port *event_base_get_iocp_(struct event_base *base);
Christopher Wileye8679812015-07-01 13:36:18 -0700194
195/* FIXME document. */
Haibo Huangb2279672019-05-31 16:12:39 -0700196EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100197int event_base_start_iocp_(struct event_base *base, int n_cpus);
198void event_base_stop_iocp_(struct event_base *base);
Christopher Wileye8679812015-07-01 13:36:18 -0700199
200/* FIXME document. */
Haibo Huangb2279672019-05-31 16:12:39 -0700201EVENT2_EXPORT_SYMBOL
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100202struct bufferevent *bufferevent_async_new_(struct event_base *base,
Christopher Wileye8679812015-07-01 13:36:18 -0700203 evutil_socket_t fd, int options);
204
205/* FIXME document. */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100206void bufferevent_async_set_connected_(struct bufferevent *bev);
207int bufferevent_async_can_connect_(struct bufferevent *bev);
208int bufferevent_async_connect_(struct bufferevent *bev, evutil_socket_t fd,
Christopher Wileye8679812015-07-01 13:36:18 -0700209 const struct sockaddr *sa, int socklen);
210
211#ifdef __cplusplus
212}
213#endif
214
215#endif