Class: Mumble::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mumble-ruby/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, cert_manager) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
# File 'lib/mumble-ruby/connection.rb', line 7

def initialize(host, port, cert_manager)
  @host = host
  @port = port
  @cert_manager = cert_manager
  @write_lock = Mutex.new
end

Instance Method Details

#connectObject



14
15
16
17
18
19
20
21
# File 'lib/mumble-ruby/connection.rb', line 14

def connect
  context = OpenSSL::SSL::SSLContext.new(:TLSv1)
  context.verify_mode = OpenSSL::SSL::VERIFY_NONE
  [:key, :cert].each { |s| context.send("#{s}=", @cert_manager.send(s)) }
  tcp_sock = TCPSocket.new @host, @port
  @sock = OpenSSL::SSL::SSLSocket.new tcp_sock, context
  @sock.connect
end

#disconnectObject



23
24
25
# File 'lib/mumble-ruby/connection.rb', line 23

def disconnect
  @sock.close
end

#read_messageObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mumble-ruby/connection.rb', line 27

def read_message
  header = read_data 6
  type, len = header.unpack Messages::HEADER_FORMAT
  data = read_data len
  if type == message_type(:udp_tunnel)
    # UDP Packet -- No Protobuf
    message = message_class(:udp_tunnel).new
    message.packet = data
  else
    message = message_raw type, data
  end
  message
end

#send_message(sym, attrs) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/mumble-ruby/connection.rb', line 46

def send_message(sym, attrs)
  type, klass = message(sym)
  message = klass.new
  attrs.each { |k, v| message.send("#{k}=", v) }
  serial = message.serialize_to_string
  header = [type, serial.size].pack Messages::HEADER_FORMAT
  send_data(header + serial)
end

#send_udp_packet(packet) ⇒ Object



41
42
43
44
# File 'lib/mumble-ruby/connection.rb', line 41

def send_udp_packet(packet)
  header = [message_type(:udp_tunnel), packet.length].pack Messages::HEADER_FORMAT
  send_data(header + packet)
end