| Class | DRb::DRbTCPSocket |
| In: |
drb/drb.rb
|
| Parent: | Object |
The default drb protocol.
Communicates over a TCP socket.
Attributes
| uri | [R] | Get the URI that we are connected to. |
Public Class methods
# File drb/drb.rb, line 827
827: def self.getservername
828: host = Socket::gethostname
829: begin
830: Socket::gethostbyname(host)[0]
831: rescue
832: host
833: end
834: 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 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 drb/drb.rb, line 819
819: def self.open(uri, config)
820: host, port, option = parse_uri(uri)
821: host.untaint
822: port.untaint
823: soc = TCPSocket.open(host, port)
824: self.new(uri, soc, config)
825: end
Open a server listening for connections at uri using configuration config.
# File drb/drb.rb, line 855
855: def self.open_server(uri, config)
856: uri = 'druby://:0' unless uri
857: host, port, opt = parse_uri(uri)
858: if host.size == 0
859: host = getservername
860: soc = open_server_inaddr_any(host, port)
861: else
862: soc = TCPServer.open(host, port)
863: end
864: port = soc.addr[1] if port == 0
865: uri = "druby://#{host}:#{port}"
866: self.new(uri, soc, config)
867: end
# File drb/drb.rb, line 836
836: def self.open_server_inaddr_any(host, port)
837: infos = Socket::getaddrinfo(host, nil,
838: Socket::AF_UNSPEC,
839: Socket::SOCK_STREAM,
840: 0,
841: Socket::AI_PASSIVE)
842: family = infos.collect { |af, *_| af }.uniq
843: case family
844: when ['AF_INET']
845: return TCPServer.open('0.0.0.0', port)
846: when ['AF_INET6']
847: return TCPServer.open('::', port)
848: else
849: return TCPServer.open(port)
850: end
851: end
Parse uri into a [uri, option] pair.
# File 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
Public Instance methods
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 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: self.class.new(nil, s, @config)
946: end
Check to see if this connection is alive.
# File drb/drb.rb, line 949
949: def alive?
950: return false unless @socket
951: if IO.select([@socket], nil, nil, 0)
952: close
953: return false
954: end
955: true
956: 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 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 drb/drb.rb, line 894
894: def peeraddr
895: @socket.peeraddr
896: end
On the client side, receive a reply from the server.
# File 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 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 drb/drb.rb, line 912
912: def send_reply(succ, result)
913: @msg.send_reply(stream, succ, result)
914: end