eventmachine-maglev- 0.12.10 → 1.0.0.beta.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. data/.gitignore +7 -0
  2. data/.yardopts +7 -0
  3. data/Gemfile +3 -0
  4. data/README.md +109 -0
  5. data/Rakefile +14 -368
  6. data/docs/DocumentationGuidesIndex.md +27 -0
  7. data/docs/GettingStarted.md +521 -0
  8. data/docs/old/DEFERRABLES +246 -0
  9. data/docs/{KEYBOARD → old/KEYBOARD} +15 -11
  10. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  11. data/docs/old/SMTP +4 -0
  12. data/docs/old/SPAWNED_PROCESSES +148 -0
  13. data/eventmachine.gemspec +20 -26
  14. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  15. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  16. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  17. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  18. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  19. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  20. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  21. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  22. data/examples/{ex_channel.rb → old/ex_channel.rb} +3 -3
  23. data/examples/old/ex_tick_loop_array.rb +15 -0
  24. data/examples/old/ex_tick_loop_counter.rb +32 -0
  25. data/ext/binder.cpp +0 -1
  26. data/ext/cmain.cpp +40 -29
  27. data/ext/ed.cpp +189 -134
  28. data/ext/ed.h +34 -40
  29. data/ext/em.cpp +388 -340
  30. data/ext/em.h +29 -32
  31. data/ext/eventmachine.h +7 -6
  32. data/ext/extconf.rb +57 -48
  33. data/ext/fastfilereader/extconf.rb +5 -3
  34. data/ext/fastfilereader/mapper.cpp +1 -1
  35. data/ext/fastfilereader/rubymain.cpp +0 -1
  36. data/ext/kb.cpp +1 -3
  37. data/ext/pipe.cpp +9 -11
  38. data/ext/project.h +12 -8
  39. data/ext/rubymain.cpp +158 -112
  40. data/java/src/com/rubyeventmachine/EmReactor.java +3 -2
  41. data/lib/em/buftok.rb +35 -63
  42. data/lib/em/callback.rb +43 -11
  43. data/lib/em/channel.rb +22 -15
  44. data/lib/em/completion.rb +303 -0
  45. data/lib/em/connection.rb +341 -208
  46. data/lib/em/deferrable/pool.rb +2 -0
  47. data/lib/em/deferrable.rb +20 -2
  48. data/lib/em/file_watch.rb +37 -18
  49. data/lib/em/iterator.rb +270 -0
  50. data/lib/em/pool.rb +146 -0
  51. data/lib/em/process_watch.rb +5 -4
  52. data/lib/em/processes.rb +8 -4
  53. data/lib/em/protocols/httpclient.rb +27 -11
  54. data/lib/em/protocols/httpclient2.rb +15 -5
  55. data/lib/em/protocols/line_protocol.rb +29 -0
  56. data/lib/em/protocols/memcache.rb +17 -9
  57. data/lib/em/protocols/object_protocol.rb +2 -1
  58. data/lib/em/protocols/postgres3.rb +2 -1
  59. data/lib/em/protocols/smtpclient.rb +19 -11
  60. data/lib/em/protocols/smtpserver.rb +101 -8
  61. data/lib/em/protocols/stomp.rb +9 -7
  62. data/lib/em/protocols/tcptest.rb +3 -2
  63. data/lib/em/protocols.rb +1 -1
  64. data/lib/{pr_eventmachine.rb → em/pure_ruby.rb} +188 -205
  65. data/lib/em/queue.rb +23 -13
  66. data/lib/em/resolver.rb +192 -0
  67. data/lib/em/spawnable.rb +9 -10
  68. data/lib/em/streamer.rb +34 -46
  69. data/lib/em/threaded_resource.rb +90 -0
  70. data/lib/em/tick_loop.rb +85 -0
  71. data/lib/em/timers.rb +8 -3
  72. data/lib/em/version.rb +1 -1
  73. data/lib/eventmachine.rb +582 -686
  74. data/lib/jeventmachine.rb +25 -3
  75. data/tasks/package.rake +98 -0
  76. data/tasks/test.rake +8 -0
  77. data/tests/em_test_helper.rb +64 -0
  78. data/tests/test_attach.rb +56 -56
  79. data/tests/test_basic.rb +111 -168
  80. data/tests/test_channel.rb +5 -6
  81. data/tests/test_completion.rb +177 -0
  82. data/tests/test_connection_count.rb +1 -3
  83. data/tests/test_defer.rb +3 -32
  84. data/tests/test_deferrable.rb +35 -0
  85. data/tests/test_epoll.rb +27 -57
  86. data/tests/test_error_handler.rb +10 -7
  87. data/tests/test_exc.rb +6 -33
  88. data/tests/test_file_watch.rb +51 -35
  89. data/tests/test_futures.rb +10 -38
  90. data/tests/test_get_sock_opt.rb +27 -20
  91. data/tests/test_handler_check.rb +1 -3
  92. data/tests/test_hc.rb +49 -112
  93. data/tests/test_httpclient.rb +34 -62
  94. data/tests/test_httpclient2.rb +14 -39
  95. data/tests/test_inactivity_timeout.rb +44 -40
  96. data/tests/test_kb.rb +26 -52
  97. data/tests/test_ltp.rb +27 -71
  98. data/tests/test_ltp2.rb +1 -30
  99. data/tests/test_next_tick.rb +2 -31
  100. data/tests/test_object_protocol.rb +8 -9
  101. data/tests/test_pause.rb +45 -37
  102. data/tests/test_pending_connect_timeout.rb +42 -38
  103. data/tests/test_pool.rb +128 -0
  104. data/tests/test_process_watch.rb +37 -37
  105. data/tests/test_processes.rb +92 -110
  106. data/tests/test_proxy_connection.rb +137 -61
  107. data/tests/test_pure.rb +30 -67
  108. data/tests/test_queue.rb +10 -4
  109. data/tests/test_resolver.rb +55 -0
  110. data/tests/test_running.rb +1 -29
  111. data/tests/test_sasl.rb +8 -33
  112. data/tests/test_send_file.rb +163 -188
  113. data/tests/test_servers.rb +12 -55
  114. data/tests/test_shutdown_hooks.rb +23 -0
  115. data/tests/test_smtpclient.rb +1 -29
  116. data/tests/test_smtpserver.rb +1 -29
  117. data/tests/test_spawn.rb +2 -31
  118. data/tests/test_ssl_args.rb +9 -10
  119. data/tests/test_ssl_methods.rb +1 -3
  120. data/tests/test_ssl_verify.rb +63 -63
  121. data/tests/test_threaded_resource.rb +53 -0
  122. data/tests/test_tick_loop.rb +59 -0
  123. data/tests/test_timers.rb +52 -91
  124. data/tests/test_ud.rb +1 -29
  125. data/tests/test_unbind_reason.rb +31 -0
  126. metadata +113 -70
  127. data/README +0 -82
  128. data/docs/DEFERRABLES +0 -133
  129. data/docs/LIGHTWEIGHT_CONCURRENCY +0 -70
  130. data/docs/SMTP +0 -2
  131. data/docs/SPAWNED_PROCESSES +0 -89
  132. data/ext/cplusplus.cpp +0 -202
  133. data/ext/emwin.cpp +0 -300
  134. data/ext/emwin.h +0 -94
  135. data/ext/epoll.cpp +0 -26
  136. data/ext/epoll.h +0 -25
  137. data/ext/eventmachine_cpp.h +0 -96
  138. data/ext/files.cpp +0 -94
  139. data/ext/files.h +0 -65
  140. data/ext/sigs.cpp +0 -89
  141. data/ext/sigs.h +0 -32
  142. data/java/src/com/rubyeventmachine/application/Application.java +0 -194
  143. data/java/src/com/rubyeventmachine/application/Connection.java +0 -74
  144. data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +0 -37
  145. data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +0 -46
  146. data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +0 -38
  147. data/java/src/com/rubyeventmachine/application/Timer.java +0 -54
  148. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +0 -109
  149. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +0 -148
  150. data/java/src/com/rubyeventmachine/tests/EMTest.java +0 -80
  151. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +0 -53
  152. data/java/src/com/rubyeventmachine/tests/TestServers.java +0 -75
  153. data/java/src/com/rubyeventmachine/tests/TestTimers.java +0 -90
  154. data/lib/evma/callback.rb +0 -32
  155. data/lib/evma/container.rb +0 -75
  156. data/lib/evma/factory.rb +0 -77
  157. data/lib/evma/protocol.rb +0 -87
  158. data/lib/evma/reactor.rb +0 -48
  159. data/lib/evma.rb +0 -32
  160. data/setup.rb +0 -1585
  161. data/tests/test_errors.rb +0 -82
  162. data/tests/testem.rb +0 -31
  163. data/web/whatis +0 -7
  164. /data/{docs/GNU → GNU} +0 -0
  165. /data/{docs/COPYING → LICENSE} +0 -0
  166. /data/docs/{ChangeLog → old/ChangeLog} +0 -0
  167. /data/docs/{EPOLL → old/EPOLL} +0 -0
  168. /data/docs/{INSTALL → old/INSTALL} +0 -0
  169. /data/docs/{LEGAL → old/LEGAL} +0 -0
  170. /data/docs/{PURE_RUBY → old/PURE_RUBY} +0 -0
  171. /data/docs/{RELEASE_NOTES → old/RELEASE_NOTES} +0 -0
  172. /data/docs/{TODO → old/TODO} +0 -0
  173. /data/examples/{ex_queue.rb → old/ex_queue.rb} +0 -0
  174. /data/examples/{helper.rb → old/helper.rb} +0 -0
data/lib/em/connection.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module EventMachine
2
- class FileNotFoundException < Exception # :nodoc:
2
+ class FileNotFoundException < Exception
3
3
  end
4
4
 
5
5
  # EventMachine::Connection is a class that is instantiated
@@ -8,7 +8,7 @@ module EventMachine
8
8
  # to a remote server or accepted locally from a remote client.)
9
9
  # When a Connection object is instantiated, it <i>mixes in</i>
10
10
  # the functionality contained in the user-defined module
11
- # specified in calls to EventMachine#connect or EventMachine#start_server.
11
+ # specified in calls to {EventMachine.connect} or {EventMachine.start_server}.
12
12
  # User-defined handler modules may redefine any or all of the standard
13
13
  # methods defined here, as well as add arbitrary additional code
14
14
  # that will also be mixed in.
@@ -22,21 +22,34 @@ module EventMachine
22
22
  #
23
23
  # This class is never instantiated by user code, and does not publish an
24
24
  # initialize method. The instance methods of EventMachine::Connection
25
- # which may be called by the event loop are: post_init, receive_data,
26
- # and unbind. All of the other instance methods defined here are called
27
- # only by user code.
25
+ # which may be called by the event loop are:
28
26
  #
27
+ # * {#post_init}
28
+ # * {#connection_completed}
29
+ # * {#receive_data}
30
+ # * {#unbind}
31
+ # * {#ssl_verify_peer} (if TLS is used)
32
+ # * {#ssl_handshake_completed}
33
+ #
34
+ # All of the other instance methods defined here are called only by user code.
35
+ #
36
+ # @see file:docs/GettingStarted.md EventMachine tutorial
29
37
  class Connection
30
- attr_accessor :signature # :nodoc:
38
+ # @private
39
+ attr_accessor :signature
40
+
41
+ # @private
42
+ alias original_method method
31
43
 
32
44
  # Override .new so subclasses don't have to call super and can ignore
33
45
  # connection-specific arguments
34
46
  #
35
- def self.new(sig, *args) #:nodoc:
47
+ # @private
48
+ def self.new(sig, *args)
36
49
  allocate.instance_eval do
37
50
  # Store signature
38
51
  @signature = sig
39
- associate_callback_target sig
52
+ # associate_callback_target sig
40
53
 
41
54
  # Call a superclass's #initialize if it has one
42
55
  initialize(*args)
@@ -50,28 +63,26 @@ module EventMachine
50
63
 
51
64
  # Stubbed initialize so legacy superclasses can safely call super
52
65
  #
53
- def initialize(*args) #:nodoc:
66
+ # @private
67
+ def initialize(*args)
54
68
  end
55
69
 
56
- # def associate_callback_target(sig) #:nodoc:
57
- # # no-op for the time being, to match similar no-op in rubymain.cpp
58
- # end
59
-
60
- # EventMachine::Connection#post_init is called by the event loop
61
- # immediately after the network connection has been established,
70
+ # Called by the event loop immediately after the network connection has been established,
62
71
  # and before resumption of the network loop.
63
72
  # This method is generally not called by user code, but is called automatically
64
73
  # by the event loop. The base-class implementation is a no-op.
65
74
  # This is a very good place to initialize instance variables that will
66
75
  # be used throughout the lifetime of the network connection.
67
76
  #
77
+ # @see #connection_completed
78
+ # @see #unbind
79
+ # @see #send_data
80
+ # @see #receive_data
68
81
  def post_init
69
82
  end
70
83
 
71
- # EventMachine::Connection#receive_data is called by the event loop
72
- # whenever data has been received by the network connection.
73
- # It is never called by user code.
74
- # receive_data is called with a single parameter, a String containing
84
+ # Called by the event loop whenever data has been received by the network connection.
85
+ # It is never called by user code. {#receive_data} is called with a single parameter, a String containing
75
86
  # the network protocol data, which may of course be binary. You will
76
87
  # generally redefine this method to perform your own processing of the incoming data.
77
88
  #
@@ -89,60 +100,140 @@ module EventMachine
89
100
  # in your redefined implementation of receive_data. For a better understanding
90
101
  # of this, read through the examples of specific protocol handlers in EventMachine::Protocols
91
102
  #
92
- # The base-class implementation of receive_data (which will be invoked if
93
- # you don't redefine it) simply prints the size of each incoming data packet
94
- # to stdout.
103
+ # The base-class implementation (which will be invoked only if you didn't override it in your protocol handler)
104
+ # simply prints incoming data packet size to stdout.
105
+ #
106
+ # @param [String] data Opaque incoming data.
107
+ # @note Depending on the protocol, buffer sizes and OS networking stack configuration, incoming data may or may not be "a complete message".
108
+ # It is up to this handler to detect content boundaries to determine whether all the content (for example, full HTTP request)
109
+ # has been received and can be processed.
95
110
  #
111
+ # @see #post_init
112
+ # @see #connection_completed
113
+ # @see #unbind
114
+ # @see #send_data
115
+ # @see file:docs/GettingStarted.md EventMachine tutorial
96
116
  def receive_data data
97
117
  puts "............>>>#{data.length}"
98
118
  end
99
119
 
100
- # #ssl_handshake_completed is called by EventMachine when the SSL/TLS handshake has
120
+ # Called by EventMachine when the SSL/TLS handshake has
101
121
  # been completed, as a result of calling #start_tls to initiate SSL/TLS on the connection.
102
122
  #
103
- # This callback exists because #post_init and #connection_completed are <b>not</b> reliable
123
+ # This callback exists because {#post_init} and {#connection_completed} are **not** reliable
104
124
  # for indicating when an SSL/TLS connection is ready to have it's certificate queried for.
105
125
  #
106
- # See #get_peer_cert for application and example.
126
+ # @see #get_peer_cert
107
127
  def ssl_handshake_completed
108
128
  end
109
129
 
110
- # #ssl_verify_peer is called by EventMachine when :verify_peer => true has been passed to #start_tls.
130
+ # Called by EventMachine when :verify_peer => true has been passed to {#start_tls}.
111
131
  # It will be called with each certificate in the certificate chain provided by the remote peer.
112
- # The cert will be passed as a String in PEM format, the same as in #get_peer_cert. It is up to user defined
132
+ #
133
+ # The cert will be passed as a String in PEM format, the same as in {#get_peer_cert}. It is up to user defined
113
134
  # code to perform a check on the certificates. The return value from this callback is used to accept or deny the peer.
114
135
  # A return value that is not nil or false triggers acceptance. If the peer is not accepted, the connection
115
- # will be subsequently closed. See 'tests/test_ssl_verify.rb' for a simple example.
136
+ # will be subsequently closed.
137
+ #
138
+ # @example This server always accepts all peers
139
+ #
140
+ # module AcceptServer
141
+ # def post_init
142
+ # start_tls(:verify_peer => true)
143
+ # end
144
+ #
145
+ # def ssl_verify_peer(cert)
146
+ # true
147
+ # end
148
+ #
149
+ # def ssl_handshake_completed
150
+ # $server_handshake_completed = true
151
+ # end
152
+ # end
153
+ #
154
+ #
155
+ # @example This server never accepts any peers
156
+ #
157
+ # module DenyServer
158
+ # def post_init
159
+ # start_tls(:verify_peer => true)
160
+ # end
161
+ #
162
+ # def ssl_verify_peer(cert)
163
+ # # Do not accept the peer. This should now cause the connection to shut down
164
+ # # without the SSL handshake being completed.
165
+ # false
166
+ # end
167
+ #
168
+ # def ssl_handshake_completed
169
+ # $server_handshake_completed = true
170
+ # end
171
+ # end
172
+ #
173
+ # @see #start_tls
116
174
  def ssl_verify_peer(cert)
117
175
  end
118
176
 
119
- # EventMachine::Connection#unbind is called by the framework whenever a connection
120
- # (either a server or client connection) is closed. The close can occur because
121
- # your code intentionally closes it (see close_connection and close_connection_after_writing),
177
+ # called by the framework whenever a connection (either a server or client connection) is closed.
178
+ # The close can occur because your code intentionally closes it (using {#close_connection} and {#close_connection_after_writing}),
122
179
  # because the remote peer closed the connection, or because of a network error.
123
180
  # You may not assume that the network connection is still open and able to send or
124
181
  # receive data when the callback to unbind is made. This is intended only to give
125
182
  # you a chance to clean up associations your code may have made to the connection
126
183
  # object while it was open.
127
184
  #
185
+ # If you want to detect which peer has closed the connection, you can override {#close_connection} in your protocol handler
186
+ # and set an @ivar.
187
+ #
188
+ # @example Overriding Connection#close_connection to distinguish connections closed on our side
189
+ #
190
+ # class MyProtocolHandler < EventMachine::Connection
191
+ #
192
+ # # ...
193
+ #
194
+ # def close_connection(*args)
195
+ # @intentionally_closed_connection = true
196
+ # super(*args)
197
+ # end
198
+ #
199
+ # def unbind
200
+ # if @intentionally_closed_connection
201
+ # # ...
202
+ # end
203
+ # end
204
+ #
205
+ # # ...
206
+ #
207
+ # end
208
+ #
209
+ # @see #post_init
210
+ # @see #connection_completed
211
+ # @see file:docs/GettingStarted.md EventMachine tutorial
128
212
  def unbind
129
213
  end
130
214
 
131
- # EventMachine::Connection#proxy_target_unbound is called by the reactor after attempting
132
- # to relay incoming data to a descriptor (set as a proxy target descriptor with
133
- # EventMachine::enable_proxy) that has already been closed.
215
+ # Called by the reactor after attempting to relay incoming data to a descriptor (set as a proxy target descriptor with
216
+ # {EventMachine.enable_proxy}) that has already been closed.
217
+ #
218
+ # @see EventMachine.enable_proxy
134
219
  def proxy_target_unbound
135
220
  end
136
221
 
222
+ # called when the reactor finished proxying all
223
+ # of the requested bytes.
224
+ def proxy_completed
225
+ end
226
+
137
227
  # EventMachine::Connection#proxy_incoming_to is called only by user code. It sets up
138
228
  # a low-level proxy relay for all data inbound for this connection, to the connection given
139
229
  # as the argument. This is essentially just a helper method for enable_proxy.
140
- # See EventMachine::enable_proxy documentation for details.
230
+ #
231
+ # @see EventMachine.enable_proxy
141
232
  def proxy_incoming_to(conn,bufsize=0)
142
233
  EventMachine::enable_proxy(self, conn, bufsize)
143
234
  end
144
235
 
145
- # Helper method for EventMachine::disable_proxy(self)
236
+ # A helper method for {EventMachine.disable_proxy}
146
237
  def stop_proxying
147
238
  EventMachine::disable_proxy(self)
148
239
  end
@@ -160,17 +251,17 @@ module EventMachine
160
251
  # However, it's not guaranteed that a future version of EventMachine will not change
161
252
  # this behavior.
162
253
  #
163
- # close_connection will <i>silently discard</i> any outbound data which you have
164
- # sent to the connection using EventMachine::Connection#send_data but which has not
254
+ # {#close_connection} will *silently discard* any outbound data which you have
255
+ # sent to the connection using {EventMachine::Connection#send_data} but which has not
165
256
  # yet been sent across the network. If you want to avoid this behavior, use
166
- # EventMachine::Connection#close_connection_after_writing.
257
+ # {EventMachine::Connection#close_connection_after_writing}.
167
258
  #
168
259
  def close_connection after_writing = false
169
260
  EventMachine::close_connection @signature, after_writing
170
261
  end
171
262
 
172
- # EventMachine::Connection#detach will remove the given connection from the event loop.
173
- # The connection's socket remains open and its file descriptor number is returned
263
+ # Removes given connection from the event loop.
264
+ # The connection's socket remains open and its file descriptor number is returned.
174
265
  def detach
175
266
  EventMachine::detach_fd @signature
176
267
  end
@@ -179,42 +270,47 @@ module EventMachine
179
270
  EventMachine::get_sock_opt @signature, level, option
180
271
  end
181
272
 
182
- # EventMachine::Connection#close_connection_after_writing is a variant of close_connection.
273
+ # A variant of {#close_connection}.
183
274
  # All of the descriptive comments given for close_connection also apply to
184
- # close_connection_after_writing, <i>with one exception:</i> If the connection has
275
+ # close_connection_after_writing, *with one exception*: if the connection has
185
276
  # outbound data sent using send_dat but which has not yet been sent across the network,
186
- # close_connection_after_writing will schedule the connection to be closed <i>after</i>
277
+ # close_connection_after_writing will schedule the connection to be closed *after*
187
278
  # all of the outbound data has been safely written to the remote peer.
188
279
  #
189
280
  # Depending on the amount of outgoing data and the speed of the network,
190
281
  # considerable time may elapse between your call to close_connection_after_writing
191
282
  # and the actual closing of the socket (at which time the unbind callback will be called
192
- # by the event loop). During this time, you <i>may not</i> call send_data to transmit
283
+ # by the event loop). During this time, you *may not* call send_data to transmit
193
284
  # additional data (that is, the connection is closed for further writes). In very
194
- # rare cases, you may experience a receive_data callback after your call to close_connection_after_writing,
285
+ # rare cases, you may experience a receive_data callback after your call to {#close_connection_after_writing},
195
286
  # depending on whether incoming data was in the process of being received on the connection
196
- # at the moment when you called close_connection_after_writing. Your protocol handler must
287
+ # at the moment when you called {#close_connection_after_writing}. Your protocol handler must
197
288
  # be prepared to properly deal with such data (probably by ignoring it).
198
289
  #
290
+ # @see #close_connection
291
+ # @see #send_data
199
292
  def close_connection_after_writing
200
293
  close_connection true
201
294
  end
202
295
 
203
- # EventMachine::Connection#send_data is only called by user code, never by
204
- # the event loop. You call this method to send data to the remote end of the
205
- # network connection. send_data is called with a single String argument, which
206
- # may of course contain binary data. You can call send_data any number of times.
207
- # send_data is an instance method of an object derived from EventMachine::Connection
208
- # and containing your mixed-in handler code), so if you call it without qualification
209
- # within a callback function, the data will be sent to the same network connection
210
- # that generated the callback. Calling self.send_data is exactly equivalent.
296
+ # Call this method to send data to the remote end of the network connection. It takes a single String argument,
297
+ # which may contain binary data. Data is buffered to be sent at the end of this event loop tick (cycle).
211
298
  #
212
- # You can also call send_data to write to a connection <i>other than the one
213
- # whose callback you are calling send_data from.</i> This is done by recording
214
- # the value of the connection in any callback function (the value self), in any
215
- # variable visible to other callback invocations on the same or different
216
- # connection objects. (Need an example to make that clear.)
299
+ # When used in a method that is event handler (for example, {#post_init} or {#connection_completed}, it will send
300
+ # data to the other end of the connection that generated the event.
301
+ # You can also call {#send_data} to write to other connections. For more information see The Chat Server Example in the
302
+ # {file:docs/GettingStarted.md EventMachine tutorial}.
217
303
  #
304
+ # If you want to send some data and then immediately close the connection, make sure to use {#close_connection_after_writing}
305
+ # instead of {#close_connection}.
306
+ #
307
+ #
308
+ # @param [String] data Data to send asynchronously
309
+ #
310
+ # @see file:docs/GettingStarted.md EventMachine tutorial
311
+ # @see Connection#receive_data
312
+ # @see Connection#post_init
313
+ # @see Connection#unbind
218
314
  def send_data data
219
315
  data = data.to_s
220
316
  size = data.bytesize if data.respond_to?(:bytesize)
@@ -223,56 +319,58 @@ module EventMachine
223
319
  end
224
320
 
225
321
  # Returns true if the connection is in an error state, false otherwise.
226
- # In general, you can detect the occurrence of communication errors or unexpected
227
- # disconnection by the remote peer by handing the #unbind method. In some cases, however,
228
- # it's useful to check the status of the connection using #error? before attempting to send data.
229
- # This function is synchronous: it will return immediately without blocking.
230
322
  #
323
+ # In general, you can detect the occurrence of communication errors or unexpected
324
+ # disconnection by the remote peer by handing the {#unbind} method. In some cases, however,
325
+ # it's useful to check the status of the connection using {#error?} before attempting to send data.
326
+ # This function is synchronous but it will return immediately without blocking.
231
327
  #
328
+ # @return [Boolean] true if the connection is in an error state, false otherwise
232
329
  def error?
233
- EventMachine::report_connection_error_status(@signature) != 0
330
+ errno = EventMachine::report_connection_error_status(@signature)
331
+ case errno
332
+ when 0
333
+ false
334
+ when -1
335
+ true
336
+ else
337
+ EventMachine::ERRNOS[errno]
338
+ end
234
339
  end
235
340
 
236
- # #connection_completed is called by the event loop when a remote TCP connection
237
- # attempt completes successfully. You can expect to get this notification after calls
238
- # to EventMachine#connect. Remember that EventMachine makes remote connections
239
- # asynchronously, just as with any other kind of network event. #connection_completed
341
+ # Called by the event loop when a remote TCP connection attempt completes successfully.
342
+ # You can expect to get this notification after calls to {EventMachine.connect}. Remember that EventMachine makes remote connections
343
+ # asynchronously, just as with any other kind of network event. This method
240
344
  # is intended primarily to assist with network diagnostics. For normal protocol
241
- # handling, use #post_init to perform initial work on a new connection (such as
242
- # send an initial set of data).
243
- # #post_init will always be called. #connection_completed will only be called in case
244
- # of a successful completion. A connection-attempt which fails will receive a call
245
- # to #unbind after the failure.
345
+ # handling, use #post_init to perform initial work on a new connection (such as sending initial set of data).
346
+ # {Connection#post_init} will always be called. This method will only be called in case of a successful completion.
347
+ # A connection attempt which fails will result a call to {Connection#unbind} after the failure.
348
+ #
349
+ # @see Connection#post_init
350
+ # @see Connection#unbind
351
+ # @see file:docs/GettingStarted.md EventMachine tutorial
246
352
  def connection_completed
247
353
  end
248
354
 
249
- # Call #start_tls at any point to initiate TLS encryption on connected streams.
355
+ # Call {#start_tls} at any point to initiate TLS encryption on connected streams.
250
356
  # The method is smart enough to know whether it should perform a server-side
251
- # or a client-side handshake. An appropriate place to call #start_tls is in
252
- # your redefined #post_init method, or in the #connection_completed handler for
357
+ # or a client-side handshake. An appropriate place to call {#start_tls} is in
358
+ # your redefined {#post_init} method, or in the {#connection_completed} handler for
253
359
  # an outbound connection.
254
360
  #
255
- # #start_tls takes an optional parameter hash that allows you to specify certificate
256
- # and other options to be used with this Connection object. Here are the currently-supported
257
- # options:
258
361
  #
259
- # * :cert_chain_file :
260
- # takes a String, which is interpreted as the name of a readable file in the
261
- # local filesystem. The file is expected to contain a chain of X509 certificates in
262
- # PEM format, with the most-resolved certificate at the top of the file, successive
263
- # intermediate certs in the middle, and the root (or CA) cert at the bottom.
362
+ # @option args [String] :cert_chain_file (nil) local path of a readable file that contants a chain of X509 certificates in
363
+ # the [PEM format](http://en.wikipedia.org/wiki/Privacy_Enhanced_Mail),
364
+ # with the most-resolved certificate at the top of the file, successive intermediate
365
+ # certs in the middle, and the root (or CA) cert at the bottom.
264
366
  #
265
- # * :private_key_file :
266
- # takes a String, which is interpreted as the name of a readable file in the
267
- # local filesystem. The file must contain a private key in PEM format.
367
+ # @option args [String] :private_key_file (nil) local path of a readable file that must contain a private key in the [PEM format](http://en.wikipedia.org/wiki/Privacy_Enhanced_Mail).
268
368
  #
269
- # * :verify_peer :
270
- # takes either true or false. Default is false. This indicates whether a server should request a
271
- # certificate from a peer, to be verified by user code. If true, the #ssl_verify_peer callback
272
- # on the Connection object is called with each certificate in the certificate chain provided by
273
- # the peer. See documentation on #ssl_verify_peer for how to use this.
369
+ # @option args [String] :verify_peer (false) indicates whether a server should request a certificate from a peer, to be verified by user code.
370
+ # If true, the {#ssl_verify_peer} callback on the {EventMachine::Connection} object is called with each certificate
371
+ # in the certificate chain provided by the peer. See documentation on {#ssl_verify_peer} for how to use this.
274
372
  #
275
- # === Usage example:
373
+ # @example Using TLS with EventMachine
276
374
  #
277
375
  # require 'rubygems'
278
376
  # require 'eventmachine'
@@ -283,101 +381,106 @@ module EventMachine
283
381
  # end
284
382
  # end
285
383
  #
286
- # EM.run {
287
- # EM.start_server("127.0.0.1", 9999, Handler)
288
- # }
384
+ # EventMachine.run do
385
+ # EventMachine.start_server("127.0.0.1", 9999, Handler)
386
+ # end
289
387
  #
290
- #--
291
- # TODO: support passing an encryption parameter, which can be string or Proc, to get a passphrase
388
+ # @param [Hash] args
389
+ #
390
+ # @todo support passing an encryption parameter, which can be string or Proc, to get a passphrase
292
391
  # for encrypted private keys.
293
- # TODO: support passing key material via raw strings or Procs that return strings instead of
392
+ # @todo support passing key material via raw strings or Procs that return strings instead of
294
393
  # just filenames.
295
- # What will get nasty is whether we have to define a location for storing this stuff as files.
296
- # In general, the OpenSSL interfaces for dealing with certs and keys in files are much better
297
- # behaved than the ones for raw chunks of memory.
298
394
  #
395
+ # @see #ssl_verify_peer
299
396
  def start_tls args={}
300
397
  priv_key, cert_chain, verify_peer = args.values_at(:private_key_file, :cert_chain_file, :verify_peer)
301
398
 
302
399
  [priv_key, cert_chain].each do |file|
303
400
  next if file.nil? or file.empty?
304
401
  raise FileNotFoundException,
305
- "Could not find #{file} for start_tls" unless File.exists? file
402
+ "Could not find #{file} for start_tls" unless File.exists? file
306
403
  end
307
404
 
308
405
  EventMachine::set_tls_parms(@signature, priv_key || '', cert_chain || '', verify_peer)
309
406
  EventMachine::start_tls @signature
310
407
  end
311
408
 
312
- # If SSL/TLS is active on the connection, #get_peer_cert returns the remote X509 certificate
313
- # as a String, in the popular PEM format. This can then be used for arbitrary validation
409
+ # If [TLS](http://en.wikipedia.org/wiki/Transport_Layer_Security) is active on the connection, returns the remote [X509 certificate](http://en.wikipedia.org/wiki/X.509)
410
+ # as a string, in the popular [PEM format](http://en.wikipedia.org/wiki/Privacy_Enhanced_Mail). This can then be used for arbitrary validation
314
411
  # of a peer's certificate in your code.
315
412
  #
316
- # This should be called in/after the #ssl_handshake_completed callback, which indicates
413
+ # This should be called in/after the {#ssl_handshake_completed} callback, which indicates
317
414
  # that SSL/TLS is active. Using this callback is important, because the certificate may not
318
415
  # be available until the time it is executed. Using #post_init or #connection_completed is
319
416
  # not adequate, because the SSL handshake may still be taking place.
320
417
  #
321
- # #get_peer_cert will return <b>nil</b> if:
418
+ # This method will return `nil` if:
322
419
  #
323
- # * EventMachine is not built with OpenSSL support
324
- # * SSL/TLS is not active on the connection
325
- # * SSL/TLS handshake is not yet complete
420
+ # * EventMachine is not built with [OpenSSL](http://www.openssl.org) support
421
+ # * [TLS](http://en.wikipedia.org/wiki/Transport_Layer_Security) is not active on the connection
422
+ # * TLS handshake is not yet complete
326
423
  # * Remote peer for any other reason has not presented a certificate
327
424
  #
328
- # === Example:
329
425
  #
330
- # module Handler
426
+ # @example Getting peer TLS certificate information in EventMachine
331
427
  #
332
- # def post_init
333
- # puts "Starting TLS"
334
- # start_tls
335
- # end
428
+ # module Handler
429
+ # def post_init
430
+ # puts "Starting TLS"
431
+ # start_tls
432
+ # end
336
433
  #
337
- # def ssl_handshake_completed
338
- # puts get_peer_cert
339
- # close_connection
340
- # end
434
+ # def ssl_handshake_completed
435
+ # puts get_peer_cert
436
+ # close_connection
437
+ # end
341
438
  #
342
- # def unbind
343
- # EventMachine::stop_event_loop
344
- # end
439
+ # def unbind
440
+ # EventMachine::stop_event_loop
441
+ # end
442
+ # end
345
443
  #
444
+ # EventMachine.run do
445
+ # EventMachine.connect "mail.google.com", 443, Handler
346
446
  # end
347
447
  #
348
- # EM.run {
349
- # EventMachine::connect "mail.google.com", 443, Handler
350
- # }
351
- #
352
- # Output:
353
- # -----BEGIN CERTIFICATE-----
354
- # MIIDIjCCAougAwIBAgIQbldpChBPqv+BdPg4iwgN8TANBgkqhkiG9w0BAQUFADBM
355
- # MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg
356
- # THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0wODA1MDIxNjMyNTRaFw0w
357
- # OTA1MDIxNjMyNTRaMGkxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
358
- # MRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgSW5jMRgw
359
- # FgYDVQQDEw9tYWlsLmdvb2dsZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ
360
- # AoGBALlkxdh2QXegdElukCSOV2+8PKiONIS+8Tu9K7MQsYpqtLNC860zwOPQ2NLI
361
- # 3Zp4jwuXVTrtzGuiqf5Jioh35Ig3CqDXtLyZoypjZUQcq4mlLzHlhIQ4EhSjDmA7
362
- # Ffw9y3ckSOQgdBQWNLbquHh9AbEUjmhkrYxIqKXeCnRKhv6nAgMBAAGjgecwgeQw
363
- # KAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUFBwMCBglghkgBhvhCBAEwNgYDVR0f
364
- # BC8wLTAroCmgJ4YlaHR0cDovL2NybC50aGF3dGUuY29tL1RoYXd0ZVNHQ0NBLmNy
365
- # bDByBggrBgEFBQcBAQRmMGQwIgYIKwYBBQUHMAGGFmh0dHA6Ly9vY3NwLnRoYXd0
366
- # ZS5jb20wPgYIKwYBBQUHMAKGMmh0dHA6Ly93d3cudGhhd3RlLmNvbS9yZXBvc2l0
367
- # b3J5L1RoYXd0ZV9TR0NfQ0EuY3J0MAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEF
368
- # BQADgYEAsRwpLg1dgCR1gYDK185MFGukXMeQFUvhGqF8eT/CjpdvezyKVuz84gSu
369
- # 6ccMXgcPQZGQN/F4Xug+Q01eccJjRSVfdvR5qwpqCj+6BFl5oiKDBsveSkrmL5dz
370
- # s2bn7TdTSYKcLeBkjXxDLHGBqLJ6TNCJ3c4/cbbG5JhGvoema94=
371
- # -----END CERTIFICATE-----
448
+ # # Will output:
449
+ # # -----BEGIN CERTIFICATE-----
450
+ # # MIIDIjCCAougAwIBAgIQbldpChBPqv+BdPg4iwgN8TANBgkqhkiG9w0BAQUFADBM
451
+ # # MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg
452
+ # # THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0wODA1MDIxNjMyNTRaFw0w
453
+ # # OTA1MDIxNjMyNTRaMGkxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
454
+ # # MRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgSW5jMRgw
455
+ # # FgYDVQQDEw9tYWlsLmdvb2dsZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ
456
+ # # AoGBALlkxdh2QXegdElukCSOV2+8PKiONIS+8Tu9K7MQsYpqtLNC860zwOPQ2NLI
457
+ # # 3Zp4jwuXVTrtzGuiqf5Jioh35Ig3CqDXtLyZoypjZUQcq4mlLzHlhIQ4EhSjDmA7
458
+ # # Ffw9y3ckSOQgdBQWNLbquHh9AbEUjmhkrYxIqKXeCnRKhv6nAgMBAAGjgecwgeQw
459
+ # # KAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUFBwMCBglghkgBhvhCBAEwNgYDVR0f
460
+ # # BC8wLTAroCmgJ4YlaHR0cDovL2NybC50aGF3dGUuY29tL1RoYXd0ZVNHQ0NBLmNy
461
+ # # bDByBggrBgEFBQcBAQRmMGQwIgYIKwYBBQUHMAGGFmh0dHA6Ly9vY3NwLnRoYXd0
462
+ # # ZS5jb20wPgYIKwYBBQUHMAKGMmh0dHA6Ly93d3cudGhhd3RlLmNvbS9yZXBvc2l0
463
+ # # b3J5L1RoYXd0ZV9TR0NfQ0EuY3J0MAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEF
464
+ # # BQADgYEAsRwpLg1dgCR1gYDK185MFGukXMeQFUvhGqF8eT/CjpdvezyKVuz84gSu
465
+ # # 6ccMXgcPQZGQN/F4Xug+Q01eccJjRSVfdvR5qwpqCj+6BFl5oiKDBsveSkrmL5dz
466
+ # # s2bn7TdTSYKcLeBkjXxDLHGBqLJ6TNCJ3c4/cbbG5JhGvoema94=
467
+ # # -----END CERTIFICATE-----
372
468
  #
373
469
  # You can do whatever you want with the certificate String, such as load it
374
470
  # as a certificate object using the OpenSSL library, and check it's fields.
471
+ #
472
+ # @return [String] the remote [X509 certificate](http://en.wikipedia.org/wiki/X.509), in the popular [PEM format](http://en.wikipedia.org/wiki/Privacy_Enhanced_Mail),
473
+ # if TLS is active on the connection
474
+ #
475
+ # @see Connection#start_tls
476
+ # @see Connection#ssl_handshake_completed
375
477
  def get_peer_cert
376
478
  EventMachine::get_peer_cert @signature
377
479
  end
378
480
 
379
481
 
380
- # send_datagram is for sending UDP messages.
482
+ # Sends UDP messages.
483
+ #
381
484
  # This method may be called from any Connection object that refers
382
485
  # to an open datagram socket (see EventMachine#open_datagram_socket).
383
486
  # The method sends a UDP (datagram) packet containing the data you specify,
@@ -394,23 +497,28 @@ module EventMachine
394
497
  # but to be really safe, send messages smaller than the Ethernet-packet
395
498
  # size (typically about 1400 bytes). Some very restrictive WANs
396
499
  # will either drop or truncate packets larger than about 500 bytes.
397
- #--
398
- # Added the Integer wrapper around the port parameter per suggestion by
399
- # Matthieu Riou, after he passed a String and spent hours tearing his hair out.
400
500
  #
501
+ # @param [String] data Data to send asynchronously
502
+ # @param [String] recipient_address IP address of the recipient
503
+ # @param [String] recipient_port Port of the recipient
401
504
  def send_datagram data, recipient_address, recipient_port
402
505
  data = data.to_s
403
- EventMachine::send_datagram @signature, data, data.length, recipient_address, Integer(recipient_port)
506
+ size = data.bytesize if data.respond_to?(:bytesize)
507
+ size ||= data.size
508
+ EventMachine::send_datagram @signature, data, size, recipient_address, Integer(recipient_port)
404
509
  end
405
510
 
406
511
 
407
- # #get_peername is used with stream-connections to obtain the identity
512
+ # This method is used with stream-connections to obtain the identity
408
513
  # of the remotely-connected peer. If a peername is available, this method
409
514
  # returns a sockaddr structure. The method returns nil if no peername is available.
410
515
  # You can use Socket.unpack_sockaddr_in and its variants to obtain the
411
516
  # values contained in the peername structure returned from #get_peername.
412
517
  #
518
+ # @example How to get peer IP address and port with EventMachine
519
+ #
413
520
  # require 'socket'
521
+ #
414
522
  # module Handler
415
523
  # def receive_data data
416
524
  # port, ip = Socket.unpack_sockaddr_in(get_peername)
@@ -421,27 +529,39 @@ module EventMachine
421
529
  EventMachine::get_peername @signature
422
530
  end
423
531
 
424
- # #get_sockname is used with stream-connections to obtain the identity
532
+ # Used with stream-connections to obtain the identity
425
533
  # of the local side of the connection. If a local name is available, this method
426
534
  # returns a sockaddr structure. The method returns nil if no local name is available.
427
- # You can use Socket#unpack_sockaddr_in and its variants to obtain the
428
- # values contained in the local-name structure returned from #get_sockname.
535
+ # You can use {Socket.unpack_sockaddr_in} and its variants to obtain the
536
+ # values contained in the local-name structure returned from this method.
537
+ #
538
+ # @example
539
+ #
540
+ # require 'socket'
541
+ #
542
+ # module Handler
543
+ # def receive_data data
544
+ # port, ip = Socket.unpack_sockaddr_in(get_sockname)
545
+ # puts "got #{data.inspect}"
546
+ # end
547
+ # end
429
548
  def get_sockname
430
549
  EventMachine::get_sockname @signature
431
550
  end
432
551
 
433
552
  # Returns the PID (kernel process identifier) of a subprocess
434
- # associated with this Connection object. For use with EventMachine#popen
553
+ # associated with this Connection object. For use with {EventMachine.popen}
435
554
  # and similar methods. Returns nil when there is no meaningful subprocess.
436
- #--
437
555
  #
556
+ # @return [Integer]
438
557
  def get_pid
439
558
  EventMachine::get_subprocess_pid @signature
440
559
  end
441
560
 
442
- # Returns a subprocess exit status. Only useful for #popen. Call it in your
443
- # #unbind handler.
561
+ # Returns a subprocess exit status. Only useful for {EventMachine.popen}. Call it in your
562
+ # {#unbind} handler.
444
563
  #
564
+ # @return [Integer]
445
565
  def get_status
446
566
  EventMachine::get_subprocess_status @signature
447
567
  end
@@ -455,88 +575,97 @@ module EventMachine
455
575
  EventMachine::get_comm_inactivity_timeout @signature
456
576
  end
457
577
 
458
- # Alias for #set_comm_inactivity_timeout.
459
- def comm_inactivity_timeout= value
460
- self.set_comm_inactivity_timeout value
461
- end
462
-
463
- # comm_inactivity_timeout= allows you to set the inactivity-timeout property for
578
+ # Allows you to set the inactivity-timeout property for
464
579
  # a network connection or datagram socket. Specify a non-negative float value in seconds.
465
580
  # If the value is greater than zero, the connection or socket will automatically be closed
466
581
  # if no read or write activity takes place for at least that number of seconds.
467
582
  # Specify a value of zero to indicate that no automatic timeout should take place.
468
583
  # Zero is the default value.
469
- def set_comm_inactivity_timeout value
584
+ def comm_inactivity_timeout= value
470
585
  EventMachine::set_comm_inactivity_timeout @signature, value.to_f
471
586
  end
587
+ alias set_comm_inactivity_timeout comm_inactivity_timeout=
472
588
 
473
- # pending_connect_timeout is the duration after which a TCP connection in the connecting
474
- # state will fail. It is important to distinguish this value from comm_inactivity_timeout,
475
- # which looks at how long since data was passed on an already established connection.
476
- # The value is a float in seconds.
477
- def pending_connect_timeout
478
- EventMachine::get_pending_connect_timeout @signature
479
- end
589
+ # The duration after which a TCP connection in the connecting state will fail.
590
+ # It is important to distinguish this value from {EventMachine::Connection#comm_inactivity_timeout},
591
+ # which looks at how long since data was passed on an already established connection.
592
+ # The value is a float in seconds.
593
+ #
594
+ # @return [Float] The duration after which a TCP connection in the connecting state will fail, in seconds.
595
+ def pending_connect_timeout
596
+ EventMachine::get_pending_connect_timeout @signature
597
+ end
480
598
 
481
- # Alias for #set_pending_connect_timeout.
599
+ # Sets the duration after which a TCP connection in a
600
+ # connecting state will fail.
601
+ #
602
+ # @param [Float, #to_f] value Connection timeout in seconds
482
603
  def pending_connect_timeout= value
483
- self.set_pending_connect_timeout value
484
- end
485
-
486
- # set_pending_connect_timeout sets the duration after which a TCP connection in a
487
- # connecting state will fail. Takes a float in seconds.
488
- def set_pending_connect_timeout value
489
604
  EventMachine::set_pending_connect_timeout @signature, value.to_f
490
605
  end
606
+ alias set_pending_connect_timeout pending_connect_timeout=
491
607
 
492
- # Reconnect to a given host/port with the current EventMachine::Connection instance
493
- def reconnect server, port
494
- EventMachine::reconnect server, port, self
495
- end
608
+ # Reconnect to a given host/port with the current instance
609
+ #
610
+ # @param [String] server Hostname or IP address
611
+ # @param [Integer] port Port to reconnect to
612
+ def reconnect server, port
613
+ EventMachine::reconnect server, port, self
614
+ end
496
615
 
497
616
 
498
- # Like EventMachine::Connection#send_data, this sends data to the remote end of
499
- # the network connection. EventMachine::Connection@send_file_data takes a
617
+ # Like {EventMachine::Connection#send_data}, this sends data to the remote end of
618
+ # the network connection. {EventMachine::Connection#send_file_data} takes a
500
619
  # filename as an argument, though, and sends the contents of the file, in one
501
- # chunk. Contributed by Kirk Haines.
620
+ # chunk.
621
+ #
622
+ # @param [String] filename Local path of the file to send
502
623
  #
624
+ # @see #send_data
625
+ # @author Kirk Haines
503
626
  def send_file_data filename
504
627
  EventMachine::send_file_data @signature, filename
505
628
  end
506
629
 
507
630
  # Open a file on the filesystem and send it to the remote peer. This returns an
508
- # object of type EventMachine::Deferrable. The object's callbacks will be executed
631
+ # object of type {EventMachine::Deferrable}. The object's callbacks will be executed
509
632
  # on the reactor main thread when the file has been completely scheduled for
510
- # transmission to the remote peer. Its errbacks will be called in case of an error
511
- # (such as file-not-found). #stream_file_data employs various strategems to achieve
512
- # the fastest possible performance, balanced against minimum consumption of memory.
513
- #
514
- # You can control the behavior of #stream_file_data with the optional arguments parameter.
515
- # Currently-supported arguments are:
516
- # :http_chunks, a boolean flag which defaults false. If true, this flag streams the
517
- # file data in a format compatible with the HTTP chunked-transfer encoding.
633
+ # transmission to the remote peer. Its errbacks will be called in case of an error (such as file-not-found).
634
+ # This method employs various strategies to achieve the fastest possible performance,
635
+ # balanced against minimum consumption of memory.
518
636
  #
519
637
  # Warning: this feature has an implicit dependency on an outboard extension,
520
- # evma_fastfilereader. You must install this extension in order to use #stream_file_data
638
+ # evma_fastfilereader. You must install this extension in order to use {#stream_file_data}
521
639
  # with files larger than a certain size (currently 8192 bytes).
522
640
  #
641
+ # @option args [Boolean] :http_chunks (false) If true, this method will stream the file data in a format
642
+ # compatible with the HTTP chunked-transfer encoding
643
+ #
644
+ # @param [String] filename Local path of the file to stream
645
+ # @param [Hash] args Options
646
+ #
647
+ # @return [EventMachine::Deferrable]
523
648
  def stream_file_data filename, args={}
524
649
  EventMachine::FileStreamer.new( self, filename, args )
525
650
  end
526
651
 
527
- # Enable notify_readable callbacks on this connection. Only possible if the connection was created
528
- # using EM.attach and had notify_readable/notify_writable defined on the handler.
652
+ # Watches connection for readability. Only possible if the connection was created
653
+ # using {EventMachine.attach} and had {EventMachine.notify_readable}/{EventMachine.notify_writable} defined on the handler.
654
+ #
655
+ # @see #notify_readable?
529
656
  def notify_readable= mode
530
657
  EventMachine::set_notify_readable @signature, mode
531
658
  end
532
659
 
533
- # Returns true if the connection is being watched for readability.
660
+ # @return [Boolean] true if the connection is being watched for readability.
534
661
  def notify_readable?
535
662
  EventMachine::is_notify_readable @signature
536
663
  end
537
664
 
538
- # Enable notify_writable callbacks on this connection. Only possible if the connection was created
539
- # using EM.attach and had notify_readable/notify_writable defined on the handler.
665
+ # Watches connection for writeability. Only possible if the connection was created
666
+ # using {EventMachine.attach} and had {EventMachine.notify_readable}/{EventMachine.notify_writable} defined on the handler.
667
+ #
668
+ # @see #notify_writable?
540
669
  def notify_writable= mode
541
670
  EventMachine::set_notify_writable @signature, mode
542
671
  end
@@ -546,19 +675,23 @@ module EventMachine
546
675
  EventMachine::is_notify_writable @signature
547
676
  end
548
677
 
549
- # Pause a connection so that #send_data and #receive_data events are not fired until #resume is called.
678
+ # Pause a connection so that {#send_data} and {#receive_data} events are not fired until {#resume} is called.
679
+ # @see #resume
550
680
  def pause
551
681
  EventMachine::pause_connection @signature
552
682
  end
553
683
 
554
- # Resume a connection's #send_data and #receive_data events.
684
+ # Resume a connection's {#send_data} and {#receive_data} events.
685
+ # @see #pause
555
686
  def resume
556
687
  EventMachine::resume_connection @signature
557
688
  end
558
689
 
559
- # True if the connect was paused using #pause.
690
+ # @return [Boolean] true if the connect was paused using {EventMachine::Connection#pause}.
691
+ # @see #pause
692
+ # @see #resume
560
693
  def paused?
561
694
  EventMachine::connection_paused? @signature
562
695
  end
563
696
  end
564
- end
697
+ end