Skip to content

Commit a08aae2

Browse files
committed
Fixed websocket receive function to handle long payloads
1 parent 3443288 commit a08aae2

File tree

5 files changed

+70
-34
lines changed

5 files changed

+70
-34
lines changed

includes/api/WebSocketClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class WebSocketClient {
2020

2121
~WebSocketClient();
2222

23+
void testPixels() const;
24+
2325
void testAllPixels() const;
2426

2527
void reset() const;

includes/enums/Commands.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string>
99

1010
enum Commands {
11+
PIXELS,
1112
ALL,
1213
RESET,
1314
TAKE,
@@ -19,6 +20,9 @@ enum Commands {
1920
};
2021

2122
inline Commands stringToCommand(const std::string &commandStr) {
23+
if (commandStr == "pixels") {
24+
return PIXELS;
25+
}
2226
if (commandStr == "all") {
2327
return ALL;
2428
}
@@ -45,6 +49,7 @@ inline Commands stringToCommand(const std::string &commandStr) {
4549

4650
inline void printHelp() {
4751
std::cout << "Available commands:\n"
52+
<< " pixels - Test some pixels\n"
4853
<< " all - Test all pixels\n"
4954
<< " reset - Turn off all pixels\n"
5055
<< " take - Take a screenshot and send it\n"

src/BitmapConverter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//
44

55
#include "../includes/BitmapConverter.h"
6-
#include <iostream>
76
#include <sstream>
87

98
std::vector<BYTE> BitmapConverter::HBitmapToRGB(HBITMAP hBitmap, int width, int height) {

src/api/WebSocketClient.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ WebSocketClient::~WebSocketClient() {
1919
WSACleanup();
2020
}
2121

22+
void WebSocketClient::testPixels() const {
23+
connection.sendMessage(R"({"type":"pixels","data":[[0,1,1],[0,2,2],[0,3,3]]})");
24+
}
25+
2226
void WebSocketClient::testAllPixels() const {
2327
connection.sendMessage(
2428
R"({"type":"allpixels","data":"00001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333000011112222333300001111222233330000111122223333"})");
@@ -106,6 +110,9 @@ void WebSocketClient::run() {
106110
Commands command = stringToCommand(commandStr);
107111

108112
switch (command) {
113+
case PIXELS:
114+
testPixels();
115+
break;
109116
case ALL:
110117
testAllPixels();
111118
break;

src/api/WebSocketConnection.cpp

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ bool WebSocketConnection::performHandshake() const {
7171
<< "Origin: http://" << server_address << "\r\n\r\n";
7272

7373
const std::string request = handshake.str();
74-
send(sock, request.c_str(), request.size(), 0);
74+
send(sock, request.c_str(), static_cast<int>(request.size()), 0);
7575

7676
char buffer[BUFFER_SIZE];
7777
if (const int bytes_received = recv(sock, buffer, BUFFER_SIZE, 0); bytes_received > 0) {
@@ -140,49 +140,72 @@ void WebSocketConnection::sendMessage(const std::string &message) const {
140140
frame.push_back(message[i] ^ masking_key[i % 4]);
141141
}
142142

143-
send(sock, reinterpret_cast<const char *>(frame.data()), frame.size(), 0);
143+
send(sock, reinterpret_cast<const char *>(frame.data()), static_cast<int>(frame.size()), 0);
144144
}
145145

146-
// TODO: check/rework that because it throws an error if the message is too long
147146
std::string WebSocketConnection::receiveMessage() const {
148-
char buffer[BUFFER_SIZE];
149-
int bytes_received = recv(sock, buffer, BUFFER_SIZE, 0);
150-
if (bytes_received <= 0) {
151-
return "";
152-
}
147+
std::vector<unsigned char> buffer(BUFFER_SIZE);
148+
std::vector<unsigned char> message_data;
153149

154-
if ((buffer[0] & 0x0F) != 0x1) {
155-
return "";
156-
}
150+
while (true) {
151+
int bytes_received = recv(sock, reinterpret_cast<char *>(buffer.data()), static_cast<int>(buffer.size()), 0);
152+
if (bytes_received <= 0) {
153+
return "";
154+
}
157155

158-
int payload_len = buffer[1] & 0x7F;
159-
int offset = 2;
156+
if ((buffer[0] & 0x0F) != 0x1) {
157+
return "";
158+
}
160159

161-
if (payload_len == 126) {
162-
payload_len = buffer[2] << 8 | buffer[3];
163-
offset += 2;
164-
} else if (payload_len == 127) {
165-
payload_len = 0;
166-
for (int i = 0; i < 8; ++i) {
167-
payload_len = payload_len << 8 | buffer[offset++];
160+
int payload_len = buffer[1] & 0x7F;
161+
int offset = 2;
162+
163+
if (payload_len == 126) {
164+
payload_len = buffer[2] << 8 | buffer[3];
165+
offset += 2;
166+
} else if (payload_len == 127) {
167+
payload_len = 0;
168+
for (int i = 0; i < 8; ++i) {
169+
payload_len = payload_len << 8 | buffer[offset++];
170+
}
168171
}
169-
}
170172

171-
bool mask = buffer[1] & 0x80;
172-
unsigned char masking_key[4];
173-
if (mask) {
174-
std::memcpy(masking_key, buffer + offset, 4);
175-
offset += 4;
176-
}
173+
// check if the message is masked
174+
const bool mask = buffer[1] & 0x80;
175+
unsigned char masking_key[4] = {};
176+
if (mask) {
177+
std::memcpy(masking_key, buffer.data() + offset, 4);
178+
offset += 4;
179+
}
177180

178-
std::string message(buffer + offset, payload_len);
179-
if (mask) {
180-
for (int i = 0; i < payload_len; ++i) {
181-
message[i] ^= masking_key[i % 4];
181+
// read the actual payload
182+
int message_offset = offset;
183+
while (message_data.size() < payload_len) {
184+
const int chunk_size = std::min(payload_len - static_cast<int>(message_data.size()),
185+
bytes_received - message_offset);
186+
message_data.insert(message_data.end(), buffer.begin() + message_offset,
187+
buffer.begin() + message_offset + chunk_size);
188+
189+
// if we've read the entire payload, break out of the loop
190+
if (message_data.size() >= payload_len) {
191+
break;
192+
}
193+
194+
bytes_received = recv(sock, reinterpret_cast<char *>(buffer.data()), static_cast<int>(buffer.size()), 0);
195+
if (bytes_received <= 0) {
196+
return "";
197+
}
198+
message_offset = 0; // reset message offset for the next chunk
182199
}
183-
}
184200

185-
return message;
201+
if (mask) {
202+
for (int i = 0; i < payload_len; ++i) {
203+
message_data[i] ^= masking_key[i % 4];
204+
}
205+
}
206+
207+
return {message_data.begin(), message_data.end()};
208+
}
186209
}
187210

188211
void WebSocketConnection::closeConnection() {

0 commit comments

Comments
 (0)