module Ionian::Extension::Socket

A mixin for Socket objects.

This module was designed to be extended by instantiated objects that implement the standard library Socket class. my_socket.extend Ionian::Socket

Extending this module also extends Ionian::IO.

Public Class Methods

extended(obj) click to toggle source

Called automaticallly when the object is extended with extend.

# File lib/ionian/extension/socket.rb, line 17
def self.extended(obj)
  obj.extend Ionian::Extension::IO
  obj.initialize_ionian_socket
end

Public Instance Methods

cork() click to toggle source

Returns true if multiple writes are buffered into a single segment. See recork. Linux only. ( TCP_CORK )

# File lib/ionian/extension/socket.rb, line 80
def cork
  param = self.getsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_CORK)
    .data.unpack('i').first
  param > 0 ? true : false
end
Also aliased as: cork?
cork=(value) click to toggle source

Buffers multiple writes into a single segment if true. The segment is sent once the cork flag is disabled, the upper limit on the size of a segment is reached, the socket is closed, or 200ms elapses from the time the first corked byte is written. See recork. Linux only. ( TCP_CORK )

# File lib/ionian/extension/socket.rb, line 96
def cork=(value)
  param = value ? 1 : 0
  self.setsockopt ::Socket::IPPROTO_TCP, ::Socket::TCP_CORK, [param].pack('i')
end
cork?()
Alias for: cork
initialize_ionian_socket() click to toggle source

Initialize the Ionian Socket variables. This is called automatically if extend is called on an object.

# File lib/ionian/extension/socket.rb, line 24
def initialize_ionian_socket
end
ip_add_membership(address = nil, interface = nil) click to toggle source

Join a multicast group. Address is the class D multicast address (uses remote address if not specified). Interface is the local network interface to receive the multicast traffic on (all interfaces if not specified). ( IP_ADD_MEMBERSHIP )

# File lib/ionian/extension/socket.rb, line 114
def ip_add_membership(address = nil, interface = nil)
   address   ||= self.remote_address.ip_address
   interface ||= '0.0.0.0'
   
   self.setsockopt              ::Socket::IPPROTO_IP,
       ::Socket::IP_ADD_MEMBERSHIP,
       IPAddr.new(address).hton + IPAddr.new(interface).hton
 end
ip_drop_membership(address = nil, interface = nil) click to toggle source

Leave a multicast group. Address is the class D multicast address (uses remote address if not specified). Interface is the local network interface the multicast traffic is received on (all interfaces if not specified). ( IP_DROP_MEMBERSHIP )

# File lib/ionian/extension/socket.rb, line 130
def ip_drop_membership(address = nil, interface = nil)
  address   ||= self.remote_address.ip_address
  interface ||= '0.0.0.0'
  
  self.setsockopt              ::Socket::IPPROTO_IP,
      ::Socket::IP_DROP_MEMBERSHIP,
      IPAddr.new(address).hton + IPAddr.new(interface).hton
end
ip_multicast_if() click to toggle source

Returns the default interface for outgoing multicasts. ( IP_MULTICAST_IF )

# File lib/ionian/extension/socket.rb, line 142
def ip_multicast_if
  self.getsockopt(::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_IF)
    .data.unpack('CCCC').join('.')
end
ip_multicast_if=(interface = nil) click to toggle source

Specify default interface for outgoing multicasts. ( IP_MULTICAST_IF )

# File lib/ionian/extension/socket.rb, line 149
def ip_multicast_if=(interface = nil)
  interface ||= '0.0.0.0'
  
  self.setsockopt            ::Socket::IPPROTO_IP,
    ::Socket::IP_MULTICAST_IF,
    IPAddr.new(interface).hton
end
ip_multicast_loop() click to toggle source

Returns true if loopback of outgoing multicasts is enabled. ( IP_MULTICAST_LOOP )

# File lib/ionian/extension/socket.rb, line 173
def ip_multicast_loop
  param = self.getsockopt(::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_LOOP)
    .data.unpack('C').first
  param > 0 ? true : false
end
Also aliased as: ip_multicast_loop?
ip_multicast_loop=(value) click to toggle source

Enables loopback of outgoing multicasts if true. ( IP_MULTICAST_LOOP )

# File lib/ionian/extension/socket.rb, line 183
def ip_multicast_loop=(value)
  param = value ? 1 : 0
  self.setsockopt ::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_LOOP, [param].pack('C')
end
ip_multicast_loop?()
Alias for: ip_multicast_loop
ip_multicast_ttl() click to toggle source

Returns the time to live (hop limit) for outgoing multicasts. ( IP_MULTICAST_TTL )

# File lib/ionian/extension/socket.rb, line 160
def ip_multicast_ttl
  self.getsockopt(::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_TTL)
    .data.unpack('C').first
end
ip_multicast_ttl=(value) click to toggle source

Set the time to live (hop limit) for outgoing multicasts. ( IP_MULTICAST_TTL )

# File lib/ionian/extension/socket.rb, line 167
def ip_multicast_ttl=(value)
  self.setsockopt ::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_TTL, [value].pack('C')
end
ipv6_add_membership() click to toggle source
# File lib/ionian/extension/socket.rb, line 188
def ipv6_add_membership
  # TODO: Implement
  false
end
ipv6_drop_membership() click to toggle source
# File lib/ionian/extension/socket.rb, line 193
def ipv6_drop_membership
  # TODO: Implement
  false
end
ipv6_multicast_hops() click to toggle source
# File lib/ionian/extension/socket.rb, line 207
def ipv6_multicast_hops
  # TODO: Implement
  false
end
ipv6_multicast_hops=(value) click to toggle source
# File lib/ionian/extension/socket.rb, line 212
def ipv6_multicast_hops=(value)
  # TODO: Implement
end
ipv6_multicast_if() click to toggle source
# File lib/ionian/extension/socket.rb, line 198
def ipv6_multicast_if
  # TODO: Implement
  false
end
ipv6_multicast_if=(value) click to toggle source
# File lib/ionian/extension/socket.rb, line 203
def ipv6_multicast_if=(value)
  # TODO: Implement
end
ipv6_multicast_loop() click to toggle source
# File lib/ionian/extension/socket.rb, line 216
def ipv6_multicast_loop
  # TODO: Implement
  false
end
Also aliased as: ipv6_multicast_loop?
ipv6_multicast_loop=(value) click to toggle source
# File lib/ionian/extension/socket.rb, line 223
def ipv6_multicast_loop=(value)
  # TODO: Implement
end
ipv6_multicast_loop?()
Alias for: ipv6_multicast_loop
multicast(address) click to toggle source

Returns true if the given address is within the multicast range.

# File lib/ionian/extension/socket.rb, line 230
def multicast(address)
  address >= '224.0.0.0' and address <= '239.255.255.255' ? true : false
end
Also aliased as: multicast?
multicast?(address)
Alias for: multicast
no_delay() click to toggle source

Returns true if the Nagle algorithm is disabled. ( TCP_NODELAY )

# File lib/ionian/extension/socket.rb, line 61
def no_delay
  param = self.getsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY)
    .data.unpack('i').first
  param > 0 ? true : false
end
Also aliased as: no_delay?
no_delay=(value) click to toggle source

Disables the Nagle algorithm if true. ( TCP_NODELAY )

# File lib/ionian/extension/socket.rb, line 71
def no_delay=(value)
  param = value ? 1 : 0
  self.setsockopt ::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, [param].pack('i')
end
no_delay?()
Alias for: no_delay
recork() click to toggle source

Unsets cork to transmit data, then reapplies cork. ( TCP_CORK )

# File lib/ionian/extension/socket.rb, line 103
def recork
  self.setsockopt ::Socket::IPPROTO_TCP, ::Socket::TCP_CORK, [0].pack('i')
  self.setsockopt ::Socket::IPPROTO_TCP, ::Socket::TCP_CORK, [1].pack('i')
end
reuse_addr() click to toggle source

Returns true if local address reuse is allowed. ( SO_REUSEADDR )

# File lib/ionian/extension/socket.rb, line 29
def reuse_addr
  param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR)
    .data.unpack('i').first
  param > 0 ? true : false
end
Also aliased as: reuse_addr?
reuse_addr=(value) click to toggle source

Allows local address reuse if true. ( SO_REUSEADDR )

# File lib/ionian/extension/socket.rb, line 39
def reuse_addr=(value)
  param = value ? 1 : 0
  self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, [param].pack('i')
end
reuse_addr?()
Alias for: reuse_addr
ttl() click to toggle source

Returns the time to live (hop limit). ( IP_TTL )

# File lib/ionian/extension/socket.rb, line 46
def ttl
  self.getsockopt(::Socket::IPPROTO_IP, ::Socket::IP_TTL)
    .data.unpack('i').first
end
Also aliased as: ttl?
ttl=(value) click to toggle source

Sets the time to live (hop limit). ( IP_TTL )

# File lib/ionian/extension/socket.rb, line 55
def ttl=(value)
  self.setsockopt ::Socket::IPPROTO_IP, ::Socket::IP_TTL, [value].pack('i')
end
ttl?()
Alias for: ttl