Class: XS::Util
- Inherits:
-
Object
- Object
- XS::Util
- Defined in:
- lib/ffi-rxs/util.rb
Overview
Provides general utility methods
Class Method Summary collapse
-
.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.
-
.errno ⇒ Object
Returns error number.
-
.error_check(source, result_code) ⇒ Object
Called to verify there were no errors during operation.
-
.error_string ⇒ Object
Returns error string.
-
.resultcode_ok?(rc) ⇒ Boolean
We use the >= test because xs_poll() returns the number of sockets that had a read or write event triggered.
-
.version ⇒ Object
Returns libxs version number.
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.
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 |
.errno ⇒ Object
Returns error number
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.
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_string ⇒ Object
Returns error string
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.
14 15 16 |
# File 'lib/ffi-rxs/util.rb', line 14 def self.resultcode_ok? rc rc >= 0 end |
.version ⇒ Object
Returns libxs version number
Class method! Invoke as: XS::Util.version
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 |