Module: RTunnel::SocketFactory
- Defined in:
- lib/rtunnel/socket_factory.rb
Class Method Summary collapse
- .bind(socket, options) ⇒ Object
- .bind_host(options) ⇒ Object
- .bind_port(options) ⇒ Object
- .bind_socket_address(options) ⇒ Object
- .connect(socket, options) ⇒ Object
- .connect_host(options) ⇒ Object
- .connect_port(options) ⇒ Object
- .connect_socket_address(options) ⇒ Object
- .host_from_address(address) ⇒ Object
- .inbound?(options) ⇒ Boolean
- .new_socket(options) ⇒ Object
- .new_tcp_socket ⇒ Object
- .new_udp_socket ⇒ Object
- .port_from_address(address) ⇒ Object
- .set_options(socket, options) ⇒ Object
-
.set_options_on_accept_sockets(socket, options) ⇒ Object
new sockets coming out of socket.accept will have the given options set.
- .socket(options = {}) ⇒ Object
- .split_address(address) ⇒ Object
- .tcp?(options) ⇒ Boolean
Instance Method Summary collapse
Class Method Details
.bind(socket, options) ⇒ Object
65 66 67 |
# File 'lib/rtunnel/socket_factory.rb', line 65 def self.bind(socket, ) socket.bind bind_socket_address() end |
.bind_host(options) ⇒ Object
25 26 27 |
# File 'lib/rtunnel/socket_factory.rb', line 25 def self.bind_host() [:in_host] or host_from_address([:in_addr]) or '0.0.0.0' end |
.bind_port(options) ⇒ Object
29 30 31 |
# File 'lib/rtunnel/socket_factory.rb', line 29 def self.bind_port() [:in_port] or port_from_address([:in_addr]) or 0 end |
.bind_socket_address(options) ⇒ Object
33 34 35 |
# File 'lib/rtunnel/socket_factory.rb', line 33 def self.bind_socket_address() Socket::pack_sockaddr_in bind_port(), bind_host() end |
.connect(socket, options) ⇒ Object
69 70 71 |
# File 'lib/rtunnel/socket_factory.rb', line 69 def self.connect(socket, ) socket.connect connect_socket_address() end |
.connect_host(options) ⇒ Object
37 38 39 |
# File 'lib/rtunnel/socket_factory.rb', line 37 def self.connect_host() [:out_host] or host_from_address([:out_addr]) end |
.connect_port(options) ⇒ Object
41 42 43 |
# File 'lib/rtunnel/socket_factory.rb', line 41 def self.connect_port() [:out_port] or port_from_address([:out_addr]) end |
.connect_socket_address(options) ⇒ Object
45 46 47 |
# File 'lib/rtunnel/socket_factory.rb', line 45 def self.connect_socket_address() Socket::pack_sockaddr_in connect_port(), connect_host() end |
.host_from_address(address) ⇒ Object
13 14 15 |
# File 'lib/rtunnel/socket_factory.rb', line 13 def self.host_from_address(address) address and split_address(address)[0] end |
.inbound?(options) ⇒ Boolean
21 22 23 |
# File 'lib/rtunnel/socket_factory.rb', line 21 def self.inbound?() [:inbound] or [:in_port, :in_host, :in_addr].any? { |k| [k] } end |
.new_socket(options) ⇒ Object
61 62 63 |
# File 'lib/rtunnel/socket_factory.rb', line 61 def self.new_socket() tcp?() ? new_tcp_socket : new_udp_socket end |
.new_tcp_socket ⇒ Object
53 54 55 |
# File 'lib/rtunnel/socket_factory.rb', line 53 def self.new_tcp_socket Socket.new Socket::AF_INET, Socket::SOCK_STREAM, Socket::PF_UNSPEC end |
.new_udp_socket ⇒ Object
57 58 59 |
# File 'lib/rtunnel/socket_factory.rb', line 57 def self.new_udp_socket Socket.new Socket::AF_INET, Socket::SOCK_DGRAM, Socket::PF_UNSPEC end |
.port_from_address(address) ⇒ Object
17 18 19 |
# File 'lib/rtunnel/socket_factory.rb', line 17 def self.port_from_address(address) address and (port_string = split_address(address)[1]) and port_string.to_i end |
.set_options(socket, options) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rtunnel/socket_factory.rb', line 73 def self.(socket, ) if [:no_delay] socket.setsockopt Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true socket.sync = true end if [:reuse_addr] socket.setsockopt Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true end unless [:reverse_lookup] if socket.respond_to? :do_not_reverse_lookup socket.do_not_reverse_lookup = true else # work around until the patch below actually gets committed: # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/2346 BasicSocket.do_not_reverse_lookup = true end end end |
.set_options_on_accept_sockets(socket, options) ⇒ Object
new sockets coming out of socket.accept will have the given options set
95 96 97 98 99 100 101 102 |
# File 'lib/rtunnel/socket_factory.rb', line 95 def self.(socket, ) socket.instance_variable_set :@rtunnel_factory_options, def socket.accept(*args) sock, addr = super RTunnel::SocketFactory. sock, @rtunnel_factory_options return sock, addr end end |
.socket(options = {}) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rtunnel/socket_factory.rb', line 104 def self.socket( = {}) s = new_socket s, if inbound? bind s, s, else connect s, end s end |
.split_address(address) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/rtunnel/socket_factory.rb', line 4 def self.split_address(address) port_index = address.index /[^:]\:[^:]/ if port_index [address[0, port_index + 1], address[port_index + 2, address.length]] else [address, nil] end end |
.tcp?(options) ⇒ Boolean
49 50 51 |
# File 'lib/rtunnel/socket_factory.rb', line 49 def self.tcp?() [:tcp] or ![:udp] end |
Instance Method Details
#socket(options = {}) ⇒ Object
116 117 118 |
# File 'lib/rtunnel/socket_factory.rb', line 116 def socket( = {}) RTunnel::SocketFactory.socket() end |