Module: Ethon::Multi::Operations

Included in:
Ethon::Multi
Defined in:
lib/ethon/multi/operations.rb

Overview

This module contains logic to run a multi.

Constant Summary collapse

STARTED_MULTI =
"ETHON: started MULTI"
PERFORMED_MULTI =
"ETHON: performed MULTI"

Instance Method Summary collapse

Instance Method Details

#handleFFI::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.



16
17
18
# File 'lib/ethon/multi/operations.rb', line 16

def handle
  @handle ||= FFI::AutoPointer.new(Curl.multi_init, Curl.method(:multi_cleanup))
end

#init_varsvoid

This method returns an undefined value.

Initialize variables.

Examples:

Initialize variables.

multi.init_vars


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ethon/multi/operations.rb', line 26

def init_vars
  if @execution_mode == :perform
    @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)
  elsif @execution_mode == :socket_action
    @running_count_pointer = FFI::MemoryPointer.new(:int)
  end
end

#ongoing?Boolean

Return whether the multi still contains requests or not.

Examples:

Return if ongoing.

multi.ongoing?

Returns:

  • (Boolean)

    True if ongoing, else false.



113
114
115
# File 'lib/ethon/multi/operations.rb', line 113

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

#performnil

Perform multi.

Examples:

Perform multi.

multi.perform

Returns:

  • (nil)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ethon/multi/operations.rb', line 45

def perform
  ensure_execution_mode(:perform)

  Ethon.logger.debug(STARTED_MULTI)
  while ongoing?
    run
    timeout = get_timeout
    next if timeout == 0
    reset_fds
    set_fds(timeout)
  end
  Ethon.logger.debug(PERFORMED_MULTI)
  nil
end

#preparenil

Deprecated.

It is no longer necessary to call prepare.

Prepare multi.

Examples:

Prepare multi.

multi.prepare

Returns:

  • (nil)


68
69
70
71
72
73
74
# File 'lib/ethon/multi/operations.rb', line 68

def prepare
  Ethon.logger.warn(
    "ETHON: It is no longer necessay to call "+
    "Multi#prepare. Its going to be removed "+
    "in future versions."
  )
end

#socket_action(io = nil, readiness = 0) ⇒ Symbol

Continue execution with an external IO loop.

Examples:

When no sockets are ready yet, or to begin.

multi.socket_action

When a socket is readable

multi.socket_action(io_object, [:in])

When a socket is readable and writable

multi.socket_action(io_object, [:in, :out])

Returns:

  • (Symbol)

    The Curl.multi_socket_action return code.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ethon/multi/operations.rb', line 88

def socket_action(io = nil, readiness = 0)
  ensure_execution_mode(:socket_action)

  fd = if io.nil?
    ::Ethon::Curl::SOCKET_TIMEOUT
  elsif io.is_a?(Integer)
    io
  else
    io.fileno
  end

  code = Curl.multi_socket_action(handle, fd, readiness, @running_count_pointer)
  @running_count = @running_count_pointer.read_int

  check

  code
end