Module: Async::IO::Peer

Includes:
Socket::Constants
Included in:
BasicSocket, SSLSocket, TCPSocket, UNIXSocket
Defined in:
lib/async/io/peer.rb

Instance Method Summary collapse

Instance Method Details

#connected?Boolean

Is it likely that the socket is still connected? May return false positive, but won't return false negative.

Returns:

  • (Boolean)

32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/async/io/peer.rb', line 32

def connected?
	return false if @io.closed?
	
	# If we can wait for the socket to become readable, we know that the socket may still be open.
	result = to_io.recv_nonblock(1, MSG_PEEK, exception: false)
	
	# 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
	# This might be thrown by recv_nonblock.
	return false
end

#eofObject


46
47
48
# File 'lib/async/io/peer.rb', line 46

def eof
	!connected?
end

#eof?Boolean

Returns:

  • (Boolean)

50
51
52
# File 'lib/async/io/peer.rb', line 50

def eof?
	!connected?
end

#protocolObject


84
85
86
# File 'lib/async/io/peer.rb', line 84

def protocol
	self.local_address.protocol
end

#syncObject


71
72
73
74
75
76
77
78
# File 'lib/async/io/peer.rb', line 71

def sync
	case self.protocol
	when IPPROTO_TCP
		self.getsockopt(IPPROTO_TCP, TCP_NODELAY).bool
	else
		true
	end && super
end

#sync=(value) ⇒ Object

Best effort to set *_NODELAY if it makes sense. Swallows errors where possible.


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/async/io/peer.rb', line 55

def sync=(value)
	super
	
	case self.protocol
	when 0, IPPROTO_TCP
		self.setsockopt(IPPROTO_TCP, TCP_NODELAY, value ? 1 : 0)
	else
		Console.logger.warn(self) {"Unsure how to sync=#{value} for #{self.protocol}!"}
	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.
	# Console.logger.warn(self) {"Unable to set sync=#{value}!"}
end

#typeObject


80
81
82
# File 'lib/async/io/peer.rb', line 80

def type
	self.local_address.socktype
end