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?)
deprecated
Deprecated.
Discord no longer supports unencrypted voice communication.
-
#mode ⇒ Object
readonly
The UDP encryption mode.
-
#secret_key ⇒ Object
writeonly
Sets the secret key used for encryption.
Instance Method Summary collapse
-
#connect(ip, 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.
52 53 54 55 |
# File 'lib/discordrb/voice/network.rb', line 52 def initialize @socket = UDPSocket.new @encrypted = true end |
Instance Attribute Details
#encrypted ⇒ true, false Also known as: encrypted?
Discord no longer supports unencrypted voice communication.
Returns whether or not UDP communications are encrypted.
38 39 40 |
# File 'lib/discordrb/voice/network.rb', line 38 def encrypted @encrypted end |
#mode ⇒ Object
The UDP encryption mode
45 46 47 |
# File 'lib/discordrb/voice/network.rb', line 45 def mode @mode end |
#secret_key=(value) ⇒ Object (writeonly)
Sets the secret key used for encryption
42 43 44 |
# File 'lib/discordrb/voice/network.rb', line 42 def secret_key=(value) @secret_key = value end |
Instance Method Details
#connect(ip, port, ssrc) ⇒ Object
Initializes the UDP socket with data obtained from opcode 2.
62 63 64 65 66 |
# File 'lib/discordrb/voice/network.rb', line 62 def connect(ip, port, ssrc) @ip = ip @port = port @ssrc = ssrc end |
#receive_discovery_reply ⇒ Array(String, Integer)
Waits for a UDP discovery reply, and returns the sent data.
70 71 72 73 74 75 76 |
# File 'lib/discordrb/voice/network.rb', line 70 def receive_discovery_reply # Wait for a UDP message = @socket.recv(74) ip = [8..-3].delete("\0") port = [-2..].unpack1('n') [ip, port] end |
#send_audio(buf, sequence, time) ⇒ Object
Makes an audio packet from a buffer and sends it to Discord.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/discordrb/voice/network.rb', line 83 def send_audio(buf, sequence, time) # Header of the audio packet header = [0x80, 0x78, sequence, time, @ssrc].pack('CCnNN') nonce = generate_nonce(header) buf = encrypt_audio(buf, nonce) data = header + buf # xsalsa20_poly1305 does not require an appended nonce data += nonce unless @mode == 'xsalsa20_poly1305' send_packet(data) 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/discordrb/voice/network.rb', line 100 def send_discovery # Create empty packet discovery_packet = '' # Add Type request (0x1 = request, 0x2 = response) discovery_packet += [0x1].pack('n') # Add Length (excluding Type and itself = 70) discovery_packet += [70].pack('n') # Add SSRC discovery_packet += [@ssrc].pack('N') # Add 66 zeroes so the packet is 74 bytes long discovery_packet += "\0" * 66 send_packet(discovery_packet) end |