class SharedEndpoint
Pre-connect and pre-bind sockets so that it can be used between processes.
Definitions
def self.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false, **options)
Create a new SharedEndpoint
by binding to the given endpoint.
Implementation
def self.bound(endpoint, backlog: Socket::SOMAXCONN, close_on_exec: false, **options)
sockets = Array(endpoint.bind(**options))
wrappers = sockets.each do |server|
# This is somewhat optional. We want to have a generic interface as much as possible so that users of this interface can just call it without knowing a lot of internal details. Therefore, we ignore errors here if it's because the underlying socket does not support the operation.
begin
server.listen(backlog)
rescue Errno::EOPNOTSUPP
# Ignore.
end
server.close_on_exec = close_on_exec
if server.respond_to?(:reactor=)
server.reactor = nil
end
end
return self.new(endpoint, wrappers)
end
def self.connected(endpoint, close_on_exec: false)
Create a new SharedEndpoint
by connecting to the given endpoint.
Implementation
def self.connected(endpoint, close_on_exec: false)
wrapper = endpoint.connect
wrapper.close_on_exec = close_on_exec
if wrapper.respond_to?(:reactor=)
wrapper.reactor = nil
end
return self.new(endpoint, [wrapper])
end
def close
Close all the internal wrappers.
Implementation
def close
@wrappers.each(&:close)
@wrappers.clear
end