Add a bunch more text
This commit is contained in:
parent
133b4e3999
commit
a91c63ba8f
202
iocp-links.html
202
iocp-links.html
@ -1,29 +1,201 @@
|
|||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
width: 30em;
|
|
||||||
font-size: 12pt;
|
font-size: 12pt;
|
||||||
font-family: Arial;
|
font-family: Arial;
|
||||||
max-width: 30em;
|
max-width: 40em;
|
||||||
margin: 1em;
|
margin: 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
color: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover {
|
||||||
|
color: red;
|
||||||
|
}
|
||||||
|
|
||||||
|
table td {
|
||||||
|
min-width: 10em;
|
||||||
|
vertical-align: top;
|
||||||
|
padding: 0.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
table td {
|
||||||
|
border-bottom: 1px solid #bbb;
|
||||||
|
}
|
||||||
|
|
||||||
|
table tr:first-child td {
|
||||||
|
border-top: 1px solid #bbb;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
<h1>Asynchronous I/O in Windows for UNIX Programmers</h1>
|
<h1>Asynchronous I/O in Windows for UNIX Programmers</h1>
|
||||||
|
|
||||||
|
<p>This document assumes you are familiar with how non-blocking socket I/O
|
||||||
|
is done in UNIX.
|
||||||
|
|
||||||
<p>Windows has very different notions for how asynchronous and non-blocking I/O
|
<p>Windows has very different notions for how asynchronous and non-blocking I/O
|
||||||
are done. While Windows does have <code>select()</code>, it is severely
|
are done. While Windows has <code>select()</code> it supports only 64
|
||||||
limited: it supports only 64 file descriptors. (Does <code>select()</code>
|
file descriptors. Obviously Microsoft does understand how to make
|
||||||
work on named pipes in windows?) Windows has the possibility of serving
|
high-concurrency servers, they've simply choosen a different paradigm for
|
||||||
20,000 TCP connections using a different API called <a
|
this called <a
|
||||||
href="http://msdn.microsoft.com/en-us/library/ms686358(v=vs.85).aspx">overlapped
|
href="http://msdn.microsoft.com/en-us/library/ms686358(v=vs.85).aspx">overlapped
|
||||||
I/O</a> using <a
|
I/O</a>. The mechanism in Windows by which multiple sockets are polled
|
||||||
href="http://msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx">I/O
|
for compltion is called
|
||||||
completion ports</a>. Unfortunately I/O completion ports
|
<a href="http://msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx">I/O
|
||||||
|
completion ports</a>. More or less equivlant to <a
|
||||||
|
href="http://en.wikipedia.org/wiki/Kqueue">kqueue</a> (Macintosh,
|
||||||
|
FreeBSD, other BSDs), <a href="http://en.wikipedia.org/wiki/Epoll">epoll</a>
|
||||||
|
(Linux), <a
|
||||||
|
href="http://developers.sun.com/solaris/articles/event_completion.html">event
|
||||||
|
completion ports</a> (Solaris), <a href="">poll</a> (modern UNIXes), or <a
|
||||||
|
href="http://www.kernel.org/doc/man-pages/online/pages/man2/select.2.html">select</a>
|
||||||
|
(all operating systems). The main difference is that in UNIX you ask the
|
||||||
|
kernel to wait for file descriptors to change their readability or
|
||||||
|
writablity while in windows you wait for asynchronous functions to complete.
|
||||||
|
For example, instead of waiting for a socket to become writable and then
|
||||||
|
<a
|
||||||
|
href="http://www.kernel.org/doc/man-pages/online/pages/man2/write.2.html"><code>write(2)</code></a>
|
||||||
|
to it, as you do in UNIX operating systems, you rather <a
|
||||||
|
href="http://msdn.microsoft.com/en-us/library/ms742203(v=vs.85).aspx"><code>WSASend()</code></a>
|
||||||
|
a buffer and wait for it to have been sent.
|
||||||
|
The result is that non-blocking <code>write(2)</code> <code>read(2)</code>
|
||||||
|
are non-portable to Windows. This tends to throw the poor sap assigned with
|
||||||
|
the job of porting your app to Windows into complusive nervous twitches.
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Almost every socket operation that you're familar with has an
|
||||||
|
overlapped counter-part (<a href="#table-foot">see table</a>).
|
||||||
|
|
||||||
|
|
||||||
|
<p id="table-foot">
|
||||||
|
<table cellspacing=0>
|
||||||
|
<!-- TODO: links -->
|
||||||
|
<tr>
|
||||||
|
<td></td>
|
||||||
|
<td>
|
||||||
|
<pre>int fd;</pre>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<pre>HANDLE handle;</pre>
|
||||||
|
<pre>SOCKET socket;</pre>
|
||||||
|
(the two are the same type)
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>socket or pipe</td>
|
||||||
|
<td>
|
||||||
|
<code>send(2)</code>,
|
||||||
|
<code>write(2)</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<a href="http://msdn.microsoft.com/en-us/library/ms742203(v=vs.85).aspx"><code>WSASend()</code></a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>socket or pipe</td>
|
||||||
|
<td>
|
||||||
|
<code>recv(2)</code>,
|
||||||
|
<code>read(2)</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<a href="http://msdn.microsoft.com/en-us/library/ms741688(v=VS.85).aspx"><code>WSARecv()</code></a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>socket or pipe</td>
|
||||||
|
<td>
|
||||||
|
<code>connect(2)</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<a href="http://msdn.microsoft.com/en-us/library/ms737606(VS.85).aspx"><code>ConnectEx()</code></a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>file</td>
|
||||||
|
<td>
|
||||||
|
<code>write(2)</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<a
|
||||||
|
href="http://msdn.microsoft.com/en-us/library/aa365748(v=VS.85).aspx"><code>WriteFileEx()</code></a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>file</td>
|
||||||
|
<td>
|
||||||
|
<code>read(2)</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<a
|
||||||
|
href="http://msdn.microsoft.com/en-us/library/aa365468(v=VS.85).aspx"><code>ReadFileEx()</code></a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>socket and file</td>
|
||||||
|
<td>
|
||||||
|
<code>sendfile()</code> [<a href="#sendfile-foot">1</a>]
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<a
|
||||||
|
href="http://msdn.microsoft.com/en-us/library/ms740565(v=VS.85).aspx"><code>TransmitFile()</code></a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>tty</td>
|
||||||
|
<td>
|
||||||
|
<a
|
||||||
|
href="http://www.kernel.org/doc/man-pages/online/pages/man3/tcsetattr.3.html"><code>tcsetattr(3)</code></a>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<a href="http://msdn.microsoft.com/en-us/library/ms686033(VS.85).aspx"><code>SetConsoleMode()</code></a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>tty</td>
|
||||||
|
<td>
|
||||||
|
<code>read(2)</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<a
|
||||||
|
href="http://msdn.microsoft.com/en-us/library/ms684958(v=VS.85).aspx"><code>ReadConsole()</code></a>
|
||||||
|
and
|
||||||
|
<a
|
||||||
|
href="http://msdn.microsoft.com/en-us/library/ms684961(v=VS.85).aspx"><code>ReadConsoleInput()</code></a>
|
||||||
|
do not support overlapped I/O and there are no overlapped
|
||||||
|
counter-parts. One strategy to get around this is
|
||||||
|
<pre><a
|
||||||
|
href="http://msdn.microsoft.com/en-us/library/ms685061(VS.85).aspx">RegisterWaitForSingleObject</a>(&tty_wait_handle, tty_handle,
|
||||||
|
tty_want_poll, NULL, INFINITE, WT_EXECUTEINWAITTHREAD |
|
||||||
|
WT_EXECUTEONLYONCE)</pre>
|
||||||
|
which will execute <code>tty_want_poll()</code> in a different thread.
|
||||||
|
You can use this to notify the calling thread that
|
||||||
|
<code>ReadConsoleInput()</code> will not block.
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>tty</td>
|
||||||
|
<td>
|
||||||
|
<code>write(2)</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<a
|
||||||
|
href="http://msdn.microsoft.com/en-us/library/ms687401(v=VS.85).aspx"><code>WriteConsole()</code></a>
|
||||||
|
is also blocking but this is probably acceptable.
|
||||||
|
</td>
|
||||||
|
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<p id="sendfile-foot">[1] <code>sendfile()</code> on UNIX has not been agreed
|
||||||
|
on yet. Each operating system has a slightly different API.
|
||||||
|
|
||||||
|
|
||||||
|
<p id="foot2">
|
||||||
|
<p>
|
||||||
tips
|
tips
|
||||||
<ul>
|
<ul>
|
||||||
<li> overlapped = non-blocking.
|
<li> overlapped = non-blocking.
|
||||||
@ -31,6 +203,7 @@ tips
|
|||||||
<li> <a href=http://msdn.microsoft.com/en-us/library/ms740673(VS.85).aspx"><code>Windows Sockets 2</code></a>
|
<li> <a href=http://msdn.microsoft.com/en-us/library/ms740673(VS.85).aspx"><code>Windows Sockets 2</code></a>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
<p>
|
||||||
IOCP:
|
IOCP:
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="http://msdn.microsoft.com/en-us/library/ms686358(v=vs.85).aspx">Synchronization and Overlapped Input and Output</a>
|
<li><a href="http://msdn.microsoft.com/en-us/library/ms686358(v=vs.85).aspx">Synchronization and Overlapped Input and Output</a>
|
||||||
@ -48,8 +221,15 @@ IOCP:
|
|||||||
— an async <code>sendfile()</code> for windows.
|
— an async <code>sendfile()</code> for windows.
|
||||||
<li><a href="http://msdn.microsoft.com/en-us/library/ms741565(v=VS.85).aspx"><code>WSADuplicateSocket()</code></a>
|
<li><a href="http://msdn.microsoft.com/en-us/library/ms741565(v=VS.85).aspx"><code>WSADuplicateSocket()</code></a>
|
||||||
— describes how to share a socket between two processes.
|
— describes how to share a socket between two processes.
|
||||||
|
<li><a href="http://msdn.microsoft.com/en-us/library/6e3b887c.aspx"><code>_setmaxstdio()</code></a>
|
||||||
|
— something like setting the maximum number of file decriptors
|
||||||
|
and <a
|
||||||
|
href="http://www.kernel.org/doc/man-pages/online/pages/man2/setrlimit.2.html"><code>setrlimit(3)</code></a>
|
||||||
|
AKA <code>ulimit -n</code>. Note the file descriptor limit on windows is
|
||||||
|
2048.
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
<p>
|
||||||
APC:
|
APC:
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="http://msdn.microsoft.com/en-us/library/ms681951(v=vs.85).aspx">Asynchronous Procedure Calls</a>
|
<li><a href="http://msdn.microsoft.com/en-us/library/ms681951(v=vs.85).aspx">Asynchronous Procedure Calls</a>
|
||||||
|
|||||||
11
ol.h
11
ol.h
@ -50,9 +50,20 @@ int ol_socket_pause(ol_socket* socket);
|
|||||||
int ol_socket_resume(ol_socket* socket);
|
int ol_socket_resume(ol_socket* socket);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get local address. addr is filled.
|
||||||
|
*/
|
||||||
int ol_socket_address(ol_socket* socket, ol_addr* addr);
|
int ol_socket_address(ol_socket* socket, ol_addr* addr);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns file descriptor. There may be only limited numbers of file
|
||||||
|
* descriptors allowed by the operating system. On Windows this limit is
|
||||||
|
* 2048 (see _setmaxstdio[http://msdn.microsoft.com/en-us/library/6e3b887c.aspx])
|
||||||
|
*/
|
||||||
|
int ol_socket_get_fd(ol_socket* socket);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send data to socket. User responsible for bufs until callback is made.
|
* Send data to socket. User responsible for bufs until callback is made.
|
||||||
* Multiple ol_socket_write() calls may be issued before the previous ones
|
* Multiple ol_socket_write() calls may be issued before the previous ones
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user