Initial commit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shweetandClaude Opus 5.5 committed 2026-11-08 01:30:33 -05:00
commit 2007fb7edc
200 files changed
+27710

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
#pragma once
#include "protocol.h"
#include <map>
#include <memory>
#include <vector>
struct Game;
enum NetState
{
NET_OFFLINE,
NET_CONNECTING,
NET_JOINING,
NET_LOBBY
};
// A binary message from another member, on the reliable channel (in order, never lost) or the inputs one (either may
// fail).
struct NetPacket
{
int from = -1;
bool isReliable = true;
std::string bytes;
};
// Someone in the lobby (this screen's player too): their server id and name, whether a direct connection to them is
// open, and the round trip over it in milliseconds (-1 until measured). This screen's own member is always connected.
struct NetMember
{
int id = -1;
std::string name;
bool isConnected = false;
float ping = -1;
};
// Online play: a WebSocket to the server (see protocol.h) to make or join a lobby and pass signaling, and a
// WebRTC data channel to each other member, connected directly. A newcomer connects out to everyone already there.
// The network calls back on its own threads (on desktop), so everything it reports is queued and handled in update,
// once a frame on the main thread. Members are in the order they joined, so the first hosts. Error holds why the last
// request failed or the connection was lost, until the next request.
class Net
{
public:
Net();
~Net();
void lobby_create(Game& game);
void lobby_join(Game& game, const char* code);
void lobby_leave();
void update();
void shutdown();
void heartbeat();
void send(int to, const std::string& bytes, bool isReliable);
void kick_send(int member, const std::string& reason);
bool is_connected_all();
NetState state = NET_OFFLINE;
std::string code;
std::string error;
int selfId = -1;
int hostId = -1;
std::vector<NetMember> members;
// Binary messages from other members, oldest first, for the session to take.
std::vector<NetPacket> packets;
// Lines for the chat log from the server (like this screen being kicked), oldest first, for the chat to take.
std::vector<std::string> notices;
// Members the server said were kicked, with the reason (empty for none given).
std::map<int, std::string> kicks;
private:
struct Impl;
std::unique_ptr<Impl> impl;
void request_send(Game& game, const std::string& request);
void send_now(int to, const std::string& bytes, bool isReliable);
void text_send(int to, const std::string& text);
void text_send_now(int to, const std::string& text);
void message_handle(const std::string& text);
void peer_open(int id, bool isOfferer);
void peer_close(int id);
void signal_handle(int from, const std::string& payload);
NetMember* member_get(int id);
};