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

Instance Method Details

#checkObject

Check.

Examples:

Check.

multi.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_timeoutObject

Get timeout.

Examples:

Get timeout.

multi.get_timeout

Raises:



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.

Examples:

Return multi handle.

multi.handle

Returns:

  • (::FFI::Pointer)

    The multi handle.



14
15
16
# File 'lib/ethon/multi/operations.rb', line 14

def handle
  @handle ||= Curl.multi_init
end

#init_varsObject

Initialize variables.

Examples:

Initialize variables.

multi.init_vars


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.

Examples:

Return if ongoing.

multi.ongoing?

Returns:

  • (Boolean)

    True if ongoing, else false.



37
38
39
# File 'lib/ethon/multi/operations.rb', line 37

def ongoing?
  easy_handles.size > 0 || (!defined?(@running_count) || running_count > 0)
end

#performObject

Perform multi.

Examples:

Perform multi.

multi.perform


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

#prepareObject

Prepare multi.

Examples:

Prepare multi.

multi.prepare


62
63
64
# File 'lib/ethon/multi/operations.rb', line 62

def prepare
  set_options
end

#reset_fdsObject

Reset file describtors.

Examples:

Reset fds.

multi.reset_fds


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

#runObject

Run.

Examples:

Run

multi.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_countInteger

Return number of running requests.

Examples:

Return count.

multi.running_count

Returns:

  • (Integer)

    Number 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.

Examples:

Set fds.

multi.set_fds

Raises:



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

#triggerObject

Trigger.

Examples:

Trigger.

multi.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