Class: Soren::Socket::IO

Inherits:
Object show all
Defined in:
lib/soren/socket/io.rb

Instance Method Summary collapse

Constructor Details

#initialize(socket, request, options, host:) ⇒ IO

: ((TCPSocket | OpenSSL::SSL::SSLSocket), Soren::Request, Soren::Options, host: Soren::Types::Connection::Host) -> void



13
14
15
16
17
18
# File 'lib/soren/socket/io.rb', line 13

def initialize(socket, request, options, host:)
  @socket = socket #: (TCPSocket | OpenSSL::SSL::SSLSocket)
  @request = request #: Soren::Request
  @options = options #: Soren::Options
  @host = host #: Soren::Types::Connection::Host
end

Instance Method Details

#read_responseObject

: -> Hash[Symbol, untyped]



42
43
44
45
# File 'lib/soren/socket/io.rb', line 42

def read_response
  deadline = Deadline.start(@options.read_timeout.to_f)
  Soren::Parsers::Response.new(@socket, deadline: deadline).parse
end

#write_requestObject

: -> Integer



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/soren/socket/io.rb', line 21

def write_request
  data = @request.to_http(host: @host.to_s)
  total_written = 0

  deadline = Deadline.start(@options.write_timeout.to_f)

  while total_written < data.bytesize
    raise Soren::Error::WriteTimeout if deadline&.expired?

    begin
      written = @socket.write_nonblock(data.byteslice(total_written..))
      total_written += written
    rescue StandardError => e
      handle_write_error(e, deadline)
    end
  end

  total_written
end