Class: EasySockets::BasicSocket Abstract

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/easy_sockets/basic_socket.rb

Overview

This class is abstract.

Please check the following subclasses: TcpSocket and UnixSocket.

Author:

  • Marcos Ortiz

Direct Known Subclasses

TcpSocket, UdpSocket, UnixSocket

Constant Summary collapse

DEFAULT_TIMEOUT =
0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#log

Constructor Details

#initialize(opts = {}) ⇒ BasicSocket

Returns a new instance of BasicSocket.

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a socket with.

Options Hash (opts):

  • :logger (Logger) — default: nil

    An instance of Logger.

  • :timeout (Float) — default: 0.5

    Timeout in seconds for socket connect, read and write operations.

  • :separator (String) — default: "\r\n"

    Message separator.

  • :no_msg_separator (Boolean) — default: nil

    If true, the socket will not use message separators.



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

#connectedObject (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

#loggerObject (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

#connectObject

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

#disconnectObject

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.

Parameters:

  • msg (String)

    The message to send.

  • read_response (Boolean) (defaults to: true)

    Whether or not to read from the server after sending the message. Defaul to true.



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