foolio 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +21 -0
- data/examples/timer.rb +20 -0
- data/ext/foolio/extconf.rb +34 -0
- data/ext/foolio/foolio_ext.c +921 -0
- data/ext/foolio/gen.rb +50 -0
- data/ext/foolio/make_table.rb +12 -0
- data/ext/foolio/templ +243 -0
- data/ext/libuv/.gitignore +33 -0
- data/ext/libuv/.mailmap +13 -0
- data/ext/libuv/.travis.yml +9 -0
- data/ext/libuv/AUTHORS +61 -0
- data/ext/libuv/LICENSE +44 -0
- data/ext/libuv/Makefile +71 -0
- data/ext/libuv/README.md +90 -0
- data/ext/libuv/common.gypi +178 -0
- data/ext/libuv/gyp_uv +73 -0
- data/ext/libuv/include/uv-private/eio.h +403 -0
- data/ext/libuv/include/uv-private/ev.h +838 -0
- data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
- data/ext/libuv/include/uv-private/tree.h +768 -0
- data/ext/libuv/include/uv-private/uv-unix.h +324 -0
- data/ext/libuv/include/uv-private/uv-win.h +517 -0
- data/ext/libuv/include/uv.h +1838 -0
- data/ext/libuv/src/fs-poll.c +235 -0
- data/ext/libuv/src/inet.c +293 -0
- data/ext/libuv/src/unix/async.c +148 -0
- data/ext/libuv/src/unix/core.c +696 -0
- data/ext/libuv/src/unix/cygwin.c +83 -0
- data/ext/libuv/src/unix/darwin.c +342 -0
- data/ext/libuv/src/unix/dl.c +83 -0
- data/ext/libuv/src/unix/eio/Changes +63 -0
- data/ext/libuv/src/unix/eio/LICENSE +36 -0
- data/ext/libuv/src/unix/eio/Makefile.am +15 -0
- data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
- data/ext/libuv/src/unix/eio/autogen.sh +3 -0
- data/ext/libuv/src/unix/eio/config.h.in +86 -0
- data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
- data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
- data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
- data/ext/libuv/src/unix/eio/config_linux.h +94 -0
- data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
- data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
- data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
- data/ext/libuv/src/unix/eio/configure.ac +22 -0
- data/ext/libuv/src/unix/eio/demo.c +194 -0
- data/ext/libuv/src/unix/eio/ecb.h +370 -0
- data/ext/libuv/src/unix/eio/eio.3 +3428 -0
- data/ext/libuv/src/unix/eio/eio.c +2593 -0
- data/ext/libuv/src/unix/eio/eio.pod +969 -0
- data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
- data/ext/libuv/src/unix/eio/xthread.h +164 -0
- data/ext/libuv/src/unix/error.c +105 -0
- data/ext/libuv/src/unix/ev/Changes +388 -0
- data/ext/libuv/src/unix/ev/LICENSE +36 -0
- data/ext/libuv/src/unix/ev/Makefile.am +18 -0
- data/ext/libuv/src/unix/ev/Makefile.in +771 -0
- data/ext/libuv/src/unix/ev/README +58 -0
- data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
- data/ext/libuv/src/unix/ev/autogen.sh +6 -0
- data/ext/libuv/src/unix/ev/config.guess +1526 -0
- data/ext/libuv/src/unix/ev/config.h.in +125 -0
- data/ext/libuv/src/unix/ev/config.sub +1658 -0
- data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
- data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
- data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
- data/ext/libuv/src/unix/ev/config_linux.h +141 -0
- data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
- data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
- data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
- data/ext/libuv/src/unix/ev/configure +13037 -0
- data/ext/libuv/src/unix/ev/configure.ac +18 -0
- data/ext/libuv/src/unix/ev/depcomp +630 -0
- data/ext/libuv/src/unix/ev/ev++.h +816 -0
- data/ext/libuv/src/unix/ev/ev.3 +5311 -0
- data/ext/libuv/src/unix/ev/ev.c +3925 -0
- data/ext/libuv/src/unix/ev/ev.pod +5243 -0
- data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
- data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
- data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
- data/ext/libuv/src/unix/ev/ev_port.c +179 -0
- data/ext/libuv/src/unix/ev/ev_select.c +310 -0
- data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
- data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
- data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
- data/ext/libuv/src/unix/ev/event.c +402 -0
- data/ext/libuv/src/unix/ev/event.h +170 -0
- data/ext/libuv/src/unix/ev/install-sh +294 -0
- data/ext/libuv/src/unix/ev/libev.m4 +39 -0
- data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
- data/ext/libuv/src/unix/ev/missing +336 -0
- data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
- data/ext/libuv/src/unix/freebsd.c +326 -0
- data/ext/libuv/src/unix/fs.c +739 -0
- data/ext/libuv/src/unix/internal.h +188 -0
- data/ext/libuv/src/unix/kqueue.c +120 -0
- data/ext/libuv/src/unix/linux/inotify.c +239 -0
- data/ext/libuv/src/unix/linux/linux-core.c +557 -0
- data/ext/libuv/src/unix/linux/syscalls.c +388 -0
- data/ext/libuv/src/unix/linux/syscalls.h +124 -0
- data/ext/libuv/src/unix/loop-watcher.c +62 -0
- data/ext/libuv/src/unix/loop.c +94 -0
- data/ext/libuv/src/unix/netbsd.c +108 -0
- data/ext/libuv/src/unix/openbsd.c +295 -0
- data/ext/libuv/src/unix/pipe.c +259 -0
- data/ext/libuv/src/unix/poll.c +114 -0
- data/ext/libuv/src/unix/process.c +495 -0
- data/ext/libuv/src/unix/signal.c +269 -0
- data/ext/libuv/src/unix/stream.c +990 -0
- data/ext/libuv/src/unix/sunos.c +481 -0
- data/ext/libuv/src/unix/tcp.c +393 -0
- data/ext/libuv/src/unix/thread.c +251 -0
- data/ext/libuv/src/unix/timer.c +136 -0
- data/ext/libuv/src/unix/tty.c +145 -0
- data/ext/libuv/src/unix/udp.c +659 -0
- data/ext/libuv/src/unix/uv-eio.c +107 -0
- data/ext/libuv/src/unix/uv-eio.h +13 -0
- data/ext/libuv/src/uv-common.c +380 -0
- data/ext/libuv/src/uv-common.h +170 -0
- data/ext/libuv/src/win/async.c +100 -0
- data/ext/libuv/src/win/atomicops-inl.h +56 -0
- data/ext/libuv/src/win/core.c +278 -0
- data/ext/libuv/src/win/dl.c +86 -0
- data/ext/libuv/src/win/error.c +155 -0
- data/ext/libuv/src/win/fs-event.c +510 -0
- data/ext/libuv/src/win/fs.c +1948 -0
- data/ext/libuv/src/win/getaddrinfo.c +365 -0
- data/ext/libuv/src/win/handle-inl.h +149 -0
- data/ext/libuv/src/win/handle.c +154 -0
- data/ext/libuv/src/win/internal.h +343 -0
- data/ext/libuv/src/win/loop-watcher.c +122 -0
- data/ext/libuv/src/win/pipe.c +1672 -0
- data/ext/libuv/src/win/poll.c +616 -0
- data/ext/libuv/src/win/process-stdio.c +500 -0
- data/ext/libuv/src/win/process.c +1013 -0
- data/ext/libuv/src/win/req-inl.h +220 -0
- data/ext/libuv/src/win/req.c +25 -0
- data/ext/libuv/src/win/signal.c +57 -0
- data/ext/libuv/src/win/stream-inl.h +67 -0
- data/ext/libuv/src/win/stream.c +167 -0
- data/ext/libuv/src/win/tcp.c +1394 -0
- data/ext/libuv/src/win/thread.c +372 -0
- data/ext/libuv/src/win/threadpool.c +74 -0
- data/ext/libuv/src/win/timer.c +224 -0
- data/ext/libuv/src/win/tty.c +1799 -0
- data/ext/libuv/src/win/udp.c +716 -0
- data/ext/libuv/src/win/util.c +864 -0
- data/ext/libuv/src/win/winapi.c +132 -0
- data/ext/libuv/src/win/winapi.h +4452 -0
- data/ext/libuv/src/win/winsock.c +557 -0
- data/ext/libuv/src/win/winsock.h +171 -0
- data/ext/libuv/test/benchmark-async-pummel.c +97 -0
- data/ext/libuv/test/benchmark-async.c +137 -0
- data/ext/libuv/test/benchmark-fs-stat.c +135 -0
- data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
- data/ext/libuv/test/benchmark-list.h +127 -0
- data/ext/libuv/test/benchmark-loop-count.c +88 -0
- data/ext/libuv/test/benchmark-million-timers.c +65 -0
- data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
- data/ext/libuv/test/benchmark-pound.c +324 -0
- data/ext/libuv/test/benchmark-pump.c +462 -0
- data/ext/libuv/test/benchmark-sizes.c +44 -0
- data/ext/libuv/test/benchmark-spawn.c +162 -0
- data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
- data/ext/libuv/test/benchmark-thread.c +64 -0
- data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
- data/ext/libuv/test/blackhole-server.c +118 -0
- data/ext/libuv/test/dns-server.c +321 -0
- data/ext/libuv/test/echo-server.c +378 -0
- data/ext/libuv/test/fixtures/empty_file +0 -0
- data/ext/libuv/test/fixtures/load_error.node +1 -0
- data/ext/libuv/test/run-benchmarks.c +64 -0
- data/ext/libuv/test/run-tests.c +138 -0
- data/ext/libuv/test/runner-unix.c +295 -0
- data/ext/libuv/test/runner-unix.h +36 -0
- data/ext/libuv/test/runner-win.c +285 -0
- data/ext/libuv/test/runner-win.h +42 -0
- data/ext/libuv/test/runner.c +355 -0
- data/ext/libuv/test/runner.h +159 -0
- data/ext/libuv/test/task.h +112 -0
- data/ext/libuv/test/test-async.c +118 -0
- data/ext/libuv/test/test-callback-order.c +76 -0
- data/ext/libuv/test/test-callback-stack.c +203 -0
- data/ext/libuv/test/test-connection-fail.c +148 -0
- data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
- data/ext/libuv/test/test-delayed-accept.c +188 -0
- data/ext/libuv/test/test-dlerror.c +58 -0
- data/ext/libuv/test/test-error.c +59 -0
- data/ext/libuv/test/test-fail-always.c +29 -0
- data/ext/libuv/test/test-fs-event.c +474 -0
- data/ext/libuv/test/test-fs-poll.c +146 -0
- data/ext/libuv/test/test-fs.c +1843 -0
- data/ext/libuv/test/test-get-currentexe.c +63 -0
- data/ext/libuv/test/test-get-loadavg.c +36 -0
- data/ext/libuv/test/test-get-memory.c +38 -0
- data/ext/libuv/test/test-getaddrinfo.c +122 -0
- data/ext/libuv/test/test-getsockname.c +342 -0
- data/ext/libuv/test/test-hrtime.c +54 -0
- data/ext/libuv/test/test-idle.c +81 -0
- data/ext/libuv/test/test-ipc-send-recv.c +209 -0
- data/ext/libuv/test/test-ipc.c +620 -0
- data/ext/libuv/test/test-list.h +427 -0
- data/ext/libuv/test/test-loop-handles.c +336 -0
- data/ext/libuv/test/test-multiple-listen.c +102 -0
- data/ext/libuv/test/test-mutexes.c +63 -0
- data/ext/libuv/test/test-pass-always.c +28 -0
- data/ext/libuv/test/test-ping-pong.c +253 -0
- data/ext/libuv/test/test-pipe-bind-error.c +140 -0
- data/ext/libuv/test/test-pipe-connect-error.c +96 -0
- data/ext/libuv/test/test-platform-output.c +87 -0
- data/ext/libuv/test/test-poll-close.c +72 -0
- data/ext/libuv/test/test-poll.c +573 -0
- data/ext/libuv/test/test-process-title.c +49 -0
- data/ext/libuv/test/test-ref.c +338 -0
- data/ext/libuv/test/test-run-once.c +48 -0
- data/ext/libuv/test/test-semaphore.c +111 -0
- data/ext/libuv/test/test-shutdown-close.c +103 -0
- data/ext/libuv/test/test-shutdown-eof.c +183 -0
- data/ext/libuv/test/test-signal.c +162 -0
- data/ext/libuv/test/test-spawn.c +863 -0
- data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
- data/ext/libuv/test/test-tcp-bind-error.c +191 -0
- data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
- data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
- data/ext/libuv/test/test-tcp-close.c +129 -0
- data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
- data/ext/libuv/test/test-tcp-connect-error.c +70 -0
- data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
- data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
- data/ext/libuv/test/test-tcp-flags.c +51 -0
- data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
- data/ext/libuv/test/test-tcp-unexpected-read.c +113 -0
- data/ext/libuv/test/test-tcp-write-error.c +168 -0
- data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
- data/ext/libuv/test/test-tcp-writealot.c +170 -0
- data/ext/libuv/test/test-thread.c +183 -0
- data/ext/libuv/test/test-threadpool.c +57 -0
- data/ext/libuv/test/test-timer-again.c +141 -0
- data/ext/libuv/test/test-timer.c +152 -0
- data/ext/libuv/test/test-tty.c +110 -0
- data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
- data/ext/libuv/test/test-udp-ipv6.c +156 -0
- data/ext/libuv/test/test-udp-multicast-join.c +139 -0
- data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
- data/ext/libuv/test/test-udp-options.c +86 -0
- data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
- data/ext/libuv/test/test-util.c +97 -0
- data/ext/libuv/test/test-walk-handles.c +77 -0
- data/ext/libuv/uv.gyp +375 -0
- data/ext/libuv/vcbuild.bat +105 -0
- data/foolio.gemspec +18 -0
- data/lib/foolio.rb +9 -0
- data/lib/foolio/handle.rb +27 -0
- data/lib/foolio/listener.rb +26 -0
- data/lib/foolio/loop.rb +79 -0
- data/lib/foolio/stream.rb +109 -0
- data/lib/foolio/version.rb +3 -0
- metadata +309 -0
@@ -0,0 +1,107 @@
|
|
1
|
+
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
2
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
3
|
+
* of this software and associated documentation files (the "Software"), to
|
4
|
+
* deal in the Software without restriction, including without limitation the
|
5
|
+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
6
|
+
* sell copies of the Software, and to permit persons to whom the Software is
|
7
|
+
* furnished to do so, subject to the following conditions:
|
8
|
+
*
|
9
|
+
* The above copyright notice and this permission notice shall be included in
|
10
|
+
* all copies or substantial portions of the Software.
|
11
|
+
*
|
12
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
13
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
14
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
15
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
16
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
17
|
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
18
|
+
* IN THE SOFTWARE.
|
19
|
+
*/
|
20
|
+
|
21
|
+
/* This file integrates the libuv event loop with the libeio thread pool */
|
22
|
+
|
23
|
+
#include "uv.h"
|
24
|
+
#include "eio.h"
|
25
|
+
#include "internal.h"
|
26
|
+
|
27
|
+
#include <assert.h>
|
28
|
+
#include <stdio.h>
|
29
|
+
|
30
|
+
|
31
|
+
static uv_once_t uv__eio_init_once_guard = UV_ONCE_INIT;
|
32
|
+
|
33
|
+
|
34
|
+
static void uv_eio_do_poll(uv_idle_t* watcher, int status) {
|
35
|
+
uv_loop_t* loop = watcher->loop;
|
36
|
+
assert(watcher == &loop->uv_eio_poller);
|
37
|
+
if (eio_poll(&loop->uv_eio_channel) != -1)
|
38
|
+
uv_idle_stop(watcher);
|
39
|
+
}
|
40
|
+
|
41
|
+
|
42
|
+
/* Called from the main thread. */
|
43
|
+
static void uv_eio_want_poll_notifier_cb(uv_async_t* watcher, int status) {
|
44
|
+
uv_loop_t* loop = watcher->loop;
|
45
|
+
assert(watcher == &loop->uv_eio_want_poll_notifier);
|
46
|
+
if (eio_poll(&loop->uv_eio_channel) == -1)
|
47
|
+
uv_idle_start(&loop->uv_eio_poller, uv_eio_do_poll);
|
48
|
+
}
|
49
|
+
|
50
|
+
|
51
|
+
static void uv_eio_done_poll_notifier_cb(uv_async_t* watcher, int revents) {
|
52
|
+
uv_loop_t* loop = watcher->loop;
|
53
|
+
assert(watcher == &loop->uv_eio_done_poll_notifier);
|
54
|
+
if (eio_poll(&loop->uv_eio_channel) != -1)
|
55
|
+
uv_idle_stop(&loop->uv_eio_poller);
|
56
|
+
}
|
57
|
+
|
58
|
+
|
59
|
+
/*
|
60
|
+
* uv_eio_want_poll() is called from the EIO thread pool each time an EIO
|
61
|
+
* request (that is, one of the node.fs.* functions) has completed.
|
62
|
+
*/
|
63
|
+
static void uv_eio_want_poll(eio_channel *channel) {
|
64
|
+
/* Signal the main thread that eio_poll need to be processed. */
|
65
|
+
uv_loop_t* loop = channel->data;
|
66
|
+
uv_async_send(&loop->uv_eio_want_poll_notifier);
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
static void uv_eio_done_poll(eio_channel *channel) {
|
71
|
+
/*
|
72
|
+
* Signal the main thread that we should stop calling eio_poll().
|
73
|
+
* from the idle watcher.
|
74
|
+
*/
|
75
|
+
uv_loop_t* loop = channel->data;
|
76
|
+
uv_async_send(&loop->uv_eio_done_poll_notifier);
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
static void uv__eio_init(void) {
|
81
|
+
eio_init(uv_eio_want_poll, uv_eio_done_poll);
|
82
|
+
}
|
83
|
+
|
84
|
+
|
85
|
+
void uv_eio_init(uv_loop_t* loop) {
|
86
|
+
if (loop->flags & UV_LOOP_EIO_INITIALIZED) return;
|
87
|
+
loop->flags |= UV_LOOP_EIO_INITIALIZED;
|
88
|
+
|
89
|
+
uv_idle_init(loop, &loop->uv_eio_poller);
|
90
|
+
uv_idle_start(&loop->uv_eio_poller, uv_eio_do_poll);
|
91
|
+
loop->uv_eio_poller.flags |= UV__HANDLE_INTERNAL;
|
92
|
+
|
93
|
+
loop->uv_eio_want_poll_notifier.data = loop;
|
94
|
+
uv_async_init(loop,
|
95
|
+
&loop->uv_eio_want_poll_notifier,
|
96
|
+
uv_eio_want_poll_notifier_cb);
|
97
|
+
loop->uv_eio_want_poll_notifier.flags |= UV__HANDLE_INTERNAL;
|
98
|
+
uv__handle_unref(&loop->uv_eio_want_poll_notifier);
|
99
|
+
|
100
|
+
uv_async_init(loop,
|
101
|
+
&loop->uv_eio_done_poll_notifier,
|
102
|
+
uv_eio_done_poll_notifier_cb);
|
103
|
+
loop->uv_eio_done_poll_notifier.flags |= UV__HANDLE_INTERNAL;
|
104
|
+
uv__handle_unref(&loop->uv_eio_done_poll_notifier);
|
105
|
+
|
106
|
+
uv_once(&uv__eio_init_once_guard, uv__eio_init);
|
107
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/* This header is private to libuv */
|
2
|
+
#ifndef UV_EIO_H_
|
3
|
+
#define UV_EIO_H_
|
4
|
+
|
5
|
+
#include "eio.h"
|
6
|
+
|
7
|
+
/*
|
8
|
+
* Call this function to integrate libeio into the libuv event loop. It is
|
9
|
+
* safe to call more than once.
|
10
|
+
* TODO: uv_eio_deinit
|
11
|
+
*/
|
12
|
+
void uv_eio_init(uv_loop_t*);
|
13
|
+
#endif
|
@@ -0,0 +1,380 @@
|
|
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 "uv.h"
|
23
|
+
#include "uv-common.h"
|
24
|
+
|
25
|
+
#include <stdio.h>
|
26
|
+
#include <assert.h>
|
27
|
+
#include <stddef.h> /* NULL */
|
28
|
+
#include <stdlib.h> /* malloc */
|
29
|
+
#include <string.h> /* memset */
|
30
|
+
|
31
|
+
|
32
|
+
#define XX(uc, lc) case UV_##uc: return sizeof(uv_##lc##_t);
|
33
|
+
|
34
|
+
size_t uv_handle_size(uv_handle_type type) {
|
35
|
+
switch (type) {
|
36
|
+
UV_HANDLE_TYPE_MAP(XX)
|
37
|
+
default:
|
38
|
+
return -1;
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
size_t uv_req_size(uv_req_type type) {
|
43
|
+
switch(type) {
|
44
|
+
UV_REQ_TYPE_MAP(XX)
|
45
|
+
default:
|
46
|
+
return -1;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
#undef XX
|
51
|
+
|
52
|
+
size_t uv_strlcpy(char* dst, const char* src, size_t size) {
|
53
|
+
size_t n;
|
54
|
+
|
55
|
+
if (size == 0)
|
56
|
+
return 0;
|
57
|
+
|
58
|
+
for (n = 0; n < (size - 1) && *src != '\0'; n++)
|
59
|
+
*dst++ = *src++;
|
60
|
+
|
61
|
+
*dst = '\0';
|
62
|
+
|
63
|
+
return n;
|
64
|
+
}
|
65
|
+
|
66
|
+
|
67
|
+
size_t uv_strlcat(char* dst, const char* src, size_t size) {
|
68
|
+
size_t n;
|
69
|
+
|
70
|
+
if (size == 0)
|
71
|
+
return 0;
|
72
|
+
|
73
|
+
for (n = 0; n < size && *dst != '\0'; n++, dst++);
|
74
|
+
|
75
|
+
if (n == size)
|
76
|
+
return n;
|
77
|
+
|
78
|
+
while (n < (size - 1) && *src != '\0')
|
79
|
+
n++, *dst++ = *src++;
|
80
|
+
|
81
|
+
*dst = '\0';
|
82
|
+
|
83
|
+
return n;
|
84
|
+
}
|
85
|
+
|
86
|
+
|
87
|
+
uv_buf_t uv_buf_init(char* base, unsigned int len) {
|
88
|
+
uv_buf_t buf;
|
89
|
+
buf.base = base;
|
90
|
+
buf.len = len;
|
91
|
+
return buf;
|
92
|
+
}
|
93
|
+
|
94
|
+
|
95
|
+
const uv_err_t uv_ok_ = { UV_OK, 0 };
|
96
|
+
|
97
|
+
#define UV_ERR_NAME_GEN(val, name, s) case UV_##name : return #name;
|
98
|
+
const char* uv_err_name(uv_err_t err) {
|
99
|
+
switch (err.code) {
|
100
|
+
UV_ERRNO_MAP(UV_ERR_NAME_GEN)
|
101
|
+
default:
|
102
|
+
assert(0);
|
103
|
+
return NULL;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
#undef UV_ERR_NAME_GEN
|
107
|
+
|
108
|
+
|
109
|
+
#define UV_STRERROR_GEN(val, name, s) case UV_##name : return s;
|
110
|
+
const char* uv_strerror(uv_err_t err) {
|
111
|
+
switch (err.code) {
|
112
|
+
UV_ERRNO_MAP(UV_STRERROR_GEN)
|
113
|
+
default:
|
114
|
+
return "Unknown system error";
|
115
|
+
}
|
116
|
+
}
|
117
|
+
#undef UV_STRERROR_GEN
|
118
|
+
|
119
|
+
|
120
|
+
int uv__set_error(uv_loop_t* loop, uv_err_code code, int sys_error) {
|
121
|
+
loop->last_err.code = code;
|
122
|
+
loop->last_err.sys_errno_ = sys_error;
|
123
|
+
return -1;
|
124
|
+
}
|
125
|
+
|
126
|
+
|
127
|
+
int uv__set_sys_error(uv_loop_t* loop, int sys_error) {
|
128
|
+
loop->last_err.code = uv_translate_sys_error(sys_error);
|
129
|
+
loop->last_err.sys_errno_ = sys_error;
|
130
|
+
return -1;
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
int uv__set_artificial_error(uv_loop_t* loop, uv_err_code code) {
|
135
|
+
loop->last_err = uv__new_artificial_error(code);
|
136
|
+
return -1;
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
uv_err_t uv__new_sys_error(int sys_error) {
|
141
|
+
uv_err_t error;
|
142
|
+
error.code = uv_translate_sys_error(sys_error);
|
143
|
+
error.sys_errno_ = sys_error;
|
144
|
+
return error;
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
uv_err_t uv__new_artificial_error(uv_err_code code) {
|
149
|
+
uv_err_t error;
|
150
|
+
error.code = code;
|
151
|
+
error.sys_errno_ = 0;
|
152
|
+
return error;
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
uv_err_t uv_last_error(uv_loop_t* loop) {
|
157
|
+
return loop->last_err;
|
158
|
+
}
|
159
|
+
|
160
|
+
|
161
|
+
struct sockaddr_in uv_ip4_addr(const char* ip, int port) {
|
162
|
+
struct sockaddr_in addr;
|
163
|
+
|
164
|
+
memset(&addr, 0, sizeof(struct sockaddr_in));
|
165
|
+
|
166
|
+
addr.sin_family = AF_INET;
|
167
|
+
addr.sin_port = htons(port);
|
168
|
+
addr.sin_addr.s_addr = inet_addr(ip);
|
169
|
+
|
170
|
+
return addr;
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
struct sockaddr_in6 uv_ip6_addr(const char* ip, int port) {
|
175
|
+
struct sockaddr_in6 addr;
|
176
|
+
|
177
|
+
memset(&addr, 0, sizeof(struct sockaddr_in6));
|
178
|
+
|
179
|
+
addr.sin6_family = AF_INET6;
|
180
|
+
addr.sin6_port = htons(port);
|
181
|
+
uv_inet_pton(AF_INET6, ip, &addr.sin6_addr);
|
182
|
+
|
183
|
+
return addr;
|
184
|
+
}
|
185
|
+
|
186
|
+
|
187
|
+
int uv_ip4_name(struct sockaddr_in* src, char* dst, size_t size) {
|
188
|
+
uv_err_t err = uv_inet_ntop(AF_INET, &src->sin_addr, dst, size);
|
189
|
+
return err.code != UV_OK;
|
190
|
+
}
|
191
|
+
|
192
|
+
|
193
|
+
int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size) {
|
194
|
+
uv_err_t err = uv_inet_ntop(AF_INET6, &src->sin6_addr, dst, size);
|
195
|
+
return err.code != UV_OK;
|
196
|
+
}
|
197
|
+
|
198
|
+
|
199
|
+
int uv_tcp_bind(uv_tcp_t* handle, struct sockaddr_in addr) {
|
200
|
+
if (handle->type != UV_TCP || addr.sin_family != AF_INET) {
|
201
|
+
uv__set_artificial_error(handle->loop, UV_EFAULT);
|
202
|
+
return -1;
|
203
|
+
}
|
204
|
+
|
205
|
+
return uv__tcp_bind(handle, addr);
|
206
|
+
}
|
207
|
+
|
208
|
+
|
209
|
+
int uv_tcp_bind6(uv_tcp_t* handle, struct sockaddr_in6 addr) {
|
210
|
+
if (handle->type != UV_TCP || addr.sin6_family != AF_INET6) {
|
211
|
+
uv__set_artificial_error(handle->loop, UV_EFAULT);
|
212
|
+
return -1;
|
213
|
+
}
|
214
|
+
|
215
|
+
return uv__tcp_bind6(handle, addr);
|
216
|
+
}
|
217
|
+
|
218
|
+
|
219
|
+
int uv_udp_bind(uv_udp_t* handle, struct sockaddr_in addr,
|
220
|
+
unsigned int flags) {
|
221
|
+
if (handle->type != UV_UDP || addr.sin_family != AF_INET) {
|
222
|
+
uv__set_artificial_error(handle->loop, UV_EFAULT);
|
223
|
+
return -1;
|
224
|
+
}
|
225
|
+
|
226
|
+
return uv__udp_bind(handle, addr, flags);
|
227
|
+
}
|
228
|
+
|
229
|
+
|
230
|
+
int uv_udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr,
|
231
|
+
unsigned int flags) {
|
232
|
+
if (handle->type != UV_UDP || addr.sin6_family != AF_INET6) {
|
233
|
+
uv__set_artificial_error(handle->loop, UV_EFAULT);
|
234
|
+
return -1;
|
235
|
+
}
|
236
|
+
|
237
|
+
return uv__udp_bind6(handle, addr, flags);
|
238
|
+
}
|
239
|
+
|
240
|
+
|
241
|
+
int uv_tcp_connect(uv_connect_t* req,
|
242
|
+
uv_tcp_t* handle,
|
243
|
+
struct sockaddr_in address,
|
244
|
+
uv_connect_cb cb) {
|
245
|
+
if (handle->type != UV_TCP || address.sin_family != AF_INET) {
|
246
|
+
uv__set_artificial_error(handle->loop, UV_EINVAL);
|
247
|
+
return -1;
|
248
|
+
}
|
249
|
+
|
250
|
+
return uv__tcp_connect(req, handle, address, cb);
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
int uv_tcp_connect6(uv_connect_t* req,
|
255
|
+
uv_tcp_t* handle,
|
256
|
+
struct sockaddr_in6 address,
|
257
|
+
uv_connect_cb cb) {
|
258
|
+
if (handle->type != UV_TCP || address.sin6_family != AF_INET6) {
|
259
|
+
uv__set_artificial_error(handle->loop, UV_EINVAL);
|
260
|
+
return -1;
|
261
|
+
}
|
262
|
+
|
263
|
+
return uv__tcp_connect6(req, handle, address, cb);
|
264
|
+
}
|
265
|
+
|
266
|
+
|
267
|
+
#ifdef _WIN32
|
268
|
+
static UINT __stdcall uv__thread_start(void *ctx_v)
|
269
|
+
#else
|
270
|
+
static void *uv__thread_start(void *ctx_v)
|
271
|
+
#endif
|
272
|
+
{
|
273
|
+
void (*entry)(void *arg);
|
274
|
+
void *arg;
|
275
|
+
|
276
|
+
struct {
|
277
|
+
void (*entry)(void *arg);
|
278
|
+
void *arg;
|
279
|
+
} *ctx;
|
280
|
+
|
281
|
+
ctx = ctx_v;
|
282
|
+
arg = ctx->arg;
|
283
|
+
entry = ctx->entry;
|
284
|
+
free(ctx);
|
285
|
+
entry(arg);
|
286
|
+
|
287
|
+
return 0;
|
288
|
+
}
|
289
|
+
|
290
|
+
|
291
|
+
int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
|
292
|
+
struct {
|
293
|
+
void (*entry)(void *arg);
|
294
|
+
void *arg;
|
295
|
+
} *ctx;
|
296
|
+
|
297
|
+
if ((ctx = malloc(sizeof *ctx)) == NULL)
|
298
|
+
return -1;
|
299
|
+
|
300
|
+
ctx->entry = entry;
|
301
|
+
ctx->arg = arg;
|
302
|
+
|
303
|
+
#ifdef _WIN32
|
304
|
+
*tid = (HANDLE) _beginthreadex(NULL, 0, uv__thread_start, ctx, 0, NULL);
|
305
|
+
if (*tid == 0) {
|
306
|
+
#else
|
307
|
+
if (pthread_create(tid, NULL, uv__thread_start, ctx)) {
|
308
|
+
#endif
|
309
|
+
free(ctx);
|
310
|
+
return -1;
|
311
|
+
}
|
312
|
+
|
313
|
+
return 0;
|
314
|
+
}
|
315
|
+
|
316
|
+
|
317
|
+
void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg) {
|
318
|
+
ngx_queue_t* q;
|
319
|
+
uv_handle_t* h;
|
320
|
+
|
321
|
+
ngx_queue_foreach(q, &loop->handle_queue) {
|
322
|
+
h = ngx_queue_data(q, uv_handle_t, handle_queue);
|
323
|
+
if (h->flags & UV__HANDLE_INTERNAL) continue;
|
324
|
+
walk_cb(h, arg);
|
325
|
+
}
|
326
|
+
}
|
327
|
+
|
328
|
+
|
329
|
+
#ifndef NDEBUG
|
330
|
+
static void uv__print_handles(uv_loop_t* loop, int only_active) {
|
331
|
+
const char* type;
|
332
|
+
ngx_queue_t* q;
|
333
|
+
uv_handle_t* h;
|
334
|
+
|
335
|
+
if (loop == NULL)
|
336
|
+
loop = uv_default_loop();
|
337
|
+
|
338
|
+
ngx_queue_foreach(q, &loop->handle_queue) {
|
339
|
+
h = ngx_queue_data(q, uv_handle_t, handle_queue);
|
340
|
+
|
341
|
+
if (only_active && !uv__is_active(h))
|
342
|
+
continue;
|
343
|
+
|
344
|
+
switch (h->type) {
|
345
|
+
#define X(uc, lc) case UV_##uc: type = #lc; break;
|
346
|
+
UV_HANDLE_TYPE_MAP(X)
|
347
|
+
#undef X
|
348
|
+
default: type = "<unknown>";
|
349
|
+
}
|
350
|
+
|
351
|
+
fprintf(stderr,
|
352
|
+
"[%c%c%c] %-8s %p\n",
|
353
|
+
"R-"[!(h->flags & UV__HANDLE_REF)],
|
354
|
+
"A-"[!(h->flags & UV__HANDLE_ACTIVE)],
|
355
|
+
"I-"[!(h->flags & UV__HANDLE_INTERNAL)],
|
356
|
+
type,
|
357
|
+
(void*)h);
|
358
|
+
}
|
359
|
+
}
|
360
|
+
|
361
|
+
|
362
|
+
void uv_print_all_handles(uv_loop_t* loop) {
|
363
|
+
uv__print_handles(loop, 0);
|
364
|
+
}
|
365
|
+
|
366
|
+
|
367
|
+
void uv_print_active_handles(uv_loop_t* loop) {
|
368
|
+
uv__print_handles(loop, 1);
|
369
|
+
}
|
370
|
+
#endif
|
371
|
+
|
372
|
+
|
373
|
+
void uv_ref(uv_handle_t* handle) {
|
374
|
+
uv__handle_ref(handle);
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
void uv_unref(uv_handle_t* handle) {
|
379
|
+
uv__handle_unref(handle);
|
380
|
+
}
|