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