Class: RaptorIO::Socket::Comm::Local
- Inherits:
-
RaptorIO::Socket::Comm
- Object
- RaptorIO::Socket::Comm
- RaptorIO::Socket::Comm::Local
- Defined in:
- lib/raptor-io/socket/comm/local.rb
Overview
Local communication using Ruby ‘::Socket`s
Instance Method Summary collapse
-
#create_tcp(options) ⇒ Socket::TCP
Connect to ‘:peer_host`.
-
#create_tcp_server(options) ⇒ Object
Listen locally on ‘:local_port`.
-
#resolve(hostname) ⇒ Object
Resolves a hostname to an IP address using this comm.
-
#reverse_resolve(ip_address) ⇒ Object
Resolves an IP address to a hostname using this comm.
-
#support_ipv6? ⇒ Boolean
Determine whether we support IPv6.
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`
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( ) phost = IPAddr.parse( [: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 [:local_port] || [:local_host] socket.bind(::Socket.pack_sockaddr_in([:local_port], [:local_host])) end begin socket.connect_nonblock(::Socket.pack_sockaddr_in([: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, [:connect_timeout] || 2) if res.nil? raise RaptorIO::Socket::Error::ConnectionTimeout end end if [:ssl_context] RaptorIO::Socket::TCP::SSL.new(socket, ) else RaptorIO::Socket::TCP.new(socket, ) end end |
#create_tcp_server(options) ⇒ Object
Listen locally on ‘:local_port`
84 85 86 87 88 89 90 91 92 |
# File 'lib/raptor-io/socket/comm/local.rb', line 84 def create_tcp_server( ) socket = TCPServer.new( [:local_host], [:local_port] ) if ([:context] = .delete(:ssl_context)) RaptorIO::Socket::TCPServer::SSL.new( socket, ) else RaptorIO::Socket::TCPServer.new( socket, ) end end |
#resolve(hostname) ⇒ Object
Resolves a hostname to an IP address using this comm.
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.
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
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 |