Class: XS::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-rxs/util.rb

Overview

Provides general utility methods

Class Method Summary collapse

Class Method Details

.bind_to_random_tcp_port(host = '127.0.0.1', max_tries = 500) ⇒ Object

Attempts to bind to a random tcp port on host up to max_tries times.

Returns:

  • port number upon success

  • nil upon failure



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ffi-rxs/util.rb', line 52

def self.bind_to_random_tcp_port host = '127.0.0.1', max_tries = 500
  tries = 0
  rc = -1

  while !resultcode_ok?(rc) && tries < max_tries
    tries += 1
    random = random_port
    rc = socket.bind "tcp://#{host}:#{random}"
  end

  resultcode_ok?(rc) ? random : nil
end

.errnoObject

Returns error number

Returns:

  • errno as set by the libxs library.



21
22
23
# File 'lib/ffi-rxs/util.rb', line 21

def self.errno
  LibXS.xs_errno
end

.error_check(source, result_code) ⇒ Object

Called to verify there were no errors during operation. If any are found, raise the appropriate XSError.

Returns:

  • true when no error is found which is behavior used internally by #send and #recv.



70
71
72
73
74
75
76
77
# File 'lib/ffi-rxs/util.rb', line 70

def self.error_check source, result_code
  if result_code < 0
    raise_error source, result_code
  end

  # used by Socket::send/recv, ignored by others
  true
end

.error_stringObject

Returns error string

Returns:

  • string corresponding to the currently set #errno. These error strings are defined by libxs.



29
30
31
# File 'lib/ffi-rxs/util.rb', line 29

def self.error_string
  LibXS.xs_strerror(errno).read_string
end

.resultcode_ok?(rc) ⇒ Boolean

We use the >= test because xs_poll() returns the number of sockets that had a read or write event triggered. So, a >= 0 result means it succeeded.

Returns:

  • (Boolean)

    true when rc is greater than or equal to 0

  • false otherwise



14
15
16
# File 'lib/ffi-rxs/util.rb', line 14

def self.resultcode_ok? rc
  rc >= 0
end

.versionObject

Returns libxs version number

Class method! Invoke as: XS::Util.version

Returns:

  • array of the form [major, minor, patch] to represent the version of libxs



39
40
41
42
43
44
45
# File 'lib/ffi-rxs/util.rb', line 39

def self.version
  major = FFI::MemoryPointer.new :int
  minor = FFI::MemoryPointer.new :int
  patch = FFI::MemoryPointer.new :int
  LibXS.xs_version major, minor, patch
  [major.read_int, minor.read_int, patch.read_int]
end