Module: ZZQ::Transport::IPC

Defined in:
lib/zzq/transport/ipc.rb

Overview

IPC transport using Unix domain sockets.

Endpoint forms:

* `ipc:///var/run/zzq.sock` — filesystem path (portable).
* `ipc://@zzq-local`        — Linux abstract namespace (the
  leading `@` becomes a NUL byte in sockaddr.sun_path).

Defined Under Namespace

Classes: Listener

Class Method Summary collapse

Class Method Details

.abstract?(path) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/zzq/transport/ipc.rb', line 51

def abstract?(path)
  path.start_with?("@")
end

.bind(endpoint, engine) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/zzq/transport/ipc.rb', line 20

def bind(endpoint, engine, **)
  path      = parse_path(endpoint)
  sock_path = to_socket_path(path)

  File.delete(sock_path) if !abstract?(path) && File.exist?(sock_path)

  server = UNIXServer.new(sock_path)
  Listener.new(endpoint, server, path, engine)
end

.connect(endpoint, engine) ⇒ Object



31
32
33
34
35
36
# File 'lib/zzq/transport/ipc.rb', line 31

def connect(endpoint, engine, **)
  path      = parse_path(endpoint)
  sock_path = to_socket_path(path)
  sock      = UNIXSocket.new(sock_path)
  engine.handle_connected(IO::Stream::Buffered.wrap(sock), endpoint: endpoint)
end

.parse_path(endpoint) ⇒ Object



39
40
41
# File 'lib/zzq/transport/ipc.rb', line 39

def parse_path(endpoint)
  endpoint.sub(%r{\Aipc://}, "")
end

.to_socket_path(path) ⇒ Object

Linux abstract-namespace Unix sockets: leading ‘@` becomes a NUL byte in sun_path. Only supported on Linux.



46
47
48
# File 'lib/zzq/transport/ipc.rb', line 46

def to_socket_path(path)
  abstract?(path) ? "\0#{path[1..]}" : path
end