Class: Discordrb::Voice::VoiceUDP
- Inherits:
-
Object
- Object
- Discordrb::Voice::VoiceUDP
- Defined in:
- lib/discordrb/voice/network.rb
Overview
Represents a UDP connection to a voice server. This connection is used to send the actual audio data.
Instance Attribute Summary collapse
-
#encrypted ⇒ true, false
(also: #encrypted?)
Whether or not UDP communications are encrypted.
-
#secret_key ⇒ Object
writeonly
Sets the secret key used for encryption.
Instance Method Summary collapse
-
#connect(endpoint, port, ssrc) ⇒ Object
Initializes the UDP socket with data obtained from opcode 2.
-
#initialize ⇒ VoiceUDP
constructor
Creates a new UDP connection.
-
#receive_discovery_reply ⇒ Array(String, Integer)
Waits for a UDP discovery reply, and returns the sent data.
-
#send_audio(buf, sequence, time) ⇒ Object
Makes an audio packet from a buffer and sends it to Discord.
-
#send_discovery ⇒ Object
Sends the UDP discovery packet with the internally stored SSRC.
Constructor Details
#initialize ⇒ VoiceUDP
Creates a new UDP connection. Only creates a socket as the discovery reply may come before the data is initialized.
34 35 36 |
# File 'lib/discordrb/voice/network.rb', line 34 def initialize @socket = UDPSocket.new end |
Instance Attribute Details
#encrypted ⇒ true, false Also known as: encrypted?
Returns whether or not UDP communications are encrypted.
26 27 28 |
# File 'lib/discordrb/voice/network.rb', line 26 def encrypted @encrypted end |
#secret_key=(value) ⇒ Object (writeonly)
Sets the secret key used for encryption
30 31 32 |
# File 'lib/discordrb/voice/network.rb', line 30 def secret_key=(value) @secret_key = value end |
Instance Method Details
#connect(endpoint, port, ssrc) ⇒ Object
Initializes the UDP socket with data obtained from opcode 2.
43 44 45 46 47 48 49 50 51 |
# File 'lib/discordrb/voice/network.rb', line 43 def connect(endpoint, port, ssrc) @endpoint = endpoint @endpoint = @endpoint[6..-1] if @endpoint.start_with? 'wss://' @endpoint.gsub!(':80', '') # The endpoint may contain a port, we don't want that @endpoint = Resolv.getaddress @endpoint @port = port @ssrc = ssrc end |
#receive_discovery_reply ⇒ Array(String, Integer)
Waits for a UDP discovery reply, and returns the sent data.
55 56 57 58 59 60 61 |
# File 'lib/discordrb/voice/network.rb', line 55 def receive_discovery_reply # Wait for a UDP message = @socket.recv(70) ip = [4..-3].delete("\0") port = [-2..-1].to_i [ip, port] end |
#send_audio(buf, sequence, time) ⇒ Object
Makes an audio packet from a buffer and sends it to Discord.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/discordrb/voice/network.rb', line 68 def send_audio(buf, sequence, time) # Header of the audio packet header = [0x80, 0x78, sequence, time, @ssrc].pack('CCnNN') # Encrypt data, if necessary if encrypted? buf = encrypt_audio(header, buf) end send_packet(header + buf) end |
#send_discovery ⇒ Object
Sends the UDP discovery packet with the internally stored SSRC. Discord will send a reply afterwards which can be received using #receive_discovery_reply
82 83 84 85 86 87 88 |
# File 'lib/discordrb/voice/network.rb', line 82 def send_discovery discovery_packet = [@ssrc].pack('N') # Add 66 zeroes so the packet is 70 bytes long discovery_packet += "\0" * 66 send_packet(discovery_packet) end |