Class: Minx::SocketChannel

Inherits:
IOChannel show all
Defined in:
lib/minx/socket_channel.rb

Instance Method Summary (collapse)

Methods inherited from IOChannel

#<<, #each, #read, #write

Constructor Details

- (SocketChannel) initialize(socket)

A new instance of SocketChannel



7
8
9
10
# File 'lib/minx/socket_channel.rb', line 7

def initialize(socket)
  @socket = socket
  super(@socket)
end

Instance Method Details

- (Object) connect(host, options = {})

Connect to host on the port specified in the :port option.

Parameters:

  • host (String)

    the name of the host that is to be used

  • options (Hash) (defaults to: {})

    socket options

Options Hash (options):

  • :port (Integer)

    the remote port that is to be used

Raises:

  • (ArgumentError)

    if no port is specified in the options



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/minx/socket_channel.rb', line 19

def connect(host, options = {})
  raise ArgumentError.new("Missing option: port") if options[:port].nil?

  port = options[:port]

  sockaddr = Socket.sockaddr_in(port, host)

  @socket.connect_nonblock(sockaddr)
rescue Errno::EINPROGRESS
  Minx.yield until ::IO.select([], [@socket], nil, 0)

  @socket.connect_nonblock(sockaddr)
end

- (Object) disconnect

Close the connection.



34
35
36
# File 'lib/minx/socket_channel.rb', line 34

def disconnect
  @socket.close
end