Class: Volt::SocketWithTimeout

Inherits:
Object
  • Object
show all
Defined in:
lib/volt/server/message_bus/peer_to_peer/socket_with_timeout.rb

Class Method Summary collapse

Class Method Details

.new(host, port, timeout = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/volt/server/message_bus/peer_to_peer/socket_with_timeout.rb', line 8

def self.new(host, port, timeout=nil)
  if RUBY_PLATFORM == 'java'
    TCPSocket.new(host, port)
  else
    addr = Socket.getaddrinfo(host, nil)
    sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)

    if timeout
      secs = Integer(timeout)
      usecs = Integer((timeout - secs) * 1_000_000)
      optval = [secs, usecs].pack("l_2")
      sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
      sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
    end
    sock.connect(Socket.pack_sockaddr_in(port, addr[0][3]))
    sock
  end
end