Class: DRb::DRbTCPSocket

Inherits:
Object show all
Defined in:
lib/drb/drb.rb

Overview

The default drb protocol.

Communicates over a TCP socket.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (DRbTCPSocket) initialize(uri, soc, config = {})

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.



880
881
882
883
884
885
886
887
# File 'lib/drb/drb.rb', line 880

def initialize(uri, soc, config={})
  @uri = uri
  @socket = soc
  @config = config
  @acl = config[:tcp_acl]
  @msg = DRbMessage.new(config)
  set_sockopt(@socket)
end

Instance Attribute Details

- (Object) uri (readonly)

Get the URI that we are connected to.



890
891
892
# File 'lib/drb/drb.rb', line 890

def uri
  @uri
end

Class Method Details

+ (Object) getservername



830
831
832
833
834
835
836
837
# File 'lib/drb/drb.rb', line 830

def self.getservername
  host = Socket::gethostname
  begin
    Socket::gethostbyname(host)[0]
  rescue
    'localhost'
  end
end

+ (Object) open(uri, config)

Open a client connection to uri using configuration config.



822
823
824
825
826
827
828
# File 'lib/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

+ (Object) open_server(uri, config)

Open a server listening for connections at uri using configuration config.



853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'lib/drb/drb.rb', line 853

def self.open_server(uri, config)
  uri = 'druby://:0' unless uri
  host, port, opt = parse_uri(uri)
  config = {:tcp_original_host => host}.update(config)
  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
  config[:tcp_port] = port
  uri = "druby://#{host}:#{port}"
  self.new(uri, soc, config)
end

+ (Object) open_server_inaddr_any(host, port)



839
840
841
842
843
844
845
846
847
848
849
# File 'lib/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)
  families = Hash[*infos.collect { |af, *_| af }.uniq.zip([]).flatten]
  return TCPServer.open('0.0.0.0', port) if families.has_key?('AF_INET')
  return TCPServer.open('::', port) if families.has_key?('AF_INET6')
  return TCPServer.open(port)
end

+ (Object) uri_option(uri, config)

Parse uri into a [uri, option] pair.



870
871
872
873
# File 'lib/drb/drb.rb', line 870

def self.uri_option(uri, config)
  host, port, option = parse_uri(uri)
  return "druby://#{host}:#{port}", option
end

Instance Method Details

- (Object) accept

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.



939
940
941
942
943
944
945
946
947
948
949
950
951
# File 'lib/drb/drb.rb', line 939

def accept
  while true
	s = @socket.accept
	break if (@acl ? @acl.allow_socket?(s) : true)
	s.close
  end
  if @config[:tcp_original_host].to_s.size == 0
    uri = "druby://#{s.addr[3]}:#{@config[:tcp_port]}"
  else
    uri = @uri
  end
  self.class.new(uri, s, @config)
end

- (Boolean) alive?

Check to see if this connection is alive.

Returns:

  • (Boolean)


954
955
956
957
958
959
960
961
# File 'lib/drb/drb.rb', line 954

def alive?
  return false unless @socket
  if IO.select([@socket], nil, nil, 0)
	close
	return false
  end
  true
end

- (Object) close

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.



929
930
931
932
933
934
# File 'lib/drb/drb.rb', line 929

def close
  if @socket
	@socket.close
	@socket = nil
  end
end

- (Object) peeraddr

Get the address of our TCP peer (the other end of the socket we are bound to.



894
895
896
# File 'lib/drb/drb.rb', line 894

def peeraddr
  @socket.peeraddr
end

- (Object) recv_reply

On the client side, receive a reply from the server.



917
918
919
# File 'lib/drb/drb.rb', line 917

def recv_reply
  @msg.recv_reply(stream)
end

- (Object) recv_request

On the server side, receive a request from the client.



907
908
909
# File 'lib/drb/drb.rb', line 907

def recv_request
  @msg.recv_request(stream)
end

- (Object) send_reply(succ, result)

On the server side, send a reply to the client.



912
913
914
# File 'lib/drb/drb.rb', line 912

def send_reply(succ, result)
  @msg.send_reply(stream, succ, result)
end

- (Object) send_request(ref, msg_id, arg, b)

On the client side, send a request to the server.



902
903
904
# File 'lib/drb/drb.rb', line 902

def send_request(ref, msg_id, arg, b)
  @msg.send_request(stream, ref, msg_id, arg, b)
end

- (Object) set_sockopt(soc)

:nodoc:



963
964
965
966
# File 'lib/drb/drb.rb', line 963

def set_sockopt(soc) # :nodoc:
  soc.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
  soc.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::FD_CLOEXEC
end

- (Object) stream

Get the socket.



899
# File 'lib/drb/drb.rb', line 899

def stream; @socket; end