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

Returns a new instance of EvmaTCPClient.



911
912
913
914
915
# File 'lib/em/pure_ruby.rb', line 911

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

Class Method Details

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



898
899
900
901
902
903
904
905
906
907
908
909
# File 'lib/em/pure_ruby.rb', line 898

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

Returns:

  • (Boolean)


925
926
927
928
929
930
931
932
933
934
935
936
# File 'lib/em/pure_ruby.rb', line 925

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

Returns:

  • (Boolean)


938
939
940
941
942
943
944
945
946
947
# File 'lib/em/pure_ruby.rb', line 938

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

#ready?Boolean

Returns:

  • (Boolean)


917
918
919
920
921
922
923
# File 'lib/em/pure_ruby.rb', line 917

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

Returns:

  • (Boolean)


954
955
956
957
# File 'lib/em/pure_ruby.rb', line 954

def select_for_reading?
  pending?
  super
end

#select_for_writing?Boolean

Returns:

  • (Boolean)


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

def select_for_writing?
  pending?
  super
end