Class: Excon::Socket
- Inherits:
-
Object
- Object
- Excon::Socket
- Extended by:
- Forwardable
- Defined in:
- lib/excon/socket.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(params = {}, proxy = {}) ⇒ Socket
constructor
A new instance of Socket.
- #read(max_length = nil) ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(params = {}, proxy = {}) ⇒ Socket
Returns a new instance of Socket.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/excon/socket.rb', line 11 def initialize(params = {}, proxy = {}) @params, @proxy = params, proxy @read_buffer, @write_buffer = '', '' @sockaddr = if @proxy ::Socket.sockaddr_in(@proxy[:port].to_i, @proxy[:host]) else ::Socket.sockaddr_in(@params[:port].to_i, @params[:host]) end @socket = ::Socket.new(::Socket::Constants::AF_INET, ::Socket::Constants::SOCK_STREAM, 0) connect @socket end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
6 7 8 |
# File 'lib/excon/socket.rb', line 6 def params @params end |
Instance Method Details
#connect ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/excon/socket.rb', line 28 def connect # nonblocking connect begin @socket.connect_nonblock(@sockaddr) rescue Errno::EINPROGRESS IO.select(nil, [@socket], nil, @params[:connect_timeout]) begin @socket.connect_nonblock(@sockaddr) rescue Errno::EISCONN end end end |
#read(max_length = nil) ⇒ Object
41 42 43 44 45 46 47 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 |
# File 'lib/excon/socket.rb', line 41 def read(max_length=nil) begin if max_length until @read_buffer.length >= max_length @read_buffer << @socket.read_nonblock(max_length - @read_buffer.length) end else while true @read_buffer << @socket.read_nonblock(CHUNK_SIZE) end end rescue OpenSSL::SSL::SSLError => error if error. == 'read would block' if IO.select([@socket], nil, nil, @params[:read_timeout]) retry else raise(Excon::Errors::Timeout.new("read timeout reached")) end end rescue Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitReadable if IO.select([@socket], nil, nil, @params[:read_timeout]) retry else raise(Excon::Errors::Timeout.new("read timeout reached")) end rescue EOFError end if max_length @read_buffer.slice!(0, max_length) else # read until EOFError, so return everything @read_buffer.slice!(0, @read_buffer.length) end end |
#write(data) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/excon/socket.rb', line 76 def write(data) @write_buffer << data until @write_buffer.empty? begin max_length = [@write_buffer.length, Excon::CHUNK_SIZE].min written = @socket.write_nonblock(@write_buffer.slice(0, max_length)) @write_buffer.slice!(0, written) rescue OpenSSL::SSL::SSLError => error if error. == 'write would block' if IO.select(nil, [@socket], nil, @params[:write_timeout]) retry else raise(Excon::Errors::Timeout.new("write timeout reached")) end end rescue Errno::EAGAIN, Errno::EWOULDBLOCK, IO::WaitWritable if IO.select(nil, [@socket], nil, @params[:write_timeout]) retry else raise(Excon::Errors::Timeout.new("write timeout reached")) end end end end |