Class: UNIXSocket

Inherits:
BasicSocket show all
Includes:
IO::TransferIO
Defined in:
lib/socket/unix_socket.rb

Direct Known Subclasses

UNIXServer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicSocket

#close_read, #close_write, #connect_address, do_not_reverse_lookup, #do_not_reverse_lookup, do_not_reverse_lookup=, #do_not_reverse_lookup=, for_fd, #getpeereid, #getpeername, #getsockname, #getsockopt, #recv, #recv_nonblock, #recvmsg, #recvmsg_nonblock, #send, #sendmsg, #sendmsg_nonblock, #setsockopt, #shutdown

Constructor Details

#initialize(path) ⇒ UNIXSocket

Returns a new instance of UNIXSocket.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/socket/unix_socket.rb', line 17

def initialize(path)
  @no_reverse_lookup = self.class.do_not_reverse_lookup
  @path              = '' # empty for client sockets

  fd = RubySL::Socket::Foreign.socket(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)

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

  IO.setup(self, fd, 'r+', true)
  binmode

  sockaddr = Socket.sockaddr_un(path)
  status   = RubySL::Socket::Foreign.connect(descriptor, sockaddr)

  Errno.handle('connect(2)') if status < 0
end

Class Method Details

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



4
5
6
7
8
9
10
11
# File 'lib/socket/unix_socket.rb', line 4

def self.socketpair(type = Socket::SOCK_STREAM, protocol = 0)
  family = Socket::AF_UNIX
  type   = RubySL::Socket.socket_type(type)

  fd0, fd1 = RubySL::Socket::Foreign.socketpair(family, type, protocol)

  [for_fd(fd0), for_fd(fd1)]
end

Instance Method Details

#addrObject



42
43
44
# File 'lib/socket/unix_socket.rb', line 42

def addr
  ['AF_UNIX', path]
end

#local_addressObject



68
69
70
71
72
# File 'lib/socket/unix_socket.rb', line 68

def local_address
  address = addr

  Addrinfo.new(Socket.pack_sockaddr_un(address[1]), :UNIX, :STREAM)
end

#pathObject



38
39
40
# File 'lib/socket/unix_socket.rb', line 38

def path
  @path ||= RubySL::Socket::Foreign.getsockname(descriptor).unpack('SZ*')[1]
end

#peeraddrObject



46
47
48
49
50
# File 'lib/socket/unix_socket.rb', line 46

def peeraddr
  path = RubySL::Socket::Foreign.getpeername(descriptor).unpack('SZ*')[1]

  ['AF_UNIX', path]
end

#recv_io(klass = IO, mode = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/socket/unix_socket.rb', line 52

def recv_io(klass = IO, mode = nil)
  begin
    fd = recv_fd
  rescue PrimitiveFailure
    raise SocketError, "file descriptor was not passed"
  end

  return fd unless klass

  if klass.is_a?(BasicSocket)
    klass.for_fd(fd)
  else
    klass.for_fd(fd, mode)
  end
end

#recvfrom(bytes_read, flags = 0) ⇒ Object



34
35
36
# File 'lib/socket/unix_socket.rb', line 34

def recvfrom(bytes_read, flags = 0)
  socket_recv(bytes_read, flags, 2)
end

#remote_addressObject



74
75
76
77
78
# File 'lib/socket/unix_socket.rb', line 74

def remote_address
  address = peeraddr

  Addrinfo.new(Socket.pack_sockaddr_un(address[1]), :UNIX, :STREAM)
end