Class | DRb::DRbTCPSocket |
In: |
lib/drb/drb.rb
|
Parent: | Object |
The default drb protocol.
Communicates over a TCP socket.
uri | [R] | Get the URI that we are connected to. |
# File lib/drb/drb.rb, line 830 830: def self.getservername 831: host = Socket::gethostname 832: begin 833: Socket::gethostbyname(host)[0] 834: rescue 835: 'localhost' 836: end 837: 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 lib/drb/drb.rb, line 885 885: def initialize(uri, soc, config={}) 886: @uri = uri 887: @socket = soc 888: @config = config 889: @acl = config[:tcp_acl] 890: @msg = DRbMessage.new(config) 891: set_sockopt(@socket) 892: end
Open a client connection to uri using configuration config.
# File lib/drb/drb.rb, line 822 822: def self.open(uri, config) 823: host, port, option = parse_uri(uri) 824: host.untaint 825: port.untaint 826: soc = TCPSocket.open(host, port) 827: self.new(uri, soc, config) 828: end
Open a server listening for connections at uri using configuration config.
# File lib/drb/drb.rb, line 858 858: def self.open_server(uri, config) 859: uri = 'druby://:0' unless uri 860: host, port, opt = parse_uri(uri) 861: config = {:tcp_original_host => host}.update(config) 862: if host.size == 0 863: host = getservername 864: soc = open_server_inaddr_any(host, port) 865: else 866: soc = TCPServer.open(host, port) 867: end 868: port = soc.addr[1] if port == 0 869: config[:tcp_port] = port 870: uri = "druby://#{host}:#{port}" 871: self.new(uri, soc, config) 872: end
# File lib/drb/drb.rb, line 839 839: def self.open_server_inaddr_any(host, port) 840: infos = Socket::getaddrinfo(host, nil, 841: Socket::AF_UNSPEC, 842: Socket::SOCK_STREAM, 843: 0, 844: Socket::AI_PASSIVE) 845: family = infos.collect { |af, *_| af }.uniq 846: case family 847: when ['AF_INET'] 848: return TCPServer.open('0.0.0.0', port) 849: when ['AF_INET6'] 850: return TCPServer.open('::', port) 851: else 852: return TCPServer.open(port) 853: end 854: end
Parse uri into a [uri, option] pair.
# File lib/drb/drb.rb, line 875 875: def self.uri_option(uri, config) 876: host, port, option = parse_uri(uri) 877: return "druby://#{host}:#{port}", option 878: end
# File lib/drb/drb.rb, line 807 807: def self.parse_uri(uri) 808: if uri =~ /^druby:\/\/(.*?):(\d+)(\?(.*))?$/ 809: host = $1 810: port = $2.to_i 811: option = $4 812: [host, port, option] 813: else 814: raise(DRbBadScheme, uri) unless uri =~ /^druby:/ 815: raise(DRbBadURI, 'can\'t parse uri:' + uri) 816: end 817: 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 lib/drb/drb.rb, line 944 944: def accept 945: while true 946: s = @socket.accept 947: break if (@acl ? @acl.allow_socket?(s) : true) 948: s.close 949: end 950: if @config[:tcp_original_host].to_s.size == 0 951: uri = "druby://#{s.addr[3]}:#{@config[:tcp_port]}" 952: else 953: uri = @uri 954: end 955: self.class.new(uri, s, @config) 956: end
Check to see if this connection is alive.
# File lib/drb/drb.rb, line 959 959: def alive? 960: return false unless @socket 961: if IO.select([@socket], nil, nil, 0) 962: close 963: return false 964: end 965: true 966: 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 lib/drb/drb.rb, line 934 934: def close 935: if @socket 936: @socket.close 937: @socket = nil 938: end 939: end
Get the address of our TCP peer (the other end of the socket we are bound to.
# File lib/drb/drb.rb, line 899 899: def peeraddr 900: @socket.peeraddr 901: end
On the client side, receive a reply from the server.
# File lib/drb/drb.rb, line 922 922: def recv_reply 923: @msg.recv_reply(stream) 924: end
On the server side, receive a request from the client.
# File lib/drb/drb.rb, line 912 912: def recv_request 913: @msg.recv_request(stream) 914: end
On the server side, send a reply to the client.
# File lib/drb/drb.rb, line 917 917: def send_reply(succ, result) 918: @msg.send_reply(stream, succ, result) 919: end