Class: CMUX::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/cmux/connection.rb

Defined Under Namespace

Classes: Pointer

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cmux/connection.rb', line 10

def initialize
  buffer = FFI::Buffer.new :pointer, 1
  context = Library.cmux_create buffer

  if context.null?
    ptr = buffer.read_pointer
    begin
      error = ptr.read_string
      raise "MUX creation failed: #{error}"
    ensure
      Library.cmux_free ptr
    end
  end

  @handle = Pointer.new context
end

Instance Method Details

#activateObject



36
37
38
39
40
41
42
43
# File 'lib/cmux/connection.rb', line 36

def activate
  status = Library.cmux_activate @handle
  if status == -1
    raise Library.cmux_error(@handle)
  end

  nil
end

#closeObject



45
46
47
48
# File 'lib/cmux/connection.rb', line 45

def close
  @handle.free
  @handle = nil
end

#close_port(index) ⇒ Object



65
66
67
68
69
70
# File 'lib/cmux/connection.rb', line 65

def close_port(index)
  status = Library.cmux_close_port @handle, index
  if status == -1
    raise Library.cmux_error(@handle)
  end
end

#open(device) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/cmux/connection.rb', line 27

def open(device)
  status = Library.cmux_open @handle, device
  if status == -1
    raise Library.cmux_error(@handle)
  end

  nil
end

#open_port(index) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cmux/connection.rb', line 50

def open_port(index)
  buffer = FFI::Buffer.new :pointer, 1
  status = Library.cmux_open_port @handle, index, buffer
  if status == -1
    raise Library.cmux_error(@handle)
  end

  pointer = buffer.read_pointer
  begin
    pointer.read_string
  ensure
    Library.cmux_free pointer
  end
end