Class: UNIXSocket

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

Direct Known Subclasses

UNIXServer

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

Class Method Summary collapse

Instance Method Summary collapse

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, #send, #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(path, &block) ⇒ UNIXSocket

Returns a new instance of UNIXSocket



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 471

def initialize(path, &block)
  if self.is_a? UNIXServer
    super(path, "r")
  else
    super(Socket._socket(Socket::AF_UNIX, Socket::SOCK_STREAM, 0), "r+")
    Socket._connect(self.fileno, Socket.sockaddr_un(path))

    if block_given?
      begin
        yield self
      ensure
        begin
          self.close unless self.closed?
        rescue StandardError
        end
      end
    end
  end
end

Class Method Details

.socketpair(type = Socket::SOCK_STREAM, protocol = 0) ⇒ Object Also known as: pair



491
492
493
494
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 491

def self.socketpair(type=Socket::SOCK_STREAM, protocol=0)
  a = Socket.socketpair(Socket::AF_UNIX, type, protocol)
  [ UNIXSocket.for_fd(a[0]), UNIXSocket.for_fd(a[1]) ]
end

Instance Method Details

#addrObject



500
501
502
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 500

def addr
  [ "AF_UNIX", path ]
end

#pathObject



504
505
506
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 504

def path
  Addrinfo.new(self.getsockname).unix_path
end

#peeraddrObject



508
509
510
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 508

def peeraddr
  [ "AF_UNIX", Addrinfo.new(self.getpeername).unix_path ]
end

#recvfrom(maxlen, flags = 0) ⇒ Object

def recv_io



514
515
516
517
518
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 514

def recvfrom(maxlen, flags=0)
  msg, sa = _recvfrom(maxlen, flags)
  path = (sa.size > 0) ? Addrinfo.new(sa).unix_path : ""
  [ msg, [ "AF_UNIX", path ] ]
end