Class: TCPServer

Inherits:
TCPSocket show all
Defined in:
mrbgems/mruby-socket/mrblib/socket.rb

Instance Attribute Summary

Attributes inherited from BasicSocket

#do_not_reverse_lookup

Instance Method Summary collapse

Methods inherited from TCPSocket

_new_with_prelude

Methods inherited from IPSocket

#addr, getaddress, #peeraddr, #recvfrom

Methods inherited from BasicSocket

do_not_reverse_lookup, do_not_reverse_lookup=, for_fd, #local_address, #recv_nonblock, #remote_address

Methods inherited from IO

#<<, #each, #each_byte, #each_char, #hash, open, pipe, popen, #pos=, #print, #printf, #puts, read, #rewind, #ungetbyte

Constructor Details

#initialize(host = nil, service) ⇒ TCPServer

Returns a new instance of TCPServer.



265
266
267
268
269
270
271
272
273
274
275
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 265

def initialize(host=nil, service)
  ai = Addrinfo.getaddrinfo(host, service, nil, nil, nil, Socket::AI_PASSIVE)[0]
  @init_with_fd = true
  super(Socket._socket(ai.afamily, Socket::SOCK_STREAM, 0), "r+")
  if Socket.const_defined?(:SO_REUSEADDR)
    self.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
  end
  Socket._bind(self.fileno, ai.to_sockaddr)
  listen(5)
  self
end

Instance Method Details

#acceptObject



277
278
279
280
281
282
283
284
285
286
287
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 277

def accept
  fd = self.sysaccept
  begin
    TCPSocket._new_with_prelude(fd, "r+") {
      @init_with_fd = true
    }
  rescue
    IO._sysclose(fd) rescue nil
    raise
  end
end

#accept_nonblockObject



289
290
291
292
293
294
295
296
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 289

def accept_nonblock
  begin
    self._setnonblock(true)
    self.accept
  ensure
    self._setnonblock(false)
  end
end

#listen(backlog) ⇒ Object



298
299
300
301
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 298

def listen(backlog)
  Socket._listen(self.fileno, backlog)
  0
end

#sysacceptObject



303
304
305
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 303

def sysaccept
  Socket._accept(self.fileno)
end