Class: RaptorIO::Socket::Comm::Local

Inherits:
RaptorIO::Socket::Comm show all
Defined in:
lib/raptor-io/socket/comm/local.rb

Overview

Local communication using Ruby ‘::Socket`s

Instance Method Summary collapse

Methods inherited from RaptorIO::Socket::Comm

#create, #create_udp, #create_udp_server, from_uri

Instance Method Details

#create_tcp(options) ⇒ Socket::TCP

Connect to ‘:peer_host`

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

Returns:

Raises:

  • (RaptorIO::Socket::Error::ConnectTimeout)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/raptor-io/socket/comm/local.rb', line 48

def create_tcp( options )
  phost = IPAddr.parse( options[:peer_host] )

  # Passing an explicit ::Socket::IPPROTO_TCP is broken on jruby
  #  See https://github.com/jruby/jruby/issues/785
  socket = ::Socket.new(phost.family, ::Socket::SOCK_STREAM, 0)
  socket.do_not_reverse_lookup = true

  if options[:local_port] || options[:local_host]
    socket.bind(::Socket.pack_sockaddr_in(options[:local_port], options[:local_host]))
  end

  begin
    socket.connect_nonblock(::Socket.pack_sockaddr_in(options[:peer_port], phost.to_s))
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET
    raise RaptorIO::Socket::Error::ConnectionRefused
  rescue Errno::EINPROGRESS
    # This should almost always be raised with a call to
    # connect_nonblock. When the socket finishes connecting it
    # becomes available for writing.
    res = select(nil, [socket], nil, options[:connect_timeout] || 2)
    if res.nil?
      raise RaptorIO::Socket::Error::ConnectionTimeout
    end
  end

  if options[:ssl_context]
    RaptorIO::Socket::TCP::SSL.new(socket, options)
  else
    RaptorIO::Socket::TCP.new(socket, options)
  end
end

#create_tcp_server(options) ⇒ Object

Listen locally on ‘:local_port`

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :local_host (String, IPAddr)
  • :local_port (Fixnum)
  • :ssl_context (OpenSSL::SSL::Context)


84
85
86
87
88
89
90
91
92
# File 'lib/raptor-io/socket/comm/local.rb', line 84

def create_tcp_server( options )
  socket = TCPServer.new( options[:local_host], options[:local_port] )

  if (options[:context] = options.delete(:ssl_context))
    RaptorIO::Socket::TCPServer::SSL.new( socket, options )
  else
    RaptorIO::Socket::TCPServer.new( socket, options )
  end
end

#resolve(hostname) ⇒ Object

Resolves a hostname to an IP address using this comm.

Parameters:



32
33
34
# File 'lib/raptor-io/socket/comm/local.rb', line 32

def resolve( hostname )
  ::Resolv.getaddress hostname
end

#reverse_resolve(ip_address) ⇒ Object

Resolves an IP address to a hostname using this comm.

Parameters:



39
40
41
# File 'lib/raptor-io/socket/comm/local.rb', line 39

def reverse_resolve( ip_address )
  ::Resolv.getname ip_address
end

#support_ipv6?Boolean

Determine whether we support IPv6

We attempt to discover this by creating an unbound UDP socket with the AF_INET6 address family

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/raptor-io/socket/comm/local.rb', line 13

def support_ipv6?
  return @supports_ipv6 unless @supports_ipv6.nil?

  @supports_ipv6 = false

  if ::Socket.const_defined?('AF_INET6')
    begin
      ::Socket.new(::Socket::AF_INET6, ::Socket::SOCK_DGRAM, ::Socket::IPPROTO_UDP).close
      @supports_ipv6 = true
    rescue
    end
  end

  @supports_ipv6
end