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.



276
277
278
# File 'lib/ionian/extension/socket.rb', line 276

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.



280
281
282
# File 'lib/ionian/extension/socket.rb', line 280

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

Instance Method Details

- (Object) broadcast Also known as: broadcast?

Returns true if sending broadcast datagrams is permitted. ( SO_BROADCAST )



29
30
31
32
33
# File 'lib/ionian/extension/socket.rb', line 29

def broadcast
  param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST)
    .data.unpack('i').first
  param > 0 ? true : false
end

- (Object) broadcast=(value)

Permit sending broadcast datagrams if true. ( SO_BROADCAST )



37
38
39
40
# File 'lib/ionian/extension/socket.rb', line 37

def broadcast= value
  param = value ? 1 : 0
  self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, [param].pack('i')
end

- (Object) cork Also known as: cork?

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



118
119
120
121
122
# File 'lib/ionian/extension/socket.rb', line 118

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 )



134
135
136
137
# File 'lib/ionian/extension/socket.rb', line 134

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 )



152
153
154
155
156
157
158
159
160
# File 'lib/ionian/extension/socket.rb', line 152

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 )



168
169
170
171
172
173
174
175
176
# File 'lib/ionian/extension/socket.rb', line 168

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 )



180
181
182
183
# File 'lib/ionian/extension/socket.rb', line 180

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 )



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

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 )



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

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 )



221
222
223
224
# File 'lib/ionian/extension/socket.rb', line 221

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 )



198
199
200
201
# File 'lib/ionian/extension/socket.rb', line 198

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 )



205
206
207
# File 'lib/ionian/extension/socket.rb', line 205

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.



227
228
229
230
# File 'lib/ionian/extension/socket.rb', line 227

def ipv6_add_membership
  # TODO: Implement
  false
end

- (Object) ipv6_drop_membership

Not yet implemented.



233
234
235
236
# File 'lib/ionian/extension/socket.rb', line 233

def ipv6_drop_membership
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_hops

Not yet implemented.



250
251
252
253
# File 'lib/ionian/extension/socket.rb', line 250

def ipv6_multicast_hops
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_hops=(value)

Not yet implemented.



256
257
258
# File 'lib/ionian/extension/socket.rb', line 256

def ipv6_multicast_hops= value
  # TODO: Implement
end

- (Object) ipv6_multicast_if

Not yet implemented.



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

def ipv6_multicast_if
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_if=(value)

Not yet implemented.



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

def ipv6_multicast_if= value
  # TODO: Implement
end

- (Object) ipv6_multicast_loop Also known as: ipv6_multicast_loop?

Not yet implemented.



261
262
263
264
# File 'lib/ionian/extension/socket.rb', line 261

def ipv6_multicast_loop
  # TODO: Implement
  false
end

- (Object) ipv6_multicast_loop=(value)

Not yet implemented.



269
270
271
# File 'lib/ionian/extension/socket.rb', line 269

def ipv6_multicast_loop= value
  # TODO: Implement
end

- (Object) linger Also known as: linger?

For connection-oriented protocols, prevent #close from returning immediately and try to deliver any data in the send buffer if value is true. ( SO_LINGER )



48
49
50
51
52
# File 'lib/ionian/extension/socket.rb', line 48

def linger
  param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_LINGER)
    .data.unpack('i').first
  param > 0 ? true : false
end

- (Object) linger=(value)

For connection-oriented protocols, prevent #close from returning immediately and try to deliver any data in the send buffer if value is true. ( SO_LINGER )



58
59
60
61
# File 'lib/ionian/extension/socket.rb', line 58

def linger= value
  param = value ? 1 : 0
  self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_LINGER, [param].pack('i')
end

- (Object) multicast Also known as: multicast?

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



284
285
286
# File 'lib/ionian/extension/socket.rb', line 284

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 )



99
100
101
102
103
# File 'lib/ionian/extension/socket.rb', line 99

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 )



109
110
111
112
# File 'lib/ionian/extension/socket.rb', line 109

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 )



141
142
143
144
# File 'lib/ionian/extension/socket.rb', line 141

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 )



67
68
69
70
71
# File 'lib/ionian/extension/socket.rb', line 67

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 )



77
78
79
80
# File 'lib/ionian/extension/socket.rb', line 77

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 )



84
85
86
87
# File 'lib/ionian/extension/socket.rb', line 84

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 )



93
94
95
# File 'lib/ionian/extension/socket.rb', line 93

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