Class: IOPromise::Faraday::MultiSocketAction

Inherits:
Ethon::Multi
  • Object
show all
Defined in:
lib/iopromise/faraday/multi_socket_action.rb

Constant Summary collapse

CURL_POLL_NONE =
0
CURL_POLL_IN =
1
CURL_POLL_OUT =
2
CURL_POLL_INOUT =
3
CURL_POLL_REMOVE =
4
CURL_SOCKET_BAD =
-1
CURL_SOCKET_TIMEOUT =
CURL_SOCKET_BAD
CURLM_OK =
0
CURL_CSELECT_IN =
0x01
CURL_CSELECT_OUT =
0x02
CURL_CSELECT_ERR =
0x04

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MultiSocketAction

Returns a new instance of MultiSocketAction.



28
29
30
31
32
33
34
35
36
37
38
39
40
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
# 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

Instance Attribute Details

#iop_handlerObject

Returns the value of attribute iop_handler.



26
27
28
# File 'lib/iopromise/faraday/multi_socket_action.rb', line 26

def iop_handler
  @iop_handler
end

Instance Method Details

#execute_continueObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# 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

#performObject



68
69
70
# 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

#runObject



72
73
74
# 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) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# 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