summaryrefslogtreecommitdiff
path: root/protocols/Discord/src/gateway.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'protocols/Discord/src/gateway.cpp')
-rw-r--r--protocols/Discord/src/gateway.cpp23
1 files changed, 17 insertions, 6 deletions
diff --git a/protocols/Discord/src/gateway.cpp b/protocols/Discord/src/gateway.cpp
index 43c923ff00..7454383878 100644
--- a/protocols/Discord/src/gateway.cpp
+++ b/protocols/Discord/src/gateway.cpp
@@ -206,25 +206,36 @@ void CDiscordProto::GatewayThreadWorker()
// we have some additional data, not only opcode
if (bufSize > headerSize) {
- if (bIsFinal && dataBuf == NULL) { // it fits, no need to reallocate a buffer
+ if (bIsFinal && payloadSize < _countof(buf)) { // it fits, no need to reallocate a buffer
bDataBufAllocated = false;
dataBuf = (char*)buf + headerSize;
dataBufSize = bufSize - headerSize;
}
else {
bDataBufAllocated = true;
- size_t newSize = dataBufSize + bufSize - headerSize;
+ size_t newSize = dataBufSize + payloadSize;
+ size_t currPacketSize = bufSize - headerSize;
dataBuf = (char*)mir_realloc(dataBuf, newSize+1);
- memcpy(dataBuf + dataBufSize, buf + headerSize, bufSize - headerSize);
+ memcpy(dataBuf + dataBufSize, buf + headerSize, currPacketSize);
+ while (currPacketSize < payloadSize) {
+ int result = Netlib_Recv(m_hGatewayConnection, dataBuf + dataBufSize + currPacketSize, int(payloadSize - currPacketSize), 0);
+ if (result == 0) {
+ debugLogA("Gateway connection gracefully closed");
+ break;
+ }
+ if (result < 0) {
+ debugLogA("Gateway connection error, exiting");
+ break;
+ }
+ currPacketSize += result;
+ }
+
dataBufSize = newSize;
debugLogA("data buffer reallocated to %d bytes", dataBufSize);
}
dataBuf[dataBufSize] = 0;
}
- if (dataBufSize < payloadSize)
- continue;
-
switch (opCode){
case 0: // text packet
case 1: // binary packet