rbuv 0.0.1 → 0.0.2

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