Module: Async::IO::Peer

Included in:
BasicSocket, SSLSocket, TCPSocket, UNIXSocket
Defined in:
lib/async/io/socket.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)


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

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, Socket::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

#protocolObject



72
73
74
# File 'lib/async/io/socket.rb', line 72

def protocol
	self.local_address.protocol
end

#syncObject



59
60
61
62
63
64
65
66
# File 'lib/async/io/socket.rb', line 59

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

#sync=(value) ⇒ Object

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



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/async/io/socket.rb', line 46

def sync=(value)
	super
	
	case self.protocol
	when 0, Socket::IPPROTO_TCP
		self.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, value ? 1 : 0)
	else
		warn "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.
end

#typeObject



68
69
70
# File 'lib/async/io/socket.rb', line 68

def type
	self.local_address.socktype
end