Class: OnyxCord::Voice::VoiceUDP
- Inherits:
-
Object
- Object
- OnyxCord::Voice::VoiceUDP
- Defined in:
- lib/onyxcord/voice/network/udp.rb
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
- #close ⇒ Object
-
#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.
21 22 23 24 |
# File 'lib/onyxcord/voice/network/udp.rb', line 21 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.
7 8 9 |
# File 'lib/onyxcord/voice/network/udp.rb', line 7 def encrypted @encrypted end |
#mode ⇒ Object
The UDP encryption mode
14 15 16 |
# File 'lib/onyxcord/voice/network/udp.rb', line 14 def mode @mode end |
#secret_key=(value) ⇒ Object (writeonly)
Sets the secret key used for encryption
11 12 13 |
# File 'lib/onyxcord/voice/network/udp.rb', line 11 def secret_key=(value) @secret_key = value end |
Instance Method Details
#close ⇒ Object
87 88 89 |
# File 'lib/onyxcord/voice/network/udp.rb', line 87 def close @socket.close unless @socket.closed? end |
#connect(ip, port, ssrc) ⇒ Object
Initializes the UDP socket with data obtained from opcode 2.
31 32 33 34 35 |
# File 'lib/onyxcord/voice/network/udp.rb', line 31 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.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/onyxcord/voice/network/udp.rb', line 39 def receive_discovery_reply # Wait for a UDP message with timeout raise Timeout::Error, 'Timed out waiting for UDP discovery reply' unless @socket.wait_readable(5) = @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.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/onyxcord/voice/network/udp.rb', line 55 def send_audio(buf, sequence, time) # Header of the audio packet header = generate_header(sequence, time) nonce = generate_nonce buf = encrypt_audio(buf, header, nonce) data = header + buf + nonce.byteslice(0, 4) 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/onyxcord/voice/network/udp.rb', line 68 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 |