class IOPromise::Faraday::MultiSocketAction

Constants

CURLM_OK
CURL_CSELECT_ERR
CURL_CSELECT_IN
CURL_CSELECT_OUT
CURL_POLL_IN
CURL_POLL_INOUT
CURL_POLL_NONE
CURL_POLL_OUT
CURL_POLL_REMOVE
CURL_SOCKET_BAD
CURL_SOCKET_TIMEOUT

Attributes

iop_handler[RW]

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/iopromise/faraday/multi_socket_action.rb, line 28
def initialize(options = {})
  super(options)
    
  @ios = {}
  @iop_handler = nil
  @notified_fds = 0
    
  self.socketfunction = @keep_socketfunction = proc do |handle, sock, what, userp, socketp|
    if what == CURL_POLL_REMOVE
      io = @ios.delete(sock)
      iop_handler.monitor_remove(io) unless io.nil?
    else
      # reuse existing if we have it anywhere
      io = @ios[sock]
      if io.nil?
        io = @ios[sock] = IO.for_fd(sock).tap { |io| io.autoclose = false }
        iop_handler.monitor_add(io)
      end
      if what == CURL_POLL_INOUT
        iop_handler.set_interests(io, :rw)
      elsif what == CURL_POLL_IN
        iop_handler.set_interests(io, :r)
      elsif what == CURL_POLL_OUT
        iop_handler.set_interests(io, :w)
      end
    end
    CURLM_OK
  end
    
  self.timerfunction = @keep_timerfunction = proc do |handle, timeout_ms, userp|
    if timeout_ms > 0x7fffffffffffffff # FIXME: wrongly encoded
      select_timeout = nil
    else
      select_timeout = timeout_ms.to_f / 1_000
    end
    iop_handler.set_timeout(select_timeout)
    CURLM_OK
  end
end

Public Instance Methods

execute_continue() click to toggle source
# File lib/iopromise/faraday/multi_socket_action.rb, line 87
def execute_continue
  running_handles = ::FFI::MemoryPointer.new(:int)
    
  if @notified_fds == 0
    # no FDs were readable/writable so we send the timeout fd, which lets
    # curl perform housekeeping.
    Ethon::Curl.multi_socket_action(handle, CURL_SOCKET_TIMEOUT, 0, running_handles)
  else
    @notified_fds = 0
  end
    
  check
end
perform() click to toggle source
# File lib/iopromise/faraday/multi_socket_action.rb, line 68
def perform
  # stubbed out, we don't want any of the multi_perform logic
end
run() click to toggle source
# File lib/iopromise/faraday/multi_socket_action.rb, line 72
def run
  # stubbed out, we don't want any of the multi_perform logic
end
socket_is_ready(io, readable, writable) click to toggle source
# File lib/iopromise/faraday/multi_socket_action.rb, line 76
def socket_is_ready(io, readable, writable)
  running_handles = ::FFI::MemoryPointer.new(:int)

  bitmask = 0
  bitmask |= CURL_CSELECT_IN if readable
  bitmask |= CURL_CSELECT_OUT if writable

  Ethon::Curl.multi_socket_action(handle, io.fileno, bitmask, running_handles)
  @notified_fds += 1
end