Module: Ethon::Multi::Operations
- Included in:
- Ethon::Multi
- Defined in:
- lib/ethon/multi/operations.rb
Overview
This module contains logic to run a multi.
Instance Method Summary collapse
-
#check ⇒ Object
Check.
-
#get_timeout ⇒ Object
Get timeout.
-
#handle ⇒ ::FFI::Pointer
Return the multi handle.
-
#init_vars ⇒ Object
Initialize variables.
-
#ongoing? ⇒ Boolean
Return wether the multi still requests or not.
-
#perform ⇒ Object
Perform multi.
-
#prepare ⇒ Object
Prepare multi.
-
#reset_fds ⇒ Object
Reset file describtors.
-
#run ⇒ Object
Run.
-
#running_count ⇒ Integer
Return number of running requests.
-
#set_fds(timeout) ⇒ Object
Set fds.
-
#trigger ⇒ Object
Trigger.
Instance Method Details
#check ⇒ Object
Check.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ethon/multi/operations.rb', line 115 def check msgs_left = ::FFI::MemoryPointer.new(:int) while true msg = Curl.multi_info_read(handle, msgs_left) break if msg.null? next if msg[:code] != :done easy = easy_handles.find{ |e| e.handle == msg[:easy_handle] } easy.return_code = msg[:data][:code] delete(easy) easy.complete Ethon.logger.debug("ETHON: performed #{easy.log_inspect}") end end |
#get_timeout ⇒ Object
Get timeout.
72 73 74 75 76 77 78 |
# File 'lib/ethon/multi/operations.rb', line 72 def get_timeout code = Curl.multi_timeout(handle, @timeout) raise Errors::MultiTimeout.new(code) unless code == :ok timeout = @timeout.read_long timeout = 1 if timeout < 0 timeout end |
#handle ⇒ ::FFI::Pointer
Return the multi handle. Inititialize multi handle, in case it didn’t happened already.
14 15 16 |
# File 'lib/ethon/multi/operations.rb', line 14 def handle @handle ||= Curl.multi_init end |
#init_vars ⇒ Object
Initialize variables.
22 23 24 25 26 27 28 29 |
# File 'lib/ethon/multi/operations.rb', line 22 def init_vars @timeout = ::FFI::MemoryPointer.new(:long) @timeval = Curl::Timeval.new @fd_read = Curl::FDSet.new @fd_write = Curl::FDSet.new @fd_excep = Curl::FDSet.new @max_fd = ::FFI::MemoryPointer.new(:int) end |
#ongoing? ⇒ Boolean
Return wether the multi still requests or not.
37 38 39 |
# File 'lib/ethon/multi/operations.rb', line 37 def ongoing? easy_handles.size > 0 || (!defined?(@running_count) || running_count > 0) end |
#perform ⇒ Object
Perform multi.
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ethon/multi/operations.rb', line 45 def perform Ethon.logger.debug("ETHON: started MULTI") while ongoing? run timeout = get_timeout next if timeout == 0 reset_fds set_fds(timeout) end Ethon.logger.debug("ETHON: performed MULTI") nil end |
#prepare ⇒ Object
Prepare multi.
62 63 64 |
# File 'lib/ethon/multi/operations.rb', line 62 def prepare end |
#reset_fds ⇒ Object
Reset file describtors.
84 85 86 87 88 |
# File 'lib/ethon/multi/operations.rb', line 84 def reset_fds @fd_read.clear @fd_write.clear @fd_excep.clear end |
#run ⇒ Object
Run.
133 134 135 136 |
# File 'lib/ethon/multi/operations.rb', line 133 def run begin code = trigger end while code == :call_multi_perform check end |
#running_count ⇒ Integer
Return number of running requests.
155 156 157 |
# File 'lib/ethon/multi/operations.rb', line 155 def running_count @running_count ||= nil end |
#set_fds(timeout) ⇒ Object
Set fds.
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ethon/multi/operations.rb', line 97 def set_fds(timeout) code = Curl.multi_fdset(handle, @fd_read, @fd_write, @fd_excep, @max_fd) raise Errors::MultiFdset.new(code) unless code == :ok max_fd = @max_fd.read_int if max_fd == -1 sleep(0.001) else @timeval[:sec] = timeout / 1000 @timeval[:usec] = (timeout * 1000) % 1000000 code = Curl.select(max_fd + 1, @fd_read, @fd_write, @fd_excep, @timeval) raise Errors::Select.new(::FFI.errno) if code < 0 end end |
#trigger ⇒ Object
Trigger.
142 143 144 145 146 147 |
# File 'lib/ethon/multi/operations.rb', line 142 def trigger running_count = FFI::MemoryPointer.new(:int) code = Curl.multi_perform(handle, running_count) @running_count = running_count.read_int code end |