Class: Ionian::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/ionian/socket.rb

Overview

A convenient wrapper for TCP, UDP, and Unix client sockets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(existing_socket = nil, **kwargs) {|socket| ... } ⇒ Socket

Creates a new socket or wraps an existing socket.

Parameters:

  • existing_socket (Socket) (defaults to: nil)

    An instantiated socket to be wrapped in and returned as an Ionian::Socket (for example, TCPSocket). A new socket will be created if this parameter is nil.

  • kwargs (Hash)

    :host is mandatory.

Options Hash (**kwargs):

  • :host (String)

    IP or hostname to connect to. Can contain the port in the format “host:port”.

  • :port (Fixnum) — default: 23

    Connection’s port number. Unused by the :unix protocol.

  • :protocol (:tcp, :udp, :unix) — default: :tcp

    Type of socket to create. :udp will be automatically selected for addresses in the multicast range, or if the broadcast flag is set.

  • :connect_timeout (Fixnum) — default: nil

    Number of seconds to wait when connecting before timing out. Raises Errno::EHOSTUNREACH.

  • :persistent (Boolean) — default: true

    The socket remains open after data is sent if this is true. The socket closes after data is sent and a packet is received if this is false.

  • :bind_port (Boolean) — default: :port

    Local UDP port to bind to for receiving data, if different than the remote port being connected to.

  • :broadcast (Boolean) — default: false

    Enable the SO_BROADCAST flag. Sets protocol to :udp implicitly.

  • :reuse_addr (Boolean) — default: false

    Enable the SO_REUSEADDR flag. Allows local address reuse.

  • :no_delay (Boolean) — default: false

    Enable the TCP_NODELAY flag. Disables Nagle algorithm.

  • :cork (Boolean) — default: false

    Enable the TCP_CORK flag. Buffers multiple writes into one segment.

  • :linger (Boolean) — default: false

    Enable the SO_LINGER flag. When #close is called, waits for the send buffer to empty before closing the socket.

  • :expression (Regexp, String)

    Overrides the Extension::IO#read_match regular expression for received data.

Yield Parameters:

  • socket (Ionian::Socket)

    This socket is yielded to the block. Socket flushes and closes when exiting the block.



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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ionian/socket.rb', line 76

def initialize existing_socket = nil, **kwargs, &block
  @socket = existing_socket
  
  @ionian_listeners = []
  
  @expression = kwargs.fetch :expression, nil
  
  if existing_socket
    # Convert existing socket.
    @socket.extend Ionian::Extension::IO
    @socket.extend Ionian::Extension::Socket
    
    if existing_socket.is_a? UNIXSocket
      @host = existing_socket.path
      @port = nil
    else
      @host = existing_socket.remote_address.ip_address if existing_socket
      @port = existing_socket.remote_address.ip_port if existing_socket
    end
  
    if @socket.is_a? TCPSocket
      @protocol = :tcp
    elsif @socket.is_a? UDPSocket
      @protocol = :udp
    elsif @socket.is_a? UNIXSocket
      @protocol = :unix
    end
    
    @persistent = true # Existing sockets are always persistent.
    
    @socket.expression = @expression if @expression
    
    initialize_socket_methods
    
  else
    # Initialize new socket.
    
    # Parse host out of "host:port" if specified.
    host_port_array = kwargs.fetch(:host).to_s.split ':'
    
    @host           = host_port_array[0]
    @port           = kwargs.fetch :port, (host_port_array[1] || 23).to_i
    @bind_port      = kwargs.fetch :bind_port, @port
    @connect_timeout = kwargs.fetch :connect_timeout, nil
    
    @broadcast      = kwargs.fetch :broadcast, false
    
    # Automatically select UDP for the multicast range. Otherwise default to TCP.
    default_protocol = :tcp
    default_protocol = :udp  if Ionian::Extension::Socket.multicast? @host
    default_protocol = :unix if @host.start_with? '/'
    default_protocol = :udp  if @broadcast
    
    @protocol       = kwargs.fetch :protocol,   default_protocol
    @persistent     = kwargs.fetch :persistent, true
    @persistent     = true if @protocol == :udp
    
    @reuse_addr     = kwargs.fetch :reuse_addr, false
    @cork           = kwargs.fetch :cork,       false
    @no_delay       = kwargs.fetch :no_delay,   @persistent ? false : true
    
    # Default to false for persistent sockets, true for
    # nonpersistent sockets. When nonpersistent, the socket
    # should remain open to send data in the buffer after
    # close is called (typically right after write).
    # @linger         = kwargs.fetch :linger,     @persistent ? false : true
    # TODO: For some reason linger = true is causing tests to fail.
    @linger         = kwargs.fetch :linger,     false
  
    
    create_socket if @persistent
  end
  
  if block
    block.call self
    unless self.closed?
      self.flush
      self.close
    end
  end
end

Instance Attribute Details

#bind_portObject (readonly)

Local port number.



15
16
17
# File 'lib/ionian/socket.rb', line 15

def bind_port
  @bind_port
end

#hostObject (readonly)

IP address or URL of server.



9
10
11
# File 'lib/ionian/socket.rb', line 9

def host
  @host
end

#portObject (readonly)

Remote port number.



12
13
14
# File 'lib/ionian/socket.rb', line 12

def port
  @port
end

#protocolObject (readonly) Also known as: protocol?

Returns a symbol of the type of protocol this socket uses: :tcp, :udp, :unix



19
20
21
# File 'lib/ionian/socket.rb', line 19

def protocol
  @protocol
end

Instance Method Details

#closed?Boolean

Returns true if the socket is closed.

Returns:

  • (Boolean)


226
227
228
229
# File 'lib/ionian/socket.rb', line 226

def closed?
  return true unless @socket
  @socket.closed?
end

#cmd(data, **kwargs) {|match| ... } ⇒ Array<MatchData>

Send a command (data) to the socket.

Parameters:

Yield Parameters:

  • match (MatchData)

    Received match.

Returns:

  • (Array<MatchData>)

    An array of received matches.

See Also:



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ionian/socket.rb', line 183

def cmd data, **kwargs, &block
  create_socket unless @persistent
  
  write data
  @socket.flush
  
  matches = @socket.read_match(kwargs) { |match| yield match if block_given? }
  @socket.close unless @persistent
  
  matches
end

#expressionObject

Returns the regular expression used to match incoming data.



159
160
161
# File 'lib/ionian/socket.rb', line 159

def expression
  @expression || @socket.expression
end

#expression=(exp) ⇒ Object

Set the regular expression used to match incoming data.



164
165
166
167
# File 'lib/ionian/socket.rb', line 164

def expression= exp
  @expression = exp
  @socket.expression = exp if @socket
end

#flushObject

Flushes buffered data to the operating system. This method has no effect on non-persistent sockets.



233
234
235
# File 'lib/ionian/socket.rb', line 233

def flush
  @socket.flush if @persistent
end

#has_data?(**kwargs) ⇒ Boolean

Returns true if there is data in the receive buffer. Args:

Timeout: Number of seconds to wait for data until
  giving up. Set to nil for blocking.

Returns:

  • (Boolean)


220
221
222
223
# File 'lib/ionian/socket.rb', line 220

def has_data? **kwargs
  return false unless @socket
  @socket.has_data? kwargs
end

#persistent?Boolean

Returns true if the socket remains open after writing data.

Returns:

  • (Boolean)


170
171
172
# File 'lib/ionian/socket.rb', line 170

def persistent?
  @persistent == false || @persistent == nil ? false : true
end

#puts(*string) ⇒ Object

Writes the given string(s) to the socket and appends a newline character to any string not already ending with one.



239
240
241
# File 'lib/ionian/socket.rb', line 239

def puts *string
  self.write string.map{ |s| s.chomp }.join("\n") + "\n"
end

#register_observer(&block) ⇒ Object Also known as: on_match

Register a block to be called when #run_match receives matched data. Method callbacks can be registered with &object.method(:method). Returns a reference to the given block. block = ionian_socket.register_observer { … }



199
200
201
202
203
# File 'lib/ionian/socket.rb', line 199

def register_observer &block
  @ionian_listeners << block unless @ionian_listeners.include? block
  @socket.register_observer &block if @socket
  block
end

#unregister_observer(&block) ⇒ Object

Unregister a block from being called when matched data is received.



208
209
210
211
212
# File 'lib/ionian/socket.rb', line 208

def unregister_observer &block
  @ionian_listeners.delete_if { |o| o == block }
  @socket.unregister_observer &block if @socket
  block
end

#write(string) ⇒ Object Also known as: <<

Writes the given string to the socket. Returns the number of bytes written.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/ionian/socket.rb', line 245

def write string
  create_socket unless @persistent
  
  num_bytes = 0
  
  if @protocol == :udp
    num_bytes = @socket.send string, 0
  else
    num_bytes = @socket.write string
  end
  
  unless @persistent
    @socket.flush
    
    # Read in data to prevent RST packets.
    # TODO: Shutdown read stream instead?
    @socket.read_all nonblocking: true
    
    # TODO: Sleep added so that data can be read on the receiving
    # end. Can this be changed to shutdown write?
    # Why isn't so_linger taking care of this?
    # sleep 0.01
    @socket.close
  end
  
  num_bytes
end