Class: EasySockets::BasicSocket Abstract
- Inherits:
-
Object
- Object
- EasySockets::BasicSocket
- Includes:
- Utils
- Defined in:
- lib/easy_sockets/basic_socket.rb
Overview
This class is abstract.
Please check the following subclasses: TcpSocket and UnixSocket.
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_TIMEOUT =
0.5
Instance Attribute Summary collapse
-
#connected ⇒ Object
(also: #connected?)
readonly
Returns the value of attribute connected.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#connect ⇒ Object
Connects to the server.
-
#disconnect ⇒ Object
Disconnects to the server.
-
#initialize(opts = {}) ⇒ BasicSocket
constructor
A new instance of BasicSocket.
-
#send_msg(msg, read_response = true) ⇒ Object
Sends the message to the server, and reads and return the response if read_response=true.
Methods included from Utils
Constructor Details
#initialize(opts = {}) ⇒ BasicSocket
Returns a new instance of BasicSocket.
26 27 28 29 |
# File 'lib/easy_sockets/basic_socket.rb', line 26 def initialize(opts={}) setup_opts(opts) @connected = false end |
Instance Attribute Details
#connected ⇒ Object (readonly) Also known as: connected?
Returns the value of attribute connected.
17 18 19 |
# File 'lib/easy_sockets/basic_socket.rb', line 17 def connected @connected end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
17 18 19 |
# File 'lib/easy_sockets/basic_socket.rb', line 17 def logger @logger end |
Instance Method Details
#connect ⇒ Object
Connects to the server. This is an idempotent operation.
34 35 36 37 38 |
# File 'lib/easy_sockets/basic_socket.rb', line 34 def connect return if @connected && (@socket && !@socket.closed?) on_connect @connected = true end |
#disconnect ⇒ Object
Disconnects to the server. This is an idempotent operation.
43 44 45 46 47 48 49 50 51 |
# File 'lib/easy_sockets/basic_socket.rb', line 43 def disconnect return unless @connected if @socket && !@socket.closed? @socket.close log(:debug, "Socket successfully disconnected") @connected = false return true end end |
#send_msg(msg, read_response = true) ⇒ Object
Sends the message to the server, and reads and return the response if read_response=true. If you call this method and the socket is not connected yet, it will automatically connect the socket.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/easy_sockets/basic_socket.rb', line 60 def send_msg(msg, read_response=true) msg_to_send = msg.dup msg_to_send << @separator unless @separator.nil? || msg.end_with?(@separator) # This is an idempotent operation connect log(:debug, "Sending #{msg_to_send.inspect}") send_non_block(msg_to_send) if read_response resp = receive_non_block log(:debug, "Got #{resp.inspect}") resp end # Raised by some IO operations when reaching the end of file. Many IO methods exist in two forms, # one that returns nil when the end of file is reached, the other raises EOFError EOFError. # EOFError is a subclass of IOError. rescue EOFError => e log(:info, "Server disconnected.") self.disconnect raise e # "Connection reset by peer" is the TCP/IP equivalent of slamming the phone back on the hook. # It's more polite than merely not replying, leaving one hanging. # But it's not the FIN-ACK expected of the truly polite TCP/IP converseur. rescue Errno::ECONNRESET => e log(:info, 'Connection reset by peer.') self.disconnect raise e rescue Errno::EPIPE => e log(:info, 'Broken pipe.') self.disconnect raise e rescue Errno::ECONNREFUSED => e log(:info, 'Connection refused by peer.') self.disconnect raise e rescue Exception => e @socket.close if @socket && !@socket.closed? raise e end |