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,52 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: binder.h
6
+ Date: 07Apr06
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
+ #ifndef __ObjectBindings__H_
21
+ #define __ObjectBindings__H_
22
+
23
+
24
+ #if __cplusplus >= 201103L
25
+ #define NO_EXCEPT_FALSE noexcept(false)
26
+ #else
27
+ #define NO_EXCEPT_FALSE
28
+ #endif
29
+
30
+ class Bindable_t
31
+ {
32
+ public:
33
+ static uintptr_t CreateBinding();
34
+ static Bindable_t *GetObject (const uintptr_t);
35
+ static std::map<uintptr_t, Bindable_t*> BindingBag;
36
+
37
+ public:
38
+ Bindable_t();
39
+ virtual ~Bindable_t() NO_EXCEPT_FALSE;
40
+
41
+ const uintptr_t GetBinding() {return Binding;}
42
+
43
+ private:
44
+ uintptr_t Binding;
45
+ };
46
+
47
+
48
+
49
+
50
+
51
+ #endif // __ObjectBindings__H_
52
+
@@ -0,0 +1,1046 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: cmain.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
+
22
+ /* 21Sep09: ruby 1.9 defines macros for common i/o functions that point to rb_w32_* implementations.
23
+ We need to undef the stat to fix a build failure in evma_send_file_data_to_connection.
24
+ See http://groups.google.com/group/eventmachine/browse_thread/thread/fc60d9bb738ffc71
25
+ */
26
+ #if defined(BUILD_FOR_RUBY) && defined(OS_WIN32)
27
+ #undef stat
28
+ #undef fstat
29
+ #endif
30
+
31
+ static EventMachine_t *EventMachine;
32
+ static Poller_t Poller = Poller_Default;
33
+
34
+ extern "C" void ensure_eventmachine (const char *caller = "unknown caller")
35
+ {
36
+ if (!EventMachine) {
37
+ const int err_size = 128;
38
+ char err_string[err_size];
39
+ snprintf (err_string, err_size, "eventmachine not initialized: %s", caller);
40
+ #ifdef BUILD_FOR_RUBY
41
+ rb_raise(rb_eRuntimeError, "%s", err_string);
42
+ #else
43
+ throw std::runtime_error (err_string);
44
+ #endif
45
+ }
46
+ }
47
+
48
+ /***********************
49
+ evma_initialize_library
50
+ ***********************/
51
+
52
+ extern "C" void evma_initialize_library (EMCallback cb)
53
+ {
54
+ if (EventMachine)
55
+ #ifdef BUILD_FOR_RUBY
56
+ rb_raise(rb_eRuntimeError, "eventmachine already initialized: evma_initialize_library");
57
+ #else
58
+ throw std::runtime_error ("eventmachine already initialized: evma_initialize_library");
59
+ #endif
60
+
61
+ EventMachine = new EventMachine_t (cb, Poller);
62
+ }
63
+
64
+
65
+ /********************
66
+ evma_release_library
67
+ ********************/
68
+
69
+ extern "C" void evma_release_library()
70
+ {
71
+ ensure_eventmachine("evma_release_library");
72
+ delete EventMachine;
73
+ EventMachine = NULL;
74
+ }
75
+
76
+
77
+ /*********************
78
+ evma_run_machine_once
79
+ *********************/
80
+
81
+ extern "C" bool evma_run_machine_once()
82
+ {
83
+ ensure_eventmachine("evma_run_machine_once");
84
+ return EventMachine->RunOnce();
85
+ }
86
+
87
+
88
+ /****************
89
+ evma_run_machine
90
+ ****************/
91
+
92
+ extern "C" void evma_run_machine()
93
+ {
94
+ ensure_eventmachine("evma_run_machine");
95
+ EventMachine->Run();
96
+ }
97
+
98
+ /********************************
99
+ evma_get_timer_count
100
+ ********************************/
101
+
102
+ extern "C" const size_t evma_get_timer_count ()
103
+ {
104
+ ensure_eventmachine("evma_get_timer_count");
105
+ return EventMachine->GetTimerCount();
106
+ }
107
+
108
+ /**************************
109
+ evma_install_oneshot_timer
110
+ **************************/
111
+
112
+ extern "C" const uintptr_t evma_install_oneshot_timer (uint64_t milliseconds)
113
+ {
114
+ ensure_eventmachine("evma_install_oneshot_timer");
115
+ return EventMachine->InstallOneshotTimer (milliseconds);
116
+ }
117
+
118
+
119
+ /**********************
120
+ evma_connect_to_server
121
+ **********************/
122
+
123
+ extern "C" const uintptr_t evma_connect_to_server (const char *bind_addr, int bind_port, const char *server, int port)
124
+ {
125
+ ensure_eventmachine("evma_connect_to_server");
126
+ return EventMachine->ConnectToServer (bind_addr, bind_port, server, port);
127
+ }
128
+
129
+ /***************************
130
+ evma_connect_to_unix_server
131
+ ***************************/
132
+
133
+ extern "C" const uintptr_t evma_connect_to_unix_server (const char *server)
134
+ {
135
+ ensure_eventmachine("evma_connect_to_unix_server");
136
+ return EventMachine->ConnectToUnixServer (server);
137
+ }
138
+
139
+ /**************
140
+ evma_attach_fd
141
+ **************/
142
+
143
+ extern "C" const uintptr_t evma_attach_fd (int file_descriptor, int watch_mode)
144
+ {
145
+ ensure_eventmachine("evma_attach_fd");
146
+ return EventMachine->AttachFD (file_descriptor, watch_mode ? true : false);
147
+ }
148
+
149
+ /**************
150
+ evma_detach_fd
151
+ **************/
152
+
153
+ extern "C" int evma_detach_fd (const uintptr_t binding)
154
+ {
155
+ ensure_eventmachine("evma_detach_fd");
156
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
157
+ if (ed)
158
+ return EventMachine->DetachFD (ed);
159
+ else
160
+ #ifdef BUILD_FOR_RUBY
161
+ rb_raise(rb_eRuntimeError, "invalid binding to detach");
162
+ #else
163
+ throw std::runtime_error ("invalid binding to detach");
164
+ #endif
165
+ return -1;
166
+ }
167
+
168
+ /************************
169
+ evma_get_file_descriptor
170
+ ************************/
171
+
172
+ extern "C" int evma_get_file_descriptor (const uintptr_t binding)
173
+ {
174
+ ensure_eventmachine("evma_get_file_descriptor");
175
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
176
+ if (ed)
177
+ return ed->GetSocket();
178
+ else
179
+ #ifdef BUILD_FOR_RUBY
180
+ rb_raise(rb_eRuntimeError, "invalid binding to get_fd");
181
+ #else
182
+ throw std::runtime_error ("invalid binding to get_fd");
183
+ #endif
184
+ return -1;
185
+ }
186
+
187
+ /***********************
188
+ evma_is_notify_readable
189
+ ***********************/
190
+
191
+ extern "C" int evma_is_notify_readable (const uintptr_t binding)
192
+ {
193
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
194
+ if (cd)
195
+ return cd->IsNotifyReadable() ? 1 : 0;
196
+ return -1;
197
+ }
198
+
199
+ /************************
200
+ evma_set_notify_readable
201
+ ************************/
202
+
203
+ extern "C" void evma_set_notify_readable (const uintptr_t binding, int mode)
204
+ {
205
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
206
+ if (cd)
207
+ cd->SetNotifyReadable (mode ? true : false);
208
+ }
209
+
210
+ /***********************
211
+ evma_is_notify_writable
212
+ ***********************/
213
+
214
+ extern "C" int evma_is_notify_writable (const uintptr_t binding)
215
+ {
216
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
217
+ if (cd)
218
+ return cd->IsNotifyWritable() ? 1 : 0;
219
+ return -1;
220
+ }
221
+
222
+ /************************
223
+ evma_set_notify_writable
224
+ ************************/
225
+
226
+ extern "C" void evma_set_notify_writable (const uintptr_t binding, int mode)
227
+ {
228
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
229
+ if (cd)
230
+ cd->SetNotifyWritable (mode ? true : false);
231
+ }
232
+
233
+ /**********
234
+ evma_pause
235
+ **********/
236
+
237
+ extern "C" int evma_pause (const uintptr_t binding)
238
+ {
239
+ EventableDescriptor *cd = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
240
+ if (cd)
241
+ return cd->Pause() ? 1 : 0;
242
+
243
+ return 0;
244
+ }
245
+
246
+ /***********
247
+ evma_resume
248
+ ***********/
249
+
250
+ extern "C" int evma_resume (const uintptr_t binding)
251
+ {
252
+ EventableDescriptor *cd = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
253
+ if (cd)
254
+ return cd->Resume() ? 1 : 0;
255
+
256
+ return 0;
257
+ }
258
+
259
+ /**************
260
+ evma_is_paused
261
+ **************/
262
+
263
+ extern "C" int evma_is_paused (const uintptr_t binding)
264
+ {
265
+ EventableDescriptor *cd = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
266
+ if (cd)
267
+ return cd->IsPaused() ? 1 : 0;
268
+
269
+ return 0;
270
+ }
271
+
272
+ /************************
273
+ evma_num_close_scheduled
274
+ ************************/
275
+
276
+ extern "C" int evma_num_close_scheduled ()
277
+ {
278
+ ensure_eventmachine("evma_num_close_scheduled");
279
+ return EventMachine->NumCloseScheduled;
280
+ }
281
+
282
+ /**********************
283
+ evma_create_tcp_server
284
+ **********************/
285
+
286
+ extern "C" const uintptr_t evma_create_tcp_server (const char *address, int port)
287
+ {
288
+ ensure_eventmachine("evma_create_tcp_server");
289
+ return EventMachine->CreateTcpServer (address, port);
290
+ }
291
+
292
+ /******************************
293
+ evma_create_unix_domain_server
294
+ ******************************/
295
+
296
+ extern "C" const uintptr_t evma_create_unix_domain_server (const char *filename)
297
+ {
298
+ ensure_eventmachine("evma_create_unix_domain_server");
299
+ return EventMachine->CreateUnixDomainServer (filename);
300
+ }
301
+
302
+ /***********************
303
+ evma_attach_sd
304
+ ************************/
305
+
306
+ extern "C" const uintptr_t evma_attach_sd (int sd)
307
+ {
308
+ ensure_eventmachine("evma_attach_sd");
309
+ return EventMachine->AttachSD (sd);
310
+ }
311
+
312
+ /*************************
313
+ evma_open_datagram_socket
314
+ *************************/
315
+
316
+ extern "C" const uintptr_t evma_open_datagram_socket (const char *address, int port)
317
+ {
318
+ ensure_eventmachine("evma_open_datagram_socket");
319
+ return EventMachine->OpenDatagramSocket (address, port);
320
+ }
321
+
322
+ /******************
323
+ evma_open_keyboard
324
+ ******************/
325
+
326
+ extern "C" const uintptr_t evma_open_keyboard()
327
+ {
328
+ ensure_eventmachine("evma_open_keyboard");
329
+ return EventMachine->OpenKeyboard();
330
+ }
331
+
332
+ /*******************
333
+ evma_watch_filename
334
+ *******************/
335
+
336
+ extern "C" const uintptr_t evma_watch_filename (const char *fname)
337
+ {
338
+ ensure_eventmachine("evma_watch_filename");
339
+ return EventMachine->WatchFile(fname);
340
+ }
341
+
342
+ /*********************
343
+ evma_unwatch_filename
344
+ *********************/
345
+
346
+ extern "C" void evma_unwatch_filename (const uintptr_t sig)
347
+ {
348
+ ensure_eventmachine("evma_unwatch_file");
349
+ EventMachine->UnwatchFile(sig);
350
+ }
351
+
352
+ /**************
353
+ evma_watch_pid
354
+ **************/
355
+
356
+ extern "C" const uintptr_t evma_watch_pid (int pid)
357
+ {
358
+ ensure_eventmachine("evma_watch_pid");
359
+ return EventMachine->WatchPid(pid);
360
+ }
361
+
362
+ /****************
363
+ evma_unwatch_pid
364
+ ****************/
365
+
366
+ extern "C" void evma_unwatch_pid (const uintptr_t sig)
367
+ {
368
+ ensure_eventmachine("evma_unwatch_pid");
369
+ EventMachine->UnwatchPid(sig);
370
+ }
371
+
372
+ /*****************
373
+ evma_is_watch_only
374
+ *****************/
375
+
376
+ extern "C" int evma_is_watch_only (const uintptr_t binding)
377
+ {
378
+ EventableDescriptor *cd = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
379
+ if (cd)
380
+ return cd->IsWatchOnly() ? 1 : 0;
381
+ return -1;
382
+ }
383
+
384
+ /****************************
385
+ evma_send_data_to_connection
386
+ ****************************/
387
+
388
+ extern "C" int evma_send_data_to_connection (const uintptr_t binding, const char *data, int data_length)
389
+ {
390
+ ensure_eventmachine("evma_send_data_to_connection");
391
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
392
+ if (ed)
393
+ return ed->SendOutboundData(data, data_length);
394
+ return -1;
395
+ }
396
+
397
+ /******************
398
+ evma_send_datagram
399
+ ******************/
400
+
401
+ extern "C" int evma_send_datagram (const uintptr_t binding, const char *data, int data_length, const char *address, int port)
402
+ {
403
+ ensure_eventmachine("evma_send_datagram");
404
+ DatagramDescriptor *dd = dynamic_cast <DatagramDescriptor*> (Bindable_t::GetObject (binding));
405
+ if (dd)
406
+ return dd->SendOutboundDatagram(data, data_length, address, port);
407
+ return -1;
408
+ }
409
+
410
+
411
+ /*********************
412
+ evma_close_connection
413
+ *********************/
414
+
415
+ extern "C" void evma_close_connection (const uintptr_t binding, int after_writing)
416
+ {
417
+ ensure_eventmachine("evma_close_connection");
418
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
419
+ if (ed)
420
+ ed->ScheduleClose (after_writing ? true : false);
421
+ }
422
+
423
+ /***********************************
424
+ evma_report_connection_error_status
425
+ ***********************************/
426
+
427
+ extern "C" int evma_report_connection_error_status (const uintptr_t binding)
428
+ {
429
+ ensure_eventmachine("evma_report_connection_error_status");
430
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
431
+ if (ed)
432
+ return ed->ReportErrorStatus();
433
+ return -1;
434
+ }
435
+
436
+ /********************
437
+ evma_stop_tcp_server
438
+ ********************/
439
+
440
+ extern "C" void evma_stop_tcp_server (const uintptr_t binding)
441
+ {
442
+ ensure_eventmachine("evma_stop_tcp_server");
443
+ AcceptorDescriptor::StopAcceptor (binding);
444
+ }
445
+
446
+
447
+ /*****************
448
+ evma_stop_machine
449
+ *****************/
450
+
451
+ extern "C" void evma_stop_machine()
452
+ {
453
+ ensure_eventmachine("evma_stop_machine");
454
+ EventMachine->ScheduleHalt();
455
+ }
456
+
457
+ /*****************
458
+ evma_stopping
459
+ *****************/
460
+
461
+ extern "C" bool evma_stopping()
462
+ {
463
+ ensure_eventmachine("evma_stopping");
464
+ return EventMachine->Stopping();
465
+ }
466
+
467
+ /**************
468
+ evma_start_tls
469
+ **************/
470
+
471
+ extern "C" void evma_start_tls (const uintptr_t binding)
472
+ {
473
+ ensure_eventmachine("evma_start_tls");
474
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
475
+ if (ed)
476
+ ed->StartTls();
477
+ }
478
+
479
+ /******************
480
+ evma_set_tls_parms
481
+ ******************/
482
+
483
+ extern "C" void evma_set_tls_parms (const uintptr_t binding, const char *privatekey_filename, const char *certchain_filename, int verify_peer, int fail_if_no_peer_cert, const char *sni_hostname, const char *cipherlist, const char *ecdh_curve, const char *dhparam, int ssl_version)
484
+ {
485
+ ensure_eventmachine("evma_set_tls_parms");
486
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
487
+ if (ed)
488
+ ed->SetTlsParms (privatekey_filename, certchain_filename, (verify_peer == 1 ? true : false), (fail_if_no_peer_cert == 1 ? true : false), sni_hostname, cipherlist, ecdh_curve, dhparam, ssl_version);
489
+ }
490
+
491
+ /******************
492
+ evma_get_peer_cert
493
+ ******************/
494
+
495
+ #ifdef WITH_SSL
496
+ extern "C" X509 *evma_get_peer_cert (const uintptr_t binding)
497
+ {
498
+ ensure_eventmachine("evma_get_peer_cert");
499
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
500
+ if (ed)
501
+ return ed->GetPeerCert();
502
+ return NULL;
503
+ }
504
+ #endif
505
+
506
+ /******************
507
+ evma_get_cipher_bits
508
+ ******************/
509
+
510
+ #ifdef WITH_SSL
511
+ extern "C" int evma_get_cipher_bits (const uintptr_t binding)
512
+ {
513
+ ensure_eventmachine("evma_get_cipher_bits");
514
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
515
+ if (ed)
516
+ return ed->GetCipherBits();
517
+ return -1;
518
+ }
519
+ #endif
520
+
521
+ /******************
522
+ evma_get_cipher_name
523
+ ******************/
524
+
525
+ #ifdef WITH_SSL
526
+ extern "C" const char *evma_get_cipher_name (const uintptr_t binding)
527
+ {
528
+ ensure_eventmachine("evma_get_cipher_name");
529
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
530
+ if (ed)
531
+ return ed->GetCipherName();
532
+ return NULL;
533
+ }
534
+ #endif
535
+
536
+ /******************
537
+ evma_get_cipher_protocol
538
+ ******************/
539
+
540
+ #ifdef WITH_SSL
541
+ extern "C" const char *evma_get_cipher_protocol (const uintptr_t binding)
542
+ {
543
+ ensure_eventmachine("evma_get_cipher_protocol");
544
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
545
+ if (ed)
546
+ return ed->GetCipherProtocol();
547
+ return NULL;
548
+ }
549
+ #endif
550
+
551
+ /******************
552
+ evma_get_sni_hostname
553
+ ******************/
554
+
555
+ #ifdef WITH_SSL
556
+ extern "C" const char *evma_get_sni_hostname (const uintptr_t binding)
557
+ {
558
+ ensure_eventmachine("evma_get_sni_hostname");
559
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
560
+ if (ed)
561
+ return ed->GetSNIHostname();
562
+ return NULL;
563
+ }
564
+ #endif
565
+
566
+ /********************
567
+ evma_accept_ssl_peer
568
+ ********************/
569
+
570
+ #ifdef WITH_SSL
571
+ extern "C" void evma_accept_ssl_peer (const uintptr_t binding)
572
+ {
573
+ ensure_eventmachine("evma_accept_ssl_peer");
574
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
575
+ if (cd)
576
+ cd->AcceptSslPeer();
577
+ }
578
+ #endif
579
+
580
+ /*****************
581
+ evma_get_peername
582
+ *****************/
583
+
584
+ extern "C" int evma_get_peername (const uintptr_t binding, struct sockaddr *sa, socklen_t *len)
585
+ {
586
+ ensure_eventmachine("evma_get_peername");
587
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
588
+ if (ed) {
589
+ return ed->GetPeername (sa, len) ? 1 : 0;
590
+ }
591
+ else
592
+ return 0;
593
+ }
594
+
595
+ /*****************
596
+ evma_get_sockname
597
+ *****************/
598
+
599
+ extern "C" int evma_get_sockname (const uintptr_t binding, struct sockaddr *sa, socklen_t *len)
600
+ {
601
+ ensure_eventmachine("evma_get_sockname");
602
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
603
+ if (ed) {
604
+ return ed->GetSockname (sa, len) ? 1 : 0;
605
+ }
606
+ else
607
+ return 0;
608
+ }
609
+
610
+ /***********************
611
+ evma_get_subprocess_pid
612
+ ***********************/
613
+
614
+ #ifdef OS_UNIX
615
+ extern "C" int evma_get_subprocess_pid (const uintptr_t binding, pid_t *pid)
616
+ {
617
+ ensure_eventmachine("evma_get_subprocess_pid");
618
+ PipeDescriptor *pd = dynamic_cast <PipeDescriptor*> (Bindable_t::GetObject (binding));
619
+ if (pd) {
620
+ return pd->GetSubprocessPid (pid) ? 1 : 0;
621
+ }
622
+ else if (pid && EventMachine->SubprocessPid) {
623
+ *pid = EventMachine->SubprocessPid;
624
+ return 1;
625
+ }
626
+ else
627
+ return 0;
628
+ }
629
+ #else
630
+ extern "C" int evma_get_subprocess_pid (const uintptr_t binding UNUSED, pid_t *pid UNUSED)
631
+ {
632
+ return 0;
633
+ }
634
+ #endif
635
+
636
+ /**************************
637
+ evma_get_subprocess_status
638
+ **************************/
639
+
640
+ extern "C" int evma_get_subprocess_status (const uintptr_t binding UNUSED, int *status)
641
+ {
642
+ ensure_eventmachine("evma_get_subprocess_status");
643
+ if (status) {
644
+ *status = EventMachine->SubprocessExitStatus;
645
+ return 1;
646
+ }
647
+ else
648
+ return 0;
649
+ }
650
+
651
+ /*************************
652
+ evma_get_connection_count
653
+ *************************/
654
+
655
+ extern "C" int evma_get_connection_count()
656
+ {
657
+ ensure_eventmachine("evma_get_connection_count");
658
+ return EventMachine->GetConnectionCount();
659
+ }
660
+
661
+ /*********************
662
+ evma_signal_loopbreak
663
+ *********************/
664
+
665
+ extern "C" void evma_signal_loopbreak()
666
+ {
667
+ ensure_eventmachine("evma_signal_loopbreak");
668
+ EventMachine->SignalLoopBreaker();
669
+ }
670
+
671
+
672
+
673
+ /********************************
674
+ evma_get_comm_inactivity_timeout
675
+ ********************************/
676
+
677
+ extern "C" float evma_get_comm_inactivity_timeout (const uintptr_t binding)
678
+ {
679
+ ensure_eventmachine("evma_get_comm_inactivity_timeout");
680
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
681
+ if (ed) {
682
+ return ((float)ed->GetCommInactivityTimeout() / 1000);
683
+ }
684
+ else
685
+ return 0.0; //Perhaps this should be an exception. Access to an unknown binding.
686
+ }
687
+
688
+ /********************************
689
+ evma_set_comm_inactivity_timeout
690
+ ********************************/
691
+
692
+ extern "C" int evma_set_comm_inactivity_timeout (const uintptr_t binding, float value)
693
+ {
694
+ ensure_eventmachine("evma_set_comm_inactivity_timeout");
695
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
696
+ if (ed) {
697
+ return ed->SetCommInactivityTimeout ((uint64_t)(value * 1000));
698
+ }
699
+ else
700
+ return 0; //Perhaps this should be an exception. Access to an unknown binding.
701
+ }
702
+
703
+
704
+ /********************************
705
+ evma_get_pending_connect_timeout
706
+ ********************************/
707
+
708
+ extern "C" float evma_get_pending_connect_timeout (const uintptr_t binding)
709
+ {
710
+ ensure_eventmachine("evma_get_pending_connect_timeout");
711
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
712
+ if (ed) {
713
+ return ((float)ed->GetPendingConnectTimeout() / 1000);
714
+ }
715
+ else
716
+ return 0.0;
717
+ }
718
+
719
+
720
+ /********************************
721
+ evma_set_pending_connect_timeout
722
+ ********************************/
723
+
724
+ extern "C" int evma_set_pending_connect_timeout (const uintptr_t binding, float value)
725
+ {
726
+ ensure_eventmachine("evma_set_pending_connect_timeout");
727
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
728
+ if (ed) {
729
+ return ed->SetPendingConnectTimeout ((uint64_t)(value * 1000));
730
+ }
731
+ else
732
+ return 0;
733
+ }
734
+
735
+
736
+ /**********************
737
+ evma_set_timer_quantum
738
+ **********************/
739
+
740
+ extern "C" void evma_set_timer_quantum (int interval)
741
+ {
742
+ ensure_eventmachine("evma_set_timer_quantum");
743
+ EventMachine->SetTimerQuantum (interval);
744
+ }
745
+
746
+
747
+ /************************
748
+ evma_get_max_timer_count
749
+ ************************/
750
+
751
+ extern "C" int evma_get_max_timer_count()
752
+ {
753
+ return EventMachine_t::GetMaxTimerCount();
754
+ }
755
+
756
+ /************************
757
+ evma_set_max_timer_count
758
+ ************************/
759
+
760
+ extern "C" void evma_set_max_timer_count (int ct)
761
+ {
762
+ // This may only be called if the reactor is not running.
763
+
764
+ if (EventMachine)
765
+ #ifdef BUILD_FOR_RUBY
766
+ rb_raise(rb_eRuntimeError, "eventmachine already initialized: evma_set_max_timer_count");
767
+ #else
768
+ throw std::runtime_error ("eventmachine already initialized: evma_set_max_timer_count");
769
+ #endif
770
+ EventMachine_t::SetMaxTimerCount (ct);
771
+ }
772
+
773
+ /******************
774
+ evma_get/set_simultaneous_accept_count
775
+ ******************/
776
+
777
+ extern "C" void evma_set_simultaneous_accept_count (int count)
778
+ {
779
+ EventMachine_t::SetSimultaneousAcceptCount(count);
780
+ }
781
+
782
+ extern "C" int evma_get_simultaneous_accept_count()
783
+ {
784
+ return EventMachine_t::GetSimultaneousAcceptCount();
785
+ }
786
+
787
+
788
+ /******************
789
+ evma_setuid_string
790
+ ******************/
791
+
792
+ extern "C" void evma_setuid_string (const char *username)
793
+ {
794
+ // We do NOT need to be running an EM instance because this method is static.
795
+ EventMachine_t::SetuidString (username);
796
+ }
797
+
798
+
799
+ /**********
800
+ evma_popen
801
+ **********/
802
+
803
+ extern "C" const uintptr_t evma_popen (char * const*cmd_strings)
804
+ {
805
+ ensure_eventmachine("evma_popen");
806
+ return EventMachine->Socketpair (cmd_strings);
807
+ }
808
+
809
+
810
+ /***************************
811
+ evma_get_outbound_data_size
812
+ ***************************/
813
+
814
+ extern "C" int evma_get_outbound_data_size (const uintptr_t binding)
815
+ {
816
+ ensure_eventmachine("evma_get_outbound_data_size");
817
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
818
+ return ed ? ed->GetOutboundDataSize() : 0;
819
+ }
820
+
821
+ /*********************
822
+ evma_enable_keepalive
823
+ *********************/
824
+
825
+ extern "C" int evma_enable_keepalive (const uintptr_t binding, int idle, int intvl, int cnt)
826
+ {
827
+ ensure_eventmachine("evma_enable_keepalive");
828
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
829
+ if (ed)
830
+ return ed->EnableKeepalive(idle, intvl, cnt);
831
+ else
832
+ #ifdef BUILD_FOR_RUBY
833
+ rb_raise(rb_eRuntimeError, "invalid binding to enable keepalive");
834
+ #else
835
+ throw std::runtime_error ("invalid binding to enable keepalive");
836
+ #endif
837
+ return -1;
838
+ }
839
+
840
+ /**********************
841
+ evma_disable_keepalive
842
+ **********************/
843
+
844
+ extern "C" int evma_disable_keepalive (const uintptr_t binding)
845
+ {
846
+ ensure_eventmachine("evma_disable_keepalive");
847
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
848
+ if (ed)
849
+ return ed->DisableKeepalive();
850
+ else
851
+ #ifdef BUILD_FOR_RUBY
852
+ rb_raise(rb_eRuntimeError, "invalid binding to enable keepalive");
853
+ #else
854
+ throw std::runtime_error ("invalid binding to enable keepalive");
855
+ #endif
856
+ return -1;
857
+ }
858
+
859
+ /**************
860
+ evma_set_epoll
861
+ **************/
862
+
863
+ extern "C" void evma_set_epoll (int use)
864
+ {
865
+ if (use)
866
+ Poller = Poller_Epoll;
867
+ else
868
+ Poller = Poller_Default;
869
+ }
870
+
871
+ /***************
872
+ evma_set_kqueue
873
+ ***************/
874
+
875
+ extern "C" void evma_set_kqueue (int use)
876
+ {
877
+ if (use)
878
+ Poller = Poller_Kqueue;
879
+ else
880
+ Poller = Poller_Default;
881
+ }
882
+
883
+
884
+ /**********************
885
+ evma_set_rlimit_nofile
886
+ **********************/
887
+
888
+ extern "C" int evma_set_rlimit_nofile (int nofiles)
889
+ {
890
+ return EventMachine_t::SetRlimitNofile (nofiles);
891
+ }
892
+
893
+
894
+ /*********************************
895
+ evma_send_file_data_to_connection
896
+ *********************************/
897
+
898
+ extern "C" int evma_send_file_data_to_connection (const uintptr_t binding, const char *filename)
899
+ {
900
+ /* This is a sugaring over send_data_to_connection that reads a file into a
901
+ * locally-allocated buffer, and sends the file data to the remote peer.
902
+ * Return the number of bytes written to the caller.
903
+ * TODO, needs to impose a limit on the file size. This is intended only for
904
+ * small files. (I don't know, maybe 8K or less.) For larger files, use interleaved
905
+ * I/O to avoid slowing the rest of the system down.
906
+ * TODO: we should return a code rather than barf, in case of file-not-found.
907
+ * TODO, does this compile on Windows?
908
+ * TODO, given that we want this to work only with small files, how about allocating
909
+ * the buffer on the stack rather than the heap?
910
+ *
911
+ * Modified 25Jul07. This now returns -1 on file-too-large; 0 for success, and a positive
912
+ * errno in case of other errors.
913
+ *
914
+ * Contributed by Kirk Haines.
915
+ */
916
+
917
+ char data[32*1024];
918
+ int r;
919
+
920
+ ensure_eventmachine("evma_send_file_data_to_connection");
921
+
922
+ #if defined(OS_WIN32)
923
+ int Fd = open (filename, O_RDONLY|O_BINARY);
924
+ #else
925
+ int Fd = open (filename, O_RDONLY);
926
+ #endif
927
+ if (Fd < 0)
928
+ return errno;
929
+ // From here on, all early returns MUST close Fd.
930
+
931
+ struct stat st;
932
+ if (fstat (Fd, &st)) {
933
+ int e = errno;
934
+ close (Fd);
935
+ return e;
936
+ }
937
+
938
+ off_t filesize = st.st_size;
939
+ if (filesize <= 0) {
940
+ close (Fd);
941
+ return 0;
942
+ }
943
+ else if (filesize > (off_t) sizeof(data)) {
944
+ close (Fd);
945
+ return -1;
946
+ }
947
+
948
+ r = read (Fd, data, filesize);
949
+ if (r != filesize) {
950
+ int e = errno;
951
+ close (Fd);
952
+ return e;
953
+ }
954
+ evma_send_data_to_connection (binding, data, r);
955
+ close (Fd);
956
+
957
+ return 0;
958
+ }
959
+
960
+
961
+ /****************
962
+ evma_start_proxy
963
+ *****************/
964
+
965
+ extern "C" void evma_start_proxy (const uintptr_t from, const uintptr_t to, const unsigned long bufsize, const unsigned long length)
966
+ {
967
+ ensure_eventmachine("evma_start_proxy");
968
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (from));
969
+ if (ed)
970
+ ed->StartProxy(to, bufsize, length);
971
+ }
972
+
973
+
974
+ /***************
975
+ evma_stop_proxy
976
+ ****************/
977
+
978
+ extern "C" void evma_stop_proxy (const uintptr_t from)
979
+ {
980
+ ensure_eventmachine("evma_stop_proxy");
981
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (from));
982
+ if (ed)
983
+ ed->StopProxy();
984
+ }
985
+
986
+ /******************
987
+ evma_proxied_bytes
988
+ *******************/
989
+
990
+ extern "C" unsigned long evma_proxied_bytes (const uintptr_t from)
991
+ {
992
+ ensure_eventmachine("evma_proxied_bytes");
993
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (from));
994
+ if (ed)
995
+ return ed->GetProxiedBytes();
996
+ else
997
+ return 0;
998
+ }
999
+
1000
+
1001
+ /***************************
1002
+ evma_get_last_activity_time
1003
+ ****************************/
1004
+
1005
+ extern "C" uint64_t evma_get_last_activity_time(const uintptr_t from)
1006
+ {
1007
+ ensure_eventmachine("evma_get_last_activity_time");
1008
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (from));
1009
+ if (ed)
1010
+ return ed->GetLastActivity();
1011
+ else
1012
+ return 0;
1013
+ }
1014
+
1015
+
1016
+ /***************************
1017
+ evma_get_heartbeat_interval
1018
+ ****************************/
1019
+
1020
+ extern "C" float evma_get_heartbeat_interval()
1021
+ {
1022
+ ensure_eventmachine("evma_get_heartbeat_interval");
1023
+ return EventMachine->GetHeartbeatInterval();
1024
+ }
1025
+
1026
+
1027
+ /***************************
1028
+ evma_set_heartbeat_interval
1029
+ ****************************/
1030
+
1031
+ extern "C" int evma_set_heartbeat_interval(float interval)
1032
+ {
1033
+ ensure_eventmachine("evma_set_heartbeat_interval");
1034
+ return EventMachine->SetHeartbeatInterval(interval);
1035
+ }
1036
+
1037
+
1038
+ /**************************
1039
+ evma_get_current_loop_time
1040
+ ***************************/
1041
+
1042
+ extern "C" uint64_t evma_get_current_loop_time()
1043
+ {
1044
+ ensure_eventmachine("evma_get_current_loop_time");
1045
+ return EventMachine->GetCurrentLoopTime();
1046
+ }