foolio 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. data/.gitignore +22 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +21 -0
  6. data/examples/timer.rb +20 -0
  7. data/ext/foolio/extconf.rb +34 -0
  8. data/ext/foolio/foolio_ext.c +921 -0
  9. data/ext/foolio/gen.rb +50 -0
  10. data/ext/foolio/make_table.rb +12 -0
  11. data/ext/foolio/templ +243 -0
  12. data/ext/libuv/.gitignore +33 -0
  13. data/ext/libuv/.mailmap +13 -0
  14. data/ext/libuv/.travis.yml +9 -0
  15. data/ext/libuv/AUTHORS +61 -0
  16. data/ext/libuv/LICENSE +44 -0
  17. data/ext/libuv/Makefile +71 -0
  18. data/ext/libuv/README.md +90 -0
  19. data/ext/libuv/common.gypi +178 -0
  20. data/ext/libuv/gyp_uv +73 -0
  21. data/ext/libuv/include/uv-private/eio.h +403 -0
  22. data/ext/libuv/include/uv-private/ev.h +838 -0
  23. data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
  24. data/ext/libuv/include/uv-private/tree.h +768 -0
  25. data/ext/libuv/include/uv-private/uv-unix.h +324 -0
  26. data/ext/libuv/include/uv-private/uv-win.h +517 -0
  27. data/ext/libuv/include/uv.h +1838 -0
  28. data/ext/libuv/src/fs-poll.c +235 -0
  29. data/ext/libuv/src/inet.c +293 -0
  30. data/ext/libuv/src/unix/async.c +148 -0
  31. data/ext/libuv/src/unix/core.c +696 -0
  32. data/ext/libuv/src/unix/cygwin.c +83 -0
  33. data/ext/libuv/src/unix/darwin.c +342 -0
  34. data/ext/libuv/src/unix/dl.c +83 -0
  35. data/ext/libuv/src/unix/eio/Changes +63 -0
  36. data/ext/libuv/src/unix/eio/LICENSE +36 -0
  37. data/ext/libuv/src/unix/eio/Makefile.am +15 -0
  38. data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
  39. data/ext/libuv/src/unix/eio/autogen.sh +3 -0
  40. data/ext/libuv/src/unix/eio/config.h.in +86 -0
  41. data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
  42. data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
  43. data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
  44. data/ext/libuv/src/unix/eio/config_linux.h +94 -0
  45. data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
  46. data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
  47. data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
  48. data/ext/libuv/src/unix/eio/configure.ac +22 -0
  49. data/ext/libuv/src/unix/eio/demo.c +194 -0
  50. data/ext/libuv/src/unix/eio/ecb.h +370 -0
  51. data/ext/libuv/src/unix/eio/eio.3 +3428 -0
  52. data/ext/libuv/src/unix/eio/eio.c +2593 -0
  53. data/ext/libuv/src/unix/eio/eio.pod +969 -0
  54. data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
  55. data/ext/libuv/src/unix/eio/xthread.h +164 -0
  56. data/ext/libuv/src/unix/error.c +105 -0
  57. data/ext/libuv/src/unix/ev/Changes +388 -0
  58. data/ext/libuv/src/unix/ev/LICENSE +36 -0
  59. data/ext/libuv/src/unix/ev/Makefile.am +18 -0
  60. data/ext/libuv/src/unix/ev/Makefile.in +771 -0
  61. data/ext/libuv/src/unix/ev/README +58 -0
  62. data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
  63. data/ext/libuv/src/unix/ev/autogen.sh +6 -0
  64. data/ext/libuv/src/unix/ev/config.guess +1526 -0
  65. data/ext/libuv/src/unix/ev/config.h.in +125 -0
  66. data/ext/libuv/src/unix/ev/config.sub +1658 -0
  67. data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
  68. data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
  69. data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
  70. data/ext/libuv/src/unix/ev/config_linux.h +141 -0
  71. data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
  72. data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
  73. data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
  74. data/ext/libuv/src/unix/ev/configure +13037 -0
  75. data/ext/libuv/src/unix/ev/configure.ac +18 -0
  76. data/ext/libuv/src/unix/ev/depcomp +630 -0
  77. data/ext/libuv/src/unix/ev/ev++.h +816 -0
  78. data/ext/libuv/src/unix/ev/ev.3 +5311 -0
  79. data/ext/libuv/src/unix/ev/ev.c +3925 -0
  80. data/ext/libuv/src/unix/ev/ev.pod +5243 -0
  81. data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
  82. data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
  83. data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
  84. data/ext/libuv/src/unix/ev/ev_port.c +179 -0
  85. data/ext/libuv/src/unix/ev/ev_select.c +310 -0
  86. data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
  87. data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
  88. data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
  89. data/ext/libuv/src/unix/ev/event.c +402 -0
  90. data/ext/libuv/src/unix/ev/event.h +170 -0
  91. data/ext/libuv/src/unix/ev/install-sh +294 -0
  92. data/ext/libuv/src/unix/ev/libev.m4 +39 -0
  93. data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
  94. data/ext/libuv/src/unix/ev/missing +336 -0
  95. data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
  96. data/ext/libuv/src/unix/freebsd.c +326 -0
  97. data/ext/libuv/src/unix/fs.c +739 -0
  98. data/ext/libuv/src/unix/internal.h +188 -0
  99. data/ext/libuv/src/unix/kqueue.c +120 -0
  100. data/ext/libuv/src/unix/linux/inotify.c +239 -0
  101. data/ext/libuv/src/unix/linux/linux-core.c +557 -0
  102. data/ext/libuv/src/unix/linux/syscalls.c +388 -0
  103. data/ext/libuv/src/unix/linux/syscalls.h +124 -0
  104. data/ext/libuv/src/unix/loop-watcher.c +62 -0
  105. data/ext/libuv/src/unix/loop.c +94 -0
  106. data/ext/libuv/src/unix/netbsd.c +108 -0
  107. data/ext/libuv/src/unix/openbsd.c +295 -0
  108. data/ext/libuv/src/unix/pipe.c +259 -0
  109. data/ext/libuv/src/unix/poll.c +114 -0
  110. data/ext/libuv/src/unix/process.c +495 -0
  111. data/ext/libuv/src/unix/signal.c +269 -0
  112. data/ext/libuv/src/unix/stream.c +990 -0
  113. data/ext/libuv/src/unix/sunos.c +481 -0
  114. data/ext/libuv/src/unix/tcp.c +393 -0
  115. data/ext/libuv/src/unix/thread.c +251 -0
  116. data/ext/libuv/src/unix/timer.c +136 -0
  117. data/ext/libuv/src/unix/tty.c +145 -0
  118. data/ext/libuv/src/unix/udp.c +659 -0
  119. data/ext/libuv/src/unix/uv-eio.c +107 -0
  120. data/ext/libuv/src/unix/uv-eio.h +13 -0
  121. data/ext/libuv/src/uv-common.c +380 -0
  122. data/ext/libuv/src/uv-common.h +170 -0
  123. data/ext/libuv/src/win/async.c +100 -0
  124. data/ext/libuv/src/win/atomicops-inl.h +56 -0
  125. data/ext/libuv/src/win/core.c +278 -0
  126. data/ext/libuv/src/win/dl.c +86 -0
  127. data/ext/libuv/src/win/error.c +155 -0
  128. data/ext/libuv/src/win/fs-event.c +510 -0
  129. data/ext/libuv/src/win/fs.c +1948 -0
  130. data/ext/libuv/src/win/getaddrinfo.c +365 -0
  131. data/ext/libuv/src/win/handle-inl.h +149 -0
  132. data/ext/libuv/src/win/handle.c +154 -0
  133. data/ext/libuv/src/win/internal.h +343 -0
  134. data/ext/libuv/src/win/loop-watcher.c +122 -0
  135. data/ext/libuv/src/win/pipe.c +1672 -0
  136. data/ext/libuv/src/win/poll.c +616 -0
  137. data/ext/libuv/src/win/process-stdio.c +500 -0
  138. data/ext/libuv/src/win/process.c +1013 -0
  139. data/ext/libuv/src/win/req-inl.h +220 -0
  140. data/ext/libuv/src/win/req.c +25 -0
  141. data/ext/libuv/src/win/signal.c +57 -0
  142. data/ext/libuv/src/win/stream-inl.h +67 -0
  143. data/ext/libuv/src/win/stream.c +167 -0
  144. data/ext/libuv/src/win/tcp.c +1394 -0
  145. data/ext/libuv/src/win/thread.c +372 -0
  146. data/ext/libuv/src/win/threadpool.c +74 -0
  147. data/ext/libuv/src/win/timer.c +224 -0
  148. data/ext/libuv/src/win/tty.c +1799 -0
  149. data/ext/libuv/src/win/udp.c +716 -0
  150. data/ext/libuv/src/win/util.c +864 -0
  151. data/ext/libuv/src/win/winapi.c +132 -0
  152. data/ext/libuv/src/win/winapi.h +4452 -0
  153. data/ext/libuv/src/win/winsock.c +557 -0
  154. data/ext/libuv/src/win/winsock.h +171 -0
  155. data/ext/libuv/test/benchmark-async-pummel.c +97 -0
  156. data/ext/libuv/test/benchmark-async.c +137 -0
  157. data/ext/libuv/test/benchmark-fs-stat.c +135 -0
  158. data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
  159. data/ext/libuv/test/benchmark-list.h +127 -0
  160. data/ext/libuv/test/benchmark-loop-count.c +88 -0
  161. data/ext/libuv/test/benchmark-million-timers.c +65 -0
  162. data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
  163. data/ext/libuv/test/benchmark-pound.c +324 -0
  164. data/ext/libuv/test/benchmark-pump.c +462 -0
  165. data/ext/libuv/test/benchmark-sizes.c +44 -0
  166. data/ext/libuv/test/benchmark-spawn.c +162 -0
  167. data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
  168. data/ext/libuv/test/benchmark-thread.c +64 -0
  169. data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
  170. data/ext/libuv/test/blackhole-server.c +118 -0
  171. data/ext/libuv/test/dns-server.c +321 -0
  172. data/ext/libuv/test/echo-server.c +378 -0
  173. data/ext/libuv/test/fixtures/empty_file +0 -0
  174. data/ext/libuv/test/fixtures/load_error.node +1 -0
  175. data/ext/libuv/test/run-benchmarks.c +64 -0
  176. data/ext/libuv/test/run-tests.c +138 -0
  177. data/ext/libuv/test/runner-unix.c +295 -0
  178. data/ext/libuv/test/runner-unix.h +36 -0
  179. data/ext/libuv/test/runner-win.c +285 -0
  180. data/ext/libuv/test/runner-win.h +42 -0
  181. data/ext/libuv/test/runner.c +355 -0
  182. data/ext/libuv/test/runner.h +159 -0
  183. data/ext/libuv/test/task.h +112 -0
  184. data/ext/libuv/test/test-async.c +118 -0
  185. data/ext/libuv/test/test-callback-order.c +76 -0
  186. data/ext/libuv/test/test-callback-stack.c +203 -0
  187. data/ext/libuv/test/test-connection-fail.c +148 -0
  188. data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
  189. data/ext/libuv/test/test-delayed-accept.c +188 -0
  190. data/ext/libuv/test/test-dlerror.c +58 -0
  191. data/ext/libuv/test/test-error.c +59 -0
  192. data/ext/libuv/test/test-fail-always.c +29 -0
  193. data/ext/libuv/test/test-fs-event.c +474 -0
  194. data/ext/libuv/test/test-fs-poll.c +146 -0
  195. data/ext/libuv/test/test-fs.c +1843 -0
  196. data/ext/libuv/test/test-get-currentexe.c +63 -0
  197. data/ext/libuv/test/test-get-loadavg.c +36 -0
  198. data/ext/libuv/test/test-get-memory.c +38 -0
  199. data/ext/libuv/test/test-getaddrinfo.c +122 -0
  200. data/ext/libuv/test/test-getsockname.c +342 -0
  201. data/ext/libuv/test/test-hrtime.c +54 -0
  202. data/ext/libuv/test/test-idle.c +81 -0
  203. data/ext/libuv/test/test-ipc-send-recv.c +209 -0
  204. data/ext/libuv/test/test-ipc.c +620 -0
  205. data/ext/libuv/test/test-list.h +427 -0
  206. data/ext/libuv/test/test-loop-handles.c +336 -0
  207. data/ext/libuv/test/test-multiple-listen.c +102 -0
  208. data/ext/libuv/test/test-mutexes.c +63 -0
  209. data/ext/libuv/test/test-pass-always.c +28 -0
  210. data/ext/libuv/test/test-ping-pong.c +253 -0
  211. data/ext/libuv/test/test-pipe-bind-error.c +140 -0
  212. data/ext/libuv/test/test-pipe-connect-error.c +96 -0
  213. data/ext/libuv/test/test-platform-output.c +87 -0
  214. data/ext/libuv/test/test-poll-close.c +72 -0
  215. data/ext/libuv/test/test-poll.c +573 -0
  216. data/ext/libuv/test/test-process-title.c +49 -0
  217. data/ext/libuv/test/test-ref.c +338 -0
  218. data/ext/libuv/test/test-run-once.c +48 -0
  219. data/ext/libuv/test/test-semaphore.c +111 -0
  220. data/ext/libuv/test/test-shutdown-close.c +103 -0
  221. data/ext/libuv/test/test-shutdown-eof.c +183 -0
  222. data/ext/libuv/test/test-signal.c +162 -0
  223. data/ext/libuv/test/test-spawn.c +863 -0
  224. data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
  225. data/ext/libuv/test/test-tcp-bind-error.c +191 -0
  226. data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
  227. data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
  228. data/ext/libuv/test/test-tcp-close.c +129 -0
  229. data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  230. data/ext/libuv/test/test-tcp-connect-error.c +70 -0
  231. data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
  232. data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
  233. data/ext/libuv/test/test-tcp-flags.c +51 -0
  234. data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  235. data/ext/libuv/test/test-tcp-unexpected-read.c +113 -0
  236. data/ext/libuv/test/test-tcp-write-error.c +168 -0
  237. data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
  238. data/ext/libuv/test/test-tcp-writealot.c +170 -0
  239. data/ext/libuv/test/test-thread.c +183 -0
  240. data/ext/libuv/test/test-threadpool.c +57 -0
  241. data/ext/libuv/test/test-timer-again.c +141 -0
  242. data/ext/libuv/test/test-timer.c +152 -0
  243. data/ext/libuv/test/test-tty.c +110 -0
  244. data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
  245. data/ext/libuv/test/test-udp-ipv6.c +156 -0
  246. data/ext/libuv/test/test-udp-multicast-join.c +139 -0
  247. data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
  248. data/ext/libuv/test/test-udp-options.c +86 -0
  249. data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
  250. data/ext/libuv/test/test-util.c +97 -0
  251. data/ext/libuv/test/test-walk-handles.c +77 -0
  252. data/ext/libuv/uv.gyp +375 -0
  253. data/ext/libuv/vcbuild.bat +105 -0
  254. data/foolio.gemspec +18 -0
  255. data/lib/foolio.rb +9 -0
  256. data/lib/foolio/handle.rb +27 -0
  257. data/lib/foolio/listener.rb +26 -0
  258. data/lib/foolio/loop.rb +79 -0
  259. data/lib/foolio/stream.rb +109 -0
  260. data/lib/foolio/version.rb +3 -0
  261. metadata +309 -0
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.o
19
+ *.bundle
20
+ *.log
21
+ *.a
22
+ ext/foolio/Makefile
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in foolio.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 mzp
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Foolio
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'foolio'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install foolio
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ task :default => :compile
5
+
6
+ task :compile do
7
+ Dir.chdir File.expand_path("../ext/foolio", __FILE__)
8
+ system "ruby extconf.rb"
9
+ system "make"
10
+ end
11
+
12
+ task :clean do
13
+ Dir.chdir File.expand_path("../ext/libuv", __FILE__)
14
+ system "make clean"
15
+ Dir.chdir File.expand_path("../ext/foolio", __FILE__)
16
+ system "rm -f *.o *.bundle *.a Makefile"
17
+ end
18
+
19
+ task :irb => :compile do
20
+ system "ruby -I./lib -I./ext -rfoolio -rirb -e 'IRB.start' -- --simple-prompt"
21
+ end
@@ -0,0 +1,20 @@
1
+ # -*- mode:ruby; coding:utf-8 -*-
2
+ require 'foolio'
3
+
4
+ loop = Foolio::UV.default_loop
5
+ timer = Foolio::UV.timer_init loop
6
+
7
+ count = 0
8
+ Foolio::UV.timer_start(timer,lambda{|handle, status|
9
+ p count
10
+ count += 1
11
+ },2000,2000)
12
+
13
+ Thread.start {
14
+ Foolio::UV.run(loop)
15
+ }
16
+
17
+ loop {
18
+ puts "main thread"
19
+ sleep 1
20
+ }
@@ -0,0 +1,34 @@
1
+ require "mkmf"
2
+
3
+ $CFLAGS << " -std=gnu99 -fPIC"
4
+
5
+ $DLDFLAGS << " -fPIC"
6
+ $warnflags.gsub!("-Wdeclaration-after-statement","")
7
+
8
+ if RUBY_PLATFORM =~ /darwin/
9
+ $DLDFLAGS << " -framework CoreServices"
10
+ end
11
+
12
+ libuv_dir = File.expand_path("../../libuv", __FILE__)
13
+ foolio_dir = File.expand_path("../", __FILE__)
14
+
15
+ Dir.chdir(libuv_dir) do
16
+ system "make"
17
+ end
18
+
19
+ Dir.chdir(foolio_dir) do
20
+ require 'fileutils'
21
+ FileUtils.cp "#{libuv_dir}/uv.a", "#{foolio_dir}/libuv.a"
22
+ end
23
+
24
+ dir_config "uv", "#{libuv_dir}/include", foolio_dir
25
+
26
+ case RbConfig::CONFIG['host_os'].downcase
27
+ when /mingw|mswin/
28
+ have_library "ws2_32"
29
+ have_library "psapi"
30
+ have_library "iphlpapi"
31
+ end
32
+ have_library "uv"
33
+
34
+ create_makefile "foolio/foolio_ext"
@@ -0,0 +1,921 @@
1
+ #include <ruby.h>
2
+ #include <uv.h>
3
+ #include <stdbool.h>
4
+
5
+ void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1);
6
+
7
+ #define Wrap(expr, f) Data_Wrap_Struct(klass, 0, (f), (expr))
8
+ #define Method(name, n) rb_define_singleton_method(klass, #name, foolio_##name, n)
9
+ #define Decl(T) \
10
+ uv_##T##_t * T##_p; \
11
+ Data_Get_Struct(T, uv_##T##_t, T##_p)
12
+
13
+ #define InitHandle(T) \
14
+ uv_##T##_t * handle = malloc(sizeof(uv_##T##_t)); \
15
+ Decl(loop); \
16
+ if( uv_##T##_init(loop_p, handle) == 0) { \
17
+ return Data_Wrap_Struct(klass, 0, 0, handle); \
18
+ } else { \
19
+ return Qnil; \
20
+ } \
21
+
22
+ #define CHECK(X) \
23
+ if(X == -1) { \
24
+ rb_raise(rb_eException, "libuv failed by %s", uv_strerror(uv_last_error(handle_->loop))); \
25
+ }
26
+
27
+ // Foolio::UV class
28
+ VALUE klass;
29
+
30
+ struct LoopData {
31
+ bool gvl; // has GVL?
32
+ };
33
+
34
+ // ------------------------------
35
+ // callback
36
+ // ------------------------------
37
+ struct CallbackData {
38
+ VALUE cb;
39
+ int argc;
40
+ VALUE* argv;
41
+
42
+ VALUE on_close;
43
+ };
44
+
45
+ struct CallbackData* callback(VALUE cb) {
46
+ struct CallbackData* data = malloc(sizeof(struct CallbackData));
47
+ data->cb = cb;
48
+ data->argc = 0;
49
+ data->argv = NULL;
50
+ return data;
51
+ }
52
+
53
+ static void* foolio__cb__apply(void* p) {
54
+ struct CallbackData* data = (struct CallbackData*)p;
55
+ VALUE arity =
56
+ rb_funcall2(data->cb, rb_intern("arity"), 0, NULL);
57
+ if(NUM2INT(arity) != data->argc) {
58
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for %d)", data->argc, NUM2INT(arity));
59
+ } else {
60
+ rb_funcall2(data->cb, rb_intern("call"), data->argc, data->argv);
61
+ return NULL;
62
+ }
63
+ }
64
+
65
+ static void foolio__cb_apply(uv_loop_t* loop, struct CallbackData* data, int argc, VALUE argv[]) {
66
+ struct LoopData* p = loop->data;
67
+ data->argc = argc;
68
+ data->argv = argv;
69
+ if(p->gvl) {
70
+ foolio__cb__apply((void*)data);
71
+ } else {
72
+ // All callback function are run without gvl.
73
+ // But we need GVL to call Ruby methods.
74
+ p->gvl = true;
75
+ rb_thread_call_with_gvl(foolio__cb__apply, (void*)data);
76
+ p->gvl = false;
77
+ }
78
+ }
79
+
80
+ void foolio__cb_free(struct CallbackData* data) {
81
+ free(data);
82
+ }
83
+
84
+ // ------------------------------
85
+ // Loop
86
+ // ------------------------------
87
+ VALUE foolio_default_loop(VALUE self) {
88
+ return Wrap( uv_default_loop(), 0 );
89
+ }
90
+
91
+ VALUE foolio_loop_new(VALUE self){
92
+ return Wrap( uv_loop_new(), 0);
93
+ }
94
+
95
+ VALUE foolio_loop_delete(VALUE self, VALUE loop) {
96
+ Decl(loop);
97
+ uv_loop_delete(loop_p);
98
+ return Qnil;
99
+ }
100
+
101
+ /*
102
+ typedef void (*uv_walk_cb)(uv_handle_t* handle, void* arg);
103
+ typedef void (*uv_close_cb)(uv_handle_t* handle);
104
+ UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg);
105
+ UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
106
+ */
107
+ void foolio__close_cb(uv_handle_t* handle) {
108
+ struct CallbackData* data = (struct CallbackData*)handle->data;
109
+ data->cb = data->on_close;
110
+ foolio__cb_apply(handle->loop, data, 0, 0);
111
+ foolio__cb_free(handle->data);
112
+ free(handle);
113
+ }
114
+
115
+ void foolio__close(uv_handle_t* handle, VALUE cb) {
116
+ if(uv_is_closing(handle)){ return; }
117
+
118
+ if(handle->data == NULL) {
119
+ handle->data = malloc(sizeof(struct CallbackData*));
120
+ }
121
+ ((struct CallbackData*)handle->data)->on_close = (VALUE)cb;
122
+
123
+ uv_close(handle, foolio__close_cb);
124
+ }
125
+
126
+ void foolio__close_all(uv_handle_t* handle, void* cb){
127
+ foolio__close(handle, (VALUE)cb);
128
+ }
129
+
130
+ VALUE foolio_close_all(VALUE self, VALUE loop, VALUE cb) {
131
+ uv_loop_t* loop_;
132
+ Data_Get_Struct(loop, uv_loop_t, loop_);
133
+ uv_walk(loop_, foolio__close_all, (void*)cb);
134
+ }
135
+
136
+ VALUE foolio_close(VALUE self, VALUE handle, VALUE cb) {
137
+ uv_handle_t* handle_;
138
+ Data_Get_Struct(handle, uv_handle_t, handle_);
139
+ foolio__close(handle_, cb);
140
+ }
141
+
142
+ VALUE foolio_is_active(VALUE handle) {
143
+ uv_handle_t* handle_;
144
+ Data_Get_Struct(handle, uv_handle_t, handle_);
145
+ int retval = uv_is_active(handle_);
146
+ CHECK(retval);
147
+ return INT2NUM(retval);
148
+ }
149
+
150
+ // ------------------------------
151
+ // run
152
+ // ------------------------------
153
+ VALUE foolio__run(void* loop) {
154
+ return INT2NUM(uv_run(loop));
155
+ }
156
+
157
+ VALUE foolio_run(VALUE self, VALUE loop) {
158
+ Decl(loop);
159
+ struct LoopData data = { false };
160
+ loop_p->data = (void*)&data;
161
+ rb_thread_blocking_region(foolio__run, (void*)loop_p, RUBY_UBF_IO, NULL);
162
+ }
163
+
164
+ VALUE foolio_run_once(VALUE self, VALUE loop) {
165
+ Decl(loop);
166
+ struct LoopData data = { true };
167
+ loop_p->data = (void*)&data;
168
+ return INT2NUM(uv_run_once(loop_p));
169
+ }
170
+
171
+ VALUE foolio_ip4_addr(VALUE self, VALUE ip, VALUE port) {
172
+ struct sockaddr_in* p = malloc(sizeof(struct sockaddr_in));
173
+ *p = uv_ip4_addr((const char*)StringValueCStr(ip), NUM2INT(port));
174
+ return Wrap(p, free);
175
+ }
176
+
177
+ VALUE foolio_ip_name(VALUE self, VALUE src) {
178
+ struct sockaddr_in* addr_;
179
+ Data_Get_Struct(src, struct sockaddr_in, addr_);
180
+ if(addr_ == NULL) { return Qnil; }
181
+ if(addr_->sin_family == AF_INET6) {
182
+ char str[INET6_ADDRSTRLEN];
183
+ uv_ip6_name((struct sockaddr_in6*)addr_, str, INET_ADDRSTRLEN);
184
+ return rb_str_new_cstr(str);
185
+ } else {
186
+ char str[INET_ADDRSTRLEN];
187
+ uv_ip4_name(addr_, str, INET_ADDRSTRLEN);
188
+ return rb_str_new_cstr(str);
189
+ }
190
+ }
191
+
192
+ VALUE foolio_port(VALUE self, VALUE src) {
193
+ struct sockaddr_in* addr_;
194
+ Data_Get_Struct(src, struct sockaddr_in, addr_);
195
+ if(addr_ == NULL) { return Qnil; }
196
+ if(addr_->sin_family == AF_INET6) {
197
+ int port = ntohs(((struct sockaddr_in6*)addr_)->sin6_port);
198
+ return INT2FIX(port);
199
+ } else {
200
+ int port = ntohs(addr_->sin_port);
201
+ return INT2FIX(port);
202
+ }
203
+ }
204
+
205
+ /*VALUE foolio_ip6_name(VALUE self, VALUE src) {
206
+ struct sockaddr_in6* addr_;
207
+ Data_Get_Struct(addr, struct sockaddr_in6, addr_);
208
+ char str[INET_ADDRSTRLEN];
209
+ uv_ip4_name(addr_, str, INET_ADDRSTRLEN);
210
+ return rb_str_new_cstr(str);
211
+ }*/
212
+
213
+
214
+ VALUE foolio_ip6_addr(VALUE self, VALUE ip, VALUE port) {
215
+ struct sockaddr_in6* p = malloc(sizeof(struct sockaddr_in6));
216
+ *p = uv_ip6_addr((const char*)StringValueCStr(ip), NUM2INT(port));
217
+ return Wrap(p, free);
218
+ }
219
+
220
+ void foolio__connection_cb(uv_stream_t* server, int status) {
221
+ VALUE argv[] = { INT2FIX(status) };
222
+ foolio__cb_apply(server->loop, server->data, 1, argv);
223
+ }
224
+
225
+ // UV_EXTERN int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb)
226
+ VALUE foolio_listen(VALUE self, VALUE stream, VALUE backlog, VALUE cb) {
227
+ uv_stream_t* handle_;
228
+ Data_Get_Struct(stream, uv_stream_t, handle_);
229
+ int backlog_ = NUM2INT(backlog);
230
+ handle_->data = (void*)callback(cb);
231
+ int retval = uv_listen(handle_, backlog_, foolio__connection_cb);
232
+ CHECK(retval);
233
+ return INT2FIX(retval);
234
+ }
235
+
236
+ // EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client);
237
+ VALUE foolio_accept(VALUE self, VALUE handle, VALUE client) {
238
+ uv_stream_t* handle_;
239
+ Data_Get_Struct(handle, uv_stream_t, handle_);
240
+ uv_stream_t* client_;
241
+ Data_Get_Struct(client, uv_stream_t, client_);
242
+ //uv_stream_t* server, uv_stream_t* client) {
243
+ int retval = uv_accept(handle_, client_);
244
+ CHECK(retval);
245
+ return INT2FIX(retval);
246
+ }
247
+
248
+ uv_buf_t foolio__alloc(uv_handle_t* handle, size_t suggested_size) {
249
+ return uv_buf_init((char*)malloc(suggested_size), (unsigned int)suggested_size);
250
+ }
251
+
252
+ struct MakeBufArg {
253
+ char* ptr;
254
+ ssize_t size;
255
+ };
256
+
257
+ void* foolio___make_buf(void* args) {
258
+ struct MakeBufArg* args_ = (struct MakeBufArg*) args;
259
+ return (void*)rb_str_new(args_->ptr, args_->size);
260
+ }
261
+
262
+ VALUE foolio__make_buf(char* ptr, ssize_t size) {
263
+ struct MakeBufArg args = { ptr, size };
264
+ if(size == -1) {
265
+ return Qnil;
266
+ } else {
267
+ VALUE str = (VALUE)rb_thread_call_with_gvl(foolio___make_buf, &args);
268
+ free(ptr);
269
+ return str;
270
+ }
271
+ }
272
+
273
+ void foolio__read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf){
274
+ VALUE argv[] = { foolio__make_buf(buf.base, nread) };
275
+ foolio__cb_apply(stream->loop, stream->data, 1, argv);
276
+ }
277
+
278
+ void foolio__read2_cb(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf, uv_handle_type pending) {
279
+ VALUE argv[] = { foolio__make_buf(buf.base, nread), INT2FIX(pending) };
280
+ foolio__cb_apply(pipe->loop, pipe->data, 2, argv);
281
+ }
282
+
283
+ // UV_EXTERN int uv_read_start(uv_stream_t*, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
284
+ static VALUE foolio_read_start(VALUE self, VALUE stream, VALUE read_cb) {
285
+ uv_stream_t* handle_;
286
+ Data_Get_Struct(stream, uv_stream_t, handle_);
287
+ handle_->data = (void*)callback(read_cb);
288
+ int retval = uv_read_start(handle_, foolio__alloc, foolio__read_cb);
289
+ CHECK(retval);
290
+ return INT2NUM(retval);
291
+ }
292
+
293
+ // UV_EXTERN int uv_read_stop(uv_stream_t*)
294
+ VALUE foolio_read_stop(VALUE self, VALUE stream) {
295
+ uv_stream_t* handle_;
296
+ Data_Get_Struct(stream, uv_stream_t, handle_);
297
+ int retval = uv_read_stop(handle_);
298
+ foolio__cb_free(handle_->data);
299
+ handle_->data = NULL;
300
+ CHECK(retval);
301
+ return NUM2INT(retval);
302
+ }
303
+
304
+ // UV_EXTERN int uv_read2_start(uv_stream_t*, uv_alloc_cb alloc_cb, uv_read2_cb read_cb)
305
+ VALUE foolio_read2_start(VALUE self, VALUE stream, VALUE read_cb) {
306
+ uv_stream_t* handle_;
307
+ Data_Get_Struct(stream, uv_stream_t, handle_);
308
+ handle_->data = (void*)callback(read_cb);
309
+ int retval = uv_read2_start(handle_, foolio__alloc, foolio__read2_cb);
310
+ CHECK(retval);
311
+ return INT2NUM(retval);
312
+ }
313
+
314
+ void foolio__write_cb(uv_write_t* req, int status){
315
+ VALUE argv[] = { INT2FIX(status) };
316
+ foolio__cb_apply(req->handle->loop, req->handle->data, 1, argv);
317
+ }
318
+
319
+ // UV_EXTERN int uv_write(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt, uv_write_cb cb)
320
+ VALUE foolio_write(VALUE self, VALUE req, VALUE handle, VALUE buf, VALUE cb) {
321
+ uv_write_t* req_;
322
+ if( req == Qnil) {
323
+ req_ = malloc(sizeof(uv_write_t));
324
+ req = Wrap(req_, free);
325
+ } else {
326
+ Data_Get_Struct(req, uv_write_t, req_);
327
+ }
328
+ uv_stream_t* handle_;
329
+ Data_Get_Struct(handle, uv_stream_t, handle_);
330
+ uv_buf_t bufs[] = {
331
+ uv_buf_init(StringValueCStr(buf), (int)rb_str_strlen(buf))
332
+ };
333
+ handle_->data = (void*)callback(cb);
334
+ int retval = uv_write(req_, handle_, bufs, 1, foolio__write_cb);
335
+ CHECK(retval);
336
+ return retval == 0 ? req : Qnil;
337
+ }
338
+
339
+ // UV_EXTERN int uv_write2(uv_write_t* req, uv_stream_t* handle, uv_buf_t bufs[], int bufcnt, uv_stream_t* send_handle, uv_write_cb cb)
340
+ VALUE foolio_write2(VALUE self, VALUE req, VALUE handle, VALUE buf, VALUE send_handle, VALUE cb) {
341
+ uv_write_t* req_;
342
+ if( req == Qnil) {
343
+ req_ = malloc(sizeof(uv_write_t));
344
+ req = Wrap(req_, free);
345
+ } else {
346
+ Data_Get_Struct(req, uv_write_t, req_);
347
+ }
348
+ uv_stream_t* handle_;
349
+ Data_Get_Struct(handle, uv_stream_t, handle_);
350
+ uv_buf_t bufs[] = {
351
+ uv_buf_init(StringValueCStr(buf), (int)rb_str_strlen(buf))
352
+ };
353
+ uv_stream_t* send_handle_;
354
+ Data_Get_Struct(send_handle, uv_stream_t, send_handle_);
355
+ handle_->data = (void*)callback(cb);
356
+ int retval = uv_write2(req_, handle_, bufs, 1, send_handle_, foolio__write_cb);
357
+ return retval == 0 ? req : Qnil;
358
+ }
359
+
360
+ // UV_EXTERN int uv_is_readable(const uv_stream_t* handle)
361
+ VALUE foolio_is_readable(VALUE self, VALUE handle) {
362
+ const uv_stream_t* handle_;
363
+ Data_Get_Struct(handle, const uv_stream_t, handle_);
364
+ int retval = uv_is_readable(handle_);
365
+ CHECK(retval);
366
+ return INT2NUM(retval);
367
+ }
368
+
369
+ // UV_EXTERN int uv_is_writable(const uv_stream_t* handle)
370
+ VALUE foolio_is_writable(VALUE self, VALUE handle) {
371
+ const uv_stream_t* handle_;
372
+ Data_Get_Struct(handle, const uv_stream_t, handle_);
373
+ int retval = uv_is_writable(handle_);
374
+ CHECK(retval);
375
+ return INT2NUM(retval);
376
+ }
377
+
378
+ // UV_EXTERN int uv_is_closing(const uv_handle_t* handle)
379
+ VALUE foolio_is_closing(VALUE self, VALUE handle) {
380
+ const uv_handle_t* handle_;
381
+ Data_Get_Struct(handle, const uv_handle_t, handle_);
382
+ int retval = uv_is_closing(handle_);
383
+ CHECK(retval);
384
+ return INT2NUM(retval);
385
+ }
386
+
387
+ // UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle)
388
+ VALUE foolio_udp_init(VALUE self, VALUE loop) {
389
+ InitHandle(udp);
390
+ }
391
+
392
+ // UV_EXTERN int uv_udp_bind(uv_udp_t* handle, struct sockaddr_in addr, unsigned int flags)
393
+ VALUE foolio_udp_bind(VALUE self, VALUE handle, VALUE addr, VALUE flags) {
394
+ uv_udp_t* handle_;
395
+ Data_Get_Struct(handle, uv_udp_t, handle_);
396
+ struct sockaddr_in* addr_;
397
+ Data_Get_Struct(addr, struct sockaddr_in, addr_);
398
+ unsigned int flags_ = NUM2UINT(flags);
399
+ int retval = uv_udp_bind(handle_, *addr_, flags_);
400
+ CHECK(retval);
401
+ return INT2NUM(retval);
402
+ }
403
+
404
+ // UV_EXTERN int uv_udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr, unsigned int flags)
405
+ VALUE foolio_udp_bind6(VALUE self, VALUE handle, VALUE addr, VALUE flags) {
406
+ uv_udp_t* handle_;
407
+ Data_Get_Struct(handle, uv_udp_t, handle_);
408
+ struct sockaddr_in6* addr_;
409
+ Data_Get_Struct(addr, struct sockaddr_in6, addr_);
410
+ unsigned int flags_ = NUM2UINT(flags);
411
+ int retval = uv_udp_bind6(handle_, *addr_, flags_);
412
+ CHECK(retval);
413
+ return INT2NUM(retval);
414
+ }
415
+
416
+ // UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name, int* namelen)
417
+ VALUE foolio_udp_getsockname(VALUE self, VALUE handle, VALUE name, VALUE namelen) {
418
+ uv_udp_t* handle_;
419
+ Data_Get_Struct(handle, uv_udp_t, handle_);
420
+ struct sockaddr* name_ = malloc(sizeof(struct sockaddr));
421
+ int size;
422
+ int retval = uv_udp_getsockname(handle_, name_, &size);
423
+ CHECK(retval);
424
+ return Wrap(name_, free);
425
+ }
426
+
427
+ // UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle, const char* multicast_addr, const char* interface_addr, uv_membership membership)
428
+ VALUE foolio_udp_set_membership(VALUE self, VALUE handle, VALUE multicast_addr, VALUE interface_addr, VALUE membership) {
429
+ uv_udp_t* handle_;
430
+ Data_Get_Struct(handle, uv_udp_t, handle_);
431
+ const char* multicast_addr_ = StringValueCStr(multicast_addr);
432
+ const char* interface_addr_ = StringValueCStr(interface_addr);
433
+ uv_membership membership_ = NUM2INT(membership);
434
+ int retval = uv_udp_set_membership(handle_, multicast_addr_, interface_addr_, membership_);
435
+ CHECK(retval);
436
+ return INT2NUM(retval);
437
+ }
438
+
439
+ // UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on)
440
+ VALUE foolio_udp_set_multicast_loop(VALUE self, VALUE handle, VALUE on) {
441
+ uv_udp_t* handle_;
442
+ Data_Get_Struct(handle, uv_udp_t, handle_);
443
+ int on_ = NUM2INT(on);
444
+ int retval = uv_udp_set_multicast_loop(handle_, on_);
445
+ CHECK(retval);
446
+ return INT2NUM(retval);
447
+ }
448
+
449
+
450
+ // UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl)
451
+ VALUE foolio_udp_set_multicast_ttl(VALUE self, VALUE handle, VALUE ttl) {
452
+ uv_udp_t* handle_;
453
+ Data_Get_Struct(handle, uv_udp_t, handle_);
454
+ int ttl_ = NUM2INT(ttl);
455
+ int retval = uv_udp_set_multicast_ttl(handle_, ttl_);
456
+ CHECK(retval);
457
+ return INT2NUM(retval);
458
+ }
459
+
460
+ // UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on)
461
+ VALUE foolio_udp_set_broadcast(VALUE self, VALUE handle, VALUE on) {
462
+ uv_udp_t* handle_;
463
+ Data_Get_Struct(handle, uv_udp_t, handle_);
464
+ int on_ = NUM2INT(on);
465
+ int retval = uv_udp_set_broadcast(handle_, on_);
466
+ CHECK(retval);
467
+ return INT2NUM(retval);
468
+ }
469
+
470
+ // UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl)
471
+ VALUE foolio_udp_set_ttl(VALUE self, VALUE handle, VALUE ttl) {
472
+ uv_udp_t* handle_;
473
+ Data_Get_Struct(handle, uv_udp_t, handle_);
474
+ int ttl_ = NUM2INT(ttl);
475
+ int retval = uv_udp_set_ttl(handle_, ttl_);
476
+ CHECK(retval);
477
+ return INT2NUM(retval);
478
+ }
479
+
480
+ void foolio__udp_send_cb(uv_udp_send_t* req, int status) {
481
+ uv_udp_t* handle_ = req->handle;
482
+ CHECK(status);
483
+ // VALUE argv[] = { INT2FIX(status) };
484
+ // foolio__cb_apply(req->handle->loop, req->handle->data, 1 , argv);
485
+ }
486
+
487
+ // UV_EXTERN int uv_udp_send(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[], int bufcnt, struct sockaddr_in addr, uv_udp_send_cb send_cb)
488
+ VALUE foolio_udp_send(VALUE self, VALUE req, VALUE handle, VALUE buf, VALUE addr, VALUE send_cb) {
489
+ uv_udp_send_t* req_;
490
+ if( req == Qnil) {
491
+ req_ = malloc(sizeof(uv_udp_send_t));
492
+ req = Wrap(req_, free);
493
+ } else {
494
+ Data_Get_Struct(req, uv_udp_send_t, req_);
495
+ }
496
+ uv_udp_t* handle_;
497
+ Data_Get_Struct(handle, uv_udp_t, handle_);
498
+
499
+ struct sockaddr_in* addr_;
500
+ Data_Get_Struct(addr, struct sockaddr_in, addr_);
501
+ uv_buf_t bufs[] = {
502
+ uv_buf_init(StringValueCStr(buf), (int)rb_str_strlen(buf))
503
+ };
504
+ int retval = uv_udp_send(req_, handle_, bufs, 1 , *addr_, foolio__udp_send_cb);
505
+ CHECK(retval);
506
+ return INT2NUM(retval);
507
+ }
508
+
509
+ // UV_EXTERN int uv_udp_send6(uv_udp_send_t* req, uv_udp_t* handle, uv_buf_t bufs[], int bufcnt, struct sockaddr_in6 addr, uv_udp_send_cb send_cb)
510
+ VALUE foolio_udp_send6(VALUE self, VALUE req, VALUE handle, VALUE buf, VALUE addr, VALUE send_cb) {
511
+ uv_udp_send_t* req_;
512
+ if( req == Qnil) {
513
+ req_ = malloc(sizeof(uv_udp_send_t));
514
+ req = Wrap(req_, free);
515
+ } else {
516
+ Data_Get_Struct(req, uv_udp_send_t, req_);
517
+ }
518
+ Data_Get_Struct(req, uv_udp_send_t, req_);
519
+ uv_udp_t* handle_;
520
+ Data_Get_Struct(handle, uv_udp_t, handle_);
521
+ uv_buf_t bufs[] = {
522
+ uv_buf_init(StringValueCStr(buf), (int)rb_str_strlen(buf))
523
+ };
524
+ struct sockaddr_in6* addr_;
525
+ Data_Get_Struct(addr, struct sockaddr_in6, addr_);
526
+ handle_->data = (void*)callback(send_cb);
527
+ int retval = uv_udp_send6(req_, handle_, bufs, 1, *addr_, foolio__udp_send_cb);
528
+ CHECK(retval);
529
+ return INT2NUM(retval);
530
+ }
531
+
532
+ void* safe__wrap(void* ptr) {
533
+ return (void*)Wrap(ptr, NULL);
534
+ }
535
+ VALUE safe_wrap(void* ptr) {
536
+ return (VALUE)rb_thread_call_with_gvl(safe__wrap, ptr);
537
+ }
538
+
539
+ void foolio__udp_recv_cb(uv_udp_t* handle, ssize_t nread, uv_buf_t buf, struct sockaddr* addr, unsigned int flags) {
540
+ VALUE argv[] = {
541
+ foolio__make_buf(buf.base, nread),
542
+ safe_wrap((void*)addr),
543
+ INT2FIX(flags) };
544
+ foolio__cb_apply(handle->loop, handle->data, 3, argv);
545
+ }
546
+
547
+ // UV_EXTERN int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb, uv_udp_recv_cb recv_cb)
548
+ VALUE foolio_udp_recv_start(VALUE self, VALUE handle, VALUE recv_cb) {
549
+ uv_udp_t* handle_;
550
+ Data_Get_Struct(handle, uv_udp_t, handle_);
551
+ handle_->data = (void*)callback(recv_cb);
552
+ int retval = uv_udp_recv_start(handle_, foolio__alloc, foolio__udp_recv_cb);
553
+ CHECK(retval);
554
+ return INT2NUM(retval);
555
+ }
556
+
557
+ // UV_EXTERN int uv_udp_recv_stop(uv_udp_t* handle)
558
+ VALUE foolio_udp_recv_stop(VALUE self, VALUE handle) {
559
+ uv_udp_t* handle_;
560
+ Data_Get_Struct(handle, uv_udp_t, handle_);
561
+ int retval = uv_udp_recv_stop(handle_);
562
+ foolio__cb_free(handle_->data);
563
+ handle_->data = NULL;
564
+ CHECK(retval);
565
+ return INT2NUM(retval);
566
+ }
567
+
568
+ VALUE foolio__str(const char* str) {
569
+ return (VALUE)rb_thread_call_with_gvl((void *(*)(void *))rb_str_new_cstr, (void*)str);
570
+ }
571
+
572
+ void foolio__fs_event_cb(uv_fs_event_t* handle, const char* filename, int events, int status) {
573
+ VALUE argv[] = { INT2FIX(events), INT2FIX(status) };
574
+ foolio__cb_apply(handle->loop, handle->data, 2, argv);
575
+ }
576
+
577
+ VALUE foolio_tcp_init(VALUE self, VALUE loop) {
578
+ InitHandle(tcp);
579
+ }
580
+
581
+ // UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable)
582
+ VALUE foolio_tcp_nodelay(VALUE self, VALUE handle, VALUE enable) {
583
+ uv_tcp_t* handle_;
584
+ Data_Get_Struct(handle, uv_tcp_t, handle_);
585
+ int enable_ = NUM2INT(enable);
586
+ int retval = uv_tcp_nodelay(handle_, enable_);
587
+ CHECK(retval);
588
+ return Qnil;
589
+ }
590
+
591
+ // UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle, int enable,unsigned int delay)
592
+ VALUE foolio_tcp_keepalive(VALUE self, VALUE handle, VALUE enable, VALUE delay) {
593
+ uv_tcp_t* handle_;
594
+ Data_Get_Struct(handle, uv_tcp_t, handle_);
595
+ int enable_ = NUM2INT(enable);
596
+ unsigned int delay_ = NUM2UINT(delay);
597
+ int retval = uv_tcp_keepalive(handle_, enable_, delay_);
598
+ CHECK(retval);
599
+ return INT2NUM(retval);
600
+ }
601
+
602
+ // UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable)
603
+ VALUE foolio_tcp_simultaneous_accepts(VALUE self, VALUE handle, VALUE enable) {
604
+ uv_tcp_t* handle_;
605
+ Data_Get_Struct(handle, uv_tcp_t, handle_);
606
+ int enable_ = NUM2INT(enable);
607
+ int retval = uv_tcp_simultaneous_accepts(handle_, enable_);
608
+ CHECK(retval);
609
+ return INT2NUM(retval);
610
+ }
611
+
612
+ // UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr_in)
613
+ VALUE foolio_tcp_bind(VALUE self, VALUE handle, VALUE sockaddr_in) {
614
+ uv_tcp_t* handle_;
615
+ Data_Get_Struct(handle, uv_tcp_t, handle_);
616
+ struct sockaddr_in* sockaddr_in_;
617
+ Data_Get_Struct(sockaddr_in, struct sockaddr_in, sockaddr_in_);
618
+ int retval = uv_tcp_bind(handle_, *sockaddr_in_);
619
+ CHECK(retval);
620
+ return INT2NUM(retval);
621
+ }
622
+
623
+ // UV_EXTERN int uv_tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6)
624
+ VALUE foolio_tcp_bind6(VALUE self, VALUE handle, VALUE sockaddr_in6) {
625
+ uv_tcp_t* handle_;
626
+ Data_Get_Struct(handle, uv_tcp_t, handle_);
627
+ struct sockaddr_in6* sockaddr_in6_;
628
+ Data_Get_Struct(sockaddr_in6, struct sockaddr_in6, sockaddr_in6_);
629
+
630
+ int retval = uv_tcp_bind6(handle_, *sockaddr_in6_);
631
+ CHECK(retval);
632
+ return INT2NUM(retval);
633
+ }
634
+
635
+ // UV_EXTERN int uv_tcp_getsockname(uv_tcp_t* handle, struct sockaddr* name, int* namelen)
636
+ VALUE foolio_tcp_getsockname(VALUE self, VALUE handle) {
637
+ uv_tcp_t* handle_;
638
+ Data_Get_Struct(handle, uv_tcp_t, handle_);
639
+ struct sockaddr* name_ = malloc(sizeof(struct sockaddr));
640
+ int size;
641
+ int retval = uv_tcp_getsockname(handle_, name_, &size);
642
+ CHECK(retval);
643
+ return Wrap(name_, free);
644
+ }
645
+
646
+ // UV_EXTERN int uv_tcp_getpeername(uv_tcp_t* handle, struct sockaddr* name, int* namelen)
647
+ VALUE foolio_tcp_getpeername(VALUE self, VALUE handle, VALUE name, VALUE namelen) {
648
+ uv_tcp_t* handle_;
649
+ Data_Get_Struct(handle, uv_tcp_t, handle_);
650
+ struct sockaddr* name_ = malloc(sizeof(struct sockaddr));
651
+ int size;
652
+ int retval = uv_tcp_getpeername(handle_, name_, &size);
653
+ CHECK(retval);
654
+ return Wrap(name_, free);
655
+ }
656
+
657
+ void foolio__connect_cb(uv_connect_t* req, int status) {
658
+ VALUE argv[] = { INT2FIX(status) };
659
+ foolio__cb_apply(req->handle->loop, req->handle->data, 1, argv);
660
+ }
661
+
662
+ // UV_EXTERN int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle, struct sockaddr_in address, uv_connect_cb cb)
663
+ VALUE foolio_tcp_connect(VALUE self, VALUE req, VALUE handle, VALUE address, VALUE cb) {
664
+ uv_connect_t* req_;
665
+ if( req == Qnil) {
666
+ req_ = malloc(sizeof(uv_connect_t));
667
+ req = Wrap(req_, free);
668
+ } else {
669
+ Data_Get_Struct(req, uv_connect_t, req_);
670
+ }
671
+ uv_tcp_t* handle_;
672
+ Data_Get_Struct(handle, uv_tcp_t, handle_);
673
+ struct sockaddr_in* address_;
674
+ Data_Get_Struct(address, struct sockaddr_in, address_);
675
+ handle_->data = (void*)callback(cb);
676
+ int retval = uv_tcp_connect(req_, handle_, *address_, foolio__connect_cb);
677
+ CHECK(retval);
678
+ return req;
679
+ }
680
+
681
+ // UV_EXTERN int uv_tcp_connect6(uv_connect_t* req, uv_tcp_t* handle, struct sockaddr_in6 address, uv_connect_cb cb)
682
+ VALUE foolio_tcp_connect6(VALUE self, VALUE req, VALUE handle, VALUE address, VALUE cb) {
683
+ uv_connect_t* req_;
684
+ if( req == Qnil) {
685
+ req_ = malloc(sizeof(uv_connect_t));
686
+ req = Wrap(req_, free);
687
+ } else {
688
+ Data_Get_Struct(req, uv_connect_t, req_);
689
+ }
690
+ uv_tcp_t* handle_;
691
+ Data_Get_Struct(handle, uv_tcp_t, handle_);
692
+ struct sockaddr_in6* address_;
693
+ Data_Get_Struct(address, struct sockaddr_in6, address_);
694
+ req_->data = (void*)callback(cb);
695
+ int retval = uv_tcp_connect6(req_, handle_, *address_, foolio__connect_cb);
696
+ CHECK(retval);
697
+ return req;
698
+ }
699
+
700
+ /*
701
+ UV_EXTERN int uv_idle_init(uv_loop_t*, uv_idle_t* idle);
702
+ UV_EXTERN int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb);
703
+ UV_EXTERN int uv_idle_stop(uv_idle_t* idle);
704
+ */
705
+ VALUE foolio_idle_init(VALUE self, VALUE loop) {
706
+ InitHandle(idle);
707
+ }
708
+
709
+ void foolio__idle_cb(uv_idle_t* handle, int status) {
710
+ uv_idle_t* handle_ = handle;
711
+ CHECK(status);
712
+ foolio__cb_apply(handle->loop, handle->data, 0, NULL);
713
+ }
714
+
715
+ VALUE foolio_idle_start(VALUE self, VALUE handle, VALUE cb) {
716
+ uv_idle_t* handle_;
717
+ Data_Get_Struct(handle, uv_idle_t, handle_);
718
+ handle_->data = callback(cb);
719
+ int retval = uv_idle_start(handle_, foolio__idle_cb);
720
+ CHECK(retval);
721
+ return INT2FIX(retval);
722
+ }
723
+
724
+ VALUE foolio_idle_stop(VALUE self, VALUE handle) {
725
+ uv_idle_t* handle_;
726
+ Data_Get_Struct(handle, uv_idle_t, handle_);
727
+ foolio__cb_free(handle_->data);
728
+ handle_->data = NULL;
729
+ int retval = uv_idle_stop(handle_);
730
+ CHECK(retval);
731
+ return INT2FIX(retval);
732
+ }
733
+
734
+ /**
735
+ UV_EXTERN int uv_timer_init(uv_loop_t*, uv_timer_t* timer);
736
+ UV_EXTERN int uv_timer_start(uv_timer_t* timer, uv_timer_cb cb,
737
+ UV_EXTERN int uv_timer_stop(uv_timer_t* timer);
738
+ UV_EXTERN int uv_timer_again(uv_timer_t* timer);
739
+ UV_EXTERN void uv_timer_set_repeat(uv_timer_t* timer, int64_t repeat);
740
+ UV_EXTERN int64_t uv_timer_get_repeat(uv_timer_t* timer);
741
+ */
742
+ VALUE foolio_timer_init(VALUE self, VALUE loop) {
743
+ InitHandle(timer);
744
+ }
745
+
746
+ static void timer_callback(uv_timer_t* handle_, int status) {
747
+ CHECK(status);
748
+ foolio__cb_apply(handle_->loop, handle_->data, 0, NULL);
749
+ }
750
+
751
+ VALUE foolio_timer_start(VALUE self, VALUE handle, VALUE cb, VALUE timeout, VALUE repeat) {
752
+ uv_timer_t* handle_;
753
+ Data_Get_Struct(handle, uv_timer_t, handle_);
754
+ handle_->data = (void*)callback(cb);
755
+ int retval =
756
+ uv_timer_start(handle_, timer_callback, NUM2INT(timeout), NUM2INT(repeat));
757
+ CHECK(retval);
758
+ return INT2NUM(retval);
759
+ }
760
+
761
+ VALUE foolio_timer_stop(VALUE self, VALUE handle) {
762
+ uv_timer_t* handle_;
763
+ Data_Get_Struct(handle, uv_timer_t, handle_);
764
+ int retval = uv_timer_stop(handle_);
765
+ foolio__cb_free(handle_->data);
766
+ handle_->data = NULL;
767
+ CHECK(retval);
768
+ return INT2FIX(retval);
769
+ }
770
+
771
+ VALUE foolio_timer_again(VALUE self, VALUE handle) {
772
+ uv_timer_t* handle_;
773
+ Data_Get_Struct(handle, uv_timer_t, handle_);
774
+ int retval = uv_timer_again(handle_);
775
+ CHECK(retval);
776
+ return INT2FIX(retval);
777
+ }
778
+
779
+ VALUE foolio_timer_set_repeat(VALUE self, VALUE handle, VALUE repeat) {
780
+ uv_timer_t* handle_;
781
+ Data_Get_Struct(handle, uv_timer_t, handle_);
782
+ int64_t repeat_n = NUM2LONG(repeat);
783
+ uv_timer_set_repeat(handle_, repeat);
784
+ return Qnil;
785
+ }
786
+
787
+ VALUE foolio_timer_get_repeat(VALUE self, VALUE timer) {
788
+ Decl(timer);
789
+ int64_t retval = uv_timer_get_repeat(timer_p);
790
+ return LONG2NUM(retval);
791
+ }
792
+
793
+ // UV_EXTERN int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle, const char* filename, uv_fs_event_cb cb, int flags)
794
+ VALUE foolio_fs_event_init(VALUE self, VALUE loop, VALUE filename, VALUE cb, VALUE flags) {
795
+ uv_loop_t* loop_;
796
+ Data_Get_Struct(loop, uv_loop_t, loop_);
797
+ uv_fs_event_t* handle_ = malloc(sizeof(uv_fs_event_t));
798
+ const char* filename_ = StringValueCStr(filename);
799
+ handle_->data = (void*)callback(cb);
800
+ int flags_ = FIX2INT(flags);
801
+ int retval = uv_fs_event_init(loop_, handle_, filename_, foolio__fs_event_cb, flags_);
802
+ CHECK(retval);
803
+ return Wrap(handle_, 0);
804
+ }
805
+
806
+ // UV_EXTERN int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc)
807
+ VALUE foolio_pipe_init(VALUE self, VALUE loop, VALUE ipc) {
808
+ uv_loop_t* loop_;
809
+ Data_Get_Struct(loop, uv_loop_t, loop_);
810
+ int ipc_ = NUM2INT(ipc);
811
+
812
+ uv_pipe_t * handle = malloc(sizeof(uv_pipe_t));
813
+ if( uv_pipe_init(loop_, handle, ipc_) == 0) {
814
+ return Data_Wrap_Struct(klass, 0, 0, handle);
815
+ } else {
816
+ return Qnil;
817
+ }
818
+ }
819
+
820
+ // UV_EXTERN void uv_pipe_open(uv_pipe_t*, uv_file file)
821
+ VALUE foolio_pipe_open(VALUE self, VALUE handle, VALUE file) {
822
+ uv_pipe_t* handle_;
823
+ Data_Get_Struct(handle, uv_pipe_t, handle_);
824
+ uv_file file_ = NUM2INT(file);
825
+ uv_pipe_open(handle_, file_);
826
+ return Qnil;
827
+ }
828
+
829
+ // UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name)
830
+ VALUE foolio_pipe_bind(VALUE self, VALUE handle, VALUE name) {
831
+ uv_pipe_t* handle_;
832
+ Data_Get_Struct(handle, uv_pipe_t, handle_);
833
+ const char* name_ = StringValueCStr(name);
834
+ int retval = uv_pipe_bind(handle_, name_);
835
+ return INT2NUM(retval);
836
+ }
837
+
838
+ // UV_EXTERN void uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle, const char* name, uv_connect_cb cb)
839
+ VALUE foolio_pipe_connect(VALUE self, VALUE req, VALUE handle, VALUE name, VALUE cb) {
840
+ uv_connect_t* req_;
841
+ Data_Get_Struct(req, uv_connect_t, req_);
842
+ uv_pipe_t* handle_;
843
+ Data_Get_Struct(handle, uv_pipe_t, handle_);
844
+ const char* name_ = StringValueCStr(name);
845
+ handle_->data = (void*)callback(cb);
846
+ uv_pipe_connect(req_, handle_, name_, foolio__connect_cb);
847
+ return Qnil;
848
+ }
849
+
850
+ // UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t* handle, int count)
851
+ VALUE foolio_pipe_pending_instances(VALUE self, VALUE handle, VALUE count) {
852
+ uv_pipe_t* handle_;
853
+ Data_Get_Struct(handle, uv_pipe_t, handle_);
854
+ int count_ = NUM2INT(count);
855
+ uv_pipe_pending_instances(handle_, count_);
856
+ return Qnil;
857
+ }
858
+
859
+ void Init_foolio_ext(void) {
860
+ klass = rb_define_class_under(rb_define_module("Foolio"), "UV", rb_cObject);
861
+ Method(default_loop, 0);
862
+ Method(loop_new, 0);
863
+ Method(loop_delete, 1);
864
+ Method(close_all, 2);
865
+ Method(close, 2);
866
+ Method(is_active, 1);
867
+ Method(run, 1);
868
+ Method(run_once, 1);
869
+ Method(ip4_addr, 2);
870
+ Method(ip6_addr, 2);
871
+ Method(ip_name, 1);
872
+ Method(port, 1);
873
+ Method(listen, 3);
874
+ Method(accept, 2);
875
+ Method(read_start, 2);
876
+ Method(read_stop, 1);
877
+ Method(read2_start, 2);
878
+ Method(write, 4);
879
+ Method(write2, 5);
880
+ Method(is_readable, 1);
881
+ Method(is_writable, 1);
882
+ Method(is_closing, 1);
883
+ Method(udp_init, 1);
884
+ Method(udp_bind, 3);
885
+ Method(udp_bind6, 3);
886
+ Method(udp_getsockname, 3);
887
+ Method(udp_set_membership, 4);
888
+ Method(udp_set_multicast_loop, 2);
889
+ Method(udp_set_multicast_ttl, 2);
890
+ Method(udp_set_broadcast, 2);
891
+ Method(udp_set_ttl, 2);
892
+ Method(udp_send, 5);
893
+ Method(udp_send6, 5);
894
+ Method(udp_recv_start, 2);
895
+ Method(udp_recv_stop, 1);
896
+ Method(tcp_init, 1);
897
+ Method(tcp_nodelay, 2);
898
+ Method(tcp_keepalive, 3);
899
+ Method(tcp_simultaneous_accepts, 2);
900
+ Method(tcp_bind, 2);
901
+ Method(tcp_bind6, 2);
902
+ Method(tcp_getsockname, 1);
903
+ Method(tcp_getpeername, 3);
904
+ Method(tcp_connect, 4);
905
+ Method(tcp_connect6, 4);
906
+ Method(idle_init, 1);
907
+ Method(idle_start, 2);
908
+ Method(idle_stop, 1);
909
+ Method(timer_init, 1);
910
+ Method(timer_start, 4);
911
+ Method(timer_stop, 1);
912
+ Method(timer_again, 1);
913
+ Method(timer_set_repeat, 2);
914
+ Method(timer_get_repeat, 1);
915
+ Method(fs_event_init, 4);
916
+ Method(pipe_init, 2);
917
+ Method(pipe_open, 2);
918
+ Method(pipe_bind, 2);
919
+ Method(pipe_connect, 4);
920
+ Method(pipe_pending_instances, 2);
921
+ }