The default drb protocol.
Communicates over a TCP socket.
# File drb/drb.rb, line 830
def self.getservername
host = Socket::gethostname
begin
Socket::gethostbyname(host)[0]
rescue
'localhost'
end
end
Create a new DRbTCPSocket instance.
uri is the URI we are connected to. soc is the
tcp socket we are bound to. config is our configuration.
# File drb/drb.rb, line 883
def initialize(uri, soc, config={})
@uri = uri
@socket = soc
@config = config
@acl = config[:tcp_acl]
@msg = DRbMessage.new(config)
set_sockopt(@socket)
end
Open a client connection to uri using configuration
config.
# File drb/drb.rb, line 822
def self.open(uri, config)
host, port, option = parse_uri(uri)
host.untaint
port.untaint
soc = TCPSocket.open(host, port)
self.new(uri, soc, config)
end
Open a server listening for connections at uri using
configuration config.
# File drb/drb.rb, line 858
def self.open_server(uri, config)
uri = 'druby://:0' unless uri
host, port, opt = parse_uri(uri)
if host.size == 0
host = getservername
soc = open_server_inaddr_any(host, port)
else
soc = TCPServer.open(host, port)
end
port = soc.addr[1] if port == 0
uri = "druby://#{host}:#{port}"
self.new(uri, soc, config)
end
# File drb/drb.rb, line 839
def self.open_server_inaddr_any(host, port)
infos = Socket::getaddrinfo(host, nil,
Socket::AF_UNSPEC,
Socket::SOCK_STREAM,
0,
Socket::AI_PASSIVE)
family = infos.collect { |af, *_| af }.uniq
case family
when ['AF_INET']
return TCPServer.open('0.0.0.0', port)
when ['AF_INET6']
return TCPServer.open('::', port)
else
return TCPServer.open(port)
end
end
On the server side, for an instance returned by open_server, accept a client connection and return a new instance to handle the server's side of this client-server session.
# File drb/drb.rb, line 942
def accept
while true
s = @socket.accept
break if (@acl ? @acl.allow_socket?(s) : true)
s.close
end
self.class.new(nil, s, @config)
end
Check to see if this connection is alive.
# File drb/drb.rb, line 952
def alive?
return false unless @socket
if IO.select([@socket], nil, nil, 0)
close
return false
end
true
end
Close the connection.
If this is an instance returned by open_server, then this stops listening for new connections altogether. If this is an instance returned by open or by accept, then it closes this particular client-server session.
# File drb/drb.rb, line 932
def close
if @socket
@socket.close
@socket = nil
end
end
Get the address of our TCP peer (the other end of the socket we are bound to.
# File drb/drb.rb, line 897
def peeraddr
@socket.peeraddr
end
On the client side, receive a reply from the server.
# File drb/drb.rb, line 920
def recv_reply
@msg.recv_reply(stream)
end
On the server side, receive a request from the client.
# File drb/drb.rb, line 910
def recv_request
@msg.recv_request(stream)
end
On the server side, send a reply to the client.
# File drb/drb.rb, line 915
def send_reply(succ, result)
@msg.send_reply(stream, succ, result)
end