Class: Nl::Genl::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/nl/genl.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolver:) ⇒ Connection

Returns a new instance of Connection.



65
66
67
68
69
70
# File 'lib/nl/genl.rb', line 65

def initialize(resolver:)
  @socket = Socket.new(Core::NETLINK_GENERIC)
  @socket.bind(Socket.sockaddr_nl(0, 0))
  @resolver = resolver
  @id_cache = {}
end

Class Method Details

.open(resolver:) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/nl/genl.rb', line 52

def self.open(resolver:)
  conn = new(resolver:)
  if block_given?
    begin
      yield conn
    ensure
      conn.close
    end
  else
    conn
  end
end

Instance Method Details

#closeObject



82
83
84
# File 'lib/nl/genl.rb', line 82

def close
  @socket.close
end

#open(family_class) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/nl/genl.rb', line 72

def open(family_class)
  proto = family_class::PROTOCOL
  id = @id_cache[proto.name] ||= begin
    proto.family_id
  rescue NotImplementedError
    @resolver.call(@socket, proto.name)
  end
  family_class.new(@socket, protocol: Protocols::Genl.new(proto.name, family_id: id))
end