Class: Excon::Socket
- Inherits:
-
Object
- Object
- Excon::Socket
- Extended by:
- Forwardable
- Defined in:
- lib/excon/socket.rb
Direct Known Subclasses
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(connection_params = {}, proxy = {}) ⇒ Socket
constructor
A new instance of Socket.
- #read(max_length) ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(connection_params = {}, proxy = {}) ⇒ Socket
Returns a new instance of Socket.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/excon/socket.rb', line 9 def initialize(connection_params = {}, proxy = {}) @connection_params, @proxy = connection_params, proxy @read_buffer, @write_buffer = '', '' @sockaddr = if @proxy ::Socket.sockaddr_in(@proxy[:port], @proxy[:host]) else ::Socket.sockaddr_in(@connection_params[:port], @connection_params[:host]) end @socket = ::Socket.new(::Socket::Constants::AF_INET, ::Socket::Constants::SOCK_STREAM, 0) connect @socket end |
Instance Method Details
#connect ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/excon/socket.rb', line 26 def connect # nonblocking connect begin @socket.connect_nonblock(@sockaddr) rescue Errno::EINPROGRESS IO.select(nil, [@socket], nil, @connection_params[:connect_timeout]) begin @socket.connect_nonblock(@sockaddr) rescue Errno::EISCONN end end end |
#read(max_length) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/excon/socket.rb', line 39 def read(max_length) begin until @read_buffer.length >= max_length @read_buffer << @socket.read_nonblock(max_length) end rescue Errno::EAGAIN, Errno::EWOULDBLOCK if IO.select([@socket], nil, nil, @connection_params[:read_timeout]) retry else raise(Excon::Errors::Timeout.new("read timeout reached")) end end @read_buffer.slice!(0, max_length) end |
#write(data) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/excon/socket.rb', line 54 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 Errno::EAGAIN, Errno::EWOULDBLOCK if IO.select(nil, [@socket], nil, @connection_params[:write_timeout]) retry else raise(Excon::Errors::Timeout.new("write timeout reached")) end end end end |