Class: UDPSocket

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

Constant Summary

Constants inherited from IO

IO::BUF_SIZE, IO::SEEK_CUR, IO::SEEK_END, IO::SEEK_SET

Instance Attribute Summary

Attributes inherited from BasicSocket

#do_not_reverse_lookup

Instance Method Summary collapse

Methods inherited from IPSocket

#addr, getaddress, #peeraddr, #recvfrom

Methods inherited from BasicSocket

#_is_socket=, #_recvfrom, #_setnonblock, #close, do_not_reverse_lookup, do_not_reverse_lookup=, for_fd, #getpeereid, #getpeername, #getsockname, #getsockopt, #local_address, #recv, #recv_nonblock, #remote_address, #setsockopt, #shutdown, #sysread, #sysseek, #syswrite

Methods inherited from IO

#<<, #_check_readable, #_read_buf, #close, #close_on_exec=, #close_on_exec?, #close_write, #closed?, #each, #each_byte, #eof?, #fileno, #flush, #getc, #gets, #hash, #initialize_copy, #isatty, open, #pid, pipe, popen, #pos, #pos=, #print, #printf, #puts, read, #read, #readchar, #readline, #readlines, #rewind, #seek, #sync, #sync=, #sysread, #sysseek, #syswrite, #ungetc, #write

Constructor Details

#initialize(af = Socket::AF_INET) ⇒ UDPSocket

Returns a new instance of UDPSocket



310
311
312
313
314
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 310

def initialize(af=Socket::AF_INET)
  super(Socket._socket(af, Socket::SOCK_DGRAM, 0), "r+")
  @af = af
  self
end

Instance Method Details

#_sockaddr_in(port, host) ⇒ Object



347
348
349
350
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 347

def _sockaddr_in(port, host)
  ai = Addrinfo.getaddrinfo(host, port, @af, Socket::SOCK_DGRAM)[0]
  ai.to_sockaddr
end

#bind(host, port) ⇒ Object



316
317
318
319
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 316

def bind(host, port)
  Socket._bind(self.fileno, _sockaddr_in(port, host))
  0
end

#connect(host, port) ⇒ Object



321
322
323
324
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 321

def connect(host, port)
  Socket._connect(self.fileno, _sockaddr_in(port, host))
  0
end

#recvfrom_nonblock(*args) ⇒ Object



326
327
328
329
330
331
332
333
334
335
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 326

def recvfrom_nonblock(*args)
  s = self
  begin
    self._setnonblock(true)
    self.recvfrom(*args)
  ensure
    # XXX: self is a SystemcallException here! (should be bug)
    s._setnonblock(false)
  end
end

#send(mesg, flags, host = nil, port = nil) ⇒ Object



337
338
339
340
341
342
343
344
345
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 337

def send(mesg, flags, host=nil, port=nil)
  if port
    super(mesg, flags, _sockaddr_in(port, host))
  elsif host
    super(mesg, flags, host)
  else
    super(mesg, flags)
  end
end