123456789_123456789_123456789_123456789_123456789_

Class: Socket::Option

Relationships & Source Files
Inherits: Object
Defined in: ext/socket/option.c

Overview

Option represents a socket option used by BasicSocket#getsockopt and BasicSocket#setsockopt. A socket option contains the socket #family, protocol #level, option name #optname and option value #data.

Class Method Summary

Instance Method Summary

Constructor Details

.new(family, level, optname, data) ⇒ sockopt

Returns a new Option object.

sockopt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i"))
p sockopt #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>

Class Method Details

.bool(family, level, optname, bool) ⇒ sockopt

Creates a new Option object which contains boolean as data. Actually 0 or 1 as int is used.

p Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, true)
#=> #<Socket::Option: INET SOCKET KEEPALIVE 1>

p Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, false)
#=> #<Socket::Option: AF_INET SOCKET KEEPALIVE 0>

.byte(family, level, optname, integer) ⇒ sockopt

Creates a new Option object which contains a byte as data.

p Socket::Option.byte(:INET, :SOCKET, :KEEPALIVE, 1)
#=> #<Socket::Option: INET SOCKET KEEPALIVE 1>

.int(family, level, optname, integer) ⇒ sockopt

Creates a new Option object which contains an int as data.

The size and endian is dependent on the platform.

p Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
#=> #<Socket::Option: INET SOCKET KEEPALIVE 1>

.ipv4_multicast_loop(integer) ⇒ sockopt

Creates a new Option object for IP_MULTICAST_LOOP.

The size is dependent on the platform.

sockopt = Socket::Option.int(:INET, :IPPROTO_IP, :IP_MULTICAST_LOOP, 1)
p sockopt.int => 1

p Socket::Option.ipv4_multicast_loop(10)
#=> #<Socket::Option: INET IP MULTICAST_LOOP 10>

.ipv4_multicast_ttl(integer) ⇒ sockopt

Creates a new Option object for IP_MULTICAST_TTL.

The size is dependent on the platform.

p Socket::Option.ipv4_multicast_ttl(10)
#=> #<Socket::Option: INET IP MULTICAST_TTL 10>

.linger(onoff, secs) ⇒ sockopt

Creates a new Option object for SOL_SOCKET/SO_LINGER.

onoff should be an integer or a boolean.

secs should be the number of seconds.

p Socket::Option.linger(true, 10)
#=> #<Socket::Option: UNSPEC SOCKET LINGER on 10sec>

Instance Method Details

#boolBoolean

Returns the data in sockopt as an boolean value.

sockopt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
p sockopt.bool => true

#byteInteger

Returns the data in sockopt as an byte.

sockopt = Socket::Option.byte(:INET, :SOCKET, :KEEPALIVE, 1)
p sockopt.byte => 1

#dataString #to_sString

Alias for #to_s.

#familyInteger

returns the socket family as an integer.

p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).family
#=> 10

#inspectString

Returns a string which shows sockopt in human-readable form.

p Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i")).inspect
#=> "#<Socket::Option: INET SOCKET KEEPALIVE 1>"

#intInteger

Returns the data in sockopt as an int.

The size and endian is dependent on the platform.

sockopt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
p sockopt.int => 1

#ipv4_multicast_loopInteger

Returns the ipv4_multicast_loop data in sockopt as a integer.

sockopt = Socket::Option.ipv4_multicast_loop(10)
p sockopt.ipv4_multicast_loop => 10

#ipv4_multicast_ttlInteger

Returns the ipv4_multicast_ttl data in sockopt as a integer.

sockopt = Socket::Option.ipv4_multicast_ttl(10)
p sockopt.ipv4_multicast_ttl => 10

#levelInteger

returns the socket level as an integer.

p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).level
#=> 41

#lingerArray, seconds

Returns the linger data in sockopt as a pair of boolean and integer.

sockopt = Socket::Option.linger(true, 10)
p sockopt.linger => [true, 10]

#optnameInteger

returns the socket option name as an integer.

p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).optname
#=> 2

#dataString #to_sString
Also known as: #data

returns the socket option data as a string.

p Socket::Option.new(:INET6, :IPV6, :RECVPKTINFO, [1].pack("i!")).data
#=> "\x01\x00\x00\x00"

#unpack(template) ⇒ Array

Calls String#unpack on sockopt.data.

sockopt = Socket::Option.new(:INET, :SOCKET, :KEEPALIVE, [1].pack("i"))
p sockopt.unpack("i")      #=> [1]
p sockopt.data.unpack("i") #=> [1]