Class: EventMachine::EvmaTCPClient

Inherits:
StreamObject show all
Defined in:
lib/em/pure_ruby.rb

Instance Attribute Summary

Attributes inherited from Selectable

#io, #is_server, #uuid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StreamObject

#eventable_read, #eventable_write, #get_outbound_data_size, #get_peername, #get_sockname, #heartbeat, #send_data

Methods inherited from Selectable

#close_scheduled?, #get_peername, #get_sockname, #heartbeat, #schedule_close, #set_inactivity_timeout

Constructor Details

#initialize(io) ⇒ EvmaTCPClient



906
907
908
909
910
# File 'lib/em/pure_ruby.rb', line 906

def initialize io
  super
  @pending = true
  @handshake_complete = false
end

Class Method Details

.connect(bind_addr, bind_port, host, port) ⇒ Object



893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/em/pure_ruby.rb', line 893

def self.connect bind_addr, bind_port, host, port
  sd = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
  sd.bind( Socket.pack_sockaddr_in( bind_port, bind_addr ))  if bind_addr

  begin
    # TODO, this assumes a current Ruby snapshot.
    # We need to degrade to a nonblocking connect otherwise.
    sd.connect_nonblock( Socket.pack_sockaddr_in( port, host ))
  rescue Errno::ECONNREFUSED, Errno::EINPROGRESS
  end
  EvmaTCPClient.new sd
end

Instance Method Details

#handshake_complete?Boolean



920
921
922
923
924
925
926
927
928
929
930
931
# File 'lib/em/pure_ruby.rb', line 920

def handshake_complete?
  if !@handshake_complete && io.respond_to?(:state)
    if io.state =~ /^SSLOK/
      @handshake_complete = true
      EventMachine::event_callback uuid, SslHandshakeCompleted, ""
      EventMachine::event_callback uuid, SslVerify, io.peer_cert.to_pem if io.peer_cert
    end
  else
    @handshake_complete = true
  end
  @handshake_complete
end

#pending?Boolean



933
934
935
936
937
938
939
940
941
942
# File 'lib/em/pure_ruby.rb', line 933

def pending?
  handshake_complete?
  if @pending
    if ready?
      @pending = false
      EventMachine::event_callback uuid, ConnectionCompleted, ""
    end
  end
  @pending
end

#ready?Boolean



912
913
914
915
916
917
918
# File 'lib/em/pure_ruby.rb', line 912

def ready?
  if RUBY_PLATFORM =~ /linux/
    io.getsockopt(Socket::SOL_TCP, Socket::TCP_INFO).unpack("i").first == 1 # TCP_ESTABLISHED
  else
    io.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR).unpack("i").first == 0 # NO ERROR
  end
end

#select_for_reading?Boolean



949
950
951
952
# File 'lib/em/pure_ruby.rb', line 949

def select_for_reading?
  pending?
  super
end

#select_for_writing?Boolean



944
945
946
947
# File 'lib/em/pure_ruby.rb', line 944

def select_for_writing?
  pending?
  super
end