Module: Ionian::Extension::Socket

Defined in:
lib/ionian/extension/socket.rb

Overview

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.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) extended(obj)

Called automaticallly when the object is extended with #extend.



17
18
19
20
# File 'lib/ionian/extension/socket.rb', line 17

def self.extended(obj)
  obj.extend Ionian::Extension::IO
  obj.initialize_ionian_socket
end

+ (Object) multicast(address)

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



238
239
240
# File 'lib/ionian/extension/socket.rb', line 238

def multicast(address)
  address >= '224.0.0.0' and address <= '239.255.255.255' ? true : false
end

+ (Object) multicast?

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



242
243
244
# File 'lib/ionian/extension/socket.rb', line 242

def multicast(address)
  address >= '224.0.0.0' and address <= '239.255.255.255' ? true : false
end

Instance Method Details

- (Object) cork Also known as: cork?

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



80
81
82
83
84
# 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

- (Object) cork=(value)

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 )



96
97
98
99
# 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

- (Object) initialize_ionian_socket

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



24
25
# File 'lib/ionian/extension/socket.rb', line 24

def initialize_ionian_socket
end

- (Object) ip_add_membership(address = nil, interface = nil)

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 )



114
115
116
117
118
119
120
121
122
# 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

- (Object) ip_drop_membership(address = nil, interface = nil)

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 )



130
131
132
133
134
135
136
137
138
# 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

- (Object) ip_multicast_if

Returns the default interface for outgoing multicasts. ( IP_MULTICAST_IF )



142
143
144
145
# 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

- (Object) ip_multicast_if=(interface = nil)

Specify default interface for outgoing multicasts. ( IP_MULTICAST_IF )



149
150
151
152
153
154
155
156
# 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

- (Object) ip_multicast_loop Also known as: ip_multicast_loop?

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



173
174
175
176
177
# 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

- (Object) ip_multicast_loop=(value)

Enables loopback of outgoing multicasts if true. ( IP_MULTICAST_LOOP )



183
184
185
186
# 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

- (Object) ip_multicast_ttl

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



160
161
162
163
# 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

- (Object) ip_multicast_ttl=(value)

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



167
168
169
# 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

- (Object) ipv6_add_membership

Not yet implemented.



189
190
191
192
# File 'lib/ionian/extension/socket.rb', line 189

def ipv6_add_membership
  # TODO: Implement
  false
end

- (Object) ipv6_drop_membership

Not yet implemented.



195
196
197
198
# File 'lib/ionian/extension/socket.rb', line 195

def ipv6_drop_membership
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_hops

Not yet implemented.



212
213
214
215
# File 'lib/ionian/extension/socket.rb', line 212

def ipv6_multicast_hops
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_hops=(value)

Not yet implemented.



218
219
220
# File 'lib/ionian/extension/socket.rb', line 218

def ipv6_multicast_hops=(value)
  # TODO: Implement
end

- (Object) ipv6_multicast_if

Not yet implemented.



201
202
203
204
# File 'lib/ionian/extension/socket.rb', line 201

def ipv6_multicast_if
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_if=(value)

Not yet implemented.



207
208
209
# File 'lib/ionian/extension/socket.rb', line 207

def ipv6_multicast_if=(value)
  # TODO: Implement
end

- (Object) ipv6_multicast_loop Also known as: ipv6_multicast_loop?

Not yet implemented.



223
224
225
226
# File 'lib/ionian/extension/socket.rb', line 223

def ipv6_multicast_loop
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_loop=(value)

Not yet implemented.



231
232
233
# File 'lib/ionian/extension/socket.rb', line 231

def ipv6_multicast_loop=(value)
  # TODO: Implement
end

- (Object) multicast Also known as: multicast?

Returns true if the socket's address is in the multicast range.



246
247
248
# File 'lib/ionian/extension/socket.rb', line 246

def multicast
  Ionian::Extension::Socket.multicast self.remote_address.ip_address
end

- (Object) no_delay Also known as: no_delay?

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



61
62
63
64
65
# 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

- (Object) no_delay=(value)

Disables the Nagle algorithm if true. ( TCP_NODELAY )



71
72
73
74
# 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

- (Object) recork

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



103
104
105
106
# 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

- (Object) reuse_addr Also known as: reuse_addr?

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



29
30
31
32
33
# 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

- (Object) reuse_addr=(value)

Allows local address reuse if true. ( SO_REUSEADDR )



39
40
41
42
# 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

- (Object) ttl Also known as: ttl?

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



46
47
48
49
# File 'lib/ionian/extension/socket.rb', line 46

def ttl
  self.getsockopt(::Socket::IPPROTO_IP, ::Socket::IP_TTL)
    .data.unpack('i').first
end

- (Object) ttl=(value)

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



55
56
57
# File 'lib/ionian/extension/socket.rb', line 55

def ttl=(value)
  self.setsockopt ::Socket::IPPROTO_IP, ::Socket::IP_TTL, [value].pack('i')
end