Share online membership and game setup with the server

Profile, member join/leave and building a fresh game move into the sim library,
and the packet reader and writer into protocol.h, so the server can run lobbies
exactly as clients do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-11-08 01:54:27 -05:00
1 parent 465f75f6d7
commit 9613e85706
7 files changed
+181 -105

No files matched your search

+31
View File
@@ -22,8 +22,10 @@
//
// A signal's payload is either a description {sdp, kind} or a candidate {candidate, mid}.
#include <cstring>
#include <string>
#include <string_view>
#include <type_traits>
constexpr int PROTOCOL_VERSION = 2;
constexpr int LOBBY_MEMBERS_MAX = 4;
@@ -36,6 +38,35 @@ constexpr int KICK_REASON_MAX = 60;
constexpr const char SERVER_URL_DEFAULT[] = "wss://shweetz.net/smack-a-brother/ws";
constexpr int SERVER_URL_LENGTH = 128;
// Packets are plain values one after another, as they sit in memory (every platform agrees on these layouts).
struct PacketWriter
{
std::string bytes;
template <class T> PacketWriter& put(const T& value)
{
static_assert(std::is_trivially_copyable_v<T>);
bytes.append(reinterpret_cast<const char*>(&value), sizeof(T));
return *this;
}
};
struct PacketReader
{
const std::string& bytes;
size_t at = 0;
template <class T> bool get(T& value)
{
static_assert(std::is_trivially_copyable_v<T>);
if (at + sizeof(T) > bytes.size())
return false;
std::memcpy(static_cast<void*>(&value), bytes.data() + at, sizeof(T));
at += sizeof(T);
return true;
}
};
// Text from elsewhere (another player, or the server) made safe to show: only printable ASCII (what the game's font
// has; anything else, emoji included, is dropped), cut to max characters, with spaces at its ends trimmed.
inline std::string text_clean(std::string_view raw, size_t max)