Class: Mumble::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Connection

Returns a new instance of Connection.



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

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

Instance Method Details

#connectObject



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

def connect
  context = OpenSSL::SSL::SSLContext.new
  context.verify_mode = OpenSSL::SSL::VERIFY_NONE
  tcp_sock = TCPSocket.new @host, @port
  @sock = OpenSSL::SSL::SSLSocket.new tcp_sock, context
  @sock.connect
end

#disconnectObject



21
22
23
# File 'lib/mumble-ruby/connection.rb', line 21

def disconnect
  @sock.sysclose
end

#read_messageObject



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

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



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

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



39
40
41
42
# File 'lib/mumble-ruby/connection.rb', line 39

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