Class: Coolio::TCPSocket

Inherits:
Socket show all
Defined in:
lib/cool.io/socket.rb

Defined Under Namespace

Classes: TCPConnectResolver, TCPConnectSocket

Constant Summary collapse

PEERADDR_FAILED =
["?", 0, "name resolusion failed", "?"]

Constants inherited from IO

IO::INPUT_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Socket

#attach, #on_connect, #on_connect_failed

Methods inherited from IO

#attach, #attached?, #close, #closed?, #detach, #disable, #enable, #enabled?, #evloop, #on_close, #on_read, #on_write_complete, #write

Methods included from Meta

#event_callback, #watcher_delegate

Constructor Details

#initialize(socket) ⇒ TCPSocket

Returns a new instance of TCPSocket.


142
143
144
145
146
147
148
149
150
# File 'lib/cool.io/socket.rb', line 142

def initialize(socket)
  unless socket.is_a?(::TCPSocket) or socket.is_a?(TCPConnectSocket)
    raise TypeError, "socket must be a TCPSocket"
  end

  super

  @address_family, @remote_port, @remote_host, @remote_addr = (socket.peeraddr rescue PEERADDR_FAILED)
end

Instance Attribute Details

#address_familyObject (readonly)

Returns the value of attribute address_family


94
95
96
# File 'lib/cool.io/socket.rb', line 94

def address_family
  @address_family
end

#remote_addrObject (readonly)

Returns the value of attribute remote_addr


94
95
96
# File 'lib/cool.io/socket.rb', line 94

def remote_addr
  @remote_addr
end

#remote_hostObject (readonly)

Returns the value of attribute remote_host


94
95
96
# File 'lib/cool.io/socket.rb', line 94

def remote_host
  @remote_host
end

#remote_portObject (readonly)

Returns the value of attribute remote_port


94
95
96
# File 'lib/cool.io/socket.rb', line 94

def remote_port
  @remote_port
end

Class Method Details

.connect(addr, port, *args) ⇒ Object

Perform a non-blocking connect to the given host and port see examples/echo_client.rb addr is a string, can be an IP address or a hostname.


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cool.io/socket.rb', line 111

def self.connect(addr, port, *args)
  family = nil

  if (Resolv::IPv4.create(addr) rescue nil)
    family = ::Socket::AF_INET
  elsif(Resolv::IPv6.create(addr) rescue nil)
    family = ::Socket::AF_INET6
  end

  if family
    return super(TCPConnectSocket.new(family, addr, port), *args) # this creates a 'real' write buffer so we're ok there with regards to already having a write buffer from the get go
  end

  if host = Coolio::DNSResolver.hosts(addr)
    return connect(host, port, *args) # calls this same function
  end

  precreate(addr, port, *args)
end

.precreate(*args, &block) ⇒ Object

Similar to .new, but used in cases where the resulting object is in a “half-open” state. This is primarily used for when asynchronous DNS resolution is taking place. We don't actually have a handle to the socket we want to use to create the watcher yet, since we don't know the IP address to connect to.


102
103
104
105
106
# File 'lib/cool.io/socket.rb', line 102

def self.precreate(*args, &block)
  obj = allocate
  obj.__send__(:preinitialize, *args, &block)
  obj
end

Instance Method Details

#peeraddrObject


152
153
154
# File 'lib/cool.io/socket.rb', line 152

def peeraddr
  [@address_family, @remote_port, @remote_host, @remote_addr]
end