Class: EventMachine::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/eventmachine.rb,
lib/jeventmachine.rb,
lib/jeventmachine.rb,
lib/pr_eventmachine.rb,
ext/rubymain.cpp

Overview

EventMachine::Connection is a class that is instantiated by EventMachine’s processing loop whenever a new connection is created. (New connections can be either initiated locally to a remote server or accepted locally from a remote client.) When a Connection object is instantiated, it mixes in the functionality contained in the user-defined module specified in calls to EventMachine#connect or EventMachine#start_server. User-defined handler modules may redefine any or all of the standard methods defined here, as well as add arbitrary additional code that will also be mixed in.

EventMachine manages one object inherited from EventMachine::Connection (and containing the mixed-in user code) for every network connection that is active at any given time. The event loop will automatically call methods on EventMachine::Connection objects whenever specific events occur on the corresponding connections, as described below.

This class is never instantiated by user code, and does not publish an initialize method. The instance methods of EventMachine::Connection which may be called by the event loop are: post_init, receive_data, and unbind. All of the other instance methods defined here are called only by user code.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Connection

Stubbed initialize so legacy superclasses can safely call super



1461
1462
# File 'lib/eventmachine.rb', line 1461

def initialize(*args) #:nodoc:
end

Instance Attribute Details

#signatureObject

EXPERIMENTAL. Added the reconnect methods, which may go away.



1440
1441
1442
# File 'lib/eventmachine.rb', line 1440

def signature
  @signature
end

Class Method Details

.new(sig, *args) ⇒ Object

Override .new so subclasses don’t have to call super and can ignore connection-specific arguments



1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
# File 'lib/eventmachine.rb', line 1445

def self.new(sig, *args) #:nodoc:
  allocate.instance_eval do
    # Call a superclass's #initialize if it has one
    initialize(*args)

    # Store signature and run #post_init
    @signature = sig
    associate_callback_target sig
    post_init
  
    self
  end
end

Instance Method Details

#associate_callback_target(sig) ⇒ Object

:nodoc:



1464
1465
1466
# File 'lib/eventmachine.rb', line 1464

def associate_callback_target(sig) #:nodoc:
	# no-op for the time being, to match similar no-op in rubymain.cpp
end

#close_connection(after_writing = false) ⇒ Object

EventMachine::Connection#close_connection is called only by user code, and never by the event loop. You may call this method against a connection object in any callback handler, whether or not the callback was made against the connection you want to close. close_connection schedules the connection to be closed at the next available opportunity within the event loop. You may not assume that the connection is closed when close_connection returns. In particular, the framework will callback the unbind method for the particular connection at a point shortly after you call close_connection. You may assume that the unbind callback will take place sometime after your call to close_connection completes. In other words, the unbind callback will not re-enter your code “inside” of your call to close_connection. However, it’s not guaranteed that a future version of EventMachine will not change this behavior.

close_connection will silently discard any outbound data which you have sent to the connection using EventMachine::Connection#send_data but which has not yet been sent across the network. If you want to avoid this behavior, use EventMachine::Connection#close_connection_after_writing.



1549
1550
1551
# File 'lib/eventmachine.rb', line 1549

def close_connection after_writing = false
	EventMachine::close_connection @signature, after_writing
end

#close_connection_after_writingObject

EventMachine::Connection#close_connection_after_writing is a variant of close_connection. All of the descriptive comments given for close_connection also apply to close_connection_after_writing, with one exception: If the connection has outbound data sent using send_dat but which has not yet been sent across the network, close_connection_after_writing will schedule the connection to be closed after all of the outbound data has been safely written to the remote peer.

Depending on the amount of outgoing data and the speed of the network, considerable time may elapse between your call to close_connection_after_writing and the actual closing of the socket (at which time the unbind callback will be called by the event loop). During this time, you may not call send_data to transmit additional data (that is, the connection is closed for further writes). In very rare cases, you may experience a receive_data callback after your call to close_connection_after_writing, depending on whether incoming data was in the process of being received on the connection at the moment when you called close_connection_after_writing. Your protocol handler must be prepared to properly deal with such data (probably by ignoring it).



1576
1577
1578
# File 'lib/eventmachine.rb', line 1576

def close_connection_after_writing
	close_connection true
end

#comm_inactivity_timeoutObject

comm_inactivity_timeout returns the current value (in seconds) of the inactivity-timeout property of network-connection and datagram-socket objects. A nonzero value indicates that the connection or socket will automatically be closed if no read or write activity takes place for at least that number of seconds. A zero value (the default) specifies that no automatic timeout will take place.



1799
1800
1801
# File 'lib/eventmachine.rb', line 1799

def comm_inactivity_timeout
	EventMachine::get_comm_inactivity_timeout @signature
end

#comm_inactivity_timeout=(value) ⇒ Object

Alias for #set_comm_inactivity_timeout.



1804
1805
1806
# File 'lib/eventmachine.rb', line 1804

def comm_inactivity_timeout= value
	self.send :set_comm_inactivity_timeout, value
end

#connection_completedObject

#connection_completed is called by the event loop when a remote TCP connection attempt completes successfully. You can expect to get this notification after calls to EventMachine#connect. Remember that EventMachine makes remote connections asynchronously, just as with any other kind of network event. #connection_completed is intended primarily to assist with network diagnostics. For normal protocol handling, use #post_init to perform initial work on a new connection (such as send an initial set of data). #post_init will always be called. #connection_completed will only be called in case of a successful completion. A connection-attempt which fails will receive a call to #unbind after the failure.



1622
1623
# File 'lib/eventmachine.rb', line 1622

def connection_completed
end

#detachObject

EventMachine::Connection#detach will remove the given connection from the event loop. The connection’s socket remains open and its file descriptor number is returned



1555
1556
1557
# File 'lib/eventmachine.rb', line 1555

def detach
	EventMachine::detach_fd @signature
end

#error?Boolean

Returns true if the connection is in an error state, false otherwise. In general, you can detect the occurrence of communication errors or unexpected disconnection by the remote peer by handing the #unbind method. In some cases, however, it’s useful to check the status of the connection using #error? before attempting to send data. This function is synchronous: it will return immediately without blocking.

Returns:

  • (Boolean)


1608
1609
1610
# File 'lib/eventmachine.rb', line 1608

def error?
	EventMachine::report_connection_error_status(@signature) != 0
end

#get_outbound_data_sizeObject



215
216
217
# File 'lib/pr_eventmachine.rb', line 215

def get_outbound_data_size
  EventMachine::get_outbound_data_size @signature
end

#get_peer_certObject

If SSL/TLS is active on the connection, #get_peer_cert returns the remote X509 certificate as a String, in the popular PEM format. This can then be used for arbitrary validation of a peer’s certificate in your code.

This should be called in/after the #ssl_handshake_completed callback, which indicates that SSL/TLS is active. Using this callback is important, because the certificate may not be available until the time it is executed. Using #post_init or #connection_completed is not adequate, because the SSL handshake may still be taking place.

#get_peer_cert will return nil if:

  • EventMachine is not built with OpenSSL support

  • SSL/TLS is not active on the connection

  • SSL/TLS handshake is not yet complete

  • Remote peer for any other reason has not presented a certificate

Example:

module Handler

 def post_init
   puts "Starting TLS"
   start_tls
 end

 def ssl_handshake_completed
   puts get_peer_cert
   close_connection
 end

 def unbind
   EventMachine::stop_event_loop
 end

end

EM.run {
 EventMachine::connect "mail.google.com", 443, Handler
}

Output:

-----BEGIN CERTIFICATE-----
MIIDIjCCAougAwIBAgIQbldpChBPqv+BdPg4iwgN8TANBgkqhkiG9w0BAQUFADBM
MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg
THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0wODA1MDIxNjMyNTRaFw0w
OTA1MDIxNjMyNTRaMGkxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
MRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgSW5jMRgw
FgYDVQQDEw9tYWlsLmdvb2dsZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ
AoGBALlkxdh2QXegdElukCSOV2+8PKiONIS+8Tu9K7MQsYpqtLNC860zwOPQ2NLI
3Zp4jwuXVTrtzGuiqf5Jioh35Ig3CqDXtLyZoypjZUQcq4mlLzHlhIQ4EhSjDmA7
Ffw9y3ckSOQgdBQWNLbquHh9AbEUjmhkrYxIqKXeCnRKhv6nAgMBAAGjgecwgeQw
KAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUFBwMCBglghkgBhvhCBAEwNgYDVR0f
BC8wLTAroCmgJ4YlaHR0cDovL2NybC50aGF3dGUuY29tL1RoYXd0ZVNHQ0NBLmNy
bDByBggrBgEFBQcBAQRmMGQwIgYIKwYBBQUHMAGGFmh0dHA6Ly9vY3NwLnRoYXd0
ZS5jb20wPgYIKwYBBQUHMAKGMmh0dHA6Ly93d3cudGhhd3RlLmNvbS9yZXBvc2l0
b3J5L1RoYXd0ZV9TR0NfQ0EuY3J0MAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEF
BQADgYEAsRwpLg1dgCR1gYDK185MFGukXMeQFUvhGqF8eT/CjpdvezyKVuz84gSu
6ccMXgcPQZGQN/F4Xug+Q01eccJjRSVfdvR5qwpqCj+6BFl5oiKDBsveSkrmL5dz
s2bn7TdTSYKcLeBkjXxDLHGBqLJ6TNCJ3c4/cbbG5JhGvoema94=
-----END CERTIFICATE-----

You can do whatever you want with the certificate String, such as load it as a certificate object using the OpenSSL library, and check it’s fields.



1728
1729
1730
# File 'lib/eventmachine.rb', line 1728

def get_peer_cert
  EventMachine::get_peer_cert @signature
end

#get_peernameObject

#get_peername is used with stream-connections to obtain the identity of the remotely-connected peer. If a peername is available, this method returns a sockaddr structure. The method returns nil if no peername is available. You can use Socket#unpack_sockaddr_in and its variants to obtain the values contained in the peername structure returned from #get_peername.



1765
1766
1767
# File 'lib/eventmachine.rb', line 1765

def get_peername
	EventMachine::get_peername @signature
end

#get_pidObject

Returns the PID (kernel process identifier) of a subprocess associated with this Connection object. For use with EventMachine#popen and similar methods. Returns nil when there is no meaningful subprocess. –



1783
1784
1785
# File 'lib/eventmachine.rb', line 1783

def get_pid
	EventMachine::get_subprocess_pid @signature
end

#get_socknameObject

#get_sockname is used with stream-connections to obtain the identity of the local side of the connection. If a local name is available, this method returns a sockaddr structure. The method returns nil if no local name is available. You can use Socket#unpack_sockaddr_in and its variants to obtain the values contained in the local-name structure returned from #get_sockname.



1774
1775
1776
# File 'lib/eventmachine.rb', line 1774

def get_sockname
	EventMachine::get_sockname @signature
end

#get_statusObject

Returns a subprocess exit status. Only useful for #popen. Call it in your #unbind handler.



1790
1791
1792
# File 'lib/eventmachine.rb', line 1790

def get_status
	EventMachine::get_subprocess_status @signature
end

#post_initObject

EventMachine::Connection#post_init is called by the event loop immediately after the network connection has been established, and before resumption of the network loop. This method is generally not called by user code, but is called automatically by the event loop. The base-class implementation is a no-op. This is a very good place to initialize instance variables that will be used throughout the lifetime of the network connection.



1476
1477
# File 'lib/eventmachine.rb', line 1476

def post_init
end

#receive_data(data) ⇒ Object

EventMachine::Connection#receive_data is called by the event loop whenever data has been received by the network connection. It is never called by user code. receive_data is called with a single parameter, a String containing the network protocol data, which may of course be binary. You will generally redefine this method to perform your own processing of the incoming data.

Here’s a key point which is essential to understanding the event-driven programming model: EventMachine knows absolutely nothing about the protocol which your code implements. You must not make any assumptions about the size of the incoming data packets, or about their alignment on any particular intra-message or PDU boundaries (such as line breaks). receive_data can and will send you arbitrary chunks of data, with the only guarantee being that the data is presented to your code in the order it was collected from the network. Don’t even assume that the chunks of data will correspond to network packets, as EventMachine can and will coalesce several incoming packets into one, to improve performance. The implication for your code is that you generally will need to implement some kind of a state machine in your redefined implementation of receive_data. For a better understanding of this, read through the examples of specific protocol handlers given elsewhere in this package. (STUB, WE MUST ADD THESE!)

The base-class implementation of receive_data (which will be invoked if you don’t redefine it) simply prints the size of each incoming data packet to stdout.



1505
1506
1507
# File 'lib/eventmachine.rb', line 1505

def receive_data data
	puts "............>>>#{data.length}"
end

#reconnect(server, port) ⇒ Object

– EXPERIMENTAL. DO NOT RELY ON THIS METHOD TO REMAIN SUPPORTED. (03Nov06)



1821
1822
1823
# File 'lib/eventmachine.rb', line 1821

def reconnect server, port
	EventMachine::reconnect server, port, self
end

#send_data(data) ⇒ Object

EventMachine::Connection#send_data is only called by user code, never by the event loop. You call this method to send data to the remote end of the network connection. send_data is called with a single String argument, which may of course contain binary data. You can call send_data any number of times. send_data is an instance method of an object derived from EventMachine::Connection and containing your mixed-in handler code), so if you call it without qualification within a callback function, the data will be sent to the same network connection that generated the callback. Calling self.send_data is exactly equivalent.

You can also call send_data to write to a connection other than the one whose callback you are calling send_data from. This is done by recording the value of the connection in any callback function (the value self), in any variable visible to other callback invocations on the same or different connection objects. (Need an example to make that clear.)



1595
1596
1597
1598
1599
# File 'lib/eventmachine.rb', line 1595

def send_data data
  size = data.bytesize if data.respond_to?(:bytesize)
  size ||= data.size
	EventMachine::send_data @signature, data, size
end

#send_datagram(data, recipient_address, recipient_port) ⇒ Object

send_datagram is for sending UDP messages. This method may be called from any Connection object that refers to an open datagram socket (see EventMachine#open_datagram_socket). The method sends a UDP (datagram) packet containing the data you specify, to a remote peer specified by the IP address and port that you give as parameters to the method. Observe that you may send a zero-length packet (empty string). However, you may not send an arbitrarily-large data packet because your operating system will enforce a platform-specific limit on the size of the outbound packet. (Your kernel will respond in a platform-specific way if you send an overlarge packet: some will send a truncated packet, some will complain, and some will silently drop your request). On LANs, it’s usually OK to send datagrams up to about 4000 bytes in length, but to be really safe, send messages smaller than the Ethernet-packet size (typically about 1400 bytes). Some very restrictive WANs will either drop or truncate packets larger than about 500 bytes. – Added the Integer wrapper around the port parameter per suggestion by Matthieu Riou, after he passed a String and spent hours tearing his hair out.



1754
1755
1756
1757
# File 'lib/eventmachine.rb', line 1754

def send_datagram data, recipient_address, recipient_port
	data = data.to_s
	EventMachine::send_datagram @signature, data, data.length, recipient_address, Integer(recipient_port)
end

#send_file_data(filename) ⇒ Object

Like EventMachine::Connection#send_data, this sends data to the remote end of the network connection. EventMachine::Connection@send_file_data takes a filename as an argument, though, and sends the contents of the file, in one chunk. Contributed by Kirk Haines.



1831
1832
1833
# File 'lib/eventmachine.rb', line 1831

def send_file_data filename
	EventMachine::send_file_data @signature, filename
end

#set_comm_inactivity_timeout(value) ⇒ Object

comm_inactivity_timeout= allows you to set the inactivity-timeout property for a network connection or datagram socket. Specify a non-negative numeric value in seconds. If the value is greater than zero, the connection or socket will automatically be closed if no read or write activity takes place for at least that number of seconds. Specify a value of zero to indicate that no automatic timeout should take place. Zero is the default value.



1814
1815
1816
# File 'lib/eventmachine.rb', line 1814

def set_comm_inactivity_timeout value
	EventMachine::set_comm_inactivity_timeout @signature, value
end

#ssl_handshake_completedObject

#ssl_handshake_completed is called by EventMachine when the SSL/TLS handshake has been completed, as a result of calling #start_tls to initiate SSL/TLS on the connection.

This callback exists because #post_init and #connection_completed are not reliable for indicating when an SSL/TLS connection is ready to have it’s certificate queried for.

See #get_peer_cert for application and example.



1516
1517
# File 'lib/eventmachine.rb', line 1516

def ssl_handshake_completed
end

#start_tls(args = {}) ⇒ Object

Call #start_tls at any point to initiate TLS encryption on connected streams. The method is smart enough to know whether it should perform a server-side or a client-side handshake. An appropriate place to call #start_tls is in your redefined #post_init method, or in the #connection_completed handler for an outbound connection.

#start_tls takes an optional parameter hash that allows you to specify certificate and other options to be used with this Connection object. Here are the currently-supported options: :cert_chain_file : takes a String, which is interpreted as the name of a readable file in the

local filesystem. The file is expected to contain a chain of X509 certificates in
PEM format, with the most-resolved certificate at the top of the file, successive
intermediate certs in the middle, and the root (or CA) cert at the bottom.

:private_key_file : tales a String, which is interpreted as the name of a readable file in the

local filesystem. The file must contain a private key in PEM format.

– TODO: support passing an encryption parameter, which can be string or Proc, to get a passphrase for encrypted private keys. TODO: support passing key material via raw strings or Procs that return strings instead of just filenames. What will get nasty is whether we have to define a location for storing this stuff as files. In general, the OpenSSL interfaces for dealing with certs and keys in files are much better behaved than the ones for raw chunks of memory.



1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
# File 'lib/eventmachine.rb', line 1651

def start_tls args={}
  priv_key, cert_chain = args.values_at(:private_key_file, :cert_chain_file)
  
  [priv_key, cert_chain].each do |file|
    next if file.nil? or file.empty?
    raise FileNotFoundException, 
      "Could not find #{file} for start_tls" unless File.exists? file
   end
	
	EventMachine::set_tls_parms(@signature, priv_key || '', cert_chain || '')
	
	EventMachine::start_tls @signature
end

#stream_file_data(filename, args = {}) ⇒ Object

Open a file on the filesystem and send it to the remote peer. This returns an object of type EventMachine::Deferrable. The object’s callbacks will be executed on the reactor main thread when the file has been completely scheduled for transmission to the remote peer. Its errbacks will be called in case of an error (such as file-not-found). #stream_file_data employs various strategems to achieve the fastest possible performance, balanced against minimum consumption of memory.

You can control the behavior of #stream_file_data with the optional arguments parameter. Currently-supported arguments are: :http_chunks, a boolean flag which defaults false. If true, this flag streams the file data in a format compatible with the HTTP chunked-transfer encoding.

Warning: this feature has an implicit dependency on an outboard extension, evma_fastfilereader. You must install this extension in order to use #stream_file_data with files larger than a certain size (currently 8192 bytes).



1851
1852
1853
# File 'lib/eventmachine.rb', line 1851

def stream_file_data filename, args={}
	EventMachine::FileStreamer.new( self, filename, args )
end

#unbindObject

EventMachine::Connection#unbind is called by the framework whenever a connection (either a server or client connection) is closed. The close can occur because your code intentionally closes it (see close_connection and close_connection_after_writing), because the remote peer closed the connection, or because of a network error. You may not assume that the network connection is still open and able to send or receive data when the callback to unbind is made. This is intended only to give you a chance to clean up associations your code may have made to the connection object while it was open.



1528
1529
# File 'lib/eventmachine.rb', line 1528

def unbind
end