Class: UNIXSocket

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

Direct Known Subclasses

UNIXServer

Instance Attribute Summary

Attributes inherited from BasicSocket

#do_not_reverse_lookup

Class Method Summary collapse

Instance Method Summary collapse

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=, #printf, read, #rewind

Constructor Details

#initialize(path, &block) ⇒ UNIXSocket

call-seq:

UNIXSocket.new(path) -> unixsocket
UNIXSocket.new(path) { |sock| block } -> obj

Creates a new Unix domain socket connected to the given path. If a block is given, yields the socket and closes it when done.

sock = UNIXSocket.new("/tmp/socket")
UNIXSocket.new("/tmp/socket") { |s| s.write("data") }


951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 951

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

call-seq:

UNIXSocket.socketpair(type=Socket::SOCK_STREAM, protocol=0) -> [socket1, socket2]

Creates a pair of connected Unix domain sockets.

sock1, sock2 = UNIXSocket.socketpair
sock1, sock2 = UNIXSocket.socketpair(Socket::SOCK_DGRAM)


981
982
983
984
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 981

def 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

call-seq:

unixsocket.addr -> [family, path]

Returns the local address information as an array.

sock.addr  #=> ["AF_UNIX", "/tmp/socket"]


997
998
999
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 997

def addr
  [ "AF_UNIX", path ]
end

#pathObject

call-seq:

unixsocket.path -> string

Returns the path of the Unix domain socket.

sock.path  #=> "/tmp/socket"


1009
1010
1011
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 1009

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

#peeraddrObject

call-seq:

unixsocket.peeraddr -> [family, path]

Returns the remote address information as an array.

sock.peeraddr  #=> ["AF_UNIX", "/tmp/peer_socket"]


1021
1022
1023
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 1021

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

#recvfrom(maxlen, flags = 0) ⇒ Object

def recv_io



1027
1028
1029
1030
1031
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 1027

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