foolio 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. data/.gitignore +22 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +21 -0
  6. data/examples/timer.rb +20 -0
  7. data/ext/foolio/extconf.rb +34 -0
  8. data/ext/foolio/foolio_ext.c +921 -0
  9. data/ext/foolio/gen.rb +50 -0
  10. data/ext/foolio/make_table.rb +12 -0
  11. data/ext/foolio/templ +243 -0
  12. data/ext/libuv/.gitignore +33 -0
  13. data/ext/libuv/.mailmap +13 -0
  14. data/ext/libuv/.travis.yml +9 -0
  15. data/ext/libuv/AUTHORS +61 -0
  16. data/ext/libuv/LICENSE +44 -0
  17. data/ext/libuv/Makefile +71 -0
  18. data/ext/libuv/README.md +90 -0
  19. data/ext/libuv/common.gypi +178 -0
  20. data/ext/libuv/gyp_uv +73 -0
  21. data/ext/libuv/include/uv-private/eio.h +403 -0
  22. data/ext/libuv/include/uv-private/ev.h +838 -0
  23. data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
  24. data/ext/libuv/include/uv-private/tree.h +768 -0
  25. data/ext/libuv/include/uv-private/uv-unix.h +324 -0
  26. data/ext/libuv/include/uv-private/uv-win.h +517 -0
  27. data/ext/libuv/include/uv.h +1838 -0
  28. data/ext/libuv/src/fs-poll.c +235 -0
  29. data/ext/libuv/src/inet.c +293 -0
  30. data/ext/libuv/src/unix/async.c +148 -0
  31. data/ext/libuv/src/unix/core.c +696 -0
  32. data/ext/libuv/src/unix/cygwin.c +83 -0
  33. data/ext/libuv/src/unix/darwin.c +342 -0
  34. data/ext/libuv/src/unix/dl.c +83 -0
  35. data/ext/libuv/src/unix/eio/Changes +63 -0
  36. data/ext/libuv/src/unix/eio/LICENSE +36 -0
  37. data/ext/libuv/src/unix/eio/Makefile.am +15 -0
  38. data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
  39. data/ext/libuv/src/unix/eio/autogen.sh +3 -0
  40. data/ext/libuv/src/unix/eio/config.h.in +86 -0
  41. data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
  42. data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
  43. data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
  44. data/ext/libuv/src/unix/eio/config_linux.h +94 -0
  45. data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
  46. data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
  47. data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
  48. data/ext/libuv/src/unix/eio/configure.ac +22 -0
  49. data/ext/libuv/src/unix/eio/demo.c +194 -0
  50. data/ext/libuv/src/unix/eio/ecb.h +370 -0
  51. data/ext/libuv/src/unix/eio/eio.3 +3428 -0
  52. data/ext/libuv/src/unix/eio/eio.c +2593 -0
  53. data/ext/libuv/src/unix/eio/eio.pod +969 -0
  54. data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
  55. data/ext/libuv/src/unix/eio/xthread.h +164 -0
  56. data/ext/libuv/src/unix/error.c +105 -0
  57. data/ext/libuv/src/unix/ev/Changes +388 -0
  58. data/ext/libuv/src/unix/ev/LICENSE +36 -0
  59. data/ext/libuv/src/unix/ev/Makefile.am +18 -0
  60. data/ext/libuv/src/unix/ev/Makefile.in +771 -0
  61. data/ext/libuv/src/unix/ev/README +58 -0
  62. data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
  63. data/ext/libuv/src/unix/ev/autogen.sh +6 -0
  64. data/ext/libuv/src/unix/ev/config.guess +1526 -0
  65. data/ext/libuv/src/unix/ev/config.h.in +125 -0
  66. data/ext/libuv/src/unix/ev/config.sub +1658 -0
  67. data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
  68. data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
  69. data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
  70. data/ext/libuv/src/unix/ev/config_linux.h +141 -0
  71. data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
  72. data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
  73. data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
  74. data/ext/libuv/src/unix/ev/configure +13037 -0
  75. data/ext/libuv/src/unix/ev/configure.ac +18 -0
  76. data/ext/libuv/src/unix/ev/depcomp +630 -0
  77. data/ext/libuv/src/unix/ev/ev++.h +816 -0
  78. data/ext/libuv/src/unix/ev/ev.3 +5311 -0
  79. data/ext/libuv/src/unix/ev/ev.c +3925 -0
  80. data/ext/libuv/src/unix/ev/ev.pod +5243 -0
  81. data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
  82. data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
  83. data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
  84. data/ext/libuv/src/unix/ev/ev_port.c +179 -0
  85. data/ext/libuv/src/unix/ev/ev_select.c +310 -0
  86. data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
  87. data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
  88. data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
  89. data/ext/libuv/src/unix/ev/event.c +402 -0
  90. data/ext/libuv/src/unix/ev/event.h +170 -0
  91. data/ext/libuv/src/unix/ev/install-sh +294 -0
  92. data/ext/libuv/src/unix/ev/libev.m4 +39 -0
  93. data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
  94. data/ext/libuv/src/unix/ev/missing +336 -0
  95. data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
  96. data/ext/libuv/src/unix/freebsd.c +326 -0
  97. data/ext/libuv/src/unix/fs.c +739 -0
  98. data/ext/libuv/src/unix/internal.h +188 -0
  99. data/ext/libuv/src/unix/kqueue.c +120 -0
  100. data/ext/libuv/src/unix/linux/inotify.c +239 -0
  101. data/ext/libuv/src/unix/linux/linux-core.c +557 -0
  102. data/ext/libuv/src/unix/linux/syscalls.c +388 -0
  103. data/ext/libuv/src/unix/linux/syscalls.h +124 -0
  104. data/ext/libuv/src/unix/loop-watcher.c +62 -0
  105. data/ext/libuv/src/unix/loop.c +94 -0
  106. data/ext/libuv/src/unix/netbsd.c +108 -0
  107. data/ext/libuv/src/unix/openbsd.c +295 -0
  108. data/ext/libuv/src/unix/pipe.c +259 -0
  109. data/ext/libuv/src/unix/poll.c +114 -0
  110. data/ext/libuv/src/unix/process.c +495 -0
  111. data/ext/libuv/src/unix/signal.c +269 -0
  112. data/ext/libuv/src/unix/stream.c +990 -0
  113. data/ext/libuv/src/unix/sunos.c +481 -0
  114. data/ext/libuv/src/unix/tcp.c +393 -0
  115. data/ext/libuv/src/unix/thread.c +251 -0
  116. data/ext/libuv/src/unix/timer.c +136 -0
  117. data/ext/libuv/src/unix/tty.c +145 -0
  118. data/ext/libuv/src/unix/udp.c +659 -0
  119. data/ext/libuv/src/unix/uv-eio.c +107 -0
  120. data/ext/libuv/src/unix/uv-eio.h +13 -0
  121. data/ext/libuv/src/uv-common.c +380 -0
  122. data/ext/libuv/src/uv-common.h +170 -0
  123. data/ext/libuv/src/win/async.c +100 -0
  124. data/ext/libuv/src/win/atomicops-inl.h +56 -0
  125. data/ext/libuv/src/win/core.c +278 -0
  126. data/ext/libuv/src/win/dl.c +86 -0
  127. data/ext/libuv/src/win/error.c +155 -0
  128. data/ext/libuv/src/win/fs-event.c +510 -0
  129. data/ext/libuv/src/win/fs.c +1948 -0
  130. data/ext/libuv/src/win/getaddrinfo.c +365 -0
  131. data/ext/libuv/src/win/handle-inl.h +149 -0
  132. data/ext/libuv/src/win/handle.c +154 -0
  133. data/ext/libuv/src/win/internal.h +343 -0
  134. data/ext/libuv/src/win/loop-watcher.c +122 -0
  135. data/ext/libuv/src/win/pipe.c +1672 -0
  136. data/ext/libuv/src/win/poll.c +616 -0
  137. data/ext/libuv/src/win/process-stdio.c +500 -0
  138. data/ext/libuv/src/win/process.c +1013 -0
  139. data/ext/libuv/src/win/req-inl.h +220 -0
  140. data/ext/libuv/src/win/req.c +25 -0
  141. data/ext/libuv/src/win/signal.c +57 -0
  142. data/ext/libuv/src/win/stream-inl.h +67 -0
  143. data/ext/libuv/src/win/stream.c +167 -0
  144. data/ext/libuv/src/win/tcp.c +1394 -0
  145. data/ext/libuv/src/win/thread.c +372 -0
  146. data/ext/libuv/src/win/threadpool.c +74 -0
  147. data/ext/libuv/src/win/timer.c +224 -0
  148. data/ext/libuv/src/win/tty.c +1799 -0
  149. data/ext/libuv/src/win/udp.c +716 -0
  150. data/ext/libuv/src/win/util.c +864 -0
  151. data/ext/libuv/src/win/winapi.c +132 -0
  152. data/ext/libuv/src/win/winapi.h +4452 -0
  153. data/ext/libuv/src/win/winsock.c +557 -0
  154. data/ext/libuv/src/win/winsock.h +171 -0
  155. data/ext/libuv/test/benchmark-async-pummel.c +97 -0
  156. data/ext/libuv/test/benchmark-async.c +137 -0
  157. data/ext/libuv/test/benchmark-fs-stat.c +135 -0
  158. data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
  159. data/ext/libuv/test/benchmark-list.h +127 -0
  160. data/ext/libuv/test/benchmark-loop-count.c +88 -0
  161. data/ext/libuv/test/benchmark-million-timers.c +65 -0
  162. data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
  163. data/ext/libuv/test/benchmark-pound.c +324 -0
  164. data/ext/libuv/test/benchmark-pump.c +462 -0
  165. data/ext/libuv/test/benchmark-sizes.c +44 -0
  166. data/ext/libuv/test/benchmark-spawn.c +162 -0
  167. data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
  168. data/ext/libuv/test/benchmark-thread.c +64 -0
  169. data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
  170. data/ext/libuv/test/blackhole-server.c +118 -0
  171. data/ext/libuv/test/dns-server.c +321 -0
  172. data/ext/libuv/test/echo-server.c +378 -0
  173. data/ext/libuv/test/fixtures/empty_file +0 -0
  174. data/ext/libuv/test/fixtures/load_error.node +1 -0
  175. data/ext/libuv/test/run-benchmarks.c +64 -0
  176. data/ext/libuv/test/run-tests.c +138 -0
  177. data/ext/libuv/test/runner-unix.c +295 -0
  178. data/ext/libuv/test/runner-unix.h +36 -0
  179. data/ext/libuv/test/runner-win.c +285 -0
  180. data/ext/libuv/test/runner-win.h +42 -0
  181. data/ext/libuv/test/runner.c +355 -0
  182. data/ext/libuv/test/runner.h +159 -0
  183. data/ext/libuv/test/task.h +112 -0
  184. data/ext/libuv/test/test-async.c +118 -0
  185. data/ext/libuv/test/test-callback-order.c +76 -0
  186. data/ext/libuv/test/test-callback-stack.c +203 -0
  187. data/ext/libuv/test/test-connection-fail.c +148 -0
  188. data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
  189. data/ext/libuv/test/test-delayed-accept.c +188 -0
  190. data/ext/libuv/test/test-dlerror.c +58 -0
  191. data/ext/libuv/test/test-error.c +59 -0
  192. data/ext/libuv/test/test-fail-always.c +29 -0
  193. data/ext/libuv/test/test-fs-event.c +474 -0
  194. data/ext/libuv/test/test-fs-poll.c +146 -0
  195. data/ext/libuv/test/test-fs.c +1843 -0
  196. data/ext/libuv/test/test-get-currentexe.c +63 -0
  197. data/ext/libuv/test/test-get-loadavg.c +36 -0
  198. data/ext/libuv/test/test-get-memory.c +38 -0
  199. data/ext/libuv/test/test-getaddrinfo.c +122 -0
  200. data/ext/libuv/test/test-getsockname.c +342 -0
  201. data/ext/libuv/test/test-hrtime.c +54 -0
  202. data/ext/libuv/test/test-idle.c +81 -0
  203. data/ext/libuv/test/test-ipc-send-recv.c +209 -0
  204. data/ext/libuv/test/test-ipc.c +620 -0
  205. data/ext/libuv/test/test-list.h +427 -0
  206. data/ext/libuv/test/test-loop-handles.c +336 -0
  207. data/ext/libuv/test/test-multiple-listen.c +102 -0
  208. data/ext/libuv/test/test-mutexes.c +63 -0
  209. data/ext/libuv/test/test-pass-always.c +28 -0
  210. data/ext/libuv/test/test-ping-pong.c +253 -0
  211. data/ext/libuv/test/test-pipe-bind-error.c +140 -0
  212. data/ext/libuv/test/test-pipe-connect-error.c +96 -0
  213. data/ext/libuv/test/test-platform-output.c +87 -0
  214. data/ext/libuv/test/test-poll-close.c +72 -0
  215. data/ext/libuv/test/test-poll.c +573 -0
  216. data/ext/libuv/test/test-process-title.c +49 -0
  217. data/ext/libuv/test/test-ref.c +338 -0
  218. data/ext/libuv/test/test-run-once.c +48 -0
  219. data/ext/libuv/test/test-semaphore.c +111 -0
  220. data/ext/libuv/test/test-shutdown-close.c +103 -0
  221. data/ext/libuv/test/test-shutdown-eof.c +183 -0
  222. data/ext/libuv/test/test-signal.c +162 -0
  223. data/ext/libuv/test/test-spawn.c +863 -0
  224. data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
  225. data/ext/libuv/test/test-tcp-bind-error.c +191 -0
  226. data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
  227. data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
  228. data/ext/libuv/test/test-tcp-close.c +129 -0
  229. data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  230. data/ext/libuv/test/test-tcp-connect-error.c +70 -0
  231. data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
  232. data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
  233. data/ext/libuv/test/test-tcp-flags.c +51 -0
  234. data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  235. data/ext/libuv/test/test-tcp-unexpected-read.c +113 -0
  236. data/ext/libuv/test/test-tcp-write-error.c +168 -0
  237. data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
  238. data/ext/libuv/test/test-tcp-writealot.c +170 -0
  239. data/ext/libuv/test/test-thread.c +183 -0
  240. data/ext/libuv/test/test-threadpool.c +57 -0
  241. data/ext/libuv/test/test-timer-again.c +141 -0
  242. data/ext/libuv/test/test-timer.c +152 -0
  243. data/ext/libuv/test/test-tty.c +110 -0
  244. data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
  245. data/ext/libuv/test/test-udp-ipv6.c +156 -0
  246. data/ext/libuv/test/test-udp-multicast-join.c +139 -0
  247. data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
  248. data/ext/libuv/test/test-udp-options.c +86 -0
  249. data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
  250. data/ext/libuv/test/test-util.c +97 -0
  251. data/ext/libuv/test/test-walk-handles.c +77 -0
  252. data/ext/libuv/uv.gyp +375 -0
  253. data/ext/libuv/vcbuild.bat +105 -0
  254. data/foolio.gemspec +18 -0
  255. data/lib/foolio.rb +9 -0
  256. data/lib/foolio/handle.rb +27 -0
  257. data/lib/foolio/listener.rb +26 -0
  258. data/lib/foolio/loop.rb +79 -0
  259. data/lib/foolio/stream.rb +109 -0
  260. data/lib/foolio/version.rb +3 -0
  261. metadata +309 -0
@@ -0,0 +1,1394 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to
5
+ * deal in the Software without restriction, including without limitation the
6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ * sell copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ * IN THE SOFTWARE.
20
+ */
21
+
22
+ #include <assert.h>
23
+
24
+ #include "uv.h"
25
+ #include "internal.h"
26
+ #include "handle-inl.h"
27
+ #include "stream-inl.h"
28
+ #include "req-inl.h"
29
+
30
+
31
+ /*
32
+ * Threshold of active tcp streams for which to preallocate tcp read buffers.
33
+ * (Due to node slab allocator performing poorly under this pattern,
34
+ * the optimization is temporarily disabled (threshold=0). This will be
35
+ * revisited once node allocator is improved.)
36
+ */
37
+ const unsigned int uv_active_tcp_streams_threshold = 0;
38
+
39
+ /*
40
+ * Number of simultaneous pending AcceptEx calls.
41
+ */
42
+ const unsigned int uv_simultaneous_server_accepts = 32;
43
+
44
+ /* A zero-size buffer for use by uv_tcp_read */
45
+ static char uv_zero_[] = "";
46
+
47
+ static int uv__tcp_nodelay(uv_tcp_t* handle, SOCKET socket, int enable) {
48
+ if (setsockopt(socket,
49
+ IPPROTO_TCP,
50
+ TCP_NODELAY,
51
+ (const char*)&enable,
52
+ sizeof enable) == -1) {
53
+ uv__set_sys_error(handle->loop, errno);
54
+ return -1;
55
+ }
56
+ return 0;
57
+ }
58
+
59
+
60
+ static int uv__tcp_keepalive(uv_tcp_t* handle, SOCKET socket, int enable, unsigned int delay) {
61
+ if (setsockopt(socket,
62
+ SOL_SOCKET,
63
+ SO_KEEPALIVE,
64
+ (const char*)&enable,
65
+ sizeof enable) == -1) {
66
+ uv__set_sys_error(handle->loop, errno);
67
+ return -1;
68
+ }
69
+
70
+ if (enable && setsockopt(socket,
71
+ IPPROTO_TCP,
72
+ TCP_KEEPALIVE,
73
+ (const char*)&delay,
74
+ sizeof delay) == -1) {
75
+ uv__set_sys_error(handle->loop, errno);
76
+ return -1;
77
+ }
78
+
79
+ return 0;
80
+ }
81
+
82
+
83
+ static int uv_tcp_set_socket(uv_loop_t* loop, uv_tcp_t* handle,
84
+ SOCKET socket, int imported) {
85
+ DWORD yes = 1;
86
+ int non_ifs_lsp;
87
+
88
+ assert(handle->socket == INVALID_SOCKET);
89
+
90
+ /* Set the socket to nonblocking mode */
91
+ if (ioctlsocket(socket, FIONBIO, &yes) == SOCKET_ERROR) {
92
+ uv__set_sys_error(loop, WSAGetLastError());
93
+ return -1;
94
+ }
95
+
96
+ /* Associate it with the I/O completion port. */
97
+ /* Use uv_handle_t pointer as completion key. */
98
+ if (CreateIoCompletionPort((HANDLE)socket,
99
+ loop->iocp,
100
+ (ULONG_PTR)socket,
101
+ 0) == NULL) {
102
+ if (imported) {
103
+ handle->flags |= UV_HANDLE_EMULATE_IOCP;
104
+ } else {
105
+ uv__set_sys_error(loop, GetLastError());
106
+ return -1;
107
+ }
108
+ }
109
+
110
+ non_ifs_lsp = (handle->flags & UV_HANDLE_IPV6) ? uv_tcp_non_ifs_lsp_ipv6 :
111
+ uv_tcp_non_ifs_lsp_ipv4;
112
+
113
+ if (pSetFileCompletionNotificationModes && !non_ifs_lsp) {
114
+ if (pSetFileCompletionNotificationModes((HANDLE) socket,
115
+ FILE_SKIP_SET_EVENT_ON_HANDLE |
116
+ FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)) {
117
+ handle->flags |= UV_HANDLE_SYNC_BYPASS_IOCP;
118
+ } else if (GetLastError() != ERROR_INVALID_FUNCTION) {
119
+ uv__set_sys_error(loop, GetLastError());
120
+ return -1;
121
+ }
122
+ }
123
+
124
+ if ((handle->flags & UV_HANDLE_TCP_NODELAY) &&
125
+ uv__tcp_nodelay(handle, socket, 1)) {
126
+ return -1;
127
+ }
128
+
129
+ /* TODO: Use stored delay. */
130
+ if ((handle->flags & UV_HANDLE_TCP_KEEPALIVE) &&
131
+ uv__tcp_keepalive(handle, socket, 1, 60)) {
132
+ return -1;
133
+ }
134
+
135
+ handle->socket = socket;
136
+
137
+ return 0;
138
+ }
139
+
140
+
141
+ int uv_tcp_init(uv_loop_t* loop, uv_tcp_t* handle) {
142
+ uv_stream_init(loop, (uv_stream_t*) handle, UV_TCP);
143
+
144
+ handle->accept_reqs = NULL;
145
+ handle->pending_accepts = NULL;
146
+ handle->socket = INVALID_SOCKET;
147
+ handle->reqs_pending = 0;
148
+ handle->func_acceptex = NULL;
149
+ handle->func_connectex = NULL;
150
+ handle->processed_accepts = 0;
151
+
152
+ return 0;
153
+ }
154
+
155
+
156
+ void uv_tcp_endgame(uv_loop_t* loop, uv_tcp_t* handle) {
157
+ int status;
158
+ unsigned int i;
159
+ uv_tcp_accept_t* req;
160
+
161
+ if (handle->flags & UV_HANDLE_CONNECTION &&
162
+ handle->shutdown_req != NULL &&
163
+ handle->write_reqs_pending == 0) {
164
+
165
+ UNREGISTER_HANDLE_REQ(loop, handle, handle->shutdown_req);
166
+
167
+ if (handle->flags & UV_HANDLE_CLOSING) {
168
+ status = -1;
169
+ uv__set_artificial_error(loop, UV_ECANCELED);
170
+ } else if (shutdown(handle->socket, SD_SEND) != SOCKET_ERROR) {
171
+ status = 0;
172
+ handle->flags |= UV_HANDLE_SHUT;
173
+ } else {
174
+ status = -1;
175
+ uv__set_sys_error(loop, WSAGetLastError());
176
+ }
177
+
178
+ if (handle->shutdown_req->cb) {
179
+ handle->shutdown_req->cb(handle->shutdown_req, status);
180
+ }
181
+
182
+ handle->shutdown_req = NULL;
183
+ DECREASE_PENDING_REQ_COUNT(handle);
184
+ return;
185
+ }
186
+
187
+ if (handle->flags & UV_HANDLE_CLOSING &&
188
+ handle->reqs_pending == 0) {
189
+ assert(!(handle->flags & UV_HANDLE_CLOSED));
190
+ uv__handle_stop(handle);
191
+
192
+ if (!(handle->flags & UV_HANDLE_TCP_SOCKET_CLOSED)) {
193
+ closesocket(handle->socket);
194
+ handle->flags |= UV_HANDLE_TCP_SOCKET_CLOSED;
195
+ }
196
+
197
+ if (!(handle->flags & UV_HANDLE_CONNECTION) && handle->accept_reqs) {
198
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
199
+ for (i = 0; i < uv_simultaneous_server_accepts; i++) {
200
+ req = &handle->accept_reqs[i];
201
+ if (req->wait_handle != INVALID_HANDLE_VALUE) {
202
+ UnregisterWait(req->wait_handle);
203
+ req->wait_handle = INVALID_HANDLE_VALUE;
204
+ }
205
+ if (req->event_handle) {
206
+ CloseHandle(req->event_handle);
207
+ req->event_handle = NULL;
208
+ }
209
+ }
210
+ }
211
+
212
+ free(handle->accept_reqs);
213
+ handle->accept_reqs = NULL;
214
+ }
215
+
216
+ if (handle->flags & UV_HANDLE_CONNECTION &&
217
+ handle->flags & UV_HANDLE_EMULATE_IOCP) {
218
+ if (handle->read_req.wait_handle != INVALID_HANDLE_VALUE) {
219
+ UnregisterWait(handle->read_req.wait_handle);
220
+ handle->read_req.wait_handle = INVALID_HANDLE_VALUE;
221
+ }
222
+ if (handle->read_req.event_handle) {
223
+ CloseHandle(handle->read_req.event_handle);
224
+ handle->read_req.event_handle = NULL;
225
+ }
226
+ }
227
+
228
+ uv__handle_close(handle);
229
+ loop->active_tcp_streams--;
230
+ }
231
+ }
232
+
233
+
234
+ static int uv__bind(uv_tcp_t* handle,
235
+ int domain,
236
+ struct sockaddr* addr,
237
+ int addrsize) {
238
+ DWORD err;
239
+ int r;
240
+
241
+ if (handle->socket == INVALID_SOCKET) {
242
+ SOCKET sock = socket(domain, SOCK_STREAM, 0);
243
+ if (sock == INVALID_SOCKET) {
244
+ uv__set_sys_error(handle->loop, WSAGetLastError());
245
+ return -1;
246
+ }
247
+
248
+ /* Make the socket non-inheritable */
249
+ if (!SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0)) {
250
+ uv__set_sys_error(handle->loop, GetLastError());
251
+ closesocket(sock);
252
+ return -1;
253
+ }
254
+
255
+ if (uv_tcp_set_socket(handle->loop, handle, sock, 0) == -1) {
256
+ closesocket(sock);
257
+ return -1;
258
+ }
259
+ }
260
+
261
+ r = bind(handle->socket, addr, addrsize);
262
+
263
+ if (r == SOCKET_ERROR) {
264
+ err = WSAGetLastError();
265
+ if (err == WSAEADDRINUSE) {
266
+ /* Some errors are not to be reported until connect() or listen() */
267
+ handle->bind_error = err;
268
+ handle->flags |= UV_HANDLE_BIND_ERROR;
269
+ } else {
270
+ uv__set_sys_error(handle->loop, err);
271
+ return -1;
272
+ }
273
+ }
274
+
275
+ handle->flags |= UV_HANDLE_BOUND;
276
+
277
+ return 0;
278
+ }
279
+
280
+
281
+ int uv__tcp_bind(uv_tcp_t* handle, struct sockaddr_in addr) {
282
+ return uv__bind(handle,
283
+ AF_INET,
284
+ (struct sockaddr*)&addr,
285
+ sizeof(struct sockaddr_in));
286
+ }
287
+
288
+
289
+ int uv__tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6 addr) {
290
+ if (uv_allow_ipv6) {
291
+ handle->flags |= UV_HANDLE_IPV6;
292
+ return uv__bind(handle,
293
+ AF_INET6,
294
+ (struct sockaddr*)&addr,
295
+ sizeof(struct sockaddr_in6));
296
+
297
+ } else {
298
+ uv__set_sys_error(handle->loop, WSAEAFNOSUPPORT);
299
+ return -1;
300
+ }
301
+ }
302
+
303
+
304
+ static void CALLBACK post_completion(void* context, BOOLEAN timed_out) {
305
+ uv_req_t* req;
306
+ uv_tcp_t* handle;
307
+
308
+ req = (uv_req_t*) context;
309
+ assert(req != NULL);
310
+ handle = (uv_tcp_t*)req->data;
311
+ assert(handle != NULL);
312
+ assert(!timed_out);
313
+
314
+ if (!PostQueuedCompletionStatus(handle->loop->iocp,
315
+ req->overlapped.InternalHigh,
316
+ 0,
317
+ &req->overlapped)) {
318
+ uv_fatal_error(GetLastError(), "PostQueuedCompletionStatus");
319
+ }
320
+ }
321
+
322
+
323
+ static void CALLBACK post_write_completion(void* context, BOOLEAN timed_out) {
324
+ uv_write_t* req;
325
+ uv_tcp_t* handle;
326
+
327
+ req = (uv_write_t*) context;
328
+ assert(req != NULL);
329
+ handle = (uv_tcp_t*)req->handle;
330
+ assert(handle != NULL);
331
+ assert(!timed_out);
332
+
333
+ if (!PostQueuedCompletionStatus(handle->loop->iocp,
334
+ req->overlapped.InternalHigh,
335
+ 0,
336
+ &req->overlapped)) {
337
+ uv_fatal_error(GetLastError(), "PostQueuedCompletionStatus");
338
+ }
339
+ }
340
+
341
+
342
+ static void uv_tcp_queue_accept(uv_tcp_t* handle, uv_tcp_accept_t* req) {
343
+ uv_loop_t* loop = handle->loop;
344
+ BOOL success;
345
+ DWORD bytes;
346
+ SOCKET accept_socket;
347
+ short family;
348
+
349
+ assert(handle->flags & UV_HANDLE_LISTENING);
350
+ assert(req->accept_socket == INVALID_SOCKET);
351
+
352
+ /* choose family and extension function */
353
+ if (handle->flags & UV_HANDLE_IPV6) {
354
+ family = AF_INET6;
355
+ } else {
356
+ family = AF_INET;
357
+ }
358
+
359
+ /* Open a socket for the accepted connection. */
360
+ accept_socket = socket(family, SOCK_STREAM, 0);
361
+ if (accept_socket == INVALID_SOCKET) {
362
+ SET_REQ_ERROR(req, WSAGetLastError());
363
+ uv_insert_pending_req(loop, (uv_req_t*)req);
364
+ handle->reqs_pending++;
365
+ return;
366
+ }
367
+
368
+ /* Make the socket non-inheritable */
369
+ if (!SetHandleInformation((HANDLE) accept_socket, HANDLE_FLAG_INHERIT, 0)) {
370
+ SET_REQ_ERROR(req, GetLastError());
371
+ uv_insert_pending_req(loop, (uv_req_t*)req);
372
+ handle->reqs_pending++;
373
+ closesocket(accept_socket);
374
+ return;
375
+ }
376
+
377
+ /* Prepare the overlapped structure. */
378
+ memset(&(req->overlapped), 0, sizeof(req->overlapped));
379
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
380
+ req->overlapped.hEvent = (HANDLE) ((ULONG_PTR) req->event_handle | 1);
381
+ }
382
+
383
+ success = handle->func_acceptex(handle->socket,
384
+ accept_socket,
385
+ (void*)req->accept_buffer,
386
+ 0,
387
+ sizeof(struct sockaddr_storage),
388
+ sizeof(struct sockaddr_storage),
389
+ &bytes,
390
+ &req->overlapped);
391
+
392
+ if (UV_SUCCEEDED_WITHOUT_IOCP(success)) {
393
+ /* Process the req without IOCP. */
394
+ req->accept_socket = accept_socket;
395
+ handle->reqs_pending++;
396
+ uv_insert_pending_req(loop, (uv_req_t*)req);
397
+ } else if (UV_SUCCEEDED_WITH_IOCP(success)) {
398
+ /* The req will be processed with IOCP. */
399
+ req->accept_socket = accept_socket;
400
+ handle->reqs_pending++;
401
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
402
+ req->wait_handle == INVALID_HANDLE_VALUE &&
403
+ !RegisterWaitForSingleObject(&req->wait_handle,
404
+ req->event_handle, post_completion, (void*) req,
405
+ INFINITE, WT_EXECUTEINWAITTHREAD)) {
406
+ SET_REQ_ERROR(req, GetLastError());
407
+ uv_insert_pending_req(loop, (uv_req_t*)req);
408
+ handle->reqs_pending++;
409
+ return;
410
+ }
411
+ } else {
412
+ /* Make this req pending reporting an error. */
413
+ SET_REQ_ERROR(req, WSAGetLastError());
414
+ uv_insert_pending_req(loop, (uv_req_t*)req);
415
+ handle->reqs_pending++;
416
+ /* Destroy the preallocated client socket. */
417
+ closesocket(accept_socket);
418
+ /* Destroy the event handle */
419
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
420
+ CloseHandle(req->overlapped.hEvent);
421
+ req->event_handle = NULL;
422
+ }
423
+ }
424
+ }
425
+
426
+
427
+ static void uv_tcp_queue_read(uv_loop_t* loop, uv_tcp_t* handle) {
428
+ uv_read_t* req;
429
+ uv_buf_t buf;
430
+ int result;
431
+ DWORD bytes, flags;
432
+
433
+ assert(handle->flags & UV_HANDLE_READING);
434
+ assert(!(handle->flags & UV_HANDLE_READ_PENDING));
435
+
436
+ req = &handle->read_req;
437
+ memset(&req->overlapped, 0, sizeof(req->overlapped));
438
+
439
+ /*
440
+ * Preallocate a read buffer if the number of active streams is below
441
+ * the threshold.
442
+ */
443
+ if (loop->active_tcp_streams < uv_active_tcp_streams_threshold) {
444
+ handle->flags &= ~UV_HANDLE_ZERO_READ;
445
+ handle->read_buffer = handle->alloc_cb((uv_handle_t*) handle, 65536);
446
+ assert(handle->read_buffer.len > 0);
447
+ buf = handle->read_buffer;
448
+ } else {
449
+ handle->flags |= UV_HANDLE_ZERO_READ;
450
+ buf.base = (char*) &uv_zero_;
451
+ buf.len = 0;
452
+ }
453
+
454
+ /* Prepare the overlapped structure. */
455
+ memset(&(req->overlapped), 0, sizeof(req->overlapped));
456
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
457
+ assert(req->event_handle);
458
+ req->overlapped.hEvent = (HANDLE) ((ULONG_PTR) req->event_handle | 1);
459
+ }
460
+
461
+ flags = 0;
462
+ result = WSARecv(handle->socket,
463
+ (WSABUF*)&buf,
464
+ 1,
465
+ &bytes,
466
+ &flags,
467
+ &req->overlapped,
468
+ NULL);
469
+
470
+ if (UV_SUCCEEDED_WITHOUT_IOCP(result == 0)) {
471
+ /* Process the req without IOCP. */
472
+ handle->flags |= UV_HANDLE_READ_PENDING;
473
+ req->overlapped.InternalHigh = bytes;
474
+ handle->reqs_pending++;
475
+ uv_insert_pending_req(loop, (uv_req_t*)req);
476
+ } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) {
477
+ /* The req will be processed with IOCP. */
478
+ handle->flags |= UV_HANDLE_READ_PENDING;
479
+ handle->reqs_pending++;
480
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
481
+ req->wait_handle == INVALID_HANDLE_VALUE &&
482
+ !RegisterWaitForSingleObject(&req->wait_handle,
483
+ req->event_handle, post_completion, (void*) req,
484
+ INFINITE, WT_EXECUTEINWAITTHREAD)) {
485
+ SET_REQ_ERROR(req, GetLastError());
486
+ uv_insert_pending_req(loop, (uv_req_t*)req);
487
+ }
488
+ } else {
489
+ /* Make this req pending reporting an error. */
490
+ SET_REQ_ERROR(req, WSAGetLastError());
491
+ uv_insert_pending_req(loop, (uv_req_t*)req);
492
+ handle->reqs_pending++;
493
+ }
494
+ }
495
+
496
+
497
+ int uv_tcp_listen(uv_tcp_t* handle, int backlog, uv_connection_cb cb) {
498
+ uv_loop_t* loop = handle->loop;
499
+ unsigned int i, simultaneous_accepts;
500
+ uv_tcp_accept_t* req;
501
+
502
+ assert(backlog > 0);
503
+
504
+ if (handle->flags & UV_HANDLE_LISTENING) {
505
+ handle->connection_cb = cb;
506
+ }
507
+
508
+ if (handle->flags & UV_HANDLE_READING) {
509
+ uv__set_artificial_error(loop, UV_EISCONN);
510
+ return -1;
511
+ }
512
+
513
+ if (handle->flags & UV_HANDLE_BIND_ERROR) {
514
+ uv__set_sys_error(loop, handle->bind_error);
515
+ return -1;
516
+ }
517
+
518
+ if (!(handle->flags & UV_HANDLE_BOUND) &&
519
+ uv_tcp_bind(handle, uv_addr_ip4_any_) < 0)
520
+ return -1;
521
+
522
+ if (!handle->func_acceptex) {
523
+ if(!uv_get_acceptex_function(handle->socket, &handle->func_acceptex)) {
524
+ uv__set_sys_error(loop, WSAEAFNOSUPPORT);
525
+ return -1;
526
+ }
527
+ }
528
+
529
+ if (!(handle->flags & UV_HANDLE_SHARED_TCP_SOCKET) &&
530
+ listen(handle->socket, backlog) == SOCKET_ERROR) {
531
+ uv__set_sys_error(loop, WSAGetLastError());
532
+ return -1;
533
+ }
534
+
535
+ handle->flags |= UV_HANDLE_LISTENING;
536
+ handle->connection_cb = cb;
537
+ INCREASE_ACTIVE_COUNT(loop, handle);
538
+
539
+ simultaneous_accepts = handle->flags & UV_HANDLE_TCP_SINGLE_ACCEPT ? 1
540
+ : uv_simultaneous_server_accepts;
541
+
542
+ if(!handle->accept_reqs) {
543
+ handle->accept_reqs = (uv_tcp_accept_t*)
544
+ malloc(simultaneous_accepts * sizeof(uv_tcp_accept_t));
545
+ if (!handle->accept_reqs) {
546
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
547
+ }
548
+
549
+ for (i = 0; i < simultaneous_accepts; i++) {
550
+ req = &handle->accept_reqs[i];
551
+ uv_req_init(loop, (uv_req_t*)req);
552
+ req->type = UV_ACCEPT;
553
+ req->accept_socket = INVALID_SOCKET;
554
+ req->data = handle;
555
+
556
+ req->wait_handle = INVALID_HANDLE_VALUE;
557
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
558
+ req->event_handle = CreateEvent(NULL, 0, 0, NULL);
559
+ if (!req->event_handle) {
560
+ uv_fatal_error(GetLastError(), "CreateEvent");
561
+ }
562
+ } else {
563
+ req->event_handle = NULL;
564
+ }
565
+
566
+ uv_tcp_queue_accept(handle, req);
567
+ }
568
+ }
569
+
570
+ return 0;
571
+ }
572
+
573
+
574
+ int uv_tcp_accept(uv_tcp_t* server, uv_tcp_t* client) {
575
+ uv_loop_t* loop = server->loop;
576
+ int rv = 0;
577
+
578
+ uv_tcp_accept_t* req = server->pending_accepts;
579
+
580
+ if (!req) {
581
+ /* No valid connections found, so we error out. */
582
+ uv__set_sys_error(loop, WSAEWOULDBLOCK);
583
+ return -1;
584
+ }
585
+
586
+ if (req->accept_socket == INVALID_SOCKET) {
587
+ uv__set_sys_error(loop, WSAENOTCONN);
588
+ return -1;
589
+ }
590
+
591
+ if (uv_tcp_set_socket(client->loop, client, req->accept_socket, 0) == -1) {
592
+ closesocket(req->accept_socket);
593
+ rv = -1;
594
+ } else {
595
+ uv_connection_init((uv_stream_t*) client);
596
+ /* AcceptEx() implicitly binds the accepted socket. */
597
+ client->flags |= UV_HANDLE_BOUND;
598
+ }
599
+
600
+ /* Prepare the req to pick up a new connection */
601
+ server->pending_accepts = req->next_pending;
602
+ req->next_pending = NULL;
603
+ req->accept_socket = INVALID_SOCKET;
604
+
605
+ if (!(server->flags & UV_HANDLE_CLOSING)) {
606
+ /* Check if we're in a middle of changing the number of pending accepts. */
607
+ if (!(server->flags & UV_HANDLE_TCP_ACCEPT_STATE_CHANGING)) {
608
+ uv_tcp_queue_accept(server, req);
609
+ } else {
610
+ /* We better be switching to a single pending accept. */
611
+ assert(server->flags & UV_HANDLE_TCP_SINGLE_ACCEPT);
612
+
613
+ server->processed_accepts++;
614
+
615
+ if (server->processed_accepts >= uv_simultaneous_server_accepts) {
616
+ server->processed_accepts = 0;
617
+ /*
618
+ * All previously queued accept requests are now processed.
619
+ * We now switch to queueing just a single accept.
620
+ */
621
+ uv_tcp_queue_accept(server, &server->accept_reqs[0]);
622
+ server->flags &= ~UV_HANDLE_TCP_ACCEPT_STATE_CHANGING;
623
+ server->flags |= UV_HANDLE_TCP_SINGLE_ACCEPT;
624
+ }
625
+ }
626
+ }
627
+
628
+ loop->active_tcp_streams++;
629
+
630
+ return rv;
631
+ }
632
+
633
+
634
+ int uv_tcp_read_start(uv_tcp_t* handle, uv_alloc_cb alloc_cb,
635
+ uv_read_cb read_cb) {
636
+ uv_loop_t* loop = handle->loop;
637
+
638
+ if (!(handle->flags & UV_HANDLE_CONNECTION)) {
639
+ uv__set_sys_error(loop, WSAEINVAL);
640
+ return -1;
641
+ }
642
+
643
+ if (handle->flags & UV_HANDLE_READING) {
644
+ uv__set_sys_error(loop, WSAEALREADY);
645
+ return -1;
646
+ }
647
+
648
+ if (handle->flags & UV_HANDLE_EOF) {
649
+ uv__set_sys_error(loop, WSAESHUTDOWN);
650
+ return -1;
651
+ }
652
+
653
+ handle->flags |= UV_HANDLE_READING;
654
+ handle->read_cb = read_cb;
655
+ handle->alloc_cb = alloc_cb;
656
+ INCREASE_ACTIVE_COUNT(loop, handle);
657
+
658
+ /* If reading was stopped and then started again, there could still be a */
659
+ /* read request pending. */
660
+ if (!(handle->flags & UV_HANDLE_READ_PENDING)) {
661
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
662
+ !handle->read_req.event_handle) {
663
+ handle->read_req.event_handle = CreateEvent(NULL, 0, 0, NULL);
664
+ if (!handle->read_req.event_handle) {
665
+ uv_fatal_error(GetLastError(), "CreateEvent");
666
+ }
667
+ }
668
+ uv_tcp_queue_read(loop, handle);
669
+ }
670
+
671
+ return 0;
672
+ }
673
+
674
+
675
+ int uv__tcp_connect(uv_connect_t* req,
676
+ uv_tcp_t* handle,
677
+ struct sockaddr_in address,
678
+ uv_connect_cb cb) {
679
+ uv_loop_t* loop = handle->loop;
680
+ int addrsize = sizeof(struct sockaddr_in);
681
+ BOOL success;
682
+ DWORD bytes;
683
+
684
+ if (handle->flags & UV_HANDLE_BIND_ERROR) {
685
+ uv__set_sys_error(loop, handle->bind_error);
686
+ return -1;
687
+ }
688
+
689
+ if (!(handle->flags & UV_HANDLE_BOUND) &&
690
+ uv_tcp_bind(handle, uv_addr_ip4_any_) < 0)
691
+ return -1;
692
+
693
+ if (!handle->func_connectex) {
694
+ if(!uv_get_connectex_function(handle->socket, &handle->func_connectex)) {
695
+ uv__set_sys_error(loop, WSAEAFNOSUPPORT);
696
+ return -1;
697
+ }
698
+ }
699
+
700
+ uv_req_init(loop, (uv_req_t*) req);
701
+ req->type = UV_CONNECT;
702
+ req->handle = (uv_stream_t*) handle;
703
+ req->cb = cb;
704
+ memset(&req->overlapped, 0, sizeof(req->overlapped));
705
+
706
+ success = handle->func_connectex(handle->socket,
707
+ (struct sockaddr*) &address,
708
+ addrsize,
709
+ NULL,
710
+ 0,
711
+ &bytes,
712
+ &req->overlapped);
713
+
714
+ if (UV_SUCCEEDED_WITHOUT_IOCP(success)) {
715
+ /* Process the req without IOCP. */
716
+ handle->reqs_pending++;
717
+ REGISTER_HANDLE_REQ(loop, handle, req);
718
+ uv_insert_pending_req(loop, (uv_req_t*)req);
719
+ } else if (UV_SUCCEEDED_WITH_IOCP(success)) {
720
+ /* The req will be processed with IOCP. */
721
+ handle->reqs_pending++;
722
+ REGISTER_HANDLE_REQ(loop, handle, req);
723
+ } else {
724
+ uv__set_sys_error(loop, WSAGetLastError());
725
+ return -1;
726
+ }
727
+
728
+ return 0;
729
+ }
730
+
731
+
732
+ int uv__tcp_connect6(uv_connect_t* req,
733
+ uv_tcp_t* handle,
734
+ struct sockaddr_in6 address,
735
+ uv_connect_cb cb) {
736
+ uv_loop_t* loop = handle->loop;
737
+ int addrsize = sizeof(struct sockaddr_in6);
738
+ BOOL success;
739
+ DWORD bytes;
740
+
741
+ if (!uv_allow_ipv6) {
742
+ uv__set_sys_error(loop, WSAEAFNOSUPPORT);
743
+ return -1;
744
+ }
745
+
746
+ if (handle->flags & UV_HANDLE_BIND_ERROR) {
747
+ uv__set_sys_error(loop, handle->bind_error);
748
+ return -1;
749
+ }
750
+
751
+ if (!(handle->flags & UV_HANDLE_BOUND) &&
752
+ uv_tcp_bind6(handle, uv_addr_ip6_any_) < 0)
753
+ return -1;
754
+
755
+ if (!handle->func_connectex) {
756
+ if(!uv_get_connectex_function(handle->socket, &handle->func_connectex)) {
757
+ uv__set_sys_error(loop, WSAEAFNOSUPPORT);
758
+ return -1;
759
+ }
760
+ }
761
+
762
+ uv_req_init(loop, (uv_req_t*) req);
763
+ req->type = UV_CONNECT;
764
+ req->handle = (uv_stream_t*) handle;
765
+ req->cb = cb;
766
+ memset(&req->overlapped, 0, sizeof(req->overlapped));
767
+
768
+ success = handle->func_connectex(handle->socket,
769
+ (struct sockaddr*) &address,
770
+ addrsize,
771
+ NULL,
772
+ 0,
773
+ &bytes,
774
+ &req->overlapped);
775
+
776
+ if (UV_SUCCEEDED_WITHOUT_IOCP(success)) {
777
+ handle->reqs_pending++;
778
+ REGISTER_HANDLE_REQ(loop, handle, req);
779
+ uv_insert_pending_req(loop, (uv_req_t*)req);
780
+ } else if (UV_SUCCEEDED_WITH_IOCP(success)) {
781
+ handle->reqs_pending++;
782
+ REGISTER_HANDLE_REQ(loop, handle, req);
783
+ } else {
784
+ uv__set_sys_error(loop, WSAGetLastError());
785
+ return -1;
786
+ }
787
+
788
+ return 0;
789
+ }
790
+
791
+
792
+ int uv_tcp_getsockname(uv_tcp_t* handle, struct sockaddr* name,
793
+ int* namelen) {
794
+ uv_loop_t* loop = handle->loop;
795
+ int result;
796
+
797
+ if (!(handle->flags & UV_HANDLE_BOUND)) {
798
+ uv__set_sys_error(loop, WSAEINVAL);
799
+ return -1;
800
+ }
801
+
802
+ if (handle->flags & UV_HANDLE_BIND_ERROR) {
803
+ uv__set_sys_error(loop, handle->bind_error);
804
+ return -1;
805
+ }
806
+
807
+ result = getsockname(handle->socket, name, namelen);
808
+ if (result != 0) {
809
+ uv__set_sys_error(loop, WSAGetLastError());
810
+ return -1;
811
+ }
812
+
813
+ return 0;
814
+ }
815
+
816
+
817
+ int uv_tcp_getpeername(uv_tcp_t* handle, struct sockaddr* name,
818
+ int* namelen) {
819
+ uv_loop_t* loop = handle->loop;
820
+ int result;
821
+
822
+ if (!(handle->flags & UV_HANDLE_BOUND)) {
823
+ uv__set_sys_error(loop, WSAEINVAL);
824
+ return -1;
825
+ }
826
+
827
+ if (handle->flags & UV_HANDLE_BIND_ERROR) {
828
+ uv__set_sys_error(loop, handle->bind_error);
829
+ return -1;
830
+ }
831
+
832
+ result = getpeername(handle->socket, name, namelen);
833
+ if (result != 0) {
834
+ uv__set_sys_error(loop, WSAGetLastError());
835
+ return -1;
836
+ }
837
+
838
+ return 0;
839
+ }
840
+
841
+
842
+ int uv_tcp_write(uv_loop_t* loop, uv_write_t* req, uv_tcp_t* handle,
843
+ uv_buf_t bufs[], int bufcnt, uv_write_cb cb) {
844
+ int result;
845
+ DWORD bytes;
846
+
847
+ if (!(handle->flags & UV_HANDLE_CONNECTION)) {
848
+ uv__set_sys_error(loop, WSAEINVAL);
849
+ return -1;
850
+ }
851
+
852
+ if (handle->flags & UV_HANDLE_SHUTTING) {
853
+ uv__set_sys_error(loop, WSAESHUTDOWN);
854
+ return -1;
855
+ }
856
+
857
+ uv_req_init(loop, (uv_req_t*) req);
858
+ req->type = UV_WRITE;
859
+ req->handle = (uv_stream_t*) handle;
860
+ req->cb = cb;
861
+ memset(&req->overlapped, 0, sizeof(req->overlapped));
862
+
863
+ /* Prepare the overlapped structure. */
864
+ memset(&(req->overlapped), 0, sizeof(req->overlapped));
865
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
866
+ req->event_handle = CreateEvent(NULL, 0, 0, NULL);
867
+ if (!req->event_handle) {
868
+ uv_fatal_error(GetLastError(), "CreateEvent");
869
+ }
870
+ req->overlapped.hEvent = (HANDLE) ((ULONG_PTR) req->event_handle | 1);
871
+ req->wait_handle = INVALID_HANDLE_VALUE;
872
+ }
873
+
874
+ result = WSASend(handle->socket,
875
+ (WSABUF*)bufs,
876
+ bufcnt,
877
+ &bytes,
878
+ 0,
879
+ &req->overlapped,
880
+ NULL);
881
+
882
+ if (UV_SUCCEEDED_WITHOUT_IOCP(result == 0)) {
883
+ /* Request completed immediately. */
884
+ req->queued_bytes = 0;
885
+ handle->reqs_pending++;
886
+ handle->write_reqs_pending++;
887
+ REGISTER_HANDLE_REQ(loop, handle, req);
888
+ uv_insert_pending_req(loop, (uv_req_t*) req);
889
+ } else if (UV_SUCCEEDED_WITH_IOCP(result == 0)) {
890
+ /* Request queued by the kernel. */
891
+ req->queued_bytes = uv_count_bufs(bufs, bufcnt);
892
+ handle->reqs_pending++;
893
+ handle->write_reqs_pending++;
894
+ REGISTER_HANDLE_REQ(loop, handle, req);
895
+ handle->write_queue_size += req->queued_bytes;
896
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP &&
897
+ !RegisterWaitForSingleObject(&req->wait_handle,
898
+ req->event_handle, post_write_completion, (void*) req,
899
+ INFINITE, WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE)) {
900
+ SET_REQ_ERROR(req, GetLastError());
901
+ uv_insert_pending_req(loop, (uv_req_t*)req);
902
+ }
903
+ } else {
904
+ /* Send failed due to an error. */
905
+ uv__set_sys_error(loop, WSAGetLastError());
906
+ return -1;
907
+ }
908
+
909
+ return 0;
910
+ }
911
+
912
+
913
+ void uv_process_tcp_read_req(uv_loop_t* loop, uv_tcp_t* handle,
914
+ uv_req_t* req) {
915
+ DWORD bytes, flags, err;
916
+ uv_buf_t buf;
917
+
918
+ assert(handle->type == UV_TCP);
919
+
920
+ handle->flags &= ~UV_HANDLE_READ_PENDING;
921
+
922
+ if (!REQ_SUCCESS(req)) {
923
+ /* An error occurred doing the read. */
924
+ if ((handle->flags & UV_HANDLE_READING) ||
925
+ !(handle->flags & UV_HANDLE_ZERO_READ)) {
926
+ handle->flags &= ~UV_HANDLE_READING;
927
+ DECREASE_ACTIVE_COUNT(loop, handle);
928
+ buf = (handle->flags & UV_HANDLE_ZERO_READ) ?
929
+ uv_buf_init(NULL, 0) : handle->read_buffer;
930
+
931
+ err = GET_REQ_SOCK_ERROR(req);
932
+
933
+ if (err == WSAECONNABORTED) {
934
+ /*
935
+ * Turn WSAECONNABORTED into UV_ECONNRESET to be consistent with Unix.
936
+ */
937
+ uv__set_error(loop, UV_ECONNRESET, err);
938
+ } else {
939
+ uv__set_sys_error(loop, err);
940
+ }
941
+
942
+ handle->read_cb((uv_stream_t*)handle, -1, buf);
943
+ }
944
+ } else {
945
+ if (!(handle->flags & UV_HANDLE_ZERO_READ)) {
946
+ /* The read was done with a non-zero buffer length. */
947
+ if (req->overlapped.InternalHigh > 0) {
948
+ /* Successful read */
949
+ handle->read_cb((uv_stream_t*)handle,
950
+ req->overlapped.InternalHigh,
951
+ handle->read_buffer);
952
+ /* Read again only if bytes == buf.len */
953
+ if (req->overlapped.InternalHigh < handle->read_buffer.len) {
954
+ goto done;
955
+ }
956
+ } else {
957
+ /* Connection closed */
958
+ if (handle->flags & UV_HANDLE_READING) {
959
+ handle->flags &= ~UV_HANDLE_READING;
960
+ DECREASE_ACTIVE_COUNT(loop, handle);
961
+ }
962
+ handle->flags |= UV_HANDLE_EOF;
963
+
964
+ uv__set_error(loop, UV_EOF, ERROR_SUCCESS);
965
+ buf.base = 0;
966
+ buf.len = 0;
967
+ handle->read_cb((uv_stream_t*)handle, -1, handle->read_buffer);
968
+ goto done;
969
+ }
970
+ }
971
+
972
+ /* Do nonblocking reads until the buffer is empty */
973
+ while (handle->flags & UV_HANDLE_READING) {
974
+ buf = handle->alloc_cb((uv_handle_t*) handle, 65536);
975
+ assert(buf.len > 0);
976
+ flags = 0;
977
+ if (WSARecv(handle->socket,
978
+ (WSABUF*)&buf,
979
+ 1,
980
+ &bytes,
981
+ &flags,
982
+ NULL,
983
+ NULL) != SOCKET_ERROR) {
984
+ if (bytes > 0) {
985
+ /* Successful read */
986
+ handle->read_cb((uv_stream_t*)handle, bytes, buf);
987
+ /* Read again only if bytes == buf.len */
988
+ if (bytes < buf.len) {
989
+ break;
990
+ }
991
+ } else {
992
+ /* Connection closed */
993
+ handle->flags &= ~UV_HANDLE_READING;
994
+ DECREASE_ACTIVE_COUNT(loop, handle);
995
+ handle->flags |= UV_HANDLE_EOF;
996
+
997
+ uv__set_error(loop, UV_EOF, ERROR_SUCCESS);
998
+ handle->read_cb((uv_stream_t*)handle, -1, buf);
999
+ break;
1000
+ }
1001
+ } else {
1002
+ err = WSAGetLastError();
1003
+ if (err == WSAEWOULDBLOCK) {
1004
+ /* Read buffer was completely empty, report a 0-byte read. */
1005
+ uv__set_sys_error(loop, WSAEWOULDBLOCK);
1006
+ handle->read_cb((uv_stream_t*)handle, 0, buf);
1007
+ } else {
1008
+ /* Ouch! serious error. */
1009
+ handle->flags &= ~UV_HANDLE_READING;
1010
+ DECREASE_ACTIVE_COUNT(loop, handle);
1011
+
1012
+ if (err == WSAECONNABORTED) {
1013
+ /* Turn WSAECONNABORTED into UV_ECONNRESET to be consistent with */
1014
+ /* Unix. */
1015
+ uv__set_error(loop, UV_ECONNRESET, err);
1016
+ } else {
1017
+ uv__set_sys_error(loop, err);
1018
+ }
1019
+
1020
+ handle->read_cb((uv_stream_t*)handle, -1, buf);
1021
+ }
1022
+ break;
1023
+ }
1024
+ }
1025
+
1026
+ done:
1027
+ /* Post another read if still reading and not closing. */
1028
+ if ((handle->flags & UV_HANDLE_READING) &&
1029
+ !(handle->flags & UV_HANDLE_READ_PENDING)) {
1030
+ uv_tcp_queue_read(loop, handle);
1031
+ }
1032
+ }
1033
+
1034
+ DECREASE_PENDING_REQ_COUNT(handle);
1035
+ }
1036
+
1037
+
1038
+ void uv_process_tcp_write_req(uv_loop_t* loop, uv_tcp_t* handle,
1039
+ uv_write_t* req) {
1040
+ assert(handle->type == UV_TCP);
1041
+
1042
+ assert(handle->write_queue_size >= req->queued_bytes);
1043
+ handle->write_queue_size -= req->queued_bytes;
1044
+
1045
+ UNREGISTER_HANDLE_REQ(loop, handle, req);
1046
+
1047
+ if (handle->flags & UV_HANDLE_EMULATE_IOCP) {
1048
+ if (req->wait_handle != INVALID_HANDLE_VALUE) {
1049
+ UnregisterWait(req->wait_handle);
1050
+ }
1051
+ if (req->event_handle) {
1052
+ CloseHandle(req->event_handle);
1053
+ }
1054
+ }
1055
+
1056
+ if (req->cb) {
1057
+ uv__set_sys_error(loop, GET_REQ_SOCK_ERROR(req));
1058
+ ((uv_write_cb)req->cb)(req, loop->last_err.code == UV_OK ? 0 : -1);
1059
+ }
1060
+
1061
+ handle->write_reqs_pending--;
1062
+ if (handle->flags & UV_HANDLE_SHUTTING &&
1063
+ handle->write_reqs_pending == 0) {
1064
+ uv_want_endgame(loop, (uv_handle_t*)handle);
1065
+ }
1066
+
1067
+ DECREASE_PENDING_REQ_COUNT(handle);
1068
+ }
1069
+
1070
+
1071
+ void uv_process_tcp_accept_req(uv_loop_t* loop, uv_tcp_t* handle,
1072
+ uv_req_t* raw_req) {
1073
+ uv_tcp_accept_t* req = (uv_tcp_accept_t*) raw_req;
1074
+
1075
+ assert(handle->type == UV_TCP);
1076
+
1077
+ /* If handle->accepted_socket is not a valid socket, then */
1078
+ /* uv_queue_accept must have failed. This is a serious error. We stop */
1079
+ /* accepting connections and report this error to the connection */
1080
+ /* callback. */
1081
+ if (req->accept_socket == INVALID_SOCKET) {
1082
+ if (handle->flags & UV_HANDLE_LISTENING) {
1083
+ handle->flags &= ~UV_HANDLE_LISTENING;
1084
+ DECREASE_ACTIVE_COUNT(loop, handle);
1085
+ if (handle->connection_cb) {
1086
+ uv__set_sys_error(loop, GET_REQ_SOCK_ERROR(req));
1087
+ handle->connection_cb((uv_stream_t*)handle, -1);
1088
+ }
1089
+ }
1090
+ } else if (REQ_SUCCESS(req) &&
1091
+ setsockopt(req->accept_socket,
1092
+ SOL_SOCKET,
1093
+ SO_UPDATE_ACCEPT_CONTEXT,
1094
+ (char*)&handle->socket,
1095
+ sizeof(handle->socket)) == 0) {
1096
+ req->next_pending = handle->pending_accepts;
1097
+ handle->pending_accepts = req;
1098
+
1099
+ /* Accept and SO_UPDATE_ACCEPT_CONTEXT were successful. */
1100
+ if (handle->connection_cb) {
1101
+ handle->connection_cb((uv_stream_t*)handle, 0);
1102
+ }
1103
+ } else {
1104
+ /* Error related to accepted socket is ignored because the server */
1105
+ /* socket may still be healthy. If the server socket is broken
1106
+ /* uv_queue_accept will detect it. */
1107
+ closesocket(req->accept_socket);
1108
+ req->accept_socket = INVALID_SOCKET;
1109
+ if (handle->flags & UV_HANDLE_LISTENING) {
1110
+ uv_tcp_queue_accept(handle, req);
1111
+ }
1112
+ }
1113
+
1114
+ DECREASE_PENDING_REQ_COUNT(handle);
1115
+ }
1116
+
1117
+
1118
+ void uv_process_tcp_connect_req(uv_loop_t* loop, uv_tcp_t* handle,
1119
+ uv_connect_t* req) {
1120
+ assert(handle->type == UV_TCP);
1121
+
1122
+ UNREGISTER_HANDLE_REQ(loop, handle, req);
1123
+
1124
+ if (REQ_SUCCESS(req)) {
1125
+ if (setsockopt(handle->socket,
1126
+ SOL_SOCKET,
1127
+ SO_UPDATE_CONNECT_CONTEXT,
1128
+ NULL,
1129
+ 0) == 0) {
1130
+ uv_connection_init((uv_stream_t*)handle);
1131
+ loop->active_tcp_streams++;
1132
+ ((uv_connect_cb)req->cb)(req, 0);
1133
+ } else {
1134
+ uv__set_sys_error(loop, WSAGetLastError());
1135
+ ((uv_connect_cb)req->cb)(req, -1);
1136
+ }
1137
+ } else {
1138
+ uv__set_sys_error(loop, GET_REQ_SOCK_ERROR(req));
1139
+ ((uv_connect_cb)req->cb)(req, -1);
1140
+ }
1141
+
1142
+ DECREASE_PENDING_REQ_COUNT(handle);
1143
+ }
1144
+
1145
+
1146
+ int uv_tcp_import(uv_tcp_t* tcp, WSAPROTOCOL_INFOW* socket_protocol_info,
1147
+ int tcp_connection) {
1148
+ SOCKET socket = WSASocketW(AF_INET,
1149
+ SOCK_STREAM,
1150
+ IPPROTO_IP,
1151
+ socket_protocol_info,
1152
+ 0,
1153
+ WSA_FLAG_OVERLAPPED);
1154
+
1155
+ if (socket == INVALID_SOCKET) {
1156
+ uv__set_sys_error(tcp->loop, WSAGetLastError());
1157
+ return -1;
1158
+ }
1159
+
1160
+ if (!SetHandleInformation((HANDLE) socket, HANDLE_FLAG_INHERIT, 0)) {
1161
+ uv__set_sys_error(tcp->loop, GetLastError());
1162
+ closesocket(socket);
1163
+ return -1;
1164
+ }
1165
+
1166
+ if (uv_tcp_set_socket(tcp->loop, tcp, socket, 1) != 0) {
1167
+ closesocket(socket);
1168
+ return -1;
1169
+ }
1170
+
1171
+ if (tcp_connection) {
1172
+ uv_connection_init((uv_stream_t*)tcp);
1173
+ }
1174
+
1175
+ tcp->flags |= UV_HANDLE_BOUND;
1176
+ tcp->flags |= UV_HANDLE_SHARED_TCP_SOCKET;
1177
+
1178
+ if (socket_protocol_info->iAddressFamily == AF_INET6) {
1179
+ tcp->flags |= UV_HANDLE_IPV6;
1180
+ }
1181
+
1182
+ tcp->loop->active_tcp_streams++;
1183
+ return 0;
1184
+ }
1185
+
1186
+
1187
+ int uv_tcp_nodelay(uv_tcp_t* handle, int enable) {
1188
+ if (handle->socket != INVALID_SOCKET &&
1189
+ uv__tcp_nodelay(handle, handle->socket, enable)) {
1190
+ return -1;
1191
+ }
1192
+
1193
+ if (enable) {
1194
+ handle->flags |= UV_HANDLE_TCP_NODELAY;
1195
+ } else {
1196
+ handle->flags &= ~UV_HANDLE_TCP_NODELAY;
1197
+ }
1198
+
1199
+ return 0;
1200
+ }
1201
+
1202
+
1203
+ int uv_tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay) {
1204
+ if (handle->socket != INVALID_SOCKET &&
1205
+ uv__tcp_keepalive(handle, handle->socket, enable, delay)) {
1206
+ return -1;
1207
+ }
1208
+
1209
+ if (enable) {
1210
+ handle->flags |= UV_HANDLE_TCP_KEEPALIVE;
1211
+ } else {
1212
+ handle->flags &= ~UV_HANDLE_TCP_KEEPALIVE;
1213
+ }
1214
+
1215
+ /* TODO: Store delay if handle->socket isn't created yet. */
1216
+
1217
+ return 0;
1218
+ }
1219
+
1220
+
1221
+ int uv_tcp_duplicate_socket(uv_tcp_t* handle, int pid,
1222
+ LPWSAPROTOCOL_INFOW protocol_info) {
1223
+ if (!(handle->flags & UV_HANDLE_CONNECTION)) {
1224
+ /*
1225
+ * We're about to share the socket with another process. Because
1226
+ * this is a listening socket, we assume that the other process will
1227
+ * be accepting connections on it. So, before sharing the socket
1228
+ * with another process, we call listen here in the parent process.
1229
+ */
1230
+
1231
+ if (!(handle->flags & UV_HANDLE_LISTENING)) {
1232
+ if (!(handle->flags & UV_HANDLE_BOUND)) {
1233
+ uv__set_artificial_error(handle->loop, UV_EINVAL);
1234
+ return -1;
1235
+ }
1236
+ if (listen(handle->socket, SOMAXCONN) == SOCKET_ERROR) {
1237
+ uv__set_sys_error(handle->loop, WSAGetLastError());
1238
+ return -1;
1239
+ }
1240
+ }
1241
+ }
1242
+
1243
+ if (WSADuplicateSocketW(handle->socket, pid, protocol_info)) {
1244
+ uv__set_sys_error(handle->loop, WSAGetLastError());
1245
+ return -1;
1246
+ }
1247
+
1248
+ handle->flags |= UV_HANDLE_SHARED_TCP_SOCKET;
1249
+
1250
+ return 0;
1251
+ }
1252
+
1253
+
1254
+ int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable) {
1255
+ if (handle->flags & UV_HANDLE_CONNECTION) {
1256
+ uv__set_artificial_error(handle->loop, UV_EINVAL);
1257
+ return -1;
1258
+ }
1259
+
1260
+ /* Check if we're already in the desired mode. */
1261
+ if ((enable && !(handle->flags & UV_HANDLE_TCP_SINGLE_ACCEPT)) ||
1262
+ (!enable && handle->flags & UV_HANDLE_TCP_SINGLE_ACCEPT)) {
1263
+ return 0;
1264
+ }
1265
+
1266
+ /* Don't allow switching from single pending accept to many. */
1267
+ if (enable) {
1268
+ uv__set_artificial_error(handle->loop, UV_ENOTSUP);
1269
+ return -1;
1270
+ }
1271
+
1272
+ /* Check if we're in a middle of changing the number of pending accepts. */
1273
+ if (handle->flags & UV_HANDLE_TCP_ACCEPT_STATE_CHANGING) {
1274
+ return 0;
1275
+ }
1276
+
1277
+ handle->flags |= UV_HANDLE_TCP_SINGLE_ACCEPT;
1278
+
1279
+ /* Flip the changing flag if we have already queued multiple accepts. */
1280
+ if (handle->flags & UV_HANDLE_LISTENING) {
1281
+ handle->flags |= UV_HANDLE_TCP_ACCEPT_STATE_CHANGING;
1282
+ }
1283
+
1284
+ return 0;
1285
+ }
1286
+
1287
+
1288
+ static int uv_tcp_try_cancel_io(uv_tcp_t* tcp) {
1289
+ SOCKET socket = tcp->socket;
1290
+ int non_ifs_lsp;
1291
+
1292
+ /* Check if we have any non-IFS LSPs stacked on top of TCP */
1293
+ non_ifs_lsp = (tcp->flags & UV_HANDLE_IPV6) ? uv_tcp_non_ifs_lsp_ipv6 :
1294
+ uv_tcp_non_ifs_lsp_ipv4;
1295
+
1296
+ /* If there are non-ifs LSPs then try to obtain a base handle for the */
1297
+ /* socket. This will always fail on Windows XP/3k. */
1298
+ if (non_ifs_lsp) {
1299
+ DWORD bytes;
1300
+ if (WSAIoctl(socket,
1301
+ SIO_BASE_HANDLE,
1302
+ NULL,
1303
+ 0,
1304
+ &socket,
1305
+ sizeof socket,
1306
+ &bytes,
1307
+ NULL,
1308
+ NULL) != 0) {
1309
+ /* Failed. We can't do CancelIo. */
1310
+ return -1;
1311
+ }
1312
+ }
1313
+
1314
+ assert(socket != 0 && socket != INVALID_SOCKET);
1315
+
1316
+ if (!CancelIo((HANDLE) socket)) {
1317
+ return -1;
1318
+ }
1319
+
1320
+ /* It worked. */
1321
+ return 0;
1322
+ }
1323
+
1324
+
1325
+ void uv_tcp_close(uv_loop_t* loop, uv_tcp_t* tcp) {
1326
+ int close_socket = 1;
1327
+
1328
+ if (tcp->flags & UV_HANDLE_READ_PENDING) {
1329
+ /* In order for winsock to do a graceful close there must not be any */
1330
+ /* any pending reads, or the socket must be shut down for writing */
1331
+ if (!(tcp->flags & UV_HANDLE_SHARED_TCP_SOCKET)) {
1332
+ /* Just do shutdown on non-shared sockets, which ensures graceful close. */
1333
+ shutdown(tcp->socket, SD_SEND);
1334
+ tcp->flags |= UV_HANDLE_SHUT;
1335
+
1336
+ } else if (uv_tcp_try_cancel_io(tcp) == 0) {
1337
+ /* In case of a shared socket, we try to cancel all outstanding I/O, */
1338
+ /* If that works, don't close the socket yet - wait for the read req to */
1339
+ /* return and close the socket in uv_tcp_endgame. */
1340
+ close_socket = 0;
1341
+
1342
+ } else {
1343
+ /* When cancelling isn't possible - which could happen when an LSP is */
1344
+ /* present on an old Windows version, we will have to close the socket */
1345
+ /* with a read pending. That is not nice because trailing sent bytes */
1346
+ /* may not make it to the other side. */
1347
+ }
1348
+
1349
+ } else if ((tcp->flags & UV_HANDLE_SHARED_TCP_SOCKET) &&
1350
+ tcp->accept_reqs != NULL) {
1351
+ /* Under normal circumstances closesocket() will ensure that all pending */
1352
+ /* accept reqs are canceled. However, when the socket is shared the */
1353
+ /* presence of another reference to the socket in another process will */
1354
+ /* keep the accept reqs going, so we have to ensure that these are */
1355
+ /* canceled. */
1356
+ if (uv_tcp_try_cancel_io(tcp) != 0) {
1357
+ /* When cancellation is not possible, there is another option: we can */
1358
+ /* close the incoming sockets, which will also cancel the accept */
1359
+ /* operations. However this is not cool because we might inadvertedly */
1360
+ /* close a socket that just accepted a new connection, which will */
1361
+ /* cause the connection to be aborted. */
1362
+ unsigned int i;
1363
+ for (i = 0; i < uv_simultaneous_server_accepts; i++) {
1364
+ uv_tcp_accept_t* req = &tcp->accept_reqs[i];
1365
+ if (req->accept_socket != INVALID_SOCKET &&
1366
+ !HasOverlappedIoCompleted(&req->overlapped)) {
1367
+ closesocket(req->accept_socket);
1368
+ req->accept_socket = INVALID_SOCKET;
1369
+ }
1370
+ }
1371
+ }
1372
+ }
1373
+
1374
+ if (tcp->flags & UV_HANDLE_READING) {
1375
+ tcp->flags &= ~UV_HANDLE_READING;
1376
+ DECREASE_ACTIVE_COUNT(loop, tcp);
1377
+ }
1378
+
1379
+ if (tcp->flags & UV_HANDLE_LISTENING) {
1380
+ tcp->flags &= ~UV_HANDLE_LISTENING;
1381
+ DECREASE_ACTIVE_COUNT(loop, tcp);
1382
+ }
1383
+
1384
+ if (close_socket) {
1385
+ closesocket(tcp->socket);
1386
+ tcp->flags |= UV_HANDLE_TCP_SOCKET_CLOSED;
1387
+ }
1388
+
1389
+ uv__handle_start(tcp);
1390
+
1391
+ if (tcp->reqs_pending == 0) {
1392
+ uv_want_endgame(tcp->loop, (uv_handle_t*)tcp);
1393
+ }
1394
+ }