Class: Socket::Option
- Inherits:
-
Object
- Object
- Socket::Option
- Defined in:
- lib/socket/option.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute data.
-
#family ⇒ Object
readonly
Returns the value of attribute family.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#optname ⇒ Object
readonly
Returns the value of attribute optname.
Class Method Summary collapse
- .bool(family, level, optname, bool) ⇒ Object
- .int(family, level, optname, integer) ⇒ Object
- .linger(onoff, secs) ⇒ Object
Instance Method Summary collapse
- #bool ⇒ Object
-
#initialize(family, level, optname, data) ⇒ Option
constructor
A new instance of Option.
- #inspect ⇒ Object
- #int ⇒ Object
- #linger ⇒ Object
- #unpack(template) ⇒ Object
Constructor Details
#initialize(family, level, optname, data) ⇒ Option
Returns a new instance of Option.
26 27 28 29 30 31 |
# File 'lib/socket/option.rb', line 26 def initialize(family, level, optname, data) @family = RubySL::Socket.address_family(family) @level = RubySL::Socket::SocketOptions.socket_level(level, @family) @optname = RubySL::Socket::SocketOptions.socket_option(@level, optname) @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute data.
3 4 5 |
# File 'lib/socket/option.rb', line 3 def data @data end |
#family ⇒ Object (readonly)
Returns the value of attribute family.
3 4 5 |
# File 'lib/socket/option.rb', line 3 def family @family end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
3 4 5 |
# File 'lib/socket/option.rb', line 3 def level @level end |
#optname ⇒ Object (readonly)
Returns the value of attribute optname.
3 4 5 |
# File 'lib/socket/option.rb', line 3 def optname @optname end |
Class Method Details
.bool(family, level, optname, bool) ⇒ Object
5 6 7 |
# File 'lib/socket/option.rb', line 5 def self.bool(family, level, optname, bool) new(family, level, optname, [bool ? 1 : 0].pack('i')) end |
.int(family, level, optname, integer) ⇒ Object
9 10 11 |
# File 'lib/socket/option.rb', line 9 def self.int(family, level, optname, integer) new(family, level, optname, [integer].pack('i')) end |
Instance Method Details
#bool ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/socket/option.rb', line 41 def bool expected_size = RubySL::Socket::Foreign::SIZEOF_INT unless @data.bytesize == expected_size raise TypeError, "invalid size, expected #{expected_size} but got #{@data.bytesize}" end if @data.unpack('i')[0] > 0 true else false end end |
#inspect ⇒ Object
37 38 39 |
# File 'lib/socket/option.rb', line 37 def inspect "#<#{self.class}: #@family_name #@level_name #@opt_name #{@data.inspect}>" end |
#int ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/socket/option.rb', line 56 def int expected_size = RubySL::Socket::Foreign::SIZEOF_INT unless @data.bytesize == expected_size raise TypeError, "invalid size, expected #{expected_size} but got #{@data.bytesize}" end @data.unpack('i')[0] end |
#linger ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/socket/option.rb', line 67 def linger if @level != Socket::SOL_SOCKET || @optname != Socket::SO_LINGER raise TypeError, 'linger socket option expected' end expected_size = RubySL::Socket::Foreign::Linger.size if @data.bytesize != expected_size raise TypeError, "invalid size, expected #{expected_size} but got #{@data.bytesize}" end linger = RubySL::Socket::Foreign::Linger.from_string(@data) onoff = nil case linger.on_off when 0 onoff = false when 1 onoff = true else onoff = linger.onoff.to_i end [onoff, linger.linger] end |
#unpack(template) ⇒ Object
33 34 35 |
# File 'lib/socket/option.rb', line 33 def unpack(template) @data.unpack template end |