2026-06-15 21:09:09 +02:00
|
|
|
/*
|
2026-07-03 15:52:46 +01:00
|
|
|
* protocol/protocol.h: control-plane (de)serialization + routing.
|
2026-06-15 21:09:09 +02:00
|
|
|
*
|
2026-07-03 15:52:46 +01:00
|
|
|
* Wire format is a length-prefixed protobuf `Envelope`
|
2026-09-19 22:40:48 +02:00
|
|
|
* (proto/voicecat.proto). This layer parses frames into Envelopes, correlates
|
2026-07-03 15:52:46 +01:00
|
|
|
* request_id response, and dispatches to handlers. Media frames do NOT come through here
|
|
|
|
|
* (they use the fixed binary header
|
2026-06-15 21:09:09 +02:00
|
|
|
*
|
2026-07-03 10:20:18 +01:00
|
|
|
* FrameCodec below is used by both the client (net/transport.h) and the server
|
|
|
|
|
* (conn_session.cpp). See protocol/envelope.h for the Envelope-level encode/decode that sits
|
|
|
|
|
* on top of this.
|
2026-06-15 21:09:09 +02:00
|
|
|
*/
|
|
|
|
|
#ifndef VOICECAT_PROTOCOL_PROTOCOL_H
|
|
|
|
|
#define VOICECAT_PROTOCOL_PROTOCOL_H
|
|
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
namespace voicecat::protocol {
|
|
|
|
|
|
2026-06-15 23:48:44 +02:00
|
|
|
constexpr uint32_t kProtocolVersion = 1;
|
2026-07-03 15:52:46 +01:00
|
|
|
constexpr uint32_t kMaxFrameBytes = 16u * 1024 * 1024;
|
2026-06-15 23:48:44 +02:00
|
|
|
constexpr size_t kLengthHeaderSize = 4; // big-endian u32 prefix
|
2026-06-15 21:09:09 +02:00
|
|
|
|
2026-06-15 23:48:44 +02:00
|
|
|
// Reads/writes [u32 big-endian length][payload] frames from a byte stream.
|
2026-06-15 21:09:09 +02:00
|
|
|
class FrameCodec {
|
|
|
|
|
public:
|
2026-06-15 23:48:44 +02:00
|
|
|
// Append received bytes; pop complete frame payloads into out_frames.
|
|
|
|
|
// Returns false on protocol error (oversized frame or framing violation).
|
2026-06-15 21:09:09 +02:00
|
|
|
bool feed(const uint8_t* data, size_t len, std::vector<std::vector<uint8_t>>& out_frames);
|
2026-06-15 23:48:44 +02:00
|
|
|
|
|
|
|
|
// Serialize a frame into out: [big-endian u32 length][payload bytes].
|
|
|
|
|
static void emit(const uint8_t* payload, size_t len, std::vector<uint8_t>& out);
|
|
|
|
|
static void emit(const std::vector<uint8_t>& payload, std::vector<uint8_t>& out);
|
|
|
|
|
|
|
|
|
|
// Number of bytes buffered but not yet forming a complete frame.
|
|
|
|
|
size_t pending_bytes() const { return buf_.size(); }
|
|
|
|
|
|
|
|
|
|
void reset() { buf_.clear(); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<uint8_t> buf_;
|
2026-06-15 21:09:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace voicecat::protocol
|
|
|
|
|
|
|
|
|
|
#endif // VOICECAT_PROTOCOL_PROTOCOL_H
|