Class: BasicSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/io/stream/shim/buffered.rb,
lib/io/stream/shim/readable.rb

Instance Method Summary collapse

Instance Method Details

#buffered=(value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/io/stream/shim/buffered.rb', line 38

def buffered=(value)
	super
	
	if ip_protocol_tcp?
		# When buffered is set to true, TCP_NODELAY shold be disabled.
		self.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, value ? 0 : 1)
	end
rescue ::Errno::EINVAL
	# On Darwin, sometimes occurs when the connection is not yet fully formed. Empirically, TCP_NODELAY is enabled despite this result.
rescue ::Errno::EOPNOTSUPP
	# Some platforms may simply not support the operation.
end

#buffered?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/io/stream/shim/buffered.rb', line 28

def buffered?
	return false unless super
	
	if ip_protocol_tcp?
		return !self.getsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY).bool
	else
		return true
	end
end

#ip_protocol_tcp?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/io/stream/shim/buffered.rb', line 22

def ip_protocol_tcp?
	local_address = self.local_address
	
	return (local_address.afamily == ::Socket::AF_INET || local_address.afamily == ::Socket::AF_INET6) && local_address.socktype == ::Socket::SOCK_STREAM
end

#readable?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/io/stream/shim/readable.rb', line 19

def readable?
	# If we can wait for the socket to become readable, we know that the socket may still be open.
	result = self.recv_nonblock(1, ::Socket::MSG_PEEK, exception: false)
	
	# No data was available - newer Ruby can return nil instead of empty string:
	return false if result.nil?
	
	# Either there was some data available, or we can wait to see if there is data avaialble.
	return !result.empty? || result == :wait_readable
rescue Errno::ECONNRESET, IOError
	# This might be thrown by recv_nonblock.
	return false
end