Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ set(TARGET_NAME websocket_server)
if (MSVC)
add_compile_options(/W4 /WX)
else()
add_compile_options(-Wall -Wextra -Wpedantic)
add_compile_options(-Wall -Wextra -Wpedantic -fno-exceptions)
if (CMAKE_BUILD_TYPE MATCHES "Debug|RelWithDebInfo")
add_compile_options(
-fsanitize=address,undefined
Expand Down
3 changes: 2 additions & 1 deletion backend/src/AuthManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
AuthManager::AuthManager() {
credentials_ = {
{"shashwat", "shashwat"},
{"nero", "nero"}
{"nero", "nero"},
{"test", "test"}
};
};
// clang-format on
Expand Down
67 changes: 35 additions & 32 deletions backend/src/WebSocketServer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "include/WebSocketServer.hpp"
#include "include/MarketData.hpp"
#include <charconv>
#include <chrono>
#include <include/handlers/Handlers.hpp>
#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -108,20 +109,22 @@ void WebSocketServer::on_message(WebSocket *ws, std::string_view message,
SocketData *user = ws->getUserData();
if (!message.empty() &&
std::all_of(message.begin(), message.end(), ::isdigit)) {
try {
auto server_timestamp =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
long long client_timestamp = std::stoll(std::string(message));
long long diff = server_timestamp - client_timestamp;
std::println("[LOG]: server: {} client: {} diff: {}",
server_timestamp, client_timestamp, diff);

} catch (const std::exception &e) {
std::println(stderr, "Recieved a non-timestamp message: {} ",
long long client_timestamp{};
// https://en.cppreference.com/cpp/utility/from_chars
auto [ptr, ec] = std::from_chars(
message.data(), message.data() + message.size(), client_timestamp);
if (ec != std::errc{}) {
std::println(stderr, "Received a non-timestamp message: {}",
message);
return;
}
auto server_timestamp =
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
long long diff = server_timestamp - client_timestamp;
std::println("[LOG]: server: {} client: {} diff: {}", server_timestamp,
client_timestamp, diff);
return;
}

Expand All @@ -136,21 +139,26 @@ void WebSocketServer::on_message(WebSocket *ws, std::string_view message,
return MessageType::UNKNOWN;
};

try {
json msg = json::parse(message);
std::string type = msg.value("type", "");
MessageType mtype = message_type_from_string(type);
if (mtype != MessageType::LOGIN && !user->is_authenticated) {
ws->send(R"({"type":"error","message":"not_authenticated"})",
uWS::OpCode::TEXT);
return;
}
json msg = json::parse(message, nullptr, false);
if (msg.is_discarded()) {
std::println("[Error] Invalid JSON:");
ws->send(R"({"type":"error","message":"invalid_json"})",
uWS::OpCode::TEXT);
return;
}
std::string type = msg.value("type", "");
MessageType mtype = message_type_from_string(type);
if (mtype != MessageType::LOGIN && !user->is_authenticated) {
ws->send(R"({"type":"error","message":"not_authenticated"})",
uWS::OpCode::TEXT);
return;
}

/*
* TODO: class with virtual handler function would be the best choice
* for this :)"
*/
// clang-format off
/*
* TODO: class with virtual handler function would be the best choice
* for this :)"
*/
// clang-format off
switch (mtype) {
case MessageType::LOGIN: {
LoginHandler(this, ws, user, msg);
Expand All @@ -171,12 +179,7 @@ void WebSocketServer::on_message(WebSocket *ws, std::string_view message,
break;
}
}
// clang-format on
} catch (const std::exception &e) {
std::println("[Error] Invalid JSON:{}", e.what());
ws->send(R"({"type":"error","message":"invalid_json"})",
uWS::OpCode::TEXT);
}
// clang-format on
}

void WebSocketServer::on_close(WebSocket * /*ws*/, int /*code*/,
Expand Down
13 changes: 3 additions & 10 deletions backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@
int main() {
constexpr int listen_port = 7000;

try {
WebSocketServer server(listen_port, "../certs/key.pem",
"../certs/cert.pem");
server.run();
} catch (const std::exception &e) {
std::cerr << "An unhandled exception occurred: " << e.what()
<< std::endl;
return 1;
}

WebSocketServer server(listen_port, "../certs/key.pem",
"../certs/cert.pem");
server.run();
return 0;
}