aboutsummaryrefslogtreecommitdiff
path: root/discord/gateway.py
diff options
context:
space:
mode:
authorRapptz <[email protected]>2017-10-12 22:53:20 -0400
committerRapptz <[email protected]>2017-10-12 22:53:20 -0400
commit462191a08b5b2efb83f5bc32935dc546d35a744b (patch)
treed0551b1fe9c58b83a50b2350b85078e0cb3135b1 /discord/gateway.py
parent[commands] Minor speed-up for the BucketType.guild case. (diff)
downloaddiscord.py-462191a08b5b2efb83f5bc32935dc546d35a744b.tar.xz
discord.py-462191a08b5b2efb83f5bc32935dc546d35a744b.zip
Implement zlib streaming for the gateway.
Diffstat (limited to 'discord/gateway.py')
-rw-r--r--discord/gateway.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/discord/gateway.py b/discord/gateway.py
index 0ab02760..eceb7a28 100644
--- a/discord/gateway.py
+++ b/discord/gateway.py
@@ -186,6 +186,8 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol):
# ws related stuff
self.session_id = None
self.sequence = None
+ self._zlib = zlib.decompressobj()
+ self._buffer = bytearray()
@classmethod
@asyncio.coroutine
@@ -312,8 +314,18 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol):
self._dispatch('socket_raw_receive', msg)
if isinstance(msg, bytes):
- msg = zlib.decompress(msg, 15, 10490000) # This is 10 MiB
- msg = msg.decode('utf-8')
+ self._buffer.extend(msg)
+
+ if len(msg) >= 4:
+ suffix = int.from_bytes(msg[-4:], byteorder='big')
+ if suffix == 0xFFFF:
+ msg = self._zlib.decompress(self._buffer)
+ msg = msg.decode('utf-8')
+ self._buffer = bytearray()
+ else:
+ return
+ else:
+ return
msg = json.loads(msg)