Module: Socket::ListenAndAccept

Includes:
IO::Socketable
Included in:
Socket, TCPServer, UNIXServer
Defined in:
lib/rubysl/socket.rb

Instance Method Summary collapse

Instance Method Details

#acceptObject



566
567
568
569
570
571
572
573
574
575
# File 'lib/rubysl/socket.rb', line 566

def accept
  return if closed?

  fd = super

  socket = self.class.superclass.allocate
  IO.setup socket, fd, nil, true
  socket.binmode
  socket
end

#accept_nonblockObject

Set nonblocking and accept.



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/rubysl/socket.rb', line 580

def accept_nonblock
  return if closed?

  fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)

  fd = nil
  sockaddr = nil

  FFI::MemoryPointer.new 1024 do |sockaddr_p| # HACK from MRI
    FFI::MemoryPointer.new :int do |size_p|
      fd = Socket::Foreign.accept descriptor, sockaddr_p, size_p
    end
  end

  Errno.handle 'accept(2)' if fd < 0

  # TCPServer -> TCPSocket etc. *sigh*
  socket = self.class.superclass.allocate
  IO.setup socket, fd, nil, true
  socket
end

#listen(backlog) ⇒ Object



556
557
558
559
560
561
562
563
564
# File 'lib/rubysl/socket.rb', line 556

def listen(backlog)
  backlog = Rubinius::Type.coerce_to backlog, Fixnum, :to_int

  err = Socket::Foreign.listen descriptor, backlog

  Errno.handle 'listen(2)' unless err == 0

  err
end