Class: DRb::DRbTCPSocket

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

Overview

The default drb protocol.

Communicates over a TCP socket.

Direct Known Subclasses

DRbSSLSocket, DRbUNIXSocket

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, soc, config = {}) ⇒ DRbTCPSocket

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.



883
884
885
886
887
888
889
890
# File 'lib/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

Instance Attribute Details

#uriObject (readonly)

Get the URI that we are connected to.



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

def uri
  @uri
end

Class Method Details

.getservernameObject



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

.open(uri, config) ⇒ Object

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

.open_server(uri, config) ⇒ Object

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



858
859
860
861
862
863
864
865
866
867
868
869
870
# File 'lib/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

.open_server_inaddr_any(host, port) ⇒ Object



839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
# 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)
  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

.uri_option(uri, config) ⇒ Object

Parse uri into a [uri, option] pair.



873
874
875
876
# File 'lib/drb/drb.rb', line 873

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

Instance Method Details

#acceptObject

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.



942
943
944
945
946
947
948
949
# File 'lib/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

#alive?Boolean

Check to see if this connection is alive.

Returns:

  • (Boolean)


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

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

#closeObject

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.



932
933
934
935
936
937
# File 'lib/drb/drb.rb', line 932

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

#peeraddrObject

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



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

def peeraddr
  @socket.peeraddr
end

#recv_replyObject

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



920
921
922
# File 'lib/drb/drb.rb', line 920

def recv_reply
  @msg.recv_reply(stream)
end

#recv_requestObject

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



910
911
912
# File 'lib/drb/drb.rb', line 910

def recv_request
  @msg.recv_request(stream)
end

#send_reply(succ, result) ⇒ Object

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



915
916
917
# File 'lib/drb/drb.rb', line 915

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

#send_request(ref, msg_id, arg, b) ⇒ Object

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



905
906
907
# File 'lib/drb/drb.rb', line 905

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

#set_sockopt(soc) ⇒ Object

:nodoc:



961
962
963
964
# File 'lib/drb/drb.rb', line 961

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

#streamObject

Get the socket.



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

def stream; @socket; end