Skip to content

Commit 3b50d47

Browse files
fixed GenerateAcceptKey (websocket protocol)
1 parent 99e4c6a commit 3b50d47

7 files changed

Lines changed: 66 additions & 37 deletions

File tree

build.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/bash
2-
# build.sh
32

43
# Clone dependencies
54
mkdir -p thirdparty

include/game/GameLogic.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <nlohmann/json.hpp>
99

1010
#include "network/PredictionSystem.hpp"
11+
#include "network/WebSocketProtocol.hpp"
1112

1213
#include "game/LogicCore.hpp"
1314
#include "game/PlayerManager.hpp"

include/network/WebSocketProtocol.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string>
1010
#include <sstream>
1111
#include <vector>
12+
#include <unordered_map>
1213

1314
#include <zlib.h>
1415
#include <glm/glm.hpp>
@@ -34,6 +35,14 @@ namespace WebSocketProtocol {
3435
OP_PONG = 0xA
3536
};
3637

38+
static const std::unordered_map<std::string, int> IPCMessageTypes = {
39+
{"welcome", 1},
40+
{"heartbeat", 2},
41+
{"broadcast", 3},
42+
{"shutdown", 4},
43+
{"reload_config", 5}
44+
};
45+
3746
// WebSocket frame structure
3847
struct WebSocketFrame {
3948
bool fin{true};

src/game/GameLogic.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,25 +1104,36 @@ void GameLogic::PerformMaintenance() {
11041104
void GameLogic::HandleIPCMessage(const nlohmann::json& message) {
11051105
try {
11061106
std::string msgType = message.value("type", "");
1107-
Logger::Debug("GameLogic handling IPC message type: {}", msgType);
1108-
1109-
if (msgType == "welcome") {
1110-
Logger::Info("Received welcome message from master: {}", message.value("message", ""));
1111-
} else if (msgType == "heartbeat") {
1112-
// Handle heartbeat from master
1113-
int count = message.value("count", 0);
1114-
Logger::Debug("Received heartbeat #{} from master", count);
1115-
} else if (msgType == "broadcast") {
1116-
// Broadcast message to all players
1117-
if (message.contains("data")) {
1118-
BroadcastToAllPlayers(message["data"]);
1119-
}
1120-
} else if (msgType == "shutdown") {
1121-
Logger::Info("Received shutdown command from master");
1122-
Shutdown();
1123-
} else if (msgType == "reload_config") {
1124-
Logger::Info("Received config reload command from master");
1125-
// Reload configuration if needed
1107+
auto it = WebSocketProtocol::IPCMessageTypes.find(msgType);
1108+
if (it == WebSocketProtocol::IPCMessageTypes.end()) {
1109+
Logger::Warn("Unknown IPC message type: {}", msgType);
1110+
return;
1111+
}
1112+
int typeCode = it->second;
1113+
switch (typeCode) {
1114+
case 1: // welcome
1115+
//Logger::Info("Received welcome message from master: {}", message.value("message", ""));
1116+
break;
1117+
case 2: // heartbeat
1118+
//int count = message.value("count", 0);
1119+
//Logger::Debug("Received heartbeat #{} from master", count);
1120+
break;
1121+
case 3: // broadcast
1122+
if (message.contains("data")) {
1123+
BroadcastToAllPlayers(message["data"]);
1124+
}
1125+
break;
1126+
case 4: // shutdown
1127+
Logger::Info("Received shutdown command from master");
1128+
Shutdown();
1129+
break;
1130+
case 5: // reload_config
1131+
Logger::Info("Received config reload command from master");
1132+
// TODO: Reload configuration if needed
1133+
break;
1134+
default:
1135+
Logger::Warn("Unhandled IPC message code: {}", typeCode);
1136+
break;
11261137
}
11271138
} catch (const std::exception& e) {
11281139
Logger::Error("Error handling IPC message: {}", e.what());
@@ -1144,7 +1155,7 @@ void GameLogic::BroadcastToAllPlayers(const nlohmann::json& message) {
11441155
auto sessions = connectionManager_->GetAllSessions();
11451156

11461157
if (sessions.empty()) {
1147-
Logger::Debug("No active sessions to broadcast to");
1158+
//Logger::Debug("No active sessions to broadcast to");
11481159
return;
11491160
}
11501161

@@ -1180,7 +1191,7 @@ void GameLogic::BroadcastToAllPlayersBinary(uint16_t messageType, const std::vec
11801191
auto sessions = connectionManager_->GetAllSessions();
11811192

11821193
if (sessions.empty()) {
1183-
Logger::Debug("No active sessions to broadcast binary to");
1194+
//Logger::Debug("No active sessions to broadcast binary to");
11841195
return;
11851196
}
11861197

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void WorkerMain(int workerId, const WorkerGroupConfig& groupConfig, ProcessPool*
219219
try {
220220
// Parse JSON message
221221
auto jsonMsg = nlohmann::json::parse(message);
222-
Logger::Debug("Worker {} received IPC message: {}", workerId, jsonMsg.dump());
222+
//Logger::Debug("Worker {} received IPC message: {}", workerId, jsonMsg.dump());
223223

224224
// Handle IPC message in game logic
225225
gameLogic.HandleIPCMessage(jsonMsg);

src/network/WebSocketProtocol.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,31 +1176,40 @@ std::string GenerateWebSocketKey() {
11761176
std::string GenerateAcceptKey(const std::string& key) {
11771177
const std::string magic_guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
11781178
std::string combined = key + magic_guid;
1179-
1180-
unsigned char hash[SHA_DIGEST_LENGTH];
1179+
unsigned char hash[SHA_DIGEST_LENGTH]; // 20 bytes
11811180
SHA1(reinterpret_cast<const unsigned char*>(combined.c_str()), combined.size(), hash);
11821181

1183-
// Base64 encode
1182+
// Base64 encode according to RFC 4648
11841183
const std::string base64_chars =
1185-
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1184+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11861185

11871186
std::string result;
11881187
int i = 0;
1189-
while (i < SHA_DIGEST_LENGTH) {
1190-
uint32_t octet_a = i < SHA_DIGEST_LENGTH ? hash[i++] : 0;
1191-
uint32_t octet_b = i < SHA_DIGEST_LENGTH ? hash[i++] : 0;
1192-
uint32_t octet_c = i < SHA_DIGEST_LENGTH ? hash[i++] : 0;
1193-
1194-
uint32_t triple = (octet_a << 16) + (octet_b << 8) + octet_c;
11951188

1189+
// Process full 3‑byte groups
1190+
while (i < SHA_DIGEST_LENGTH - 2) {
1191+
uint32_t triple = (hash[i] << 16) | (hash[i+1] << 8) | hash[i+2];
11961192
result += base64_chars[(triple >> 18) & 0x3F];
11971193
result += base64_chars[(triple >> 12) & 0x3F];
11981194
result += base64_chars[(triple >> 6) & 0x3F];
11991195
result += base64_chars[triple & 0x3F];
1196+
i += 3;
12001197
}
12011198

1202-
// Remove padding
1203-
result = result.substr(0, 28);
1199+
// Handle remaining bytes with correct padding
1200+
int remaining = SHA_DIGEST_LENGTH - i;
1201+
if (remaining == 1) {
1202+
uint32_t triple = (hash[i] << 16);
1203+
result += base64_chars[(triple >> 18) & 0x3F];
1204+
result += base64_chars[(triple >> 12) & 0x3F];
1205+
result += "==";
1206+
} else if (remaining == 2) {
1207+
uint32_t triple = (hash[i] << 16) | (hash[i+1] << 8);
1208+
result += base64_chars[(triple >> 18) & 0x3F];
1209+
result += base64_chars[(triple >> 12) & 0x3F];
1210+
result += base64_chars[(triple >> 6) & 0x3F];
1211+
result += "=";
1212+
}
12041213

12051214
return result;
12061215
}

src/process/ProcessPool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ bool ProcessPool::SendToWorker(int workerId, const std::string& message) {
466466
Logger::Error("Failed to send message content to worker {}", workerId);
467467
return false;
468468
}
469-
Logger::Debug("Sent {} bytes to worker {}", message.length(), workerId);
469+
//Logger::Debug("Sent {} bytes to worker {}", message.length(), workerId);
470470
return true;
471471
}
472472

@@ -500,7 +500,7 @@ std::string ProcessPool::ReceiveFromMaster() {
500500
}
501501
buffer[msg_len] = '\0';
502502
std::string message(buffer.data(), msg_len);
503-
Logger::Debug("Received {} bytes from master", msg_len);
503+
//Logger::Debug("Received {} bytes from master", msg_len);
504504
return message;
505505
}
506506

0 commit comments

Comments
 (0)