aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Berler <[email protected]>2015-12-27 06:05:07 -0800
committerSteven Berler <[email protected]>2015-12-27 06:05:07 -0800
commit49488c91514329c0de0613f1c9647d6e35ac48b9 (patch)
tree7dcb953793df052e60474981e863a51f5a89eb02
parentfix broken ip and port in voice client (diff)
downloaddiscord.py-49488c91514329c0de0613f1c9647d6e35ac48b9.tar.xz
discord.py-49488c91514329c0de0613f1c9647d6e35ac48b9.zip
make ip/port voice packet logic more clear
Adds comments and also rewrites the logic in a way that is much easier to see what is going on. For example you can now easily see that the port is actually encoded in little endian (which is different from everything else).
-rw-r--r--discord/voice_client.py16
1 files changed, 8 insertions, 8 deletions
diff --git a/discord/voice_client.py b/discord/voice_client.py
index f2444130..da8eff3b 100644
--- a/discord/voice_client.py
+++ b/discord/voice_client.py
@@ -211,16 +211,16 @@ class VoiceClient:
self.socket.sendto(packet, (self.endpoint_ip, self.voice_port))
recv = yield from self.loop.sock_recv(self.socket, 70)
log.debug('received packet in initial_connection: {}'.format(recv))
- ip = []
- for x in range(4, len(recv)):
- val = recv[x]
- if val == 0:
- break
- ip.append(chr(val))
+ # the ip is ascii starting at the 4th byte and ending at the first null
+ ip_start = 4
+ ip_end = recv.index(0, ip_start)
+ self.ip = recv[ip_start:ip_end].decode('ascii')
+
+ # the port is a little endian unsigned short in the last two bytes
+ # yes, this is different endianness from everything else
+ self.port = struct.unpack_from('<H', recv, len(recv) - 2)[0]
- self.ip = ''.join(ip)
- self.port = recv[len(recv) - 2] | recv[len(recv) - 1] << 8
log.debug('detected ip: {} port: {}'.format(self.ip, self.port))
payload = {