sonixlabs-eventmachine-java 1.0.0.rc.4-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/.gitignore +22 -0
  2. data/.yardopts +7 -0
  3. data/GNU +281 -0
  4. data/Gemfile +3 -0
  5. data/LICENSE +60 -0
  6. data/README.md +109 -0
  7. data/Rakefile +20 -0
  8. data/docs/DocumentationGuidesIndex.md +27 -0
  9. data/docs/GettingStarted.md +521 -0
  10. data/docs/old/ChangeLog +211 -0
  11. data/docs/old/DEFERRABLES +246 -0
  12. data/docs/old/EPOLL +141 -0
  13. data/docs/old/INSTALL +13 -0
  14. data/docs/old/KEYBOARD +42 -0
  15. data/docs/old/LEGAL +25 -0
  16. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  17. data/docs/old/PURE_RUBY +75 -0
  18. data/docs/old/RELEASE_NOTES +94 -0
  19. data/docs/old/SMTP +4 -0
  20. data/docs/old/SPAWNED_PROCESSES +148 -0
  21. data/docs/old/TODO +8 -0
  22. data/eventmachine.gemspec +34 -0
  23. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  24. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  25. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  26. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  27. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  28. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  29. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  30. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  31. data/examples/old/ex_channel.rb +43 -0
  32. data/examples/old/ex_queue.rb +2 -0
  33. data/examples/old/ex_tick_loop_array.rb +15 -0
  34. data/examples/old/ex_tick_loop_counter.rb +32 -0
  35. data/examples/old/helper.rb +2 -0
  36. data/ext/binder.cpp +124 -0
  37. data/ext/binder.h +46 -0
  38. data/ext/cmain.cpp +876 -0
  39. data/ext/ed.cpp +1973 -0
  40. data/ext/ed.h +422 -0
  41. data/ext/em.cpp +2353 -0
  42. data/ext/em.h +239 -0
  43. data/ext/eventmachine.h +127 -0
  44. data/ext/extconf.rb +176 -0
  45. data/ext/fastfilereader/extconf.rb +103 -0
  46. data/ext/fastfilereader/mapper.cpp +214 -0
  47. data/ext/fastfilereader/mapper.h +59 -0
  48. data/ext/fastfilereader/rubymain.cpp +127 -0
  49. data/ext/kb.cpp +79 -0
  50. data/ext/page.cpp +107 -0
  51. data/ext/page.h +51 -0
  52. data/ext/pipe.cpp +347 -0
  53. data/ext/project.h +156 -0
  54. data/ext/rubymain.cpp +1297 -0
  55. data/ext/ssl.cpp +468 -0
  56. data/ext/ssl.h +94 -0
  57. data/java/.classpath +8 -0
  58. data/java/.project +17 -0
  59. data/java/src/com/rubyeventmachine/EmReactor.java +588 -0
  60. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  61. data/java/src/com/rubyeventmachine/EventableChannel.java +70 -0
  62. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +195 -0
  63. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  64. data/lib/em/buftok.rb +110 -0
  65. data/lib/em/callback.rb +58 -0
  66. data/lib/em/channel.rb +64 -0
  67. data/lib/em/completion.rb +304 -0
  68. data/lib/em/connection.rb +712 -0
  69. data/lib/em/deferrable.rb +210 -0
  70. data/lib/em/deferrable/pool.rb +2 -0
  71. data/lib/em/file_watch.rb +73 -0
  72. data/lib/em/future.rb +61 -0
  73. data/lib/em/iterator.rb +270 -0
  74. data/lib/em/messages.rb +66 -0
  75. data/lib/em/pool.rb +151 -0
  76. data/lib/em/process_watch.rb +45 -0
  77. data/lib/em/processes.rb +123 -0
  78. data/lib/em/protocols.rb +36 -0
  79. data/lib/em/protocols/header_and_content.rb +138 -0
  80. data/lib/em/protocols/httpclient.rb +279 -0
  81. data/lib/em/protocols/httpclient2.rb +600 -0
  82. data/lib/em/protocols/line_and_text.rb +125 -0
  83. data/lib/em/protocols/line_protocol.rb +29 -0
  84. data/lib/em/protocols/linetext2.rb +161 -0
  85. data/lib/em/protocols/memcache.rb +331 -0
  86. data/lib/em/protocols/object_protocol.rb +46 -0
  87. data/lib/em/protocols/postgres3.rb +246 -0
  88. data/lib/em/protocols/saslauth.rb +175 -0
  89. data/lib/em/protocols/smtpclient.rb +365 -0
  90. data/lib/em/protocols/smtpserver.rb +640 -0
  91. data/lib/em/protocols/socks4.rb +66 -0
  92. data/lib/em/protocols/stomp.rb +202 -0
  93. data/lib/em/protocols/tcptest.rb +54 -0
  94. data/lib/em/pure_ruby.rb +1017 -0
  95. data/lib/em/queue.rb +71 -0
  96. data/lib/em/resolver.rb +192 -0
  97. data/lib/em/spawnable.rb +84 -0
  98. data/lib/em/streamer.rb +118 -0
  99. data/lib/em/threaded_resource.rb +90 -0
  100. data/lib/em/tick_loop.rb +85 -0
  101. data/lib/em/timers.rb +61 -0
  102. data/lib/em/version.rb +3 -0
  103. data/lib/eventmachine.rb +1532 -0
  104. data/lib/jeventmachine.rb +284 -0
  105. data/lib/sonixlabs-eventmachine-java.rb +1 -0
  106. data/rakelib/cpp.rake_example +77 -0
  107. data/rakelib/package.rake +98 -0
  108. data/rakelib/test.rake +8 -0
  109. data/tests/client.crt +31 -0
  110. data/tests/client.key +51 -0
  111. data/tests/em_test_helper.rb +64 -0
  112. data/tests/test_attach.rb +126 -0
  113. data/tests/test_basic.rb +294 -0
  114. data/tests/test_channel.rb +62 -0
  115. data/tests/test_completion.rb +177 -0
  116. data/tests/test_connection_count.rb +33 -0
  117. data/tests/test_defer.rb +18 -0
  118. data/tests/test_deferrable.rb +35 -0
  119. data/tests/test_epoll.rb +130 -0
  120. data/tests/test_error_handler.rb +38 -0
  121. data/tests/test_exc.rb +28 -0
  122. data/tests/test_file_watch.rb +65 -0
  123. data/tests/test_futures.rb +170 -0
  124. data/tests/test_get_sock_opt.rb +37 -0
  125. data/tests/test_handler_check.rb +35 -0
  126. data/tests/test_hc.rb +155 -0
  127. data/tests/test_httpclient.rb +190 -0
  128. data/tests/test_httpclient2.rb +128 -0
  129. data/tests/test_idle_connection.rb +23 -0
  130. data/tests/test_inactivity_timeout.rb +54 -0
  131. data/tests/test_kb.rb +34 -0
  132. data/tests/test_ltp.rb +138 -0
  133. data/tests/test_ltp2.rb +288 -0
  134. data/tests/test_next_tick.rb +104 -0
  135. data/tests/test_object_protocol.rb +36 -0
  136. data/tests/test_pause.rb +78 -0
  137. data/tests/test_pending_connect_timeout.rb +52 -0
  138. data/tests/test_pool.rb +194 -0
  139. data/tests/test_process_watch.rb +48 -0
  140. data/tests/test_processes.rb +128 -0
  141. data/tests/test_proxy_connection.rb +180 -0
  142. data/tests/test_pure.rb +88 -0
  143. data/tests/test_queue.rb +50 -0
  144. data/tests/test_resolver.rb +55 -0
  145. data/tests/test_running.rb +14 -0
  146. data/tests/test_sasl.rb +47 -0
  147. data/tests/test_send_file.rb +217 -0
  148. data/tests/test_servers.rb +33 -0
  149. data/tests/test_set_sock_opt.rb +37 -0
  150. data/tests/test_shutdown_hooks.rb +23 -0
  151. data/tests/test_smtpclient.rb +55 -0
  152. data/tests/test_smtpserver.rb +57 -0
  153. data/tests/test_spawn.rb +293 -0
  154. data/tests/test_ssl_args.rb +78 -0
  155. data/tests/test_ssl_methods.rb +48 -0
  156. data/tests/test_ssl_verify.rb +82 -0
  157. data/tests/test_threaded_resource.rb +53 -0
  158. data/tests/test_tick_loop.rb +59 -0
  159. data/tests/test_timers.rb +123 -0
  160. data/tests/test_ud.rb +8 -0
  161. data/tests/test_unbind_reason.rb +48 -0
  162. metadata +301 -0
@@ -0,0 +1,156 @@
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
+ #ifdef OS_WIN32
26
+ #pragma warning(disable:4786)
27
+ #endif
28
+
29
+ #include <iostream>
30
+ #include <map>
31
+ #include <set>
32
+ #include <vector>
33
+ #include <deque>
34
+ #include <string>
35
+ #include <sstream>
36
+ #include <stdexcept>
37
+
38
+
39
+ #ifdef OS_UNIX
40
+ #include <signal.h>
41
+ #include <netdb.h>
42
+ #include <time.h>
43
+ #include <sys/time.h>
44
+ #include <sys/types.h>
45
+ #include <sys/stat.h>
46
+ #include <sys/socket.h>
47
+ #include <sys/un.h>
48
+ #include <sys/resource.h>
49
+ #include <sys/wait.h>
50
+ #include <assert.h>
51
+ #include <unistd.h>
52
+ #include <fcntl.h>
53
+ #include <errno.h>
54
+ #include <netinet/in.h>
55
+ #include <netinet/tcp.h>
56
+ #include <arpa/inet.h>
57
+ #include <pwd.h>
58
+ #include <string.h>
59
+ typedef int SOCKET;
60
+ #define INVALID_SOCKET -1
61
+ #define SOCKET_ERROR -1
62
+ #ifdef OS_SOLARIS8
63
+ #include <strings.h>
64
+ #include <sys/un.h>
65
+ #ifndef AF_LOCAL
66
+ #define AF_LOCAL AF_UNIX
67
+ #endif
68
+ // INADDR_NONE is undefined on Solaris < 8. Thanks to Brett Eisenberg and Tim Pease.
69
+ #ifndef INADDR_NONE
70
+ #define INADDR_NONE ((unsigned long)-1)
71
+ #endif
72
+ #endif /* OS_SOLARIS8 */
73
+
74
+ #ifdef _AIX
75
+ #include <strings.h>
76
+ #ifndef AF_LOCAL
77
+ #define AF_LOCAL AF_UNIX
78
+ #endif
79
+ #endif /* _AIX */
80
+
81
+ #endif /* OS_UNIX */
82
+
83
+ #ifdef OS_WIN32
84
+ // 21Sep09: windows limits select() to 64 sockets by default, we increase it to 1024 here (before including winsock2.h)
85
+ // 18Jun12: fd_setsize must be changed in the ruby binary (not in this extension). redefining it also causes segvs, see eventmachine/eventmachine#333
86
+ //#define FD_SETSIZE 1024
87
+
88
+ #define WIN32_LEAN_AND_MEAN
89
+ #include <windows.h>
90
+ #include <winsock2.h>
91
+ #include <ws2tcpip.h>
92
+ #include <rpc.h>
93
+ #include <fcntl.h>
94
+ #include <assert.h>
95
+
96
+ typedef int socklen_t;
97
+ typedef int pid_t;
98
+ #endif
99
+
100
+ #if !defined(_MSC_VER) || _MSC_VER > 1500
101
+ #include <stdint.h>
102
+ #endif
103
+
104
+ using namespace std;
105
+
106
+ #ifdef WITH_SSL
107
+ #include <openssl/ssl.h>
108
+ #include <openssl/err.h>
109
+ #endif
110
+
111
+ #ifdef HAVE_EPOLL
112
+ #include <sys/epoll.h>
113
+ #endif
114
+
115
+ #ifdef HAVE_KQUEUE
116
+ #include <sys/event.h>
117
+ #include <sys/queue.h>
118
+ #endif
119
+
120
+ #ifdef HAVE_INOTIFY
121
+ #include <sys/inotify.h>
122
+ #endif
123
+
124
+ #ifdef HAVE_OLD_INOTIFY
125
+ #include <sys/syscall.h>
126
+ #include <linux/inotify.h>
127
+ static inline int inotify_init (void) { return syscall (__NR_inotify_init); }
128
+ static inline int inotify_add_watch (int fd, const char *name, __u32 mask) { return syscall (__NR_inotify_add_watch, fd, name, mask); }
129
+ static inline int inotify_rm_watch (int fd, __u32 wd) { return syscall (__NR_inotify_rm_watch, fd, wd); }
130
+ #define HAVE_INOTIFY 1
131
+ #endif
132
+
133
+ #ifdef HAVE_INOTIFY
134
+ #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
135
+ #endif
136
+
137
+ #ifdef HAVE_WRITEV
138
+ #include <sys/uio.h>
139
+ #endif
140
+
141
+ #if __cplusplus
142
+ extern "C" {
143
+ #endif
144
+ typedef void (*EMCallback)(const unsigned long, int, const char*, const unsigned long);
145
+ #if __cplusplus
146
+ }
147
+ #endif
148
+
149
+ #include "binder.h"
150
+ #include "em.h"
151
+ #include "ed.h"
152
+ #include "page.h"
153
+ #include "ssl.h"
154
+ #include "eventmachine.h"
155
+
156
+ #endif // __Project__H_
@@ -0,0 +1,1297 @@
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
+ #ifndef RFLOAT_VALUE
25
+ #define RFLOAT_VALUE(arg) RFLOAT(arg)->value
26
+ #endif
27
+
28
+ /*******
29
+ Statics
30
+ *******/
31
+
32
+ static VALUE EmModule;
33
+ static VALUE EmConnection;
34
+ static VALUE EmConnsHash;
35
+ static VALUE EmTimersHash;
36
+
37
+ static VALUE EM_eConnectionError;
38
+ static VALUE EM_eUnknownTimerFired;
39
+ static VALUE EM_eConnectionNotBound;
40
+ static VALUE EM_eUnsupported;
41
+
42
+ static VALUE Intern_at_signature;
43
+ static VALUE Intern_at_timers;
44
+ static VALUE Intern_at_conns;
45
+ static VALUE Intern_at_error_handler;
46
+ static VALUE Intern_event_callback;
47
+ static VALUE Intern_run_deferred_callbacks;
48
+ static VALUE Intern_delete;
49
+ static VALUE Intern_call;
50
+ static VALUE Intern_receive_data;
51
+ static VALUE Intern_ssl_handshake_completed;
52
+ static VALUE Intern_ssl_verify_peer;
53
+ static VALUE Intern_notify_readable;
54
+ static VALUE Intern_notify_writable;
55
+ static VALUE Intern_proxy_target_unbound;
56
+ static VALUE Intern_proxy_completed;
57
+ static VALUE Intern_connection_completed;
58
+
59
+ static VALUE rb_cProcStatus;
60
+
61
+ struct em_event {
62
+ unsigned long signature;
63
+ int event;
64
+ const char *data_str;
65
+ unsigned long data_num;
66
+ };
67
+
68
+ static inline VALUE ensure_conn(const unsigned long signature)
69
+ {
70
+ VALUE conn = rb_hash_aref (EmConnsHash, ULONG2NUM (signature));
71
+ if (conn == Qnil)
72
+ rb_raise (EM_eConnectionNotBound, "unknown connection: %lu", signature);
73
+ return conn;
74
+ }
75
+
76
+
77
+ /****************
78
+ t_event_callback
79
+ ****************/
80
+
81
+ static inline void event_callback (struct em_event* e)
82
+ {
83
+ const unsigned long signature = e->signature;
84
+ int event = e->event;
85
+ const char *data_str = e->data_str;
86
+ const unsigned long data_num = e->data_num;
87
+
88
+ switch (event) {
89
+ case EM_CONNECTION_READ:
90
+ {
91
+ VALUE conn = rb_hash_aref (EmConnsHash, ULONG2NUM (signature));
92
+ if (conn == Qnil)
93
+ rb_raise (EM_eConnectionNotBound, "received %lu bytes of data for unknown signature: %lu", data_num, signature);
94
+ rb_funcall (conn, Intern_receive_data, 1, rb_str_new (data_str, data_num));
95
+ return;
96
+ }
97
+ case EM_CONNECTION_ACCEPTED:
98
+ {
99
+ rb_funcall (EmModule, Intern_event_callback, 3, ULONG2NUM(signature), INT2FIX(event), ULONG2NUM(data_num));
100
+ return;
101
+ }
102
+ case EM_CONNECTION_UNBOUND:
103
+ {
104
+ rb_funcall (EmModule, Intern_event_callback, 3, ULONG2NUM(signature), INT2FIX(event), ULONG2NUM(data_num));
105
+ return;
106
+ }
107
+ case EM_CONNECTION_COMPLETED:
108
+ {
109
+ VALUE conn = ensure_conn(signature);
110
+ rb_funcall (conn, Intern_connection_completed, 0);
111
+ return;
112
+ }
113
+ case EM_CONNECTION_NOTIFY_READABLE:
114
+ {
115
+ VALUE conn = ensure_conn(signature);
116
+ rb_funcall (conn, Intern_notify_readable, 0);
117
+ return;
118
+ }
119
+ case EM_CONNECTION_NOTIFY_WRITABLE:
120
+ {
121
+ VALUE conn = ensure_conn(signature);
122
+ rb_funcall (conn, Intern_notify_writable, 0);
123
+ return;
124
+ }
125
+ case EM_LOOPBREAK_SIGNAL:
126
+ {
127
+ rb_funcall (EmModule, Intern_run_deferred_callbacks, 0);
128
+ return;
129
+ }
130
+ case EM_TIMER_FIRED:
131
+ {
132
+ VALUE timer = rb_funcall (EmTimersHash, Intern_delete, 1, ULONG2NUM (data_num));
133
+ if (timer == Qnil) {
134
+ rb_raise (EM_eUnknownTimerFired, "no such timer: %lu", data_num);
135
+ } else if (timer == Qfalse) {
136
+ /* Timer Canceled */
137
+ } else {
138
+ rb_funcall (timer, Intern_call, 0);
139
+ }
140
+ return;
141
+ }
142
+ #ifdef WITH_SSL
143
+ case EM_SSL_HANDSHAKE_COMPLETED:
144
+ {
145
+ VALUE conn = ensure_conn(signature);
146
+ rb_funcall (conn, Intern_ssl_handshake_completed, 0);
147
+ return;
148
+ }
149
+ case EM_SSL_VERIFY:
150
+ {
151
+ VALUE conn = ensure_conn(signature);
152
+ VALUE should_accept = rb_funcall (conn, Intern_ssl_verify_peer, 1, rb_str_new(data_str, data_num));
153
+ if (RTEST(should_accept))
154
+ evma_accept_ssl_peer (signature);
155
+ return;
156
+ }
157
+ #endif
158
+ case EM_PROXY_TARGET_UNBOUND:
159
+ {
160
+ VALUE conn = ensure_conn(signature);
161
+ rb_funcall (conn, Intern_proxy_target_unbound, 0);
162
+ return;
163
+ }
164
+ case EM_PROXY_COMPLETED:
165
+ {
166
+ VALUE conn = ensure_conn(signature);
167
+ rb_funcall (conn, Intern_proxy_completed, 0);
168
+ return;
169
+ }
170
+ }
171
+ }
172
+
173
+ /*******************
174
+ event_error_handler
175
+ *******************/
176
+
177
+ static void event_error_handler(VALUE unused, VALUE err)
178
+ {
179
+ VALUE error_handler = rb_ivar_get(EmModule, Intern_at_error_handler);
180
+ rb_funcall (error_handler, Intern_call, 1, err);
181
+ }
182
+
183
+ /**********************
184
+ event_callback_wrapper
185
+ **********************/
186
+
187
+ static void event_callback_wrapper (const unsigned long signature, int event, const char *data_str, const unsigned long data_num)
188
+ {
189
+ struct em_event e;
190
+ e.signature = signature;
191
+ e.event = event;
192
+ e.data_str = data_str;
193
+ e.data_num = data_num;
194
+
195
+ if (!rb_ivar_defined(EmModule, Intern_at_error_handler))
196
+ event_callback(&e);
197
+ else
198
+ rb_rescue((VALUE (*)(ANYARGS))event_callback, (VALUE)&e, (VALUE (*)(ANYARGS))event_error_handler, Qnil);
199
+ }
200
+
201
+ /**************************
202
+ t_initialize_event_machine
203
+ **************************/
204
+
205
+ static VALUE t_initialize_event_machine (VALUE self)
206
+ {
207
+ EmConnsHash = rb_ivar_get (EmModule, Intern_at_conns);
208
+ EmTimersHash = rb_ivar_get (EmModule, Intern_at_timers);
209
+ assert(EmConnsHash != Qnil);
210
+ assert(EmTimersHash != Qnil);
211
+ evma_initialize_library ((EMCallback)event_callback_wrapper);
212
+ return Qnil;
213
+ }
214
+
215
+
216
+
217
+ /*****************************
218
+ t_run_machine_without_threads
219
+ *****************************/
220
+
221
+ static VALUE t_run_machine_without_threads (VALUE self)
222
+ {
223
+ evma_run_machine();
224
+ return Qnil;
225
+ }
226
+
227
+
228
+ /*******************
229
+ t_add_oneshot_timer
230
+ *******************/
231
+
232
+ static VALUE t_add_oneshot_timer (VALUE self, VALUE interval)
233
+ {
234
+ const unsigned long f = evma_install_oneshot_timer (FIX2INT (interval));
235
+ if (!f)
236
+ rb_raise (rb_eRuntimeError, "%s", "ran out of timers; use #set_max_timers to increase limit");
237
+ return ULONG2NUM (f);
238
+ }
239
+
240
+
241
+ /**************
242
+ t_start_server
243
+ **************/
244
+
245
+ static VALUE t_start_server (VALUE self, VALUE server, VALUE port)
246
+ {
247
+ const unsigned long f = evma_create_tcp_server (StringValuePtr(server), FIX2INT(port));
248
+ if (!f)
249
+ rb_raise (rb_eRuntimeError, "%s", "no acceptor (port is in use or requires root privileges)");
250
+ return ULONG2NUM (f);
251
+ }
252
+
253
+ /*************
254
+ t_stop_server
255
+ *************/
256
+
257
+ static VALUE t_stop_server (VALUE self, VALUE signature)
258
+ {
259
+ evma_stop_tcp_server (NUM2ULONG (signature));
260
+ return Qnil;
261
+ }
262
+
263
+
264
+ /*******************
265
+ t_start_unix_server
266
+ *******************/
267
+
268
+ static VALUE t_start_unix_server (VALUE self, VALUE filename)
269
+ {
270
+ const unsigned long f = evma_create_unix_domain_server (StringValuePtr(filename));
271
+ if (!f)
272
+ rb_raise (rb_eRuntimeError, "%s", "no unix-domain acceptor");
273
+ return ULONG2NUM (f);
274
+ }
275
+
276
+
277
+
278
+ /***********
279
+ t_send_data
280
+ ***********/
281
+
282
+ static VALUE t_send_data (VALUE self, VALUE signature, VALUE data, VALUE data_length)
283
+ {
284
+ int b = evma_send_data_to_connection (NUM2ULONG (signature), StringValuePtr (data), FIX2INT (data_length));
285
+ return INT2NUM (b);
286
+ }
287
+
288
+
289
+ /***********
290
+ t_start_tls
291
+ ***********/
292
+
293
+ static VALUE t_start_tls (VALUE self, VALUE signature)
294
+ {
295
+ evma_start_tls (NUM2ULONG (signature));
296
+ return Qnil;
297
+ }
298
+
299
+ /***************
300
+ t_set_tls_parms
301
+ ***************/
302
+
303
+ static VALUE t_set_tls_parms (VALUE self, VALUE signature, VALUE privkeyfile, VALUE certchainfile, VALUE verify_peer)
304
+ {
305
+ /* set_tls_parms takes a series of positional arguments for specifying such things
306
+ * as private keys and certificate chains.
307
+ * It's expected that the parameter list will grow as we add more supported features.
308
+ * ALL of these parameters are optional, and can be specified as empty or NULL strings.
309
+ */
310
+ evma_set_tls_parms (NUM2ULONG (signature), StringValuePtr (privkeyfile), StringValuePtr (certchainfile), (verify_peer == Qtrue ? 1 : 0));
311
+ return Qnil;
312
+ }
313
+
314
+ /***************
315
+ t_get_peer_cert
316
+ ***************/
317
+
318
+ static VALUE t_get_peer_cert (VALUE self, VALUE signature)
319
+ {
320
+ VALUE ret = Qnil;
321
+
322
+ #ifdef WITH_SSL
323
+ X509 *cert = NULL;
324
+ BUF_MEM *buf;
325
+ BIO *out;
326
+
327
+ cert = evma_get_peer_cert (NUM2ULONG (signature));
328
+
329
+ if (cert != NULL) {
330
+ out = BIO_new(BIO_s_mem());
331
+ PEM_write_bio_X509(out, cert);
332
+ BIO_get_mem_ptr(out, &buf);
333
+ ret = rb_str_new(buf->data, buf->length);
334
+ X509_free(cert);
335
+ BUF_MEM_free(buf);
336
+ }
337
+ #endif
338
+
339
+ return ret;
340
+ }
341
+
342
+ /**************
343
+ t_get_peername
344
+ **************/
345
+
346
+ static VALUE t_get_peername (VALUE self, VALUE signature)
347
+ {
348
+ char buf[1024];
349
+ socklen_t len = sizeof buf;
350
+ if (evma_get_peername (NUM2ULONG (signature), (struct sockaddr*)buf, &len)) {
351
+ return rb_str_new (buf, len);
352
+ }
353
+
354
+ return Qnil;
355
+ }
356
+
357
+ /**************
358
+ t_get_sockname
359
+ **************/
360
+
361
+ static VALUE t_get_sockname (VALUE self, VALUE signature)
362
+ {
363
+ char buf[1024];
364
+ socklen_t len = sizeof buf;
365
+ if (evma_get_sockname (NUM2ULONG (signature), (struct sockaddr*)buf, &len)) {
366
+ return rb_str_new (buf, len);
367
+ }
368
+
369
+ return Qnil;
370
+ }
371
+
372
+ /********************
373
+ t_get_subprocess_pid
374
+ ********************/
375
+
376
+ static VALUE t_get_subprocess_pid (VALUE self, VALUE signature)
377
+ {
378
+ pid_t pid;
379
+ if (evma_get_subprocess_pid (NUM2ULONG (signature), &pid)) {
380
+ return INT2NUM (pid);
381
+ }
382
+
383
+ return Qnil;
384
+ }
385
+
386
+ /***********************
387
+ t_get_subprocess_status
388
+ ***********************/
389
+
390
+ static VALUE t_get_subprocess_status (VALUE self, VALUE signature)
391
+ {
392
+ VALUE proc_status = Qnil;
393
+
394
+ int status;
395
+ pid_t pid;
396
+
397
+ if (evma_get_subprocess_status (NUM2ULONG (signature), &status)) {
398
+ if (evma_get_subprocess_pid (NUM2ULONG (signature), &pid)) {
399
+ proc_status = rb_obj_alloc(rb_cProcStatus);
400
+ rb_iv_set(proc_status, "status", INT2FIX(status));
401
+ rb_iv_set(proc_status, "pid", INT2FIX(pid));
402
+ }
403
+ }
404
+
405
+ return proc_status;
406
+ }
407
+
408
+ /**********************
409
+ t_get_connection_count
410
+ **********************/
411
+
412
+ static VALUE t_get_connection_count (VALUE self)
413
+ {
414
+ return INT2NUM(evma_get_connection_count());
415
+ }
416
+
417
+ /*****************************
418
+ t_get_comm_inactivity_timeout
419
+ *****************************/
420
+
421
+ static VALUE t_get_comm_inactivity_timeout (VALUE self, VALUE signature)
422
+ {
423
+ return rb_float_new(evma_get_comm_inactivity_timeout(NUM2ULONG (signature)));
424
+ }
425
+
426
+ /*****************************
427
+ t_set_comm_inactivity_timeout
428
+ *****************************/
429
+
430
+ static VALUE t_set_comm_inactivity_timeout (VALUE self, VALUE signature, VALUE timeout)
431
+ {
432
+ float ti = RFLOAT_VALUE(timeout);
433
+ if (evma_set_comm_inactivity_timeout(NUM2ULONG(signature), ti)) {
434
+ return Qtrue;
435
+ }
436
+ return Qfalse;
437
+ }
438
+
439
+ /*****************************
440
+ t_get_pending_connect_timeout
441
+ *****************************/
442
+
443
+ static VALUE t_get_pending_connect_timeout (VALUE self, VALUE signature)
444
+ {
445
+ return rb_float_new(evma_get_pending_connect_timeout(NUM2ULONG (signature)));
446
+ }
447
+
448
+ /*****************************
449
+ t_set_pending_connect_timeout
450
+ *****************************/
451
+
452
+ static VALUE t_set_pending_connect_timeout (VALUE self, VALUE signature, VALUE timeout)
453
+ {
454
+ float ti = RFLOAT_VALUE(timeout);
455
+ if (evma_set_pending_connect_timeout(NUM2ULONG(signature), ti)) {
456
+ return Qtrue;
457
+ }
458
+ return Qfalse;
459
+ }
460
+
461
+ /***************
462
+ t_send_datagram
463
+ ***************/
464
+
465
+ static VALUE t_send_datagram (VALUE self, VALUE signature, VALUE data, VALUE data_length, VALUE address, VALUE port)
466
+ {
467
+ int b = evma_send_datagram (NUM2ULONG (signature), StringValuePtr (data), FIX2INT (data_length), StringValuePtr(address), FIX2INT(port));
468
+ return INT2NUM (b);
469
+ }
470
+
471
+
472
+ /******************
473
+ t_close_connection
474
+ ******************/
475
+
476
+ static VALUE t_close_connection (VALUE self, VALUE signature, VALUE after_writing)
477
+ {
478
+ evma_close_connection (NUM2ULONG (signature), ((after_writing == Qtrue) ? 1 : 0));
479
+ return Qnil;
480
+ }
481
+
482
+ /********************************
483
+ t_report_connection_error_status
484
+ ********************************/
485
+
486
+ static VALUE t_report_connection_error_status (VALUE self, VALUE signature)
487
+ {
488
+ int b = evma_report_connection_error_status (NUM2ULONG (signature));
489
+ return INT2NUM (b);
490
+ }
491
+
492
+
493
+
494
+ /****************
495
+ t_connect_server
496
+ ****************/
497
+
498
+ static VALUE t_connect_server (VALUE self, VALUE server, VALUE port)
499
+ {
500
+ // Avoid FIX2INT in this case, because it doesn't deal with type errors properly.
501
+ // Specifically, if the value of port comes in as a string rather than an integer,
502
+ // NUM2INT will throw a type error, but FIX2INT will generate garbage.
503
+
504
+ try {
505
+ const unsigned long f = evma_connect_to_server (NULL, 0, StringValuePtr(server), NUM2INT(port));
506
+ if (!f)
507
+ rb_raise (EM_eConnectionError, "%s", "no connection");
508
+ return ULONG2NUM (f);
509
+ } catch (std::runtime_error e) {
510
+ rb_raise (EM_eConnectionError, "%s", e.what());
511
+ }
512
+ return Qnil;
513
+ }
514
+
515
+ /*********************
516
+ t_bind_connect_server
517
+ *********************/
518
+
519
+ static VALUE t_bind_connect_server (VALUE self, VALUE bind_addr, VALUE bind_port, VALUE server, VALUE port)
520
+ {
521
+ // Avoid FIX2INT in this case, because it doesn't deal with type errors properly.
522
+ // Specifically, if the value of port comes in as a string rather than an integer,
523
+ // NUM2INT will throw a type error, but FIX2INT will generate garbage.
524
+
525
+ try {
526
+ const unsigned long f = evma_connect_to_server (StringValuePtr(bind_addr), NUM2INT(bind_port), StringValuePtr(server), NUM2INT(port));
527
+ if (!f)
528
+ rb_raise (EM_eConnectionError, "%s", "no connection");
529
+ return ULONG2NUM (f);
530
+ } catch (std::runtime_error e) {
531
+ rb_raise (EM_eConnectionError, "%s", e.what());
532
+ }
533
+ return Qnil;
534
+ }
535
+
536
+ /*********************
537
+ t_connect_unix_server
538
+ *********************/
539
+
540
+ static VALUE t_connect_unix_server (VALUE self, VALUE serversocket)
541
+ {
542
+ const unsigned long f = evma_connect_to_unix_server (StringValuePtr(serversocket));
543
+ if (!f)
544
+ rb_raise (rb_eRuntimeError, "%s", "no connection");
545
+ return ULONG2NUM (f);
546
+ }
547
+
548
+ /***********
549
+ t_attach_fd
550
+ ***********/
551
+
552
+ static VALUE t_attach_fd (VALUE self, VALUE file_descriptor, VALUE watch_mode)
553
+ {
554
+ const unsigned long f = evma_attach_fd (NUM2INT(file_descriptor), watch_mode == Qtrue);
555
+ if (!f)
556
+ rb_raise (rb_eRuntimeError, "%s", "no connection");
557
+ return ULONG2NUM (f);
558
+ }
559
+
560
+ /***********
561
+ t_detach_fd
562
+ ***********/
563
+
564
+ static VALUE t_detach_fd (VALUE self, VALUE signature)
565
+ {
566
+ return INT2NUM(evma_detach_fd (NUM2ULONG (signature)));
567
+ }
568
+
569
+ /**************
570
+ t_get_sock_opt
571
+ **************/
572
+
573
+ static VALUE t_get_sock_opt (VALUE self, VALUE signature, VALUE lev, VALUE optname)
574
+ {
575
+ int fd = evma_get_file_descriptor (NUM2ULONG (signature));
576
+ int level = NUM2INT(lev), option = NUM2INT(optname);
577
+ socklen_t len = 128;
578
+ char buf[128];
579
+
580
+ if (getsockopt(fd, level, option, buf, &len) < 0)
581
+ rb_sys_fail("getsockopt");
582
+
583
+ return rb_str_new(buf, len);
584
+ }
585
+
586
+ /**************
587
+ t_set_sock_opt
588
+ **************/
589
+
590
+ static VALUE t_set_sock_opt (VALUE self, VALUE signature, VALUE lev, VALUE optname, VALUE optval)
591
+ {
592
+ int fd = evma_get_file_descriptor (NUM2ULONG (signature));
593
+ int level = NUM2INT(lev), option = NUM2INT(optname);
594
+ int i;
595
+ void *v;
596
+ socklen_t len;
597
+
598
+ switch (TYPE(optval)) {
599
+ case T_FIXNUM:
600
+ i = FIX2INT(optval);
601
+ goto numval;
602
+ case T_FALSE:
603
+ i = 0;
604
+ goto numval;
605
+ case T_TRUE:
606
+ i = 1;
607
+ numval:
608
+ v = (void*)&i; len = sizeof(i);
609
+ break;
610
+ default:
611
+ StringValue(optval);
612
+ v = RSTRING_PTR(optval);
613
+ len = RSTRING_LENINT(optval);
614
+ break;
615
+ }
616
+
617
+
618
+ if (setsockopt(fd, level, option, (char *)v, len) < 0)
619
+ rb_sys_fail("setsockopt");
620
+
621
+ return INT2FIX(0);
622
+ }
623
+
624
+ /********************
625
+ t_is_notify_readable
626
+ ********************/
627
+
628
+ static VALUE t_is_notify_readable (VALUE self, VALUE signature)
629
+ {
630
+ return evma_is_notify_readable(NUM2ULONG (signature)) ? Qtrue : Qfalse;
631
+ }
632
+
633
+ /*********************
634
+ t_set_notify_readable
635
+ *********************/
636
+
637
+ static VALUE t_set_notify_readable (VALUE self, VALUE signature, VALUE mode)
638
+ {
639
+ evma_set_notify_readable(NUM2ULONG (signature), mode == Qtrue);
640
+ return Qnil;
641
+ }
642
+
643
+ /********************
644
+ t_is_notify_readable
645
+ ********************/
646
+
647
+ static VALUE t_is_notify_writable (VALUE self, VALUE signature)
648
+ {
649
+ return evma_is_notify_writable(NUM2ULONG (signature)) ? Qtrue : Qfalse;
650
+ }
651
+
652
+ /*********************
653
+ t_set_notify_writable
654
+ *********************/
655
+
656
+ static VALUE t_set_notify_writable (VALUE self, VALUE signature, VALUE mode)
657
+ {
658
+ evma_set_notify_writable(NUM2ULONG (signature), mode == Qtrue);
659
+ return Qnil;
660
+ }
661
+
662
+ /*******
663
+ t_pause
664
+ *******/
665
+
666
+ static VALUE t_pause (VALUE self, VALUE signature)
667
+ {
668
+ return evma_pause(NUM2ULONG (signature)) ? Qtrue : Qfalse;
669
+ }
670
+
671
+ /********
672
+ t_resume
673
+ ********/
674
+
675
+ static VALUE t_resume (VALUE self, VALUE signature)
676
+ {
677
+ return evma_resume(NUM2ULONG (signature)) ? Qtrue : Qfalse;
678
+ }
679
+
680
+ /**********
681
+ t_paused_p
682
+ **********/
683
+
684
+ static VALUE t_paused_p (VALUE self, VALUE signature)
685
+ {
686
+ return evma_is_paused(NUM2ULONG (signature)) ? Qtrue : Qfalse;
687
+ }
688
+
689
+ /*********************
690
+ t_num_close_scheduled
691
+ *********************/
692
+
693
+ static VALUE t_num_close_scheduled (VALUE self)
694
+ {
695
+ return INT2FIX(evma_num_close_scheduled());
696
+ }
697
+
698
+ /*****************
699
+ t_open_udp_socket
700
+ *****************/
701
+
702
+ static VALUE t_open_udp_socket (VALUE self, VALUE server, VALUE port)
703
+ {
704
+ const unsigned long f = evma_open_datagram_socket (StringValuePtr(server), FIX2INT(port));
705
+ if (!f)
706
+ rb_raise (rb_eRuntimeError, "%s", "no datagram socket");
707
+ return ULONG2NUM (f);
708
+ }
709
+
710
+
711
+
712
+ /*****************
713
+ t_release_machine
714
+ *****************/
715
+
716
+ static VALUE t_release_machine (VALUE self)
717
+ {
718
+ evma_release_library();
719
+ return Qnil;
720
+ }
721
+
722
+
723
+ /******
724
+ t_stop
725
+ ******/
726
+
727
+ static VALUE t_stop (VALUE self)
728
+ {
729
+ evma_stop_machine();
730
+ return Qnil;
731
+ }
732
+
733
+ /******************
734
+ t_signal_loopbreak
735
+ ******************/
736
+
737
+ static VALUE t_signal_loopbreak (VALUE self)
738
+ {
739
+ evma_signal_loopbreak();
740
+ return Qnil;
741
+ }
742
+
743
+ /**************
744
+ t_library_type
745
+ **************/
746
+
747
+ static VALUE t_library_type (VALUE self)
748
+ {
749
+ return rb_eval_string (":extension");
750
+ }
751
+
752
+
753
+
754
+ /*******************
755
+ t_set_timer_quantum
756
+ *******************/
757
+
758
+ static VALUE t_set_timer_quantum (VALUE self, VALUE interval)
759
+ {
760
+ evma_set_timer_quantum (FIX2INT (interval));
761
+ return Qnil;
762
+ }
763
+
764
+ /********************
765
+ t_get_max_timer_count
766
+ ********************/
767
+
768
+ static VALUE t_get_max_timer_count (VALUE self)
769
+ {
770
+ return INT2FIX (evma_get_max_timer_count());
771
+ }
772
+
773
+ /********************
774
+ t_set_max_timer_count
775
+ ********************/
776
+
777
+ static VALUE t_set_max_timer_count (VALUE self, VALUE ct)
778
+ {
779
+ evma_set_max_timer_count (FIX2INT (ct));
780
+ return Qnil;
781
+ }
782
+
783
+ /***************
784
+ t_setuid_string
785
+ ***************/
786
+
787
+ static VALUE t_setuid_string (VALUE self, VALUE username)
788
+ {
789
+ evma_setuid_string (StringValuePtr (username));
790
+ return Qnil;
791
+ }
792
+
793
+
794
+
795
+ /**************
796
+ t_invoke_popen
797
+ **************/
798
+
799
+ static VALUE t_invoke_popen (VALUE self, VALUE cmd)
800
+ {
801
+ // 1.8.7+
802
+ #ifdef RARRAY_LEN
803
+ int len = RARRAY_LEN(cmd);
804
+ #else
805
+ int len = RARRAY (cmd)->len;
806
+ #endif
807
+ if (len >= 2048)
808
+ rb_raise (rb_eRuntimeError, "%s", "too many arguments to popen");
809
+ char *strings [2048];
810
+ for (int i=0; i < len; i++) {
811
+ VALUE ix = INT2FIX (i);
812
+ VALUE s = rb_ary_aref (1, &ix, cmd);
813
+ strings[i] = StringValuePtr (s);
814
+ }
815
+ strings[len] = NULL;
816
+
817
+ const unsigned long f = evma_popen (strings);
818
+ if (!f) {
819
+ char *err = strerror (errno);
820
+ char buf[100];
821
+ memset (buf, 0, sizeof(buf));
822
+ snprintf (buf, sizeof(buf)-1, "no popen: %s", (err?err:"???"));
823
+ rb_raise (rb_eRuntimeError, "%s", buf);
824
+ }
825
+ return ULONG2NUM (f);
826
+ }
827
+
828
+
829
+ /***************
830
+ t_read_keyboard
831
+ ***************/
832
+
833
+ static VALUE t_read_keyboard (VALUE self)
834
+ {
835
+ const unsigned long f = evma_open_keyboard();
836
+ if (!f)
837
+ rb_raise (rb_eRuntimeError, "%s", "no keyboard reader");
838
+ return ULONG2NUM (f);
839
+ }
840
+
841
+
842
+ /****************
843
+ t_watch_filename
844
+ ****************/
845
+
846
+ static VALUE t_watch_filename (VALUE self, VALUE fname)
847
+ {
848
+ try {
849
+ return ULONG2NUM(evma_watch_filename(StringValuePtr(fname)));
850
+ } catch (std::runtime_error e) {
851
+ rb_raise (EM_eUnsupported, "%s", e.what());
852
+ }
853
+ return Qnil;
854
+ }
855
+
856
+
857
+ /******************
858
+ t_unwatch_filename
859
+ ******************/
860
+
861
+ static VALUE t_unwatch_filename (VALUE self, VALUE sig)
862
+ {
863
+ evma_unwatch_filename(NUM2ULONG (sig));
864
+ return Qnil;
865
+ }
866
+
867
+
868
+ /***********
869
+ t_watch_pid
870
+ ***********/
871
+
872
+ static VALUE t_watch_pid (VALUE self, VALUE pid)
873
+ {
874
+ try {
875
+ return ULONG2NUM(evma_watch_pid(NUM2INT(pid)));
876
+ } catch (std::runtime_error e) {
877
+ rb_raise (EM_eUnsupported, "%s", e.what());
878
+ }
879
+ return Qnil;
880
+ }
881
+
882
+
883
+ /*************
884
+ t_unwatch_pid
885
+ *************/
886
+
887
+ static VALUE t_unwatch_pid (VALUE self, VALUE sig)
888
+ {
889
+ evma_unwatch_pid(NUM2ULONG (sig));
890
+ return Qnil;
891
+ }
892
+
893
+
894
+ /**********
895
+ t__epoll_p
896
+ **********/
897
+
898
+ static VALUE t__epoll_p (VALUE self)
899
+ {
900
+ #ifdef HAVE_EPOLL
901
+ return Qtrue;
902
+ #else
903
+ return Qfalse;
904
+ #endif
905
+ }
906
+
907
+ /********
908
+ t__epoll
909
+ ********/
910
+
911
+ static VALUE t__epoll (VALUE self)
912
+ {
913
+ evma_set_epoll (1);
914
+ return Qtrue;
915
+ }
916
+
917
+ /***********
918
+ t__epoll_set
919
+ ***********/
920
+
921
+ static VALUE t__epoll_set (VALUE self, VALUE val)
922
+ {
923
+ if (t__epoll_p(self) == Qfalse)
924
+ rb_raise (EM_eUnsupported, "%s", "epoll is not supported on this platform");
925
+
926
+ evma_set_epoll (val == Qtrue ? 1 : 0);
927
+ return val;
928
+ }
929
+
930
+
931
+ /***********
932
+ t__kqueue_p
933
+ ***********/
934
+
935
+ static VALUE t__kqueue_p (VALUE self)
936
+ {
937
+ #ifdef HAVE_KQUEUE
938
+ return Qtrue;
939
+ #else
940
+ return Qfalse;
941
+ #endif
942
+ }
943
+
944
+ /*********
945
+ t__kqueue
946
+ *********/
947
+
948
+ static VALUE t__kqueue (VALUE self)
949
+ {
950
+ evma_set_kqueue (1);
951
+ return Qtrue;
952
+ }
953
+
954
+ /*************
955
+ t__kqueue_set
956
+ *************/
957
+
958
+ static VALUE t__kqueue_set (VALUE self, VALUE val)
959
+ {
960
+ if (t__kqueue_p(self) == Qfalse)
961
+ rb_raise (EM_eUnsupported, "%s", "kqueue is not supported on this platform");
962
+
963
+ evma_set_kqueue (val == Qtrue ? 1 : 0);
964
+ return val;
965
+ }
966
+
967
+
968
+ /********
969
+ t__ssl_p
970
+ ********/
971
+
972
+ static VALUE t__ssl_p (VALUE self)
973
+ {
974
+ #ifdef WITH_SSL
975
+ return Qtrue;
976
+ #else
977
+ return Qfalse;
978
+ #endif
979
+ }
980
+
981
+
982
+ /****************
983
+ t_send_file_data
984
+ ****************/
985
+
986
+ static VALUE t_send_file_data (VALUE self, VALUE signature, VALUE filename)
987
+ {
988
+
989
+ /* The current implementation of evma_send_file_data_to_connection enforces a strict
990
+ * upper limit on the file size it will transmit (currently 32K). The function returns
991
+ * zero on success, -1 if the requested file exceeds its size limit, and a positive
992
+ * number for other errors.
993
+ * TODO: Positive return values are actually errno's, which is probably the wrong way to
994
+ * do this. For one thing it's ugly. For another, we can't be sure zero is never a real errno.
995
+ */
996
+
997
+ int b = evma_send_file_data_to_connection (NUM2ULONG (signature), StringValuePtr(filename));
998
+ if (b == -1)
999
+ rb_raise(rb_eRuntimeError, "%s", "File too large. send_file_data() supports files under 32k.");
1000
+ if (b > 0) {
1001
+ char *err = strerror (b);
1002
+ char buf[1024];
1003
+ memset (buf, 0, sizeof(buf));
1004
+ snprintf (buf, sizeof(buf)-1, ": %s %s", StringValuePtr(filename),(err?err:"???"));
1005
+
1006
+ rb_raise (rb_eIOError, "%s", buf);
1007
+ }
1008
+
1009
+ return INT2NUM (0);
1010
+ }
1011
+
1012
+
1013
+ /*******************
1014
+ t_set_rlimit_nofile
1015
+ *******************/
1016
+
1017
+ static VALUE t_set_rlimit_nofile (VALUE self, VALUE arg)
1018
+ {
1019
+ arg = (NIL_P(arg)) ? -1 : NUM2INT (arg);
1020
+ return INT2NUM (evma_set_rlimit_nofile (arg));
1021
+ }
1022
+
1023
+ /***************************
1024
+ conn_get_outbound_data_size
1025
+ ***************************/
1026
+
1027
+ static VALUE conn_get_outbound_data_size (VALUE self)
1028
+ {
1029
+ VALUE sig = rb_ivar_get (self, Intern_at_signature);
1030
+ return INT2NUM (evma_get_outbound_data_size (NUM2ULONG (sig)));
1031
+ }
1032
+
1033
+
1034
+ /******************************
1035
+ conn_associate_callback_target
1036
+ ******************************/
1037
+
1038
+ static VALUE conn_associate_callback_target (VALUE self, VALUE sig)
1039
+ {
1040
+ // No-op for the time being.
1041
+ return Qnil;
1042
+ }
1043
+
1044
+
1045
+ /***************
1046
+ t_get_loop_time
1047
+ ****************/
1048
+
1049
+ static VALUE t_get_loop_time (VALUE self)
1050
+ {
1051
+ #ifndef HAVE_RB_TIME_NEW
1052
+ static VALUE cTime = rb_path2class("Time");
1053
+ static ID at = rb_intern("at");
1054
+ #endif
1055
+
1056
+ uint64_t current_time = evma_get_current_loop_time();
1057
+ if (current_time != 0) {
1058
+ #ifndef HAVE_RB_TIME_NEW
1059
+ return rb_funcall(cTime, at, 2, INT2NUM(current_time / 1000000), INT2NUM(current_time % 1000000));
1060
+ #else
1061
+ return rb_time_new(current_time / 1000000, current_time % 1000000);
1062
+ #endif
1063
+ }
1064
+ return Qnil;
1065
+ }
1066
+
1067
+
1068
+ /*************
1069
+ t_start_proxy
1070
+ **************/
1071
+
1072
+ static VALUE t_start_proxy (VALUE self, VALUE from, VALUE to, VALUE bufsize, VALUE length)
1073
+ {
1074
+ try {
1075
+ evma_start_proxy(NUM2ULONG (from), NUM2ULONG (to), NUM2ULONG(bufsize), NUM2ULONG(length));
1076
+ } catch (std::runtime_error e) {
1077
+ rb_raise (EM_eConnectionError, "%s", e.what());
1078
+ }
1079
+ return Qnil;
1080
+ }
1081
+
1082
+
1083
+ /************
1084
+ t_stop_proxy
1085
+ *************/
1086
+
1087
+ static VALUE t_stop_proxy (VALUE self, VALUE from)
1088
+ {
1089
+ try{
1090
+ evma_stop_proxy(NUM2ULONG (from));
1091
+ } catch (std::runtime_error e) {
1092
+ rb_raise (EM_eConnectionError, "%s", e.what());
1093
+ }
1094
+ return Qnil;
1095
+ }
1096
+
1097
+ /***************
1098
+ t_proxied_bytes
1099
+ ****************/
1100
+
1101
+ static VALUE t_proxied_bytes (VALUE self, VALUE from)
1102
+ {
1103
+ try{
1104
+ return ULONG2NUM(evma_proxied_bytes(NUM2ULONG (from)));
1105
+ } catch (std::runtime_error e) {
1106
+ rb_raise (EM_eConnectionError, "%s", e.what());
1107
+ }
1108
+ return Qnil;
1109
+ }
1110
+
1111
+ /***************
1112
+ t_get_idle_time
1113
+ ****************/
1114
+
1115
+ static VALUE t_get_idle_time (VALUE self, VALUE from)
1116
+ {
1117
+ try{
1118
+ uint64_t current_time = evma_get_current_loop_time();
1119
+ uint64_t time = evma_get_last_activity_time(NUM2ULONG (from));
1120
+ if (current_time != 0 && time != 0) {
1121
+ if (time >= current_time)
1122
+ return ULONG2NUM(0);
1123
+ else {
1124
+ uint64_t diff = current_time - time;
1125
+ float seconds = diff / (1000.0*1000.0);
1126
+ return rb_float_new(seconds);
1127
+ }
1128
+ return Qnil;
1129
+ }
1130
+ } catch (std::runtime_error e) {
1131
+ rb_raise (EM_eConnectionError, "%s", e.what());
1132
+ }
1133
+ return Qnil;
1134
+ }
1135
+
1136
+ /************************
1137
+ t_get_heartbeat_interval
1138
+ *************************/
1139
+
1140
+ static VALUE t_get_heartbeat_interval (VALUE self)
1141
+ {
1142
+ return rb_float_new(evma_get_heartbeat_interval());
1143
+ }
1144
+
1145
+
1146
+ /************************
1147
+ t_set_heartbeat_interval
1148
+ *************************/
1149
+
1150
+ static VALUE t_set_heartbeat_interval (VALUE self, VALUE interval)
1151
+ {
1152
+ float iv = RFLOAT_VALUE(interval);
1153
+ if (evma_set_heartbeat_interval(iv))
1154
+ return Qtrue;
1155
+ return Qfalse;
1156
+ }
1157
+
1158
+
1159
+ /*********************
1160
+ Init_rubyeventmachine
1161
+ *********************/
1162
+
1163
+ extern "C" void Init_rubyeventmachine()
1164
+ {
1165
+ // Lookup Process::Status for get_subprocess_status
1166
+ VALUE rb_mProcess = rb_const_get(rb_cObject, rb_intern("Process"));
1167
+ rb_cProcStatus = rb_const_get(rb_mProcess, rb_intern("Status"));
1168
+
1169
+ // Tuck away some symbol values so we don't have to look 'em up every time we need 'em.
1170
+ Intern_at_signature = rb_intern ("@signature");
1171
+ Intern_at_timers = rb_intern ("@timers");
1172
+ Intern_at_conns = rb_intern ("@conns");
1173
+ Intern_at_error_handler = rb_intern("@error_handler");
1174
+
1175
+ Intern_event_callback = rb_intern ("event_callback");
1176
+ Intern_run_deferred_callbacks = rb_intern ("run_deferred_callbacks");
1177
+ Intern_delete = rb_intern ("delete");
1178
+ Intern_call = rb_intern ("call");
1179
+ Intern_receive_data = rb_intern ("receive_data");
1180
+ Intern_ssl_handshake_completed = rb_intern ("ssl_handshake_completed");
1181
+ Intern_ssl_verify_peer = rb_intern ("ssl_verify_peer");
1182
+ Intern_notify_readable = rb_intern ("notify_readable");
1183
+ Intern_notify_writable = rb_intern ("notify_writable");
1184
+ Intern_proxy_target_unbound = rb_intern ("proxy_target_unbound");
1185
+ Intern_proxy_completed = rb_intern ("proxy_completed");
1186
+ Intern_connection_completed = rb_intern ("connection_completed");
1187
+
1188
+ // INCOMPLETE, we need to define class Connections inside module EventMachine
1189
+ // run_machine and run_machine_without_threads are now identical.
1190
+ // Must deprecate the without_threads variant.
1191
+ EmModule = rb_define_module ("EventMachine");
1192
+ EmConnection = rb_define_class_under (EmModule, "Connection", rb_cObject);
1193
+
1194
+ rb_define_class_under (EmModule, "NoHandlerForAcceptedConnection", rb_eRuntimeError);
1195
+ EM_eConnectionError = rb_define_class_under (EmModule, "ConnectionError", rb_eRuntimeError);
1196
+ EM_eConnectionNotBound = rb_define_class_under (EmModule, "ConnectionNotBound", rb_eRuntimeError);
1197
+ EM_eUnknownTimerFired = rb_define_class_under (EmModule, "UnknownTimerFired", rb_eRuntimeError);
1198
+ EM_eUnsupported = rb_define_class_under (EmModule, "Unsupported", rb_eRuntimeError);
1199
+
1200
+ rb_define_module_function (EmModule, "initialize_event_machine", (VALUE(*)(...))t_initialize_event_machine, 0);
1201
+ rb_define_module_function (EmModule, "run_machine", (VALUE(*)(...))t_run_machine_without_threads, 0);
1202
+ rb_define_module_function (EmModule, "run_machine_without_threads", (VALUE(*)(...))t_run_machine_without_threads, 0);
1203
+ rb_define_module_function (EmModule, "add_oneshot_timer", (VALUE(*)(...))t_add_oneshot_timer, 1);
1204
+ rb_define_module_function (EmModule, "start_tcp_server", (VALUE(*)(...))t_start_server, 2);
1205
+ rb_define_module_function (EmModule, "stop_tcp_server", (VALUE(*)(...))t_stop_server, 1);
1206
+ rb_define_module_function (EmModule, "start_unix_server", (VALUE(*)(...))t_start_unix_server, 1);
1207
+ rb_define_module_function (EmModule, "set_tls_parms", (VALUE(*)(...))t_set_tls_parms, 4);
1208
+ rb_define_module_function (EmModule, "start_tls", (VALUE(*)(...))t_start_tls, 1);
1209
+ rb_define_module_function (EmModule, "get_peer_cert", (VALUE(*)(...))t_get_peer_cert, 1);
1210
+ rb_define_module_function (EmModule, "send_data", (VALUE(*)(...))t_send_data, 3);
1211
+ rb_define_module_function (EmModule, "send_datagram", (VALUE(*)(...))t_send_datagram, 5);
1212
+ rb_define_module_function (EmModule, "close_connection", (VALUE(*)(...))t_close_connection, 2);
1213
+ rb_define_module_function (EmModule, "report_connection_error_status", (VALUE(*)(...))t_report_connection_error_status, 1);
1214
+ rb_define_module_function (EmModule, "connect_server", (VALUE(*)(...))t_connect_server, 2);
1215
+ rb_define_module_function (EmModule, "bind_connect_server", (VALUE(*)(...))t_bind_connect_server, 4);
1216
+ rb_define_module_function (EmModule, "connect_unix_server", (VALUE(*)(...))t_connect_unix_server, 1);
1217
+
1218
+ rb_define_module_function (EmModule, "attach_fd", (VALUE (*)(...))t_attach_fd, 2);
1219
+ rb_define_module_function (EmModule, "detach_fd", (VALUE (*)(...))t_detach_fd, 1);
1220
+ rb_define_module_function (EmModule, "get_sock_opt", (VALUE (*)(...))t_get_sock_opt, 3);
1221
+ rb_define_module_function (EmModule, "set_sock_opt", (VALUE (*)(...))t_set_sock_opt, 4);
1222
+ rb_define_module_function (EmModule, "set_notify_readable", (VALUE (*)(...))t_set_notify_readable, 2);
1223
+ rb_define_module_function (EmModule, "set_notify_writable", (VALUE (*)(...))t_set_notify_writable, 2);
1224
+ rb_define_module_function (EmModule, "is_notify_readable", (VALUE (*)(...))t_is_notify_readable, 1);
1225
+ rb_define_module_function (EmModule, "is_notify_writable", (VALUE (*)(...))t_is_notify_writable, 1);
1226
+
1227
+ rb_define_module_function (EmModule, "pause_connection", (VALUE (*)(...))t_pause, 1);
1228
+ rb_define_module_function (EmModule, "resume_connection", (VALUE (*)(...))t_resume, 1);
1229
+ rb_define_module_function (EmModule, "connection_paused?", (VALUE (*)(...))t_paused_p, 1);
1230
+ rb_define_module_function (EmModule, "num_close_scheduled", (VALUE (*)(...))t_num_close_scheduled, 0);
1231
+
1232
+ rb_define_module_function (EmModule, "start_proxy", (VALUE (*)(...))t_start_proxy, 4);
1233
+ rb_define_module_function (EmModule, "stop_proxy", (VALUE (*)(...))t_stop_proxy, 1);
1234
+ rb_define_module_function (EmModule, "get_proxied_bytes", (VALUE (*)(...))t_proxied_bytes, 1);
1235
+
1236
+ rb_define_module_function (EmModule, "watch_filename", (VALUE (*)(...))t_watch_filename, 1);
1237
+ rb_define_module_function (EmModule, "unwatch_filename", (VALUE (*)(...))t_unwatch_filename, 1);
1238
+
1239
+ rb_define_module_function (EmModule, "watch_pid", (VALUE (*)(...))t_watch_pid, 1);
1240
+ rb_define_module_function (EmModule, "unwatch_pid", (VALUE (*)(...))t_unwatch_pid, 1);
1241
+
1242
+ rb_define_module_function (EmModule, "current_time", (VALUE(*)(...))t_get_loop_time, 0);
1243
+
1244
+ rb_define_module_function (EmModule, "open_udp_socket", (VALUE(*)(...))t_open_udp_socket, 2);
1245
+ rb_define_module_function (EmModule, "read_keyboard", (VALUE(*)(...))t_read_keyboard, 0);
1246
+ rb_define_module_function (EmModule, "release_machine", (VALUE(*)(...))t_release_machine, 0);
1247
+ rb_define_module_function (EmModule, "stop", (VALUE(*)(...))t_stop, 0);
1248
+ rb_define_module_function (EmModule, "signal_loopbreak", (VALUE(*)(...))t_signal_loopbreak, 0);
1249
+ rb_define_module_function (EmModule, "library_type", (VALUE(*)(...))t_library_type, 0);
1250
+ rb_define_module_function (EmModule, "set_timer_quantum", (VALUE(*)(...))t_set_timer_quantum, 1);
1251
+ rb_define_module_function (EmModule, "get_max_timer_count", (VALUE(*)(...))t_get_max_timer_count, 0);
1252
+ rb_define_module_function (EmModule, "set_max_timer_count", (VALUE(*)(...))t_set_max_timer_count, 1);
1253
+ rb_define_module_function (EmModule, "setuid_string", (VALUE(*)(...))t_setuid_string, 1);
1254
+ rb_define_module_function (EmModule, "invoke_popen", (VALUE(*)(...))t_invoke_popen, 1);
1255
+ rb_define_module_function (EmModule, "send_file_data", (VALUE(*)(...))t_send_file_data, 2);
1256
+ rb_define_module_function (EmModule, "get_heartbeat_interval", (VALUE(*)(...))t_get_heartbeat_interval, 0);
1257
+ rb_define_module_function (EmModule, "set_heartbeat_interval", (VALUE(*)(...))t_set_heartbeat_interval, 1);
1258
+ rb_define_module_function (EmModule, "get_idle_time", (VALUE(*)(...))t_get_idle_time, 1);
1259
+
1260
+ rb_define_module_function (EmModule, "get_peername", (VALUE(*)(...))t_get_peername, 1);
1261
+ rb_define_module_function (EmModule, "get_sockname", (VALUE(*)(...))t_get_sockname, 1);
1262
+ rb_define_module_function (EmModule, "get_subprocess_pid", (VALUE(*)(...))t_get_subprocess_pid, 1);
1263
+ rb_define_module_function (EmModule, "get_subprocess_status", (VALUE(*)(...))t_get_subprocess_status, 1);
1264
+ rb_define_module_function (EmModule, "get_comm_inactivity_timeout", (VALUE(*)(...))t_get_comm_inactivity_timeout, 1);
1265
+ rb_define_module_function (EmModule, "set_comm_inactivity_timeout", (VALUE(*)(...))t_set_comm_inactivity_timeout, 2);
1266
+ rb_define_module_function (EmModule, "get_pending_connect_timeout", (VALUE(*)(...))t_get_pending_connect_timeout, 1);
1267
+ rb_define_module_function (EmModule, "set_pending_connect_timeout", (VALUE(*)(...))t_set_pending_connect_timeout, 2);
1268
+ rb_define_module_function (EmModule, "set_rlimit_nofile", (VALUE(*)(...))t_set_rlimit_nofile, 1);
1269
+ rb_define_module_function (EmModule, "get_connection_count", (VALUE(*)(...))t_get_connection_count, 0);
1270
+
1271
+ rb_define_module_function (EmModule, "epoll", (VALUE(*)(...))t__epoll, 0);
1272
+ rb_define_module_function (EmModule, "epoll=", (VALUE(*)(...))t__epoll_set, 1);
1273
+ rb_define_module_function (EmModule, "epoll?", (VALUE(*)(...))t__epoll_p, 0);
1274
+
1275
+ rb_define_module_function (EmModule, "kqueue", (VALUE(*)(...))t__kqueue, 0);
1276
+ rb_define_module_function (EmModule, "kqueue=", (VALUE(*)(...))t__kqueue_set, 1);
1277
+ rb_define_module_function (EmModule, "kqueue?", (VALUE(*)(...))t__kqueue_p, 0);
1278
+
1279
+ rb_define_module_function (EmModule, "ssl?", (VALUE(*)(...))t__ssl_p, 0);
1280
+
1281
+ rb_define_method (EmConnection, "get_outbound_data_size", (VALUE(*)(...))conn_get_outbound_data_size, 0);
1282
+ rb_define_method (EmConnection, "associate_callback_target", (VALUE(*)(...))conn_associate_callback_target, 1);
1283
+
1284
+ rb_define_const (EmModule, "TimerFired", INT2NUM(100));
1285
+ rb_define_const (EmModule, "ConnectionData", INT2NUM(101));
1286
+ rb_define_const (EmModule, "ConnectionUnbound", INT2NUM(102));
1287
+ rb_define_const (EmModule, "ConnectionAccepted", INT2NUM(103));
1288
+ rb_define_const (EmModule, "ConnectionCompleted", INT2NUM(104));
1289
+ rb_define_const (EmModule, "LoopbreakSignalled", INT2NUM(105));
1290
+
1291
+ rb_define_const (EmModule, "ConnectionNotifyReadable", INT2NUM(106));
1292
+ rb_define_const (EmModule, "ConnectionNotifyWritable", INT2NUM(107));
1293
+
1294
+ rb_define_const (EmModule, "SslHandshakeCompleted", INT2NUM(108));
1295
+
1296
+ }
1297
+