Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Dec 16, 2024
1 parent 7ab60b1 commit 6865076
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(carimbo LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(SDL2_DIR "${CMAKE_BINARY_DIR}")
Expand Down
3 changes: 0 additions & 3 deletions mise.toml

This file was deleted.

1 change: 0 additions & 1 deletion src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <random>
#include <ranges>
#include <sstream>
Expand Down
26 changes: 13 additions & 13 deletions src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ socket::socket() noexcept {
emscripten_websocket_set_onopen_callback(
_socket,
this,
[](int, const EmscriptenWebSocketOpenEvent *event, void *data) noexcept {
[](int, const EmscriptenWebSocketOpenEvent *event, void *data) noexcept -> bool {
UNUSED(event);
auto *self = static_cast<socket *>(data);

Expand All @@ -32,28 +32,28 @@ socket::socket() noexcept {

self->invoke("connect");

return 0;
return false;
}
);

emscripten_websocket_set_onmessage_callback(
_socket,
this,
[](int, const EmscriptenWebSocketMessageEvent *event, void *data) noexcept {
[](int, const EmscriptenWebSocketMessageEvent *event, void *data) noexcept -> bool {
if (!event->isText) [[unlikely]] {
return 0;
return false;
}

auto *self = static_cast<socket *>(data);
const auto buffer = std::string(reinterpret_cast<const char *>(event->data), event->numBytes - 1);
const auto j = json::parse(buffer, nullptr, false);
if (j.is_discarded()) [[unlikely]] {
return 0;
return false;
}

if (const auto &command = j.value("command", std::string{}); command == "ping") {
self->send(R"({"command": "pong"})");
return 0;
return false;
}

if (const auto &event = j.value("event", json::object()); !event.empty()) {
Expand All @@ -62,7 +62,7 @@ socket::socket() noexcept {
event.at("data").dump()
);

return 0;
return false;
}

if (const auto &rpc = j.value("rpc", json::object()); !rpc.empty() && rpc.contains("response")) {
Expand All @@ -72,32 +72,32 @@ socket::socket() noexcept {
response.at("result").dump()
);

return 0;
return false;
}

return 0;
return false;
}
);

emscripten_websocket_set_onerror_callback(
_socket,
this,
[](int, const EmscriptenWebSocketErrorEvent *event, void *data) noexcept {
[](int, const EmscriptenWebSocketErrorEvent *event, void *data) noexcept -> bool {
UNUSED(event);
const auto self = static_cast<socket *>(data);
self->invoke("error", "WebSocket error occurred");
return 0;
return false;
}
);

emscripten_websocket_set_onclose_callback(
_socket,
this,
[](int, const EmscriptenWebSocketCloseEvent *event, void *data) noexcept {
[](int, const EmscriptenWebSocketCloseEvent *event, void *data) noexcept -> bool {
UNUSED(event);
const auto *self = static_cast<socket *>(data);
self->invoke("disconnect");
return 0;
return false;
}
);
}
Expand Down

0 comments on commit 6865076

Please sign in to comment.