Class: LightIO::Library::TCPSocket

Inherits:
IPSocket show all
Includes:
Base
Defined in:
lib/lightio/library/socket.rb

Direct Known Subclasses

TCPServer

Instance Method Summary collapse

Methods included from Base

included

Methods inherited from BasicSocket

#shutdown

Methods included from Module::BasicSocket::ClassMethods

#for_fd

Methods included from Wrap::IOWrapper

included

Methods inherited from IO

#binmode, #lineno, #lineno=, #rewind, #seek, #set_encoding, #to_io

Methods included from Module::IO::ClassMethods

#copy_stream, #open, #pipe, #select

Methods included from IO::IOMethods

#close, #eof, #flush, #getbyte, #getc, #gets, #lightio_initialize, #print, #printf, #puts, #read, #readbyte, #readchar, #readline, #readlines, #readpartial, #wait, #wait_readable, #wait_writable, #write

Constructor Details

#initialize(*args) ⇒ TCPSocket

Returns a new instance of TCPSocket.

Raises:

  • (ArgumentError)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/lightio/library/socket.rb', line 142

def initialize(*args)
  raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 2..4)" if args.size < 2 || args.size > 4
  host, port = args[0..1]
  local_host, local_port = args[2..3]
  addrinfo = Addrinfo.getaddrinfo(host, port, nil, :STREAM)[0]
  socket = ::Socket.send(:origin_new, addrinfo.afamily, Socket::SOCK_STREAM, 0)
  if local_host || local_port
    local_address = Socket.sockaddr_in(local_port, local_host)
    socket.bind(local_address)
  end
  remote_address = Socket.sockaddr_in(addrinfo.ip_port, addrinfo.ip_address)
  @obj = socket
  wait_nonblock(:connect_nonblock, remote_address)
  @obj
  lightio_initialize
end