wj_eventmachine 1.3.0.dev.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +179 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +110 -0
  6. data/docs/DocumentationGuidesIndex.md +27 -0
  7. data/docs/GettingStarted.md +520 -0
  8. data/docs/old/ChangeLog +211 -0
  9. data/docs/old/DEFERRABLES +246 -0
  10. data/docs/old/EPOLL +141 -0
  11. data/docs/old/INSTALL +13 -0
  12. data/docs/old/KEYBOARD +42 -0
  13. data/docs/old/LEGAL +25 -0
  14. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  15. data/docs/old/PURE_RUBY +75 -0
  16. data/docs/old/RELEASE_NOTES +94 -0
  17. data/docs/old/SMTP +4 -0
  18. data/docs/old/SPAWNED_PROCESSES +148 -0
  19. data/docs/old/TODO +8 -0
  20. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  21. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  22. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  23. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  24. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  25. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  26. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  27. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  28. data/examples/old/ex_channel.rb +43 -0
  29. data/examples/old/ex_queue.rb +2 -0
  30. data/examples/old/ex_tick_loop_array.rb +15 -0
  31. data/examples/old/ex_tick_loop_counter.rb +32 -0
  32. data/examples/old/helper.rb +2 -0
  33. data/ext/binder.cpp +124 -0
  34. data/ext/binder.h +52 -0
  35. data/ext/cmain.cpp +1046 -0
  36. data/ext/ed.cpp +2238 -0
  37. data/ext/ed.h +460 -0
  38. data/ext/em.cpp +2378 -0
  39. data/ext/em.h +266 -0
  40. data/ext/eventmachine.h +152 -0
  41. data/ext/extconf.rb +285 -0
  42. data/ext/fastfilereader/extconf.rb +120 -0
  43. data/ext/fastfilereader/mapper.cpp +214 -0
  44. data/ext/fastfilereader/mapper.h +59 -0
  45. data/ext/fastfilereader/rubymain.cpp +126 -0
  46. data/ext/kb.cpp +79 -0
  47. data/ext/page.cpp +107 -0
  48. data/ext/page.h +51 -0
  49. data/ext/pipe.cpp +354 -0
  50. data/ext/project.h +174 -0
  51. data/ext/rubymain.cpp +1610 -0
  52. data/ext/ssl.cpp +627 -0
  53. data/ext/ssl.h +103 -0
  54. data/ext/wait_for_single_fd.h +36 -0
  55. data/java/.classpath +8 -0
  56. data/java/.project +17 -0
  57. data/java/src/com/rubyeventmachine/EmReactor.java +625 -0
  58. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  59. data/java/src/com/rubyeventmachine/EmReactorInterface.java +70 -0
  60. data/java/src/com/rubyeventmachine/EventableChannel.java +72 -0
  61. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +201 -0
  62. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +415 -0
  63. data/java/src/com/rubyeventmachine/NullEmReactor.java +157 -0
  64. data/java/src/com/rubyeventmachine/NullEventableChannel.java +81 -0
  65. data/lib/em/buftok.rb +59 -0
  66. data/lib/em/callback.rb +58 -0
  67. data/lib/em/channel.rb +69 -0
  68. data/lib/em/completion.rb +307 -0
  69. data/lib/em/connection.rb +776 -0
  70. data/lib/em/deferrable.rb +210 -0
  71. data/lib/em/deferrable/pool.rb +2 -0
  72. data/lib/em/file_watch.rb +73 -0
  73. data/lib/em/future.rb +61 -0
  74. data/lib/em/io_streamer.rb +68 -0
  75. data/lib/em/iterator.rb +252 -0
  76. data/lib/em/messages.rb +66 -0
  77. data/lib/em/pool.rb +151 -0
  78. data/lib/em/process_watch.rb +45 -0
  79. data/lib/em/processes.rb +123 -0
  80. data/lib/em/protocols.rb +37 -0
  81. data/lib/em/protocols/header_and_content.rb +138 -0
  82. data/lib/em/protocols/httpclient.rb +303 -0
  83. data/lib/em/protocols/httpclient2.rb +602 -0
  84. data/lib/em/protocols/line_and_text.rb +125 -0
  85. data/lib/em/protocols/line_protocol.rb +33 -0
  86. data/lib/em/protocols/linetext2.rb +179 -0
  87. data/lib/em/protocols/memcache.rb +331 -0
  88. data/lib/em/protocols/object_protocol.rb +46 -0
  89. data/lib/em/protocols/postgres3.rb +246 -0
  90. data/lib/em/protocols/saslauth.rb +175 -0
  91. data/lib/em/protocols/smtpclient.rb +394 -0
  92. data/lib/em/protocols/smtpserver.rb +666 -0
  93. data/lib/em/protocols/socks4.rb +66 -0
  94. data/lib/em/protocols/stomp.rb +205 -0
  95. data/lib/em/protocols/tcptest.rb +54 -0
  96. data/lib/em/pure_ruby.rb +1299 -0
  97. data/lib/em/queue.rb +80 -0
  98. data/lib/em/resolver.rb +232 -0
  99. data/lib/em/spawnable.rb +84 -0
  100. data/lib/em/streamer.rb +118 -0
  101. data/lib/em/threaded_resource.rb +90 -0
  102. data/lib/em/tick_loop.rb +85 -0
  103. data/lib/em/timers.rb +61 -0
  104. data/lib/em/version.rb +3 -0
  105. data/lib/eventmachine.rb +1602 -0
  106. data/lib/jeventmachine.rb +318 -0
  107. data/rakelib/package.rake +120 -0
  108. data/rakelib/test.rake +6 -0
  109. data/rakelib/test_pure.rake +11 -0
  110. data/tests/client.crt +31 -0
  111. data/tests/client.key +51 -0
  112. data/tests/dhparam.pem +13 -0
  113. data/tests/em_ssl_handlers.rb +153 -0
  114. data/tests/em_test_helper.rb +198 -0
  115. data/tests/jruby/test_jeventmachine.rb +38 -0
  116. data/tests/test_attach.rb +199 -0
  117. data/tests/test_basic.rb +321 -0
  118. data/tests/test_channel.rb +75 -0
  119. data/tests/test_completion.rb +178 -0
  120. data/tests/test_connection_count.rb +83 -0
  121. data/tests/test_connection_write.rb +35 -0
  122. data/tests/test_defer.rb +35 -0
  123. data/tests/test_deferrable.rb +35 -0
  124. data/tests/test_epoll.rb +141 -0
  125. data/tests/test_error_handler.rb +38 -0
  126. data/tests/test_exc.rb +37 -0
  127. data/tests/test_file_watch.rb +86 -0
  128. data/tests/test_fork.rb +75 -0
  129. data/tests/test_futures.rb +170 -0
  130. data/tests/test_handler_check.rb +35 -0
  131. data/tests/test_hc.rb +155 -0
  132. data/tests/test_httpclient.rb +238 -0
  133. data/tests/test_httpclient2.rb +132 -0
  134. data/tests/test_idle_connection.rb +31 -0
  135. data/tests/test_inactivity_timeout.rb +102 -0
  136. data/tests/test_io_streamer.rb +47 -0
  137. data/tests/test_ipv4.rb +96 -0
  138. data/tests/test_ipv6.rb +107 -0
  139. data/tests/test_iterator.rb +122 -0
  140. data/tests/test_kb.rb +28 -0
  141. data/tests/test_keepalive.rb +113 -0
  142. data/tests/test_line_protocol.rb +33 -0
  143. data/tests/test_ltp.rb +155 -0
  144. data/tests/test_ltp2.rb +332 -0
  145. data/tests/test_many_fds.rb +21 -0
  146. data/tests/test_next_tick.rb +104 -0
  147. data/tests/test_object_protocol.rb +36 -0
  148. data/tests/test_pause.rb +109 -0
  149. data/tests/test_pending_connect_timeout.rb +52 -0
  150. data/tests/test_pool.rb +196 -0
  151. data/tests/test_process_watch.rb +50 -0
  152. data/tests/test_processes.rb +128 -0
  153. data/tests/test_proxy_connection.rb +180 -0
  154. data/tests/test_pure.rb +156 -0
  155. data/tests/test_queue.rb +64 -0
  156. data/tests/test_resolver.rb +129 -0
  157. data/tests/test_running.rb +14 -0
  158. data/tests/test_sasl.rb +46 -0
  159. data/tests/test_send_file.rb +217 -0
  160. data/tests/test_servers.rb +32 -0
  161. data/tests/test_shutdown_hooks.rb +23 -0
  162. data/tests/test_smtpclient.rb +75 -0
  163. data/tests/test_smtpserver.rb +90 -0
  164. data/tests/test_sock_opt.rb +53 -0
  165. data/tests/test_spawn.rb +290 -0
  166. data/tests/test_ssl_args.rb +41 -0
  167. data/tests/test_ssl_dhparam.rb +57 -0
  168. data/tests/test_ssl_ecdh_curve.rb +57 -0
  169. data/tests/test_ssl_extensions.rb +24 -0
  170. data/tests/test_ssl_methods.rb +31 -0
  171. data/tests/test_ssl_protocols.rb +190 -0
  172. data/tests/test_ssl_verify.rb +52 -0
  173. data/tests/test_stomp.rb +38 -0
  174. data/tests/test_system.rb +46 -0
  175. data/tests/test_threaded_resource.rb +68 -0
  176. data/tests/test_tick_loop.rb +58 -0
  177. data/tests/test_timers.rb +150 -0
  178. data/tests/test_ud.rb +8 -0
  179. data/tests/test_unbind_reason.rb +40 -0
  180. metadata +384 -0
@@ -0,0 +1,174 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: project.h
6
+ Date: 06Apr06
7
+
8
+ Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
9
+ Gmail: blackhedd
10
+
11
+ This program is free software; you can redistribute it and/or modify
12
+ it under the terms of either: 1) the GNU General Public License
13
+ as published by the Free Software Foundation; either version 2 of the
14
+ License, or (at your option) any later version; or 2) Ruby's License.
15
+
16
+ See the file COPYING for complete licensing information.
17
+
18
+ *****************************************************************************/
19
+
20
+
21
+ #ifndef __Project__H_
22
+ #define __Project__H_
23
+
24
+
25
+ #include <iostream>
26
+ #include <map>
27
+ #include <set>
28
+ #include <vector>
29
+ #include <deque>
30
+ #include <string>
31
+ #include <sstream>
32
+ #include <stdexcept>
33
+
34
+
35
+ #ifdef OS_UNIX
36
+ #include <signal.h>
37
+ #include <netdb.h>
38
+ #include <time.h>
39
+ #include <sys/time.h>
40
+ #include <sys/types.h>
41
+ #include <sys/stat.h>
42
+ #include <sys/socket.h>
43
+ #include <sys/un.h>
44
+ #include <sys/resource.h>
45
+ #include <sys/wait.h>
46
+ #include <assert.h>
47
+ #include <unistd.h>
48
+ #include <fcntl.h>
49
+ #include <errno.h>
50
+ #include <netinet/in.h>
51
+ #include <netinet/tcp.h>
52
+ #include <arpa/inet.h>
53
+ #include <pwd.h>
54
+ #include <string.h>
55
+ typedef int SOCKET;
56
+ #define INVALID_SOCKET -1
57
+ #define SOCKET_ERROR -1
58
+ #ifdef OS_SOLARIS8
59
+ #include <strings.h>
60
+ #include <sys/un.h>
61
+ #ifndef AF_LOCAL
62
+ #define AF_LOCAL AF_UNIX
63
+ #endif
64
+ // INADDR_NONE is undefined on Solaris < 8. Thanks to Brett Eisenberg and Tim Pease.
65
+ #ifndef INADDR_NONE
66
+ #define INADDR_NONE ((unsigned long)-1)
67
+ #endif
68
+ #endif /* OS_SOLARIS8 */
69
+
70
+ #ifdef _AIX
71
+ #include <strings.h>
72
+ #ifndef AF_LOCAL
73
+ #define AF_LOCAL AF_UNIX
74
+ #endif
75
+ #endif /* _AIX */
76
+
77
+ #ifdef OS_DARWIN
78
+ #include <mach/mach.h>
79
+ #include <mach/mach_time.h>
80
+ #endif /* OS_DARWIN */
81
+
82
+ #endif /* OS_UNIX */
83
+
84
+ #ifdef OS_WIN32
85
+ // 21Sep09: windows limits select() to 64 sockets by default, we increase it to 1024 here (before including winsock2.h)
86
+ // 18Jun12: fd_setsize must be changed in the ruby binary (not in this extension). redefining it also causes segvs, see eventmachine/eventmachine#333
87
+ //#define FD_SETSIZE 1024
88
+
89
+ // WIN32_LEAN_AND_MEAN excludes APIs such as Cryptography, DDE, RPC, Shell, and Windows Sockets.
90
+ #define WIN32_LEAN_AND_MEAN
91
+
92
+ #include <windows.h>
93
+ #include <winsock2.h>
94
+ #include <ws2tcpip.h>
95
+ #include <rpc.h>
96
+ #include <fcntl.h>
97
+ #include <assert.h>
98
+
99
+ // Older versions of MinGW in the Ruby Dev Kit do not provide the getaddrinfo hint flags
100
+ #ifndef AI_ADDRCONFIG
101
+ #define AI_ADDRCONFIG 0x0400
102
+ #endif
103
+
104
+ #ifndef AI_NUMERICSERV
105
+ #define AI_NUMERICSERV 0x0008
106
+ #endif
107
+
108
+ // Use the Win32 wrapper library that Ruby owns to be able to close sockets with the close() function
109
+ #define RUBY_EXPORT
110
+ #include <ruby/defines.h>
111
+ #include <ruby/win32.h>
112
+ #endif /* OS_WIN32 */
113
+
114
+ #if !defined(_MSC_VER) || _MSC_VER > 1500
115
+ #include <stdint.h>
116
+ #endif
117
+
118
+ #ifdef WITH_SSL
119
+ #include <openssl/ssl.h>
120
+ #include <openssl/err.h>
121
+ #endif
122
+
123
+ #ifdef HAVE_EPOLL
124
+ #include <sys/epoll.h>
125
+ #endif
126
+
127
+ #ifdef HAVE_KQUEUE
128
+ #include <sys/event.h>
129
+ #include <sys/queue.h>
130
+ #endif
131
+
132
+ #ifdef HAVE_INOTIFY
133
+ #include <sys/inotify.h>
134
+ #endif
135
+
136
+ #ifdef HAVE_OLD_INOTIFY
137
+ #include <sys/syscall.h>
138
+ #include <linux/inotify.h>
139
+ static inline int inotify_init (void) { return syscall (__NR_inotify_init); }
140
+ static inline int inotify_add_watch (int fd, const char *name, __u32 mask) { return syscall (__NR_inotify_add_watch, fd, name, mask); }
141
+ static inline int inotify_rm_watch (int fd, __u32 wd) { return syscall (__NR_inotify_rm_watch, fd, wd); }
142
+ #define HAVE_INOTIFY 1
143
+ #endif
144
+
145
+ #ifdef HAVE_INOTIFY
146
+ #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
147
+ #endif
148
+
149
+ #ifdef HAVE_WRITEV
150
+ #include <sys/uio.h>
151
+ #endif
152
+
153
+ #if __cplusplus
154
+ extern "C" {
155
+ #endif
156
+ typedef void (*EMCallback)(const unsigned long, int, const char*, const unsigned long);
157
+ #if __cplusplus
158
+ }
159
+ #endif
160
+
161
+ #if defined(__GNUC__) && (__GNUC__ >= 3)
162
+ #define UNUSED __attribute__ ((unused))
163
+ #else
164
+ #define UNUSED
165
+ #endif
166
+
167
+ #include "binder.h"
168
+ #include "em.h"
169
+ #include "ed.h"
170
+ #include "page.h"
171
+ #include "ssl.h"
172
+ #include "eventmachine.h"
173
+
174
+ #endif // __Project__H_
@@ -0,0 +1,1610 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: rubymain.cpp
6
+ Date: 06Apr06
7
+
8
+ Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
9
+ Gmail: blackhedd
10
+
11
+ This program is free software; you can redistribute it and/or modify
12
+ it under the terms of either: 1) the GNU General Public License
13
+ as published by the Free Software Foundation; either version 2 of the
14
+ License, or (at your option) any later version; or 2) Ruby's License.
15
+
16
+ See the file COPYING for complete licensing information.
17
+
18
+ *****************************************************************************/
19
+
20
+ #include "project.h"
21
+ #include "eventmachine.h"
22
+ #include <ruby.h>
23
+
24
+ /* Adapted from NUM2BSIG / BSIG2NUM in ext/fiddle/conversions.h,
25
+ * we'll call it a BSIG for Binding Signature here. */
26
+ #if SIZEOF_VOIDP == SIZEOF_LONG
27
+ # define BSIG2NUM(x) (ULONG2NUM((unsigned long)(x)))
28
+ # define NUM2BSIG(x) (NUM2ULONG(x))
29
+ # ifdef OS_WIN32
30
+ # define PRIFBSIG "I32u"
31
+ # else
32
+ # define PRIFBSIG "lu"
33
+ # endif
34
+ #else
35
+ # define BSIG2NUM(x) (ULL2NUM((unsigned long long)(x)))
36
+ # define NUM2BSIG(x) (NUM2ULL(x))
37
+ # ifdef OS_WIN32
38
+ # define PRIFBSIG "I64u"
39
+ # else
40
+ # define PRIFBSIG "llu"
41
+ # endif
42
+ #endif
43
+
44
+ /* Adapted from SWIG's changes for Ruby 2.7 compatibility.
45
+ * Before Ruby 2.7, rb_rescue takes (VALUE (*))(ANYARGS)
46
+ * whereas in Ruby 2.7, rb_rescue takes (VALUE (*))(VALUE)
47
+ * */
48
+ #if defined(__cplusplus) && !defined(RB_METHOD_DEFINITION_DECL)
49
+ # define VALUEFUNC(f) ((VALUE (*)(ANYARGS)) f)
50
+ #else
51
+ # define VALUEFUNC(f) (f)
52
+ #endif
53
+
54
+ /*******
55
+ Statics
56
+ *******/
57
+
58
+ static VALUE EmModule;
59
+ static VALUE EmConnection;
60
+ static VALUE EmConnsHash;
61
+ static VALUE EmTimersHash;
62
+
63
+ static VALUE EM_eConnectionError;
64
+ static VALUE EM_eUnknownTimerFired;
65
+ static VALUE EM_eConnectionNotBound;
66
+ static VALUE EM_eUnsupported;
67
+ static VALUE EM_eInvalidSignature;
68
+
69
+ static VALUE Intern_at_signature;
70
+ static VALUE Intern_at_timers;
71
+ static VALUE Intern_at_conns;
72
+ static VALUE Intern_at_error_handler;
73
+ static VALUE Intern_event_callback;
74
+ static VALUE Intern_run_deferred_callbacks;
75
+ static VALUE Intern_delete;
76
+ static VALUE Intern_call;
77
+ static VALUE Intern_at;
78
+ static VALUE Intern_receive_data;
79
+ static VALUE Intern_ssl_handshake_completed;
80
+ static VALUE Intern_ssl_verify_peer;
81
+ static VALUE Intern_notify_readable;
82
+ static VALUE Intern_notify_writable;
83
+ static VALUE Intern_proxy_target_unbound;
84
+ static VALUE Intern_proxy_completed;
85
+ static VALUE Intern_connection_completed;
86
+
87
+ static VALUE rb_cProcStatus;
88
+
89
+ struct em_event {
90
+ uintptr_t signature;
91
+ int event;
92
+ const char *data_str;
93
+ unsigned long data_num;
94
+ };
95
+
96
+ static inline VALUE ensure_conn(const uintptr_t signature)
97
+ {
98
+ VALUE conn = rb_hash_aref (EmConnsHash, BSIG2NUM (signature));
99
+ if (conn == Qnil)
100
+ rb_raise (EM_eConnectionNotBound, "unknown connection: %" PRIFBSIG, signature);
101
+ return conn;
102
+ }
103
+
104
+
105
+ /****************
106
+ t_event_callback
107
+ ****************/
108
+
109
+ static inline VALUE event_callback (VALUE e_value)
110
+ {
111
+ struct em_event *e = (struct em_event *)e_value;
112
+ const uintptr_t signature = e->signature;
113
+ int event = e->event;
114
+ const char *data_str = e->data_str;
115
+ const unsigned long data_num = e->data_num;
116
+
117
+ switch (event) {
118
+ case EM_CONNECTION_READ:
119
+ {
120
+ VALUE conn = rb_hash_aref (EmConnsHash, BSIG2NUM (signature));
121
+ if (conn == Qnil)
122
+ rb_raise (EM_eConnectionNotBound, "received %lu bytes of data for unknown signature: %" PRIFBSIG, data_num, signature);
123
+ rb_funcall (conn, Intern_receive_data, 1, rb_str_new (data_str, data_num));
124
+ return Qnil;
125
+ }
126
+ case EM_CONNECTION_ACCEPTED:
127
+ {
128
+ rb_funcall (EmModule, Intern_event_callback, 3, BSIG2NUM(signature), INT2FIX(event), ULONG2NUM(data_num));
129
+ return Qnil;
130
+ }
131
+ case EM_CONNECTION_UNBOUND:
132
+ {
133
+ rb_funcall (EmModule, Intern_event_callback, 3, BSIG2NUM(signature), INT2FIX(event), ULONG2NUM(data_num));
134
+ return Qnil;
135
+ }
136
+ case EM_CONNECTION_COMPLETED:
137
+ {
138
+ VALUE conn = ensure_conn(signature);
139
+ rb_funcall (conn, Intern_connection_completed, 0);
140
+ return Qnil;
141
+ }
142
+ case EM_CONNECTION_NOTIFY_READABLE:
143
+ {
144
+ VALUE conn = ensure_conn(signature);
145
+ rb_funcall (conn, Intern_notify_readable, 0);
146
+ return Qnil;
147
+ }
148
+ case EM_CONNECTION_NOTIFY_WRITABLE:
149
+ {
150
+ VALUE conn = ensure_conn(signature);
151
+ rb_funcall (conn, Intern_notify_writable, 0);
152
+ return Qnil;
153
+ }
154
+ case EM_LOOPBREAK_SIGNAL:
155
+ {
156
+ rb_funcall (EmModule, Intern_run_deferred_callbacks, 0);
157
+ return Qnil;
158
+ }
159
+ case EM_TIMER_FIRED:
160
+ {
161
+ VALUE timer = rb_funcall (EmTimersHash, Intern_delete, 1, ULONG2NUM (data_num));
162
+ if (timer == Qnil) {
163
+ rb_raise (EM_eUnknownTimerFired, "no such timer: %lu", data_num);
164
+ } else if (timer == Qfalse) {
165
+ /* Timer Canceled */
166
+ } else {
167
+ rb_funcall (timer, Intern_call, 0);
168
+ }
169
+ return Qnil;
170
+ }
171
+ #ifdef WITH_SSL
172
+ case EM_SSL_HANDSHAKE_COMPLETED:
173
+ {
174
+ VALUE conn = ensure_conn(signature);
175
+ rb_funcall (conn, Intern_ssl_handshake_completed, 0);
176
+ return Qnil;
177
+ }
178
+ case EM_SSL_VERIFY:
179
+ {
180
+ VALUE conn = ensure_conn(signature);
181
+ VALUE should_accept = rb_funcall (conn, Intern_ssl_verify_peer, 1, rb_str_new(data_str, data_num));
182
+ if (RTEST(should_accept))
183
+ evma_accept_ssl_peer (signature);
184
+ return Qnil;
185
+ }
186
+ #endif
187
+ case EM_PROXY_TARGET_UNBOUND:
188
+ {
189
+ VALUE conn = ensure_conn(signature);
190
+ rb_funcall (conn, Intern_proxy_target_unbound, 0);
191
+ return Qnil;
192
+ }
193
+ case EM_PROXY_COMPLETED:
194
+ {
195
+ VALUE conn = ensure_conn(signature);
196
+ rb_funcall (conn, Intern_proxy_completed, 0);
197
+ return Qnil;
198
+ }
199
+ }
200
+
201
+ return Qnil;
202
+ }
203
+
204
+ /*******************
205
+ event_error_handler
206
+ *******************/
207
+
208
+ static VALUE event_error_handler(VALUE self UNUSED, VALUE err)
209
+ {
210
+ VALUE error_handler = rb_ivar_get(EmModule, Intern_at_error_handler);
211
+ rb_funcall (error_handler, Intern_call, 1, err);
212
+ return Qnil;
213
+ }
214
+
215
+ /**********************
216
+ event_callback_wrapper
217
+ **********************/
218
+
219
+ static void event_callback_wrapper (const uintptr_t signature, int event, const char *data_str, const unsigned long data_num)
220
+ {
221
+ struct em_event e;
222
+ e.signature = signature;
223
+ e.event = event;
224
+ e.data_str = data_str;
225
+ e.data_num = data_num;
226
+
227
+ if (!rb_ivar_defined(EmModule, Intern_at_error_handler))
228
+ event_callback((VALUE)&e);
229
+ else
230
+ rb_rescue(VALUEFUNC(event_callback), (VALUE)&e, VALUEFUNC(event_error_handler), Qnil);
231
+ }
232
+
233
+ /**************************
234
+ t_initialize_event_machine
235
+ **************************/
236
+
237
+ static VALUE t_initialize_event_machine (VALUE self UNUSED)
238
+ {
239
+ EmConnsHash = rb_ivar_get (EmModule, Intern_at_conns);
240
+ EmTimersHash = rb_ivar_get (EmModule, Intern_at_timers);
241
+ assert(EmConnsHash != Qnil);
242
+ assert(EmTimersHash != Qnil);
243
+ evma_initialize_library ((EMCallback)event_callback_wrapper);
244
+ return Qnil;
245
+ }
246
+
247
+
248
+ /******************
249
+ t_run_machine_once
250
+ ******************/
251
+
252
+ static VALUE t_run_machine_once (VALUE self UNUSED)
253
+ {
254
+ return evma_run_machine_once () ? Qtrue : Qfalse;
255
+ }
256
+
257
+
258
+ /*************
259
+ t_run_machine
260
+ *************/
261
+
262
+ static VALUE t_run_machine (VALUE self UNUSED)
263
+ {
264
+ evma_run_machine();
265
+ return Qnil;
266
+ }
267
+
268
+ /*****************************
269
+ t_get_timer_count
270
+ *****************************/
271
+
272
+ static VALUE t_get_timer_count ()
273
+ {
274
+ return SIZET2NUM (evma_get_timer_count ());
275
+ }
276
+
277
+ /*******************
278
+ t_add_oneshot_timer
279
+ *******************/
280
+
281
+ static VALUE t_add_oneshot_timer (VALUE self UNUSED, VALUE interval)
282
+ {
283
+ const uintptr_t f = evma_install_oneshot_timer (FIX2LONG (interval));
284
+ if (!f)
285
+ rb_raise (rb_eRuntimeError, "%s", "ran out of timers; use #set_max_timers to increase limit");
286
+ return BSIG2NUM (f);
287
+ }
288
+
289
+
290
+ /**************
291
+ t_start_server
292
+ **************/
293
+
294
+ static VALUE t_start_server (VALUE self UNUSED, VALUE server, VALUE port)
295
+ {
296
+ const uintptr_t f = evma_create_tcp_server (StringValueCStr(server), FIX2INT(port));
297
+ if (!f)
298
+ rb_raise (rb_eRuntimeError, "%s", "no acceptor (port is in use or requires root privileges)");
299
+ return BSIG2NUM (f);
300
+ }
301
+
302
+ /*************
303
+ t_stop_server
304
+ *************/
305
+
306
+ static VALUE t_stop_server (VALUE self UNUSED, VALUE signature)
307
+ {
308
+ evma_stop_tcp_server (NUM2BSIG (signature));
309
+ return Qnil;
310
+ }
311
+
312
+
313
+ /*******************
314
+ t_start_unix_server
315
+ *******************/
316
+
317
+ static VALUE t_start_unix_server (VALUE self UNUSED, VALUE filename)
318
+ {
319
+ const uintptr_t f = evma_create_unix_domain_server (StringValueCStr(filename));
320
+ if (!f)
321
+ rb_raise (rb_eRuntimeError, "%s", "no unix-domain acceptor");
322
+ return BSIG2NUM (f);
323
+ }
324
+
325
+ /********************
326
+ t_attach_sd
327
+ ********************/
328
+
329
+ static VALUE t_attach_sd(VALUE self UNUSED, VALUE sd)
330
+ {
331
+ const uintptr_t f = evma_attach_sd(FIX2INT(sd));
332
+ if (!f)
333
+ rb_raise (rb_eRuntimeError, "%s", "no socket descriptor acceptor");
334
+ return BSIG2NUM (f);
335
+ }
336
+
337
+
338
+ /***********
339
+ t_send_data
340
+ ***********/
341
+
342
+ static VALUE t_send_data (VALUE self UNUSED, VALUE signature, VALUE data, VALUE data_length)
343
+ {
344
+ int b = evma_send_data_to_connection (NUM2BSIG (signature), StringValuePtr (data), FIX2INT (data_length));
345
+ return INT2NUM (b);
346
+ }
347
+
348
+
349
+ /***********
350
+ t_start_tls
351
+ ***********/
352
+
353
+ static VALUE t_start_tls (VALUE self UNUSED, VALUE signature)
354
+ {
355
+ evma_start_tls (NUM2BSIG (signature));
356
+ return Qnil;
357
+ }
358
+
359
+ /***************
360
+ t_set_tls_parms
361
+ ***************/
362
+
363
+ static VALUE t_set_tls_parms (VALUE self UNUSED, VALUE signature, VALUE privkeyfile, VALUE certchainfile, VALUE verify_peer, VALUE fail_if_no_peer_cert, VALUE snihostname, VALUE cipherlist, VALUE ecdh_curve, VALUE dhparam, VALUE ssl_version)
364
+ {
365
+ /* set_tls_parms takes a series of positional arguments for specifying such things
366
+ * as private keys and certificate chains.
367
+ * It's expected that the parameter list will grow as we add more supported features.
368
+ * ALL of these parameters are optional, and can be specified as empty or NULL strings.
369
+ */
370
+ evma_set_tls_parms (NUM2BSIG (signature), StringValueCStr (privkeyfile), StringValueCStr (certchainfile), (verify_peer == Qtrue ? 1 : 0), (fail_if_no_peer_cert == Qtrue ? 1 : 0), StringValueCStr (snihostname), StringValueCStr (cipherlist), StringValueCStr (ecdh_curve), StringValueCStr (dhparam), NUM2INT (ssl_version));
371
+ return Qnil;
372
+ }
373
+
374
+ /***************
375
+ t_get_peer_cert
376
+ ***************/
377
+
378
+ #ifdef WITH_SSL
379
+ static VALUE t_get_peer_cert (VALUE self UNUSED, VALUE signature)
380
+ {
381
+ VALUE ret = Qnil;
382
+
383
+ X509 *cert = NULL;
384
+ BUF_MEM *buf;
385
+ BIO *out;
386
+
387
+ cert = evma_get_peer_cert (NUM2BSIG (signature));
388
+
389
+ if (cert != NULL) {
390
+ out = BIO_new(BIO_s_mem());
391
+ PEM_write_bio_X509(out, cert);
392
+ BIO_get_mem_ptr(out, &buf);
393
+ ret = rb_str_new(buf->data, buf->length);
394
+ X509_free(cert);
395
+ BIO_free(out);
396
+ }
397
+
398
+ return ret;
399
+ }
400
+ #else
401
+ static VALUE t_get_peer_cert (VALUE self UNUSED, VALUE signature UNUSED)
402
+ {
403
+ return Qnil;
404
+ }
405
+ #endif
406
+
407
+ /***************
408
+ t_get_cipher_bits
409
+ ***************/
410
+
411
+ #ifdef WITH_SSL
412
+ static VALUE t_get_cipher_bits (VALUE self UNUSED, VALUE signature)
413
+ {
414
+ int bits = evma_get_cipher_bits (NUM2BSIG (signature));
415
+ if (bits == -1)
416
+ return Qnil;
417
+ return INT2NUM (bits);
418
+ }
419
+ #else
420
+ static VALUE t_get_cipher_bits (VALUE self UNUSED, VALUE signature UNUSED)
421
+ {
422
+ return Qnil;
423
+ }
424
+ #endif
425
+
426
+ /***************
427
+ t_get_cipher_name
428
+ ***************/
429
+
430
+ #ifdef WITH_SSL
431
+ static VALUE t_get_cipher_name (VALUE self UNUSED, VALUE signature)
432
+ {
433
+ const char *protocol = evma_get_cipher_name (NUM2BSIG (signature));
434
+ if (protocol)
435
+ return rb_str_new2 (protocol);
436
+
437
+ return Qnil;
438
+ }
439
+ #else
440
+ static VALUE t_get_cipher_name (VALUE self UNUSED, VALUE signature UNUSED)
441
+ {
442
+ return Qnil;
443
+ }
444
+ #endif
445
+
446
+ /***************
447
+ t_get_cipher_protocol
448
+ ***************/
449
+
450
+ #ifdef WITH_SSL
451
+ static VALUE t_get_cipher_protocol (VALUE self UNUSED, VALUE signature)
452
+ {
453
+ const char *cipher = evma_get_cipher_protocol (NUM2BSIG (signature));
454
+ if (cipher)
455
+ return rb_str_new2 (cipher);
456
+
457
+ return Qnil;
458
+ }
459
+ #else
460
+ static VALUE t_get_cipher_protocol (VALUE self UNUSED, VALUE signature UNUSED)
461
+ {
462
+ return Qnil;
463
+ }
464
+ #endif
465
+
466
+ /***************
467
+ t_get_sni_hostname
468
+ ***************/
469
+
470
+ #ifdef WITH_SSL
471
+ static VALUE t_get_sni_hostname (VALUE self UNUSED, VALUE signature)
472
+ {
473
+ const char *sni_hostname = evma_get_sni_hostname (NUM2BSIG (signature));
474
+ if (sni_hostname)
475
+ return rb_str_new2 (sni_hostname);
476
+
477
+ return Qnil;
478
+ }
479
+ #else
480
+ static VALUE t_get_sni_hostname (VALUE self UNUSED, VALUE signature UNUSED)
481
+ {
482
+ return Qnil;
483
+ }
484
+ #endif
485
+
486
+ /**************
487
+ t_get_peername
488
+ **************/
489
+
490
+ static VALUE t_get_peername (VALUE self UNUSED, VALUE signature)
491
+ {
492
+ char buf[1024];
493
+ socklen_t len = sizeof buf;
494
+ try {
495
+ if (evma_get_peername (NUM2BSIG (signature), (struct sockaddr*)buf, &len)) {
496
+ return rb_str_new (buf, len);
497
+ }
498
+ } catch (std::runtime_error e) {
499
+ rb_raise (rb_eRuntimeError, "%s", e.what());
500
+ }
501
+
502
+ return Qnil;
503
+ }
504
+
505
+ /**************
506
+ t_get_sockname
507
+ **************/
508
+
509
+ static VALUE t_get_sockname (VALUE self UNUSED, VALUE signature)
510
+ {
511
+ char buf[1024];
512
+ socklen_t len = sizeof buf;
513
+ try {
514
+ if (evma_get_sockname (NUM2BSIG (signature), (struct sockaddr*)buf, &len)) {
515
+ return rb_str_new (buf, len);
516
+ }
517
+ } catch (std::runtime_error e) {
518
+ rb_raise (rb_eRuntimeError, "%s", e.what());
519
+ }
520
+
521
+ return Qnil;
522
+ }
523
+
524
+ /********************
525
+ t_get_subprocess_pid
526
+ ********************/
527
+
528
+ static VALUE t_get_subprocess_pid (VALUE self UNUSED, VALUE signature)
529
+ {
530
+ pid_t pid;
531
+ if (evma_get_subprocess_pid (NUM2BSIG (signature), &pid)) {
532
+ return INT2NUM (pid);
533
+ }
534
+
535
+ return Qnil;
536
+ }
537
+
538
+ /***********************
539
+ t_get_subprocess_status
540
+ ***********************/
541
+
542
+ static VALUE t_get_subprocess_status (VALUE self UNUSED, VALUE signature)
543
+ {
544
+ VALUE proc_status = Qnil;
545
+
546
+ int status;
547
+ pid_t pid;
548
+
549
+ if (evma_get_subprocess_status (NUM2BSIG (signature), &status)) {
550
+ if (evma_get_subprocess_pid (NUM2BSIG (signature), &pid)) {
551
+ proc_status = rb_obj_alloc(rb_cProcStatus);
552
+
553
+ /* MRI Ruby uses hidden instance vars */
554
+ rb_iv_set(proc_status, "status", INT2FIX(status));
555
+ rb_iv_set(proc_status, "pid", INT2FIX(pid));
556
+
557
+ #ifdef RUBINIUS
558
+ /* Rubinius uses standard instance vars */
559
+ rb_iv_set(proc_status, "@pid", INT2FIX(pid));
560
+ if (WIFEXITED(status)) {
561
+ rb_iv_set(proc_status, "@status", INT2FIX(WEXITSTATUS(status)));
562
+ } else if (WIFSIGNALED(status)) {
563
+ rb_iv_set(proc_status, "@termsig", INT2FIX(WTERMSIG(status)));
564
+ } else if (WIFSTOPPED(status)) {
565
+ rb_iv_set(proc_status, "@stopsig", INT2FIX(WSTOPSIG(status)));
566
+ }
567
+ #endif
568
+ }
569
+ }
570
+
571
+ return proc_status;
572
+ }
573
+
574
+ /**********************
575
+ t_get_connection_count
576
+ **********************/
577
+
578
+ static VALUE t_get_connection_count (VALUE self UNUSED)
579
+ {
580
+ return INT2NUM(evma_get_connection_count());
581
+ }
582
+
583
+ /*****************************
584
+ t_get_comm_inactivity_timeout
585
+ *****************************/
586
+
587
+ static VALUE t_get_comm_inactivity_timeout (VALUE self UNUSED, VALUE signature)
588
+ {
589
+ return rb_float_new(evma_get_comm_inactivity_timeout(NUM2BSIG (signature)));
590
+ }
591
+
592
+ /*****************************
593
+ t_set_comm_inactivity_timeout
594
+ *****************************/
595
+
596
+ static VALUE t_set_comm_inactivity_timeout (VALUE self UNUSED, VALUE signature, VALUE timeout)
597
+ {
598
+ float ti = RFLOAT_VALUE(timeout);
599
+ if (evma_set_comm_inactivity_timeout(NUM2BSIG(signature), ti)) {
600
+ return Qtrue;
601
+ }
602
+ return Qfalse;
603
+ }
604
+
605
+ /*****************************
606
+ t_get_pending_connect_timeout
607
+ *****************************/
608
+
609
+ static VALUE t_get_pending_connect_timeout (VALUE self UNUSED, VALUE signature)
610
+ {
611
+ return rb_float_new(evma_get_pending_connect_timeout(NUM2BSIG (signature)));
612
+ }
613
+
614
+ /*****************************
615
+ t_set_pending_connect_timeout
616
+ *****************************/
617
+
618
+ static VALUE t_set_pending_connect_timeout (VALUE self UNUSED, VALUE signature, VALUE timeout)
619
+ {
620
+ float ti = RFLOAT_VALUE(timeout);
621
+ if (evma_set_pending_connect_timeout(NUM2BSIG(signature), ti)) {
622
+ return Qtrue;
623
+ }
624
+ return Qfalse;
625
+ }
626
+
627
+ /***************
628
+ t_send_datagram
629
+ ***************/
630
+
631
+ static VALUE t_send_datagram (VALUE self UNUSED, VALUE signature, VALUE data, VALUE data_length, VALUE address, VALUE port)
632
+ {
633
+ int b = evma_send_datagram (NUM2BSIG (signature), StringValuePtr (data), FIX2INT (data_length), StringValueCStr(address), FIX2INT(port));
634
+ if (b < 0)
635
+ rb_raise (EM_eConnectionError, "%s", "error in sending datagram"); // FIXME: this could be more specific.
636
+ return INT2NUM (b);
637
+ }
638
+
639
+
640
+ /******************
641
+ t_close_connection
642
+ ******************/
643
+
644
+ static VALUE t_close_connection (VALUE self UNUSED, VALUE signature, VALUE after_writing)
645
+ {
646
+ evma_close_connection (NUM2BSIG (signature), ((after_writing == Qtrue) ? 1 : 0));
647
+ return Qnil;
648
+ }
649
+
650
+ /********************************
651
+ t_report_connection_error_status
652
+ ********************************/
653
+
654
+ static VALUE t_report_connection_error_status (VALUE self UNUSED, VALUE signature)
655
+ {
656
+ int b = evma_report_connection_error_status (NUM2BSIG (signature));
657
+ return INT2NUM (b);
658
+ }
659
+
660
+
661
+
662
+ /****************
663
+ t_connect_server
664
+ ****************/
665
+
666
+ static VALUE t_connect_server (VALUE self UNUSED, VALUE server, VALUE port)
667
+ {
668
+ // Avoid FIX2INT in this case, because it doesn't deal with type errors properly.
669
+ // Specifically, if the value of port comes in as a string rather than an integer,
670
+ // NUM2INT will throw a type error, but FIX2INT will generate garbage.
671
+
672
+ try {
673
+ const uintptr_t f = evma_connect_to_server (NULL, 0, StringValueCStr(server), NUM2INT(port));
674
+ if (!f)
675
+ rb_raise (EM_eConnectionError, "%s", "no connection");
676
+ return BSIG2NUM (f);
677
+ } catch (std::runtime_error e) {
678
+ rb_raise (EM_eConnectionError, "%s", e.what());
679
+ }
680
+ return Qnil;
681
+ }
682
+
683
+ /*********************
684
+ t_bind_connect_server
685
+ *********************/
686
+
687
+ static VALUE t_bind_connect_server (VALUE self UNUSED, VALUE bind_addr, VALUE bind_port, VALUE server, VALUE port)
688
+ {
689
+ // Avoid FIX2INT in this case, because it doesn't deal with type errors properly.
690
+ // Specifically, if the value of port comes in as a string rather than an integer,
691
+ // NUM2INT will throw a type error, but FIX2INT will generate garbage.
692
+
693
+ try {
694
+ const uintptr_t f = evma_connect_to_server (StringValueCStr(bind_addr), NUM2INT(bind_port), StringValueCStr(server), NUM2INT(port));
695
+ if (!f)
696
+ rb_raise (EM_eConnectionError, "%s", "no connection");
697
+ return BSIG2NUM (f);
698
+ } catch (std::runtime_error e) {
699
+ rb_raise (EM_eConnectionError, "%s", e.what());
700
+ }
701
+ return Qnil;
702
+ }
703
+
704
+ /*********************
705
+ t_connect_unix_server
706
+ *********************/
707
+
708
+ static VALUE t_connect_unix_server (VALUE self UNUSED, VALUE serversocket)
709
+ {
710
+ const uintptr_t f = evma_connect_to_unix_server (StringValueCStr(serversocket));
711
+ if (!f)
712
+ rb_raise (rb_eRuntimeError, "%s", "no connection");
713
+ return BSIG2NUM (f);
714
+ }
715
+
716
+ /***********
717
+ t_attach_fd
718
+ ***********/
719
+
720
+ static VALUE t_attach_fd (VALUE self UNUSED, VALUE file_descriptor, VALUE watch_mode)
721
+ {
722
+ const uintptr_t f = evma_attach_fd (NUM2INT(file_descriptor), watch_mode == Qtrue);
723
+ if (!f)
724
+ rb_raise (rb_eRuntimeError, "%s", "no connection");
725
+ return BSIG2NUM (f);
726
+ }
727
+
728
+ /***********
729
+ t_detach_fd
730
+ ***********/
731
+
732
+ static VALUE t_detach_fd (VALUE self UNUSED, VALUE signature)
733
+ {
734
+ return INT2NUM(evma_detach_fd (NUM2BSIG (signature)));
735
+ }
736
+
737
+ /*********************
738
+ t_get_file_descriptor
739
+ *********************/
740
+ static VALUE t_get_file_descriptor (VALUE self UNUSED, VALUE signature)
741
+ {
742
+ return INT2NUM(evma_get_file_descriptor (NUM2BSIG (signature)));
743
+ }
744
+
745
+ /**************
746
+ t_get_sock_opt
747
+ **************/
748
+
749
+ static VALUE t_get_sock_opt (VALUE self UNUSED, VALUE signature, VALUE lev, VALUE optname)
750
+ {
751
+ int fd = evma_get_file_descriptor (NUM2BSIG (signature));
752
+ int level = NUM2INT(lev), option = NUM2INT(optname);
753
+ socklen_t len = 128;
754
+ char buf[128];
755
+
756
+ if (getsockopt(fd, level, option, buf, &len) < 0)
757
+ rb_sys_fail("getsockopt");
758
+
759
+ return rb_str_new(buf, len);
760
+ }
761
+
762
+ /**************
763
+ t_set_sock_opt
764
+ **************/
765
+
766
+ static VALUE t_set_sock_opt (VALUE self UNUSED, VALUE signature, VALUE lev, VALUE optname, VALUE optval)
767
+ {
768
+ int fd = evma_get_file_descriptor (NUM2BSIG (signature));
769
+ int level = NUM2INT(lev), option = NUM2INT(optname);
770
+ int i;
771
+ const void *v;
772
+ socklen_t len;
773
+
774
+ switch (TYPE(optval)) {
775
+ case T_FIXNUM:
776
+ i = FIX2INT(optval);
777
+ goto numval;
778
+ case T_FALSE:
779
+ i = 0;
780
+ goto numval;
781
+ case T_TRUE:
782
+ i = 1;
783
+ numval:
784
+ v = (void*)&i; len = sizeof(i);
785
+ break;
786
+ default:
787
+ StringValue(optval);
788
+ v = RSTRING_PTR(optval);
789
+ len = RSTRING_LENINT(optval);
790
+ break;
791
+ }
792
+
793
+
794
+ if (setsockopt(fd, level, option, (char *)v, len) < 0)
795
+ rb_sys_fail("setsockopt");
796
+
797
+ return INT2FIX(0);
798
+ }
799
+
800
+ /********************
801
+ t_is_notify_readable
802
+ ********************/
803
+
804
+ static VALUE t_is_notify_readable (VALUE self UNUSED, VALUE signature)
805
+ {
806
+ return evma_is_notify_readable(NUM2BSIG (signature)) ? Qtrue : Qfalse;
807
+ }
808
+
809
+ /*********************
810
+ t_set_notify_readable
811
+ *********************/
812
+
813
+ static VALUE t_set_notify_readable (VALUE self UNUSED, VALUE signature, VALUE mode)
814
+ {
815
+ evma_set_notify_readable(NUM2BSIG(signature), mode == Qtrue);
816
+ return Qnil;
817
+ }
818
+
819
+ /********************
820
+ t_is_notify_readable
821
+ ********************/
822
+
823
+ static VALUE t_is_notify_writable (VALUE self UNUSED, VALUE signature)
824
+ {
825
+ return evma_is_notify_writable(NUM2BSIG (signature)) ? Qtrue : Qfalse;
826
+ }
827
+
828
+ /*********************
829
+ t_set_notify_writable
830
+ *********************/
831
+
832
+ static VALUE t_set_notify_writable (VALUE self UNUSED, VALUE signature, VALUE mode)
833
+ {
834
+ evma_set_notify_writable(NUM2BSIG (signature), mode == Qtrue);
835
+ return Qnil;
836
+ }
837
+
838
+ /*******
839
+ t_pause
840
+ *******/
841
+
842
+ static VALUE t_pause (VALUE self UNUSED, VALUE signature)
843
+ {
844
+ return evma_pause(NUM2BSIG (signature)) ? Qtrue : Qfalse;
845
+ }
846
+
847
+ /********
848
+ t_resume
849
+ ********/
850
+
851
+ static VALUE t_resume (VALUE self UNUSED, VALUE signature)
852
+ {
853
+ return evma_resume(NUM2BSIG (signature)) ? Qtrue : Qfalse;
854
+ }
855
+
856
+ /**********
857
+ t_paused_p
858
+ **********/
859
+
860
+ static VALUE t_paused_p (VALUE self UNUSED, VALUE signature)
861
+ {
862
+ return evma_is_paused(NUM2BSIG (signature)) ? Qtrue : Qfalse;
863
+ }
864
+
865
+ /*********************
866
+ t_num_close_scheduled
867
+ *********************/
868
+
869
+ static VALUE t_num_close_scheduled (VALUE self UNUSED)
870
+ {
871
+ return INT2FIX(evma_num_close_scheduled());
872
+ }
873
+
874
+ /*****************
875
+ t_open_udp_socket
876
+ *****************/
877
+
878
+ static VALUE t_open_udp_socket (VALUE self UNUSED, VALUE server, VALUE port)
879
+ {
880
+ const uintptr_t f = evma_open_datagram_socket (StringValueCStr(server), FIX2INT(port));
881
+ if (!f)
882
+ rb_raise (rb_eRuntimeError, "%s", "no datagram socket");
883
+ return BSIG2NUM(f);
884
+ }
885
+
886
+
887
+
888
+ /*****************
889
+ t_release_machine
890
+ *****************/
891
+
892
+ static VALUE t_release_machine (VALUE self UNUSED)
893
+ {
894
+ evma_release_library();
895
+ return Qnil;
896
+ }
897
+
898
+
899
+ /******
900
+ t_stop
901
+ ******/
902
+
903
+ static VALUE t_stop (VALUE self UNUSED)
904
+ {
905
+ evma_stop_machine();
906
+ return Qnil;
907
+ }
908
+
909
+ /******************
910
+ t_signal_loopbreak
911
+ ******************/
912
+
913
+ static VALUE t_signal_loopbreak (VALUE self UNUSED)
914
+ {
915
+ evma_signal_loopbreak();
916
+ return Qnil;
917
+ }
918
+
919
+ /**************
920
+ t_library_type
921
+ **************/
922
+
923
+ static VALUE t_library_type (VALUE self UNUSED)
924
+ {
925
+ return rb_eval_string (":extension");
926
+ }
927
+
928
+
929
+
930
+ /*******************
931
+ t_set_timer_quantum
932
+ *******************/
933
+
934
+ static VALUE t_set_timer_quantum (VALUE self UNUSED, VALUE interval)
935
+ {
936
+ evma_set_timer_quantum (FIX2INT (interval));
937
+ return Qnil;
938
+ }
939
+
940
+ /********************
941
+ t_get_max_timer_count
942
+ ********************/
943
+
944
+ static VALUE t_get_max_timer_count (VALUE self UNUSED)
945
+ {
946
+ return INT2FIX (evma_get_max_timer_count());
947
+ }
948
+
949
+ /********************
950
+ t_set_max_timer_count
951
+ ********************/
952
+
953
+ static VALUE t_set_max_timer_count (VALUE self UNUSED, VALUE ct)
954
+ {
955
+ evma_set_max_timer_count (FIX2INT (ct));
956
+ return Qnil;
957
+ }
958
+
959
+ /********************
960
+ t_get/set_simultaneous_accept_count
961
+ ********************/
962
+
963
+ static VALUE t_get_simultaneous_accept_count (VALUE self UNUSED)
964
+ {
965
+ return INT2FIX (evma_get_simultaneous_accept_count());
966
+ }
967
+
968
+ static VALUE t_set_simultaneous_accept_count (VALUE self UNUSED, VALUE ct)
969
+ {
970
+ evma_set_simultaneous_accept_count (FIX2INT (ct));
971
+ return Qnil;
972
+ }
973
+
974
+ /***************
975
+ t_setuid_string
976
+ ***************/
977
+
978
+ static VALUE t_setuid_string (VALUE self UNUSED, VALUE username)
979
+ {
980
+ evma_setuid_string (StringValueCStr (username));
981
+ return Qnil;
982
+ }
983
+
984
+
985
+
986
+ /**************
987
+ t_invoke_popen
988
+ **************/
989
+
990
+ static VALUE t_invoke_popen (VALUE self UNUSED, VALUE cmd)
991
+ {
992
+ #ifdef OS_WIN32
993
+ rb_raise (EM_eUnsupported, "popen is not available on this platform");
994
+ #endif
995
+
996
+ int len = RARRAY_LEN(cmd);
997
+ if (len >= 2048)
998
+ rb_raise (rb_eRuntimeError, "%s", "too many arguments to popen");
999
+ char *strings [2048];
1000
+ for (int i=0; i < len; i++) {
1001
+ VALUE ix = INT2FIX (i);
1002
+ VALUE s = rb_ary_aref (1, &ix, cmd);
1003
+ strings[i] = StringValueCStr (s);
1004
+ }
1005
+ strings[len] = NULL;
1006
+
1007
+ uintptr_t f = 0;
1008
+ try {
1009
+ f = evma_popen (strings);
1010
+ } catch (std::runtime_error e) {
1011
+ rb_raise (rb_eRuntimeError, "%s", e.what());
1012
+ }
1013
+ if (!f) {
1014
+ char *err = strerror (errno);
1015
+ char buf[100];
1016
+ memset (buf, 0, sizeof(buf));
1017
+ snprintf (buf, sizeof(buf)-1, "no popen: %s", (err?err:"???"));
1018
+ rb_raise (rb_eRuntimeError, "%s", buf);
1019
+ }
1020
+ return BSIG2NUM (f);
1021
+ }
1022
+
1023
+
1024
+ /***************
1025
+ t_read_keyboard
1026
+ ***************/
1027
+
1028
+ static VALUE t_read_keyboard (VALUE self UNUSED)
1029
+ {
1030
+ const uintptr_t f = evma_open_keyboard();
1031
+ if (!f)
1032
+ rb_raise (rb_eRuntimeError, "%s", "no keyboard reader");
1033
+ return BSIG2NUM (f);
1034
+ }
1035
+
1036
+
1037
+ /****************
1038
+ t_watch_filename
1039
+ ****************/
1040
+
1041
+ static VALUE t_watch_filename (VALUE self UNUSED, VALUE fname)
1042
+ {
1043
+ try {
1044
+ return BSIG2NUM(evma_watch_filename(StringValueCStr(fname)));
1045
+ } catch (std::runtime_error e) {
1046
+ rb_raise (EM_eUnsupported, "%s", e.what());
1047
+ }
1048
+ return Qnil;
1049
+ }
1050
+
1051
+
1052
+ /******************
1053
+ t_unwatch_filename
1054
+ ******************/
1055
+
1056
+ static VALUE t_unwatch_filename (VALUE self UNUSED, VALUE sig)
1057
+ {
1058
+ try {
1059
+ evma_unwatch_filename(NUM2BSIG (sig));
1060
+ } catch (std::runtime_error e) {
1061
+ rb_raise (EM_eInvalidSignature, "%s", e.what());
1062
+ }
1063
+
1064
+ return Qnil;
1065
+ }
1066
+
1067
+
1068
+ /***********
1069
+ t_watch_pid
1070
+ ***********/
1071
+
1072
+ static VALUE t_watch_pid (VALUE self UNUSED, VALUE pid)
1073
+ {
1074
+ try {
1075
+ return BSIG2NUM(evma_watch_pid(NUM2INT(pid)));
1076
+ } catch (std::runtime_error e) {
1077
+ rb_raise (EM_eUnsupported, "%s", e.what());
1078
+ }
1079
+ return Qnil;
1080
+ }
1081
+
1082
+
1083
+ /*************
1084
+ t_unwatch_pid
1085
+ *************/
1086
+
1087
+ static VALUE t_unwatch_pid (VALUE self UNUSED, VALUE sig)
1088
+ {
1089
+ evma_unwatch_pid(NUM2BSIG (sig));
1090
+ return Qnil;
1091
+ }
1092
+
1093
+
1094
+ /*************
1095
+ t_watch_only_p
1096
+ *************/
1097
+
1098
+ static VALUE t_watch_only_p (VALUE self UNUSED, VALUE signature)
1099
+ {
1100
+ return evma_is_watch_only(NUM2BSIG (signature)) ? Qtrue : Qfalse;
1101
+ }
1102
+
1103
+
1104
+ /**********
1105
+ t__epoll_p
1106
+ **********/
1107
+
1108
+ static VALUE t__epoll_p (VALUE self UNUSED)
1109
+ {
1110
+ #ifdef HAVE_EPOLL
1111
+ return Qtrue;
1112
+ #else
1113
+ return Qfalse;
1114
+ #endif
1115
+ }
1116
+
1117
+ /********
1118
+ t__epoll
1119
+ ********/
1120
+
1121
+ static VALUE t__epoll (VALUE self UNUSED)
1122
+ {
1123
+ if (t__epoll_p(self) == Qfalse)
1124
+ return Qfalse;
1125
+
1126
+ evma_set_epoll (1);
1127
+ return Qtrue;
1128
+ }
1129
+
1130
+ /***********
1131
+ t__epoll_set
1132
+ ***********/
1133
+
1134
+ static VALUE t__epoll_set (VALUE self, VALUE val)
1135
+ {
1136
+ if (t__epoll_p(self) == Qfalse && val == Qtrue)
1137
+ rb_raise (EM_eUnsupported, "%s", "epoll is not supported on this platform");
1138
+
1139
+ evma_set_epoll (val == Qtrue ? 1 : 0);
1140
+ return val;
1141
+ }
1142
+
1143
+
1144
+ /***********
1145
+ t__kqueue_p
1146
+ ***********/
1147
+
1148
+ static VALUE t__kqueue_p (VALUE self UNUSED)
1149
+ {
1150
+ #ifdef HAVE_KQUEUE
1151
+ return Qtrue;
1152
+ #else
1153
+ return Qfalse;
1154
+ #endif
1155
+ }
1156
+
1157
+ /*********
1158
+ t__kqueue
1159
+ *********/
1160
+
1161
+ static VALUE t__kqueue (VALUE self UNUSED)
1162
+ {
1163
+ if (t__kqueue_p(self) == Qfalse)
1164
+ return Qfalse;
1165
+
1166
+ evma_set_kqueue (1);
1167
+ return Qtrue;
1168
+ }
1169
+
1170
+ /*************
1171
+ t__kqueue_set
1172
+ *************/
1173
+
1174
+ static VALUE t__kqueue_set (VALUE self, VALUE val)
1175
+ {
1176
+ if (t__kqueue_p(self) == Qfalse && val == Qtrue)
1177
+ rb_raise (EM_eUnsupported, "%s", "kqueue is not supported on this platform");
1178
+
1179
+ evma_set_kqueue (val == Qtrue ? 1 : 0);
1180
+ return val;
1181
+ }
1182
+
1183
+
1184
+ /********
1185
+ t__ssl_p
1186
+ ********/
1187
+
1188
+ static VALUE t__ssl_p (VALUE self UNUSED)
1189
+ {
1190
+ #ifdef WITH_SSL
1191
+ return Qtrue;
1192
+ #else
1193
+ return Qfalse;
1194
+ #endif
1195
+ }
1196
+
1197
+ /********
1198
+ t_stopping
1199
+ ********/
1200
+
1201
+ static VALUE t_stopping ()
1202
+ {
1203
+ if (evma_stopping()) {
1204
+ return Qtrue;
1205
+ } else {
1206
+ return Qfalse;
1207
+ }
1208
+ }
1209
+
1210
+
1211
+ /****************
1212
+ t_send_file_data
1213
+ ****************/
1214
+
1215
+ static VALUE t_send_file_data (VALUE self UNUSED, VALUE signature, VALUE filename)
1216
+ {
1217
+
1218
+ /* The current implementation of evma_send_file_data_to_connection enforces a strict
1219
+ * upper limit on the file size it will transmit (currently 32K). The function returns
1220
+ * zero on success, -1 if the requested file exceeds its size limit, and a positive
1221
+ * number for other errors.
1222
+ * TODO: Positive return values are actually errno's, which is probably the wrong way to
1223
+ * do this. For one thing it's ugly. For another, we can't be sure zero is never a real errno.
1224
+ */
1225
+
1226
+ int b = evma_send_file_data_to_connection (NUM2BSIG (signature), StringValueCStr(filename));
1227
+ if (b == -1)
1228
+ rb_raise(rb_eRuntimeError, "%s", "File too large. send_file_data() supports files under 32k.");
1229
+ if (b > 0) {
1230
+ char *err = strerror (b);
1231
+ char buf[1024];
1232
+ memset (buf, 0, sizeof(buf));
1233
+ snprintf (buf, sizeof(buf)-1, ": %s %s", StringValueCStr(filename),(err?err:"???"));
1234
+
1235
+ rb_raise (rb_eIOError, "%s", buf);
1236
+ }
1237
+
1238
+ return INT2NUM (0);
1239
+ }
1240
+
1241
+
1242
+ /*******************
1243
+ t_set_rlimit_nofile
1244
+ *******************/
1245
+
1246
+ static VALUE t_set_rlimit_nofile (VALUE self UNUSED, VALUE arg)
1247
+ {
1248
+ int arg_int = (NIL_P(arg)) ? -1 : NUM2INT (arg);
1249
+ return INT2NUM (evma_set_rlimit_nofile (arg_int));
1250
+ }
1251
+
1252
+ /***************************
1253
+ conn_get_outbound_data_size
1254
+ ***************************/
1255
+
1256
+ static VALUE conn_get_outbound_data_size (VALUE self)
1257
+ {
1258
+ VALUE sig = rb_ivar_get (self, Intern_at_signature);
1259
+ return INT2NUM (evma_get_outbound_data_size (NUM2BSIG (sig)));
1260
+ }
1261
+
1262
+
1263
+ /******************************
1264
+ conn_associate_callback_target
1265
+ ******************************/
1266
+
1267
+ static VALUE conn_associate_callback_target (VALUE self UNUSED, VALUE sig UNUSED)
1268
+ {
1269
+ // No-op for the time being.
1270
+ return Qnil;
1271
+ }
1272
+
1273
+
1274
+ /******************
1275
+ t_enable_keepalive
1276
+ ******************/
1277
+
1278
+ static VALUE t_enable_keepalive (int argc, VALUE *argv, VALUE self)
1279
+ {
1280
+ VALUE idle, intvl, cnt;
1281
+ rb_scan_args(argc, argv, "03", &idle, &intvl, &cnt);
1282
+
1283
+ // In ed.cpp, skip 0 values before calling setsockopt
1284
+ int i_idle = NIL_P(idle) ? 0 : NUM2INT(idle);
1285
+ int i_intvl = NIL_P(intvl) ? 0 : NUM2INT(intvl);
1286
+ int i_cnt = NIL_P(cnt) ? 0 : NUM2INT(cnt);
1287
+
1288
+ VALUE sig = rb_ivar_get (self, Intern_at_signature);
1289
+ try {
1290
+ return INT2NUM (evma_enable_keepalive(NUM2ULONG(sig), i_idle, i_intvl, i_cnt));
1291
+ } catch (std::runtime_error e) {
1292
+ rb_raise (rb_eRuntimeError, "%s", e.what());
1293
+ }
1294
+ }
1295
+
1296
+ /******************
1297
+ t_disable_keepalive
1298
+ ******************/
1299
+
1300
+ static VALUE t_disable_keepalive (VALUE self)
1301
+ {
1302
+ VALUE sig = rb_ivar_get (self, Intern_at_signature);
1303
+ try {
1304
+ return INT2NUM (evma_disable_keepalive(NUM2ULONG(sig)));
1305
+ } catch (std::runtime_error e) {
1306
+ rb_raise (rb_eRuntimeError, "%s", e.what());
1307
+ }
1308
+ }
1309
+
1310
+ /***************
1311
+ t_get_loop_time
1312
+ ****************/
1313
+
1314
+ static VALUE t_get_loop_time (VALUE self UNUSED)
1315
+ {
1316
+ uint64_t current_time = evma_get_current_loop_time();
1317
+ if (current_time == 0) {
1318
+ return Qnil;
1319
+ }
1320
+
1321
+ // Generally the industry has moved to 64-bit time_t, this is just in case we're 32-bit time_t.
1322
+ if (sizeof(time_t) < 8 && current_time > INT_MAX) {
1323
+ return rb_funcall(rb_cTime, Intern_at, 2, INT2NUM(current_time / 1000000), INT2NUM(current_time % 1000000));
1324
+ } else {
1325
+ return rb_time_new(current_time / 1000000, current_time % 1000000);
1326
+ }
1327
+ }
1328
+
1329
+
1330
+ /*************
1331
+ t_start_proxy
1332
+ **************/
1333
+
1334
+ static VALUE t_start_proxy (VALUE self UNUSED, VALUE from, VALUE to, VALUE bufsize, VALUE length)
1335
+ {
1336
+ try {
1337
+ evma_start_proxy(NUM2BSIG (from), NUM2BSIG (to), NUM2ULONG(bufsize), NUM2ULONG(length));
1338
+ } catch (std::runtime_error e) {
1339
+ rb_raise (EM_eConnectionError, "%s", e.what());
1340
+ }
1341
+ return Qnil;
1342
+ }
1343
+
1344
+
1345
+ /************
1346
+ t_stop_proxy
1347
+ *************/
1348
+
1349
+ static VALUE t_stop_proxy (VALUE self UNUSED, VALUE from)
1350
+ {
1351
+ try{
1352
+ evma_stop_proxy(NUM2BSIG (from));
1353
+ } catch (std::runtime_error e) {
1354
+ rb_raise (EM_eConnectionError, "%s", e.what());
1355
+ }
1356
+ return Qnil;
1357
+ }
1358
+
1359
+ /***************
1360
+ t_proxied_bytes
1361
+ ****************/
1362
+
1363
+ static VALUE t_proxied_bytes (VALUE self UNUSED, VALUE from)
1364
+ {
1365
+ try{
1366
+ return BSIG2NUM(evma_proxied_bytes(NUM2BSIG (from)));
1367
+ } catch (std::runtime_error e) {
1368
+ rb_raise (EM_eConnectionError, "%s", e.what());
1369
+ }
1370
+ return Qnil;
1371
+ }
1372
+
1373
+ /***************
1374
+ t_get_idle_time
1375
+ ****************/
1376
+
1377
+ static VALUE t_get_idle_time (VALUE self UNUSED, VALUE from)
1378
+ {
1379
+ try{
1380
+ uint64_t current_time = evma_get_current_loop_time();
1381
+ uint64_t time = evma_get_last_activity_time(NUM2BSIG (from));
1382
+ if (current_time != 0 && time != 0) {
1383
+ if (time >= current_time)
1384
+ return BSIG2NUM(0);
1385
+ else {
1386
+ uint64_t diff = current_time - time;
1387
+ float seconds = diff / (1000.0*1000.0);
1388
+ return rb_float_new(seconds);
1389
+ }
1390
+ return Qnil;
1391
+ }
1392
+ } catch (std::runtime_error e) {
1393
+ rb_raise (EM_eConnectionError, "%s", e.what());
1394
+ }
1395
+ return Qnil;
1396
+ }
1397
+
1398
+ /************************
1399
+ t_get_heartbeat_interval
1400
+ *************************/
1401
+
1402
+ static VALUE t_get_heartbeat_interval (VALUE self UNUSED)
1403
+ {
1404
+ return rb_float_new(evma_get_heartbeat_interval());
1405
+ }
1406
+
1407
+
1408
+ /************************
1409
+ t_set_heartbeat_interval
1410
+ *************************/
1411
+
1412
+ static VALUE t_set_heartbeat_interval (VALUE self UNUSED, VALUE interval)
1413
+ {
1414
+ float iv = RFLOAT_VALUE(interval);
1415
+ if (evma_set_heartbeat_interval(iv))
1416
+ return Qtrue;
1417
+ return Qfalse;
1418
+ }
1419
+
1420
+
1421
+ /*********************
1422
+ Init_rubyeventmachine
1423
+ *********************/
1424
+
1425
+ extern "C" void Init_rubyeventmachine()
1426
+ {
1427
+ // Lookup Process::Status for get_subprocess_status
1428
+ VALUE rb_mProcess = rb_const_get(rb_cObject, rb_intern("Process"));
1429
+ rb_cProcStatus = rb_const_get(rb_mProcess, rb_intern("Status"));
1430
+
1431
+ // Tuck away some symbol values so we don't have to look 'em up every time we need 'em.
1432
+ Intern_at_signature = rb_intern ("@signature");
1433
+ Intern_at_timers = rb_intern ("@timers");
1434
+ Intern_at_conns = rb_intern ("@conns");
1435
+ Intern_at_error_handler = rb_intern("@error_handler");
1436
+
1437
+ Intern_event_callback = rb_intern ("event_callback");
1438
+ Intern_run_deferred_callbacks = rb_intern ("run_deferred_callbacks");
1439
+ Intern_delete = rb_intern ("delete");
1440
+ Intern_call = rb_intern ("call");
1441
+ Intern_at = rb_intern("at");
1442
+ Intern_receive_data = rb_intern ("receive_data");
1443
+ Intern_ssl_handshake_completed = rb_intern ("ssl_handshake_completed");
1444
+ Intern_ssl_verify_peer = rb_intern ("ssl_verify_peer");
1445
+ Intern_notify_readable = rb_intern ("notify_readable");
1446
+ Intern_notify_writable = rb_intern ("notify_writable");
1447
+ Intern_proxy_target_unbound = rb_intern ("proxy_target_unbound");
1448
+ Intern_proxy_completed = rb_intern ("proxy_completed");
1449
+ Intern_connection_completed = rb_intern ("connection_completed");
1450
+
1451
+ // INCOMPLETE, we need to define class Connections inside module EventMachine
1452
+ // run_machine and run_machine_without_threads are now identical.
1453
+ // Must deprecate the without_threads variant.
1454
+ EmModule = rb_define_module ("EventMachine");
1455
+ EmConnection = rb_define_class_under (EmModule, "Connection", rb_cObject);
1456
+
1457
+ rb_define_class_under (EmModule, "NoHandlerForAcceptedConnection", rb_eRuntimeError);
1458
+ EM_eConnectionError = rb_define_class_under (EmModule, "ConnectionError", rb_eRuntimeError);
1459
+ EM_eConnectionNotBound = rb_define_class_under (EmModule, "ConnectionNotBound", rb_eRuntimeError);
1460
+ EM_eUnknownTimerFired = rb_define_class_under (EmModule, "UnknownTimerFired", rb_eRuntimeError);
1461
+ EM_eUnsupported = rb_define_class_under (EmModule, "Unsupported", rb_eRuntimeError);
1462
+ EM_eInvalidSignature = rb_define_class_under (EmModule, "InvalidSignature", rb_eRuntimeError);
1463
+
1464
+ rb_define_module_function (EmModule, "initialize_event_machine", (VALUE(*)(...))t_initialize_event_machine, 0);
1465
+ rb_define_module_function (EmModule, "run_machine_once", (VALUE(*)(...))t_run_machine_once, 0);
1466
+ rb_define_module_function (EmModule, "run_machine", (VALUE(*)(...))t_run_machine, 0);
1467
+ rb_define_module_function (EmModule, "run_machine_without_threads", (VALUE(*)(...))t_run_machine, 0);
1468
+ rb_define_module_function (EmModule, "get_timer_count", (VALUE(*)(...))t_get_timer_count, 0);
1469
+ rb_define_module_function (EmModule, "add_oneshot_timer", (VALUE(*)(...))t_add_oneshot_timer, 1);
1470
+ rb_define_module_function (EmModule, "start_tcp_server", (VALUE(*)(...))t_start_server, 2);
1471
+ rb_define_module_function (EmModule, "stop_tcp_server", (VALUE(*)(...))t_stop_server, 1);
1472
+ rb_define_module_function (EmModule, "start_unix_server", (VALUE(*)(...))t_start_unix_server, 1);
1473
+ rb_define_module_function (EmModule, "attach_sd", (VALUE(*)(...))t_attach_sd, 1);
1474
+ rb_define_module_function (EmModule, "set_tls_parms", (VALUE(*)(...))t_set_tls_parms, 10);
1475
+ rb_define_module_function (EmModule, "start_tls", (VALUE(*)(...))t_start_tls, 1);
1476
+ rb_define_module_function (EmModule, "get_peer_cert", (VALUE(*)(...))t_get_peer_cert, 1);
1477
+ rb_define_module_function (EmModule, "get_cipher_bits", (VALUE(*)(...))t_get_cipher_bits, 1);
1478
+ rb_define_module_function (EmModule, "get_cipher_name", (VALUE(*)(...))t_get_cipher_name, 1);
1479
+ rb_define_module_function (EmModule, "get_cipher_protocol", (VALUE(*)(...))t_get_cipher_protocol, 1);
1480
+ rb_define_module_function (EmModule, "get_sni_hostname", (VALUE(*)(...))t_get_sni_hostname, 1);
1481
+ rb_define_module_function (EmModule, "send_data", (VALUE(*)(...))t_send_data, 3);
1482
+ rb_define_module_function (EmModule, "send_datagram", (VALUE(*)(...))t_send_datagram, 5);
1483
+ rb_define_module_function (EmModule, "close_connection", (VALUE(*)(...))t_close_connection, 2);
1484
+ rb_define_module_function (EmModule, "report_connection_error_status", (VALUE(*)(...))t_report_connection_error_status, 1);
1485
+ rb_define_module_function (EmModule, "connect_server", (VALUE(*)(...))t_connect_server, 2);
1486
+ rb_define_module_function (EmModule, "bind_connect_server", (VALUE(*)(...))t_bind_connect_server, 4);
1487
+ rb_define_module_function (EmModule, "connect_unix_server", (VALUE(*)(...))t_connect_unix_server, 1);
1488
+
1489
+ rb_define_module_function (EmModule, "attach_fd", (VALUE (*)(...))t_attach_fd, 2);
1490
+ rb_define_module_function (EmModule, "detach_fd", (VALUE (*)(...))t_detach_fd, 1);
1491
+ rb_define_module_function (EmModule, "get_file_descriptor", (VALUE (*)(...))t_get_file_descriptor, 1);
1492
+ rb_define_module_function (EmModule, "get_sock_opt", (VALUE (*)(...))t_get_sock_opt, 3);
1493
+ rb_define_module_function (EmModule, "set_sock_opt", (VALUE (*)(...))t_set_sock_opt, 4);
1494
+ rb_define_module_function (EmModule, "set_notify_readable", (VALUE (*)(...))t_set_notify_readable, 2);
1495
+ rb_define_module_function (EmModule, "set_notify_writable", (VALUE (*)(...))t_set_notify_writable, 2);
1496
+ rb_define_module_function (EmModule, "is_notify_readable", (VALUE (*)(...))t_is_notify_readable, 1);
1497
+ rb_define_module_function (EmModule, "is_notify_writable", (VALUE (*)(...))t_is_notify_writable, 1);
1498
+
1499
+ rb_define_module_function (EmModule, "pause_connection", (VALUE (*)(...))t_pause, 1);
1500
+ rb_define_module_function (EmModule, "resume_connection", (VALUE (*)(...))t_resume, 1);
1501
+ rb_define_module_function (EmModule, "connection_paused?", (VALUE (*)(...))t_paused_p, 1);
1502
+ rb_define_module_function (EmModule, "num_close_scheduled", (VALUE (*)(...))t_num_close_scheduled, 0);
1503
+
1504
+ rb_define_module_function (EmModule, "start_proxy", (VALUE (*)(...))t_start_proxy, 4);
1505
+ rb_define_module_function (EmModule, "stop_proxy", (VALUE (*)(...))t_stop_proxy, 1);
1506
+ rb_define_module_function (EmModule, "get_proxied_bytes", (VALUE (*)(...))t_proxied_bytes, 1);
1507
+
1508
+ rb_define_module_function (EmModule, "watch_filename", (VALUE (*)(...))t_watch_filename, 1);
1509
+ rb_define_module_function (EmModule, "unwatch_filename", (VALUE (*)(...))t_unwatch_filename, 1);
1510
+
1511
+ rb_define_module_function (EmModule, "watch_pid", (VALUE (*)(...))t_watch_pid, 1);
1512
+ rb_define_module_function (EmModule, "unwatch_pid", (VALUE (*)(...))t_unwatch_pid, 1);
1513
+ rb_define_module_function (EmModule, "watch_only?", (VALUE (*)(...))t_watch_only_p, 1);
1514
+
1515
+ rb_define_module_function (EmModule, "current_time", (VALUE(*)(...))t_get_loop_time, 0);
1516
+
1517
+ rb_define_module_function (EmModule, "open_udp_socket", (VALUE(*)(...))t_open_udp_socket, 2);
1518
+ rb_define_module_function (EmModule, "read_keyboard", (VALUE(*)(...))t_read_keyboard, 0);
1519
+ rb_define_module_function (EmModule, "release_machine", (VALUE(*)(...))t_release_machine, 0);
1520
+ rb_define_module_function (EmModule, "stop", (VALUE(*)(...))t_stop, 0);
1521
+ rb_define_module_function (EmModule, "signal_loopbreak", (VALUE(*)(...))t_signal_loopbreak, 0);
1522
+ rb_define_module_function (EmModule, "library_type", (VALUE(*)(...))t_library_type, 0);
1523
+ rb_define_module_function (EmModule, "set_timer_quantum", (VALUE(*)(...))t_set_timer_quantum, 1);
1524
+ rb_define_module_function (EmModule, "get_max_timer_count", (VALUE(*)(...))t_get_max_timer_count, 0);
1525
+ rb_define_module_function (EmModule, "set_max_timer_count", (VALUE(*)(...))t_set_max_timer_count, 1);
1526
+ rb_define_module_function (EmModule, "get_simultaneous_accept_count", (VALUE(*)(...))t_get_simultaneous_accept_count, 0);
1527
+ rb_define_module_function (EmModule, "set_simultaneous_accept_count", (VALUE(*)(...))t_set_simultaneous_accept_count, 1);
1528
+ rb_define_module_function (EmModule, "setuid_string", (VALUE(*)(...))t_setuid_string, 1);
1529
+ rb_define_module_function (EmModule, "invoke_popen", (VALUE(*)(...))t_invoke_popen, 1);
1530
+ rb_define_module_function (EmModule, "send_file_data", (VALUE(*)(...))t_send_file_data, 2);
1531
+ rb_define_module_function (EmModule, "get_heartbeat_interval", (VALUE(*)(...))t_get_heartbeat_interval, 0);
1532
+ rb_define_module_function (EmModule, "set_heartbeat_interval", (VALUE(*)(...))t_set_heartbeat_interval, 1);
1533
+ rb_define_module_function (EmModule, "get_idle_time", (VALUE(*)(...))t_get_idle_time, 1);
1534
+
1535
+ rb_define_module_function (EmModule, "get_peername", (VALUE(*)(...))t_get_peername, 1);
1536
+ rb_define_module_function (EmModule, "get_sockname", (VALUE(*)(...))t_get_sockname, 1);
1537
+ rb_define_module_function (EmModule, "get_subprocess_pid", (VALUE(*)(...))t_get_subprocess_pid, 1);
1538
+ rb_define_module_function (EmModule, "get_subprocess_status", (VALUE(*)(...))t_get_subprocess_status, 1);
1539
+ rb_define_module_function (EmModule, "get_comm_inactivity_timeout", (VALUE(*)(...))t_get_comm_inactivity_timeout, 1);
1540
+ rb_define_module_function (EmModule, "set_comm_inactivity_timeout", (VALUE(*)(...))t_set_comm_inactivity_timeout, 2);
1541
+ rb_define_module_function (EmModule, "get_pending_connect_timeout", (VALUE(*)(...))t_get_pending_connect_timeout, 1);
1542
+ rb_define_module_function (EmModule, "set_pending_connect_timeout", (VALUE(*)(...))t_set_pending_connect_timeout, 2);
1543
+ rb_define_module_function (EmModule, "set_rlimit_nofile", (VALUE(*)(...))t_set_rlimit_nofile, 1);
1544
+ rb_define_module_function (EmModule, "get_connection_count", (VALUE(*)(...))t_get_connection_count, 0);
1545
+
1546
+ rb_define_module_function (EmModule, "epoll", (VALUE(*)(...))t__epoll, 0);
1547
+ rb_define_module_function (EmModule, "epoll=", (VALUE(*)(...))t__epoll_set, 1);
1548
+ rb_define_module_function (EmModule, "epoll?", (VALUE(*)(...))t__epoll_p, 0);
1549
+
1550
+ rb_define_module_function (EmModule, "kqueue", (VALUE(*)(...))t__kqueue, 0);
1551
+ rb_define_module_function (EmModule, "kqueue=", (VALUE(*)(...))t__kqueue_set, 1);
1552
+ rb_define_module_function (EmModule, "kqueue?", (VALUE(*)(...))t__kqueue_p, 0);
1553
+
1554
+ rb_define_module_function (EmModule, "ssl?", (VALUE(*)(...))t__ssl_p, 0);
1555
+ rb_define_module_function(EmModule, "stopping?",(VALUE(*)(...))t_stopping, 0);
1556
+
1557
+ rb_define_method (EmConnection, "get_outbound_data_size", (VALUE(*)(...))conn_get_outbound_data_size, 0);
1558
+ rb_define_method (EmConnection, "associate_callback_target", (VALUE(*)(...))conn_associate_callback_target, 1);
1559
+ rb_define_method (EmConnection, "enable_keepalive", (VALUE(*)(...))t_enable_keepalive, -1);
1560
+ rb_define_method (EmConnection, "disable_keepalive", (VALUE(*)(...))t_disable_keepalive, 0);
1561
+
1562
+ // Connection states
1563
+ rb_define_const (EmModule, "TimerFired", INT2NUM(EM_TIMER_FIRED ));
1564
+ rb_define_const (EmModule, "ConnectionData", INT2NUM(EM_CONNECTION_READ ));
1565
+ rb_define_const (EmModule, "ConnectionUnbound", INT2NUM(EM_CONNECTION_UNBOUND ));
1566
+ rb_define_const (EmModule, "ConnectionAccepted", INT2NUM(EM_CONNECTION_ACCEPTED ));
1567
+ rb_define_const (EmModule, "ConnectionCompleted", INT2NUM(EM_CONNECTION_COMPLETED ));
1568
+ rb_define_const (EmModule, "LoopbreakSignalled", INT2NUM(EM_LOOPBREAK_SIGNAL ));
1569
+ rb_define_const (EmModule, "ConnectionNotifyReadable", INT2NUM(EM_CONNECTION_NOTIFY_READABLE));
1570
+ rb_define_const (EmModule, "ConnectionNotifyWritable", INT2NUM(EM_CONNECTION_NOTIFY_WRITABLE));
1571
+ rb_define_const (EmModule, "SslHandshakeCompleted", INT2NUM(EM_SSL_HANDSHAKE_COMPLETED ));
1572
+ rb_define_const (EmModule, "SslVerify", INT2NUM(EM_SSL_VERIFY ));
1573
+ // EM_PROXY_TARGET_UNBOUND = 110,
1574
+ // EM_PROXY_COMPLETED = 111
1575
+
1576
+ // SSL Protocols
1577
+ rb_define_const (EmModule, "EM_PROTO_SSLv2", INT2NUM(EM_PROTO_SSLv2 ));
1578
+ rb_define_const (EmModule, "EM_PROTO_SSLv3", INT2NUM(EM_PROTO_SSLv3 ));
1579
+ rb_define_const (EmModule, "EM_PROTO_TLSv1", INT2NUM(EM_PROTO_TLSv1 ));
1580
+ rb_define_const (EmModule, "EM_PROTO_TLSv1_1", INT2NUM(EM_PROTO_TLSv1_1));
1581
+ rb_define_const (EmModule, "EM_PROTO_TLSv1_2", INT2NUM(EM_PROTO_TLSv1_2));
1582
+ #ifdef TLS1_3_VERSION
1583
+ rb_define_const (EmModule, "EM_PROTO_TLSv1_3", INT2NUM(EM_PROTO_TLSv1_3));
1584
+ #endif
1585
+
1586
+ #ifdef OPENSSL_NO_SSL3
1587
+ /* True if SSL3 is not available */
1588
+ rb_define_const (EmModule, "OPENSSL_NO_SSL3", Qtrue);
1589
+ rb_define_const (EmModule, "OPENSSL_NO_SSL2", Qtrue);
1590
+ #else
1591
+ rb_define_const (EmModule, "OPENSSL_NO_SSL3", Qfalse);
1592
+ #ifdef OPENSSL_NO_SSL2
1593
+ rb_define_const (EmModule, "OPENSSL_NO_SSL2", Qtrue);
1594
+ #else
1595
+ rb_define_const (EmModule, "OPENSSL_NO_SSL2", Qfalse);
1596
+ #endif
1597
+ #endif
1598
+
1599
+ // OpenSSL Build / Runtime/Load versions
1600
+
1601
+ /* Version of OpenSSL that EventMachine was compiled with */
1602
+ rb_define_const(EmModule, "OPENSSL_VERSION", rb_str_new2(OPENSSL_VERSION_TEXT));
1603
+
1604
+ #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000
1605
+ /* Version of OpenSSL that EventMachine loaded with */
1606
+ rb_define_const(EmModule, "OPENSSL_LIBRARY_VERSION", rb_str_new2(OpenSSL_version(OPENSSL_VERSION)));
1607
+ #else
1608
+ rb_define_const(EmModule, "OPENSSL_LIBRARY_VERSION", rb_str_new2(SSLeay_version(SSLEAY_VERSION)));
1609
+ #endif
1610
+ }