| 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 880
880: def initialize(uri, soc, config={})
881: @uri = uri
882: @socket = soc
883: @config = config
884: @acl = config[:tcp_acl]
885: @msg = DRbMessage.new(config)
886: set_sockopt(@socket)
887: 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 853
853: def self.open_server(uri, config)
854: uri = 'druby://:0' unless uri
855: host, port, opt = parse_uri(uri)
856: config = {:tcp_original_host => host}.update(config)
857: if host.size == 0
858: host = getservername
859: soc = open_server_inaddr_any(host, port)
860: else
861: soc = TCPServer.open(host, port)
862: end
863: port = soc.addr[1] if port == 0
864: config[:tcp_port] = port
865: uri = "druby://#{host}:#{port}"
866: self.new(uri, soc, config)
867: 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: families = Hash[*infos.collect { |af, *_| af }.uniq.zip([]).flatten]
846: return TCPServer.open('0.0.0.0', port) if families.has_key?('AF_INET')
847: return TCPServer.open('::', port) if families.has_key?('AF_INET6')
848: return TCPServer.open(port)
849: end
Parse uri into a [uri, option] pair.
# File lib/drb/drb.rb, line 870
870: def self.uri_option(uri, config)
871: host, port, option = parse_uri(uri)
872: return "druby://#{host}:#{port}", option
873: 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 939
939: def accept
940: while true
941: s = @socket.accept
942: break if (@acl ? @acl.allow_socket?(s) : true)
943: s.close
944: end
945: if @config[:tcp_original_host].to_s.size == 0
946: uri = "druby://#{s.addr[3]}:#{@config[:tcp_port]}"
947: else
948: uri = @uri
949: end
950: self.class.new(uri, s, @config)
951: end
Check to see if this connection is alive.
# File lib/drb/drb.rb, line 954
954: def alive?
955: return false unless @socket
956: if IO.select([@socket], nil, nil, 0)
957: close
958: return false
959: end
960: true
961: 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 929
929: def close
930: if @socket
931: @socket.close
932: @socket = nil
933: end
934: end
Get the address of our TCP peer (the other end of the socket we are bound to.
# File lib/drb/drb.rb, line 894
894: def peeraddr
895: @socket.peeraddr
896: end
On the client side, receive a reply from the server.
# File lib/drb/drb.rb, line 917
917: def recv_reply
918: @msg.recv_reply(stream)
919: end
On the server side, receive a request from the client.
# File lib/drb/drb.rb, line 907
907: def recv_request
908: @msg.recv_request(stream)
909: end
On the server side, send a reply to the client.
# File lib/drb/drb.rb, line 912
912: def send_reply(succ, result)
913: @msg.send_reply(stream, succ, result)
914: end