class TCPClient
Constants
- Timeout
- VERSION
Attributes
default_configuration[R]
address[R]
configuration[R]
Public Class Methods
configure(options = {}, &block)
click to toggle source
# File lib/tcp-client/default_configuration.rb, line 11 def configure(options = {}, &block) @default_configuration = Configuration.create(options, &block) end
new()
click to toggle source
# File lib/tcp-client.rb, line 26 def initialize @socket = @address = @deadline = @configuration = nil end
open(address, configuration = Configuration.default) { |client| ... }
click to toggle source
# File lib/tcp-client.rb, line 13 def self.open(address, configuration = Configuration.default) client = new client.connect(Address.new(address), configuration) return client unless block_given? begin yield(client) ensure client.close end end
Public Instance Methods
close()
click to toggle source
# File lib/tcp-client.rb, line 43 def close @socket&.close self rescue IOError self ensure @socket = @deadline = nil end
closed?()
click to toggle source
# File lib/tcp-client.rb, line 52 def closed? @socket.nil? || @socket.closed? end
connect(address, configuration, exception: nil)
click to toggle source
# File lib/tcp-client.rb, line 34 def connect(address, configuration, exception: nil) raise(NoOpenSSL) if configuration.ssl? && !defined?(SSLSocket) close @address = Address.new(address) @configuration = configuration.dup.freeze @socket = create_socket(exception) self end
flush()
click to toggle source
# File lib/tcp-client.rb, line 84 def flush @socket.flush unless closed? self end
read(nbytes, timeout: nil, exception: nil)
click to toggle source
# File lib/tcp-client.rb, line 66 def read(nbytes, timeout: nil, exception: nil) raise(NotConnected) if closed? timeout.nil? && @deadline and return read_with_deadline(nbytes, @deadline, exception) deadline = Deadline.new(timeout || @configuration.read_timeout) return @socket.read(nbytes) unless deadline.valid? read_with_deadline(nbytes, deadline, exception) end
to_s()
click to toggle source
# File lib/tcp-client.rb, line 30 def to_s @address&.to_s || '' end
with_deadline(timeout) { |self| ... }
click to toggle source
# File lib/tcp-client.rb, line 56 def with_deadline(timeout) previous_deadline = @deadline raise(NoBlockGiven) unless block_given? @deadline = Deadline.new(timeout) raise(InvalidDeadLine, timeout) unless @deadline.valid? yield(self) ensure @deadline = previous_deadline end
write(*msg, timeout: nil, exception: nil)
click to toggle source
# File lib/tcp-client.rb, line 75 def write(*msg, timeout: nil, exception: nil) raise(NotConnected) if closed? timeout.nil? && @deadline and return write_with_deadline(msg, @deadline, exception) deadline = Deadline.new(timeout || @configuration.read_timeout) return @socket.write(*msg) unless deadline.valid? write_with_deadline(msg, deadline, exception) end
Private Instance Methods
create_socket(exception)
click to toggle source
# File lib/tcp-client.rb, line 91 def create_socket(exception) exception ||= @configuration.connect_timeout_error deadline = Deadline.new(@configuration.connect_timeout) @socket = TCPSocket.new(@address, @configuration, deadline, exception) return @socket unless @configuration.ssl? SSLSocket.new(@socket, @address, @configuration, deadline, exception) end
read_with_deadline(nbytes, deadline, exception)
click to toggle source
# File lib/tcp-client.rb, line 99 def read_with_deadline(nbytes, deadline, exception) exception ||= @configuration.read_timeout_error @socket.read_with_deadline(nbytes, deadline, exception) end
write_with_deadline(msg, deadline, exception)
click to toggle source
# File lib/tcp-client.rb, line 104 def write_with_deadline(msg, deadline, exception) exception ||= @configuration.write_timeout_error msg.sum do |chunk| @socket.write_with_deadline(chunk.b, deadline, exception) end end