Module: NetHttp2::Socket
- Defined in:
- lib/net-http2/socket.rb
Class Method Summary collapse
- .create(uri, options) ⇒ Object
- .ssl_socket(uri, options) ⇒ Object
- .tcp_socket(uri, options) ⇒ Object
Class Method Details
.create(uri, options) ⇒ Object
5 6 7 |
# File 'lib/net-http2/socket.rb', line 5 def self.create(uri, ) [:ssl] ? ssl_socket(uri, ) : tcp_socket(uri, ) end |
.ssl_socket(uri, options) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/net-http2/socket.rb', line 9 def self.ssl_socket(uri, ) tcp = tcp_socket(uri, ) socket = OpenSSL::SSL::SSLSocket.new(tcp, [:ssl_context]) socket.sync_close = true socket.hostname = uri.hostname socket.connect socket end |
.tcp_socket(uri, options) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/net-http2/socket.rb', line 21 def self.tcp_socket(uri, ) family = ::Socket::AF_INET address = ::Socket.getaddrinfo(uri.host, nil, family).first[3] sockaddr = ::Socket.pack_sockaddr_in(uri.port, address) socket = ::Socket.new(family, ::Socket::SOCK_STREAM, 0) socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1) begin socket.connect_nonblock(sockaddr) rescue IO::WaitWritable if IO.select(nil, [socket], nil, [:connect_timeout]) begin socket.connect_nonblock(sockaddr) rescue Errno::EISCONN # socket is connected rescue socket.close raise end else socket.close raise Errno::ETIMEDOUT end end socket end |