class IO::Endpoint::Wrapper

def self.set_timeout(io, timeout)

def self.set_timeout(io, timeout)
	io.timeout = timeout
end

def self.set_timeout(io, timeout)

def self.set_timeout(io, timeout)
	warn "IO#timeout= not supported on this platform."
end

def accept(*arguments, backlog: SOMAXCONN, **options, &block)

Bind to a local address and accept connections in a loop.
def accept(*arguments, backlog: SOMAXCONN, **options, &block)
	bind(*arguments, **options) do |server|
		server.listen(backlog) if backlog
		
		async do
			while true
				server.accept(&block)
			end
		end
	end
end

def async

def async
	raise NotImplementedError
end

def bind(local_address, protocol: 0, **options, &block)

Options Hash: (**protocol)
  • The (Integer) -- socket protocol to use.

Parameters:
  • local_address (Address) -- The local address to bind to.
def bind(local_address, protocol: 0, **options, &block)
	socket = build(local_address.afamily, local_address.socktype, protocol, **options) do |socket|
		socket.bind(local_address.to_sockaddr)
	end
	
	return socket unless block_given?
	
	async do
		begin
			yield socket
		ensure
			socket.close
		end
	end
end

def build(*arguments, timeout: nil, reuse_address: true, reuse_port: nil, linger: nil)

Options Hash: (**reuse_address)
  • Allow (Boolean) -- this port to be bound in multiple processes.
  • Allow (Boolean) -- this port to be bound in multiple processes.
def build(*arguments, timeout: nil, reuse_address: true, reuse_port: nil, linger: nil)
	socket = ::Socket.new(*arguments)
	
	# Set the timeout:
	if timeout
		set_timeout(socket, timeout)
	end
	
	if reuse_address
		socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
	end
	
	if reuse_port
		socket.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
	end
	
	if linger
		socket.setsockopt(SOL_SOCKET, SO_LINGER, linger)
	end
	
	yield socket if block_given?
	
	return socket
rescue
	socket&.close
end

def connect(remote_address, local_address: nil, **options)

Options Hash: (**local_address)
  • The (Address) -- local address to bind to before connecting.

Parameters:
  • remote_address (Address) -- The remote address to connect to.
def connect(remote_address, local_address: nil, **options)
	socket = build(remote_address.afamily, remote_address.socktype, remote_address.protocol, **options) do |socket|
		if local_address
			if defined?(IP_BIND_ADDRESS_NO_PORT)
				# Inform the kernel (Linux 4.2+) to not reserve an ephemeral port when using bind(2) with a port number of 0. The port will later be automatically chosen at connect(2) time, in a way that allows sharing a source port as long as the 4-tuple is unique.
				socket.setsockopt(SOL_IP, IP_BIND_ADDRESS_NO_PORT, 1)
			end
			
			socket.bind(local_address.to_sockaddr)
		end
	end
	
	begin
		socket.connect(remote_address.to_sockaddr)
	rescue Exception
		socket.close
		raise
	end
	
	return socket unless block_given?
	
	begin
		yield socket
	ensure
		socket.close
	end
end