Class: Pixelflut::Client::Socket

Inherits:
Socket
  • Object
show all
Defined in:
lib/pixelflut/client/socket.rb

Direct Known Subclasses

NonblockSocket

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ Socket

Returns a new instance of Socket.



8
9
10
11
12
# File 'lib/pixelflut/client/socket.rb', line 8

def initialize(address)
  super(address.ipv6? ? :INET6 : :INET, :STREAM)
  @addr = ::Socket.pack_sockaddr_in(address.ip_port, address.ip_address)
  configure
end

Instance Method Details

#connect?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/pixelflut/client/socket.rb', line 36

def connect?
  :wait_writable != connect_nonblock(@addr, exception: false)
end

#readline_with_timeout(timeout) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pixelflut/client/socket.rb', line 20

def readline_with_timeout(timeout)
  deadline = Time.now + timeout
  ret = ''
  loop do
    got = read_nonblock(16, exception: false)
    if got == :wait_readable
      remaining_time = deadline - Time.now
      return nil if remaining_time <= 0 || wait_readable(remaining_time).nil?
      next
    end
    idx = got.index("\n")
    next ret += got unless idx
    return ret + got[0, idx]
  end
end

#write_with_timout(data, timeout) ⇒ Object



14
15
16
17
18
# File 'lib/pixelflut/client/socket.rb', line 14

def write_with_timout(data, timeout)
  return true if write_nonblock(data, exception: false) == data.bytesize
  return false unless wait_writable(timeout)
  write_nonblock(data, exception: false) == data.bytesize
end