Class: TCPServer

Inherits:
TCPSocket show all
Defined in:
lib/polyphony/extensions/socket.rb

Overview

TCPServer extensions

Instance Method Summary collapse

Methods inherited from TCPSocket

#<<, #closed?, #dont_linger, #feed_loop, #no_delay, #read, #read_nonblock, #readpartial, #recv, #recv_loop, #reuse_addr, #reuse_port, #send, #setsockopt, #write, #write_nonblock

Constructor Details

#initialize(hostname = nil, port = 0) ⇒ TCPServer

Initializes the TCP server socket.

Parameters:

  • hostname (String, nil) (defaults to: nil)

    hostname to connect to

  • port (Integer) (defaults to: 0)

    port to connect to



449
450
451
452
453
454
455
# File 'lib/polyphony/extensions/socket.rb', line 449

def initialize(hostname = nil, port = 0)
  addr = Addrinfo.tcp(hostname, port)
  @io = Socket.new addr.afamily, Socket::SOCK_STREAM
  @io.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)
  @io.bind(addr)
  @io.listen(0)
end

Instance Method Details

#acceptTCPSocket

Returns new connection.

Returns:



463
464
465
# File 'lib/polyphony/extensions/socket.rb', line 463

def accept
  Polyphony.backend_accept(@io, TCPSocket)
end

#accept_loop {|TCPSocket| ... } ⇒ nil

Accepts incoming connections in an infinite loop.

Yields:

Returns:

  • (nil)


471
472
473
# File 'lib/polyphony/extensions/socket.rb', line 471

def accept_loop(&block)
  Polyphony.backend_accept_loop(@io, TCPSocket, &block)
end

#closeTCPServer

Closes the server socket.

Returns:



481
482
483
484
# File 'lib/polyphony/extensions/socket.rb', line 481

def close
  @io.close
  self
end