racer 0.1.3

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