Module: CZTop::HasFFIDelegate

Included in:
Socket
Defined in:
lib/cztop/has_ffi_delegate.rb

Overview

This module is used to attach the low-level objects of classes within the CZMQ::FFI namespace as delegates.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ffi_delegateCZMQ::FFI::* (readonly)

Returns the attached delegate.

Returns:



13
14
15
# File 'lib/cztop/has_ffi_delegate.rb', line 13

def ffi_delegate
  @ffi_delegate
end

Class Method Details

.raise_zmq_err(msg = nil, errno: nil) ⇒ Object

Raises the appropriate exception for the reported ZMQ error.

Captures zmq_errno once to avoid a race where a subsequent ZMQ call overwrites it before we read it.

Parameters:

  • msg (String) (defaults to: nil)

    error message (default: ZMQ's strerror for current errno)

  • errno (Integer) (defaults to: nil)

    ZMQ errno (default: current zmq_errno)

Raises:

  • (ArgumentError)

    if EINVAL was reported

  • (Interrupt)

    if EINTR was reported

  • (SocketError)

    if EHOSTUNREACH was reported

  • (SystemCallError)

    any other reported error (appropriate SystemCallError subclass, if errno is known)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cztop/has_ffi_delegate.rb', line 61

def raise_zmq_err(msg = nil, errno: nil)
  errno ||= CZMQ::FFI::Errors.errno
  msg   ||= CZMQ::FFI::Errors.strerror(errno)
  case errno
  when Errno::EINVAL::Errno
    fail ArgumentError, msg, caller
  when Errno::EINTR::Errno
    fail Interrupt, msg, caller
  when Errno::EHOSTUNREACH::Errno
    fail SocketError, msg, caller
  else
    # If the errno is known, the corresponding Errno::* exception is
    # automatically constructed. Otherwise, it'll be a plain SystemCallError.
    # In any case, #errno will return the corresponding errno.
    fail SystemCallError.new(msg, errno), msg, caller
  end
end

Instance Method Details

#attach_ffi_delegate(ffi_delegate) ⇒ void

Note:

This only raises the correct exception when the creation of the new CZMQ object was the most recent thing done with the CZMQ library and thus CZMQ::FFI::Errors.errno still reports the correct error number.

This method returns an undefined value.

Attaches an FFI delegate to the current (probably new) CZTop object.

Parameters:

  • ffi_delegate

    an instance of the corresponding class in the CZMQ::FFI namespace

Raises:

  • (SystemCallError, ArgumentError, ...)

    if the FFI delegate's internal pointer is NULL

See Also:



32
33
34
35
# File 'lib/cztop/has_ffi_delegate.rb', line 32

def attach_ffi_delegate(ffi_delegate)
  raise_zmq_err if ffi_delegate.null?
  @ffi_delegate = ffi_delegate
end

#from_ffi_delegate(ffi_delegate) ⇒ CZTop::*

Same as the counterpart in ClassMethods, but usable from within an instance.

Returns:

See Also:

  • FFIDelegate::ClassMethods#from_ffi_delegate


42
43
44
# File 'lib/cztop/has_ffi_delegate.rb', line 42

def from_ffi_delegate(ffi_delegate)
  self.class.from_ffi_delegate(ffi_delegate)
end

#to_ptrFFI::Pointer

Returns FFI delegate's pointer.

Returns:

  • (FFI::Pointer)

    FFI delegate's pointer



17
18
19
# File 'lib/cztop/has_ffi_delegate.rb', line 17

def to_ptr
  @ffi_delegate.to_ptr
end