Module: NNG::Protocols

Defined in:
lib/nng/protocols.rb

Overview

Protocol-specific socket opening functions

Constant Summary collapse

PROTOCOL_FUNCTIONS =

Protocol name to function mapping

{
  pair0: :nng_pair0_open,
  pair1: :nng_pair1_open,
  pair: :nng_pair1_open, # alias for pair1
  push0: :nng_push0_open,
  push: :nng_push0_open,
  pull0: :nng_pull0_open,
  pull: :nng_pull0_open,
  pub0: :nng_pub0_open,
  pub: :nng_pub0_open,
  sub0: :nng_sub0_open,
  sub: :nng_sub0_open,
  req0: :nng_req0_open,
  req: :nng_req0_open,
  rep0: :nng_rep0_open,
  rep: :nng_rep0_open,
  surveyor0: :nng_surveyor0_open,
  surveyor: :nng_surveyor0_open,
  respondent0: :nng_respondent0_open,
  respondent: :nng_respondent0_open,
  bus0: :nng_bus0_open,
  bus: :nng_bus0_open
}.freeze

Class Method Summary collapse

Class Method Details

.attach_protocol_functionsObject

Dynamically load protocol functions from libnng



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nng/protocols.rb', line 7

def self.attach_protocol_functions
  extend ::FFI::Library

  # Use the same library as FFI module
  ffi_lib NNG::FFI.loaded_lib_path

  # Pair protocols
  attach_function :nng_pair0_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_pair0_open_raw, [FFI::NngSocket.by_ref], :int
  attach_function :nng_pair1_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_pair1_open_raw, [FFI::NngSocket.by_ref], :int
  attach_function :nng_pair1_open_poly, [FFI::NngSocket.by_ref], :int

  # Push/Pull protocols
  attach_function :nng_push0_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_push0_open_raw, [FFI::NngSocket.by_ref], :int
  attach_function :nng_pull0_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_pull0_open_raw, [FFI::NngSocket.by_ref], :int

  # Pub/Sub protocols
  attach_function :nng_pub0_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_pub0_open_raw, [FFI::NngSocket.by_ref], :int
  attach_function :nng_sub0_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_sub0_open_raw, [FFI::NngSocket.by_ref], :int

  # Req/Rep protocols
  attach_function :nng_req0_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_req0_open_raw, [FFI::NngSocket.by_ref], :int
  attach_function :nng_rep0_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_rep0_open_raw, [FFI::NngSocket.by_ref], :int

  # Surveyor/Respondent protocols
  attach_function :nng_surveyor0_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_surveyor0_open_raw, [FFI::NngSocket.by_ref], :int
  attach_function :nng_respondent0_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_respondent0_open_raw, [FFI::NngSocket.by_ref], :int

  # Bus protocol
  attach_function :nng_bus0_open, [FFI::NngSocket.by_ref], :int
  attach_function :nng_bus0_open_raw, [FFI::NngSocket.by_ref], :int
rescue ::FFI::NotFoundError => e
  # Some protocols might not be available in all builds
  warn "Warning: Some NNG protocol functions not available: #{e.message}"
end

.open_socket(protocol, raw: false) ⇒ FFI::NngSocket

Open a socket for the given protocol

Parameters:

  • protocol (Symbol)

    protocol name (:pair0, :pair1, :push, :pull, etc.)

  • raw (Boolean) (defaults to: false)

    open in raw mode

Returns:

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/nng/protocols.rb', line 81

def self.open_socket(protocol, raw: false)
  func_name = PROTOCOL_FUNCTIONS[protocol]
  raise ArgumentError, "Unknown protocol: #{protocol}" unless func_name

  func_name = "#{func_name}_raw".to_sym if raw

  socket = FFI.socket_initializer
  ret = send(func_name, socket)
  FFI.check_error(ret, "Opening #{protocol} socket")
  socket
end