Class: DRab::DRabTCPSocket

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

Direct Known Subclasses

DRabSSLSocket, DRabUNIXSocket

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DRabTCPSocket.



790
791
792
793
794
795
796
797
798
# File 'lib/drab/drab.rb', line 790

def initialize(uri, soc, config={})
  @uri = uri
  @socket = soc
  @config = config
  @acl = config[:tcp_acl]
  @msg = DRabMessage.new(config)
  set_sockopt(@socket)
  @shutdown_pipe_r, @shutdown_pipe_w = IO.pipe
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



800
801
802
# File 'lib/drab/drab.rb', line 800

def uri
  @uri
end

Class Method Details

.getservernameObject



748
749
750
751
752
753
754
755
# File 'lib/drab/drab.rb', line 748

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

.open(uri, config) ⇒ Object



740
741
742
743
744
745
746
# File 'lib/drab/drab.rb', line 740

def self.open(uri, config)
  host, port, = parse_uri(uri)
  host.untaint
  port.untaint
  soc = TCPSocket.open(host, port)
  self.new(uri, soc, config)
end

.open_server(uri, config) ⇒ Object



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/drab/drab.rb', line 769

def self.open_server(uri, config)
  uri = 'druby://:0' unless uri
  host, port, _ = 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

.open_server_inaddr_any(host, port) ⇒ Object



757
758
759
760
761
762
763
764
765
766
767
# File 'lib/drab/drab.rb', line 757

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

.uri_option(uri, config) ⇒ Object



785
786
787
788
# File 'lib/drab/drab.rb', line 785

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

Instance Method Details

#acceptObject



846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/drab/drab.rb', line 846

def accept
  while true
    s = accept_or_shutdown
    return nil unless s
    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

#alive?Boolean

Returns:

  • (Boolean)


875
876
877
878
879
880
881
882
883
# File 'lib/drab/drab.rb', line 875

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

#closeObject



826
827
828
829
830
831
832
# File 'lib/drab/drab.rb', line 826

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

#peeraddrObject



802
803
804
# File 'lib/drab/drab.rb', line 802

def peeraddr
  @socket.peeraddr
end

#recv_replyObject



820
821
822
# File 'lib/drab/drab.rb', line 820

def recv_reply
  @msg.recv_reply(stream)
end

#recv_requestObject



812
813
814
# File 'lib/drab/drab.rb', line 812

def recv_request
  @msg.recv_request(stream)
end

#send_reply(succ, result) ⇒ Object



816
817
818
# File 'lib/drab/drab.rb', line 816

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

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



808
809
810
# File 'lib/drab/drab.rb', line 808

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

#set_sockopt(soc) ⇒ Object



885
886
887
# File 'lib/drab/drab.rb', line 885

def set_sockopt(soc)
  soc.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
end

#shutdownObject



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

def shutdown
  @shutdown_pipe_w.close if @shutdown_pipe_w && !@shutdown_pipe_w.closed?
end

#streamObject



806
# File 'lib/drab/drab.rb', line 806

def stream; @socket; end