Class: Socket::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/socket/option.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dataObject (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

#familyObject (readonly)

Returns the value of attribute family.



3
4
5
# File 'lib/socket/option.rb', line 3

def family
  @family
end

#levelObject (readonly)

Returns the value of attribute level.



3
4
5
# File 'lib/socket/option.rb', line 3

def level
  @level
end

#optnameObject (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

.linger(onoff, secs) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/socket/option.rb', line 13

def self.linger(onoff, secs)
  linger = RubySL::Socket::Foreign::Linger.new

  begin
    linger.on_off = onoff
    linger.linger = secs

    new(:UNSPEC, :SOCKET, :LINGER, linger.to_s)
  ensure
    linger.free
  end
end

Instance Method Details

#boolObject



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

#inspectObject



37
38
39
# File 'lib/socket/option.rb', line 37

def inspect
  "#<#{self.class}: #@family_name #@level_name #@opt_name #{@data.inspect}>"
end

#intObject



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

#lingerObject



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