navid/veins / 39ca2c868b1762db39b7093dd7a1333dacd425f5a7d8532c00ac9102761befe9 / xveins/src/wire.h

wire.h

← xveins/src · 1949 bytes · raw · download

#pragma once

#include "net.h"

#include <cstdint>
#include <optional>
#include <string>
#include <vector>

namespace vn::wire {

constexpr std::size_t MAX_FRAME = 16u * 1024u * 1024u;

enum Kind : std::uint8_t {
    HELLO = 1,
    HELLO_OK = 2,
    AUTH = 3,
    AUTH_OK = 4,
    WHOAMI = 5,
    WHOAMI_OK = 6,
    LIST_REFS = 7,
    REFS = 8,
    NEGOTIATE = 9,
    NEGOTIATE_OK = 10,
    FETCH_SKELETON = 11,
    GET_CHUNKS = 12,
    HAVE_QUERY = 13,
    HAVE_BITMAP = 14,
    PUT_RECORDS = 15,
    UPDATE_REFS = 16,
    UPDATE_REFS_OK = 17,
    RECORDS = 18,
    END = 19,
    ERROR = 20,
};

enum ErrorCode : std::uint8_t {
    ERR_UNSUPPORTED_VERSION = 1,
    ERR_BAD_FRAME = 2,
    ERR_NOT_FOUND = 3,
    ERR_HASH_MISMATCH = 4,
    ERR_NON_CANONICAL = 5,
    ERR_MISSING_CLOSURE = 6,
    ERR_REF_CONFLICT = 7,
    ERR_REJECTED = 8,
    ERR_INTERNAL = 9,
    ERR_UNAUTHENTICATED = 10,
    ERR_FORBIDDEN = 11,
    ERR_AUTH_FAILED = 12,
    ERR_QUOTA = 13,
    ERR_TOO_LARGE = 14,
};

struct Frame {
    std::uint8_t kind = 0;
    std::uint32_t request_id = 0;
    std::vector<std::uint8_t> payload;
};

std::optional<Frame> read_frame(net::Stream& stream);
bool write_frame(net::Stream& stream, std::uint8_t kind, std::uint32_t request_id, Bytes payload);
bool write_simple(net::Stream& stream, std::uint8_t kind, std::uint32_t request_id);
bool write_error(net::Stream& stream, std::uint32_t request_id, std::uint8_t code,
                 std::string_view message);

std::uint8_t read_error(Bytes payload);
std::string error_message(Bytes payload);
std::string_view error_code_name(std::uint8_t code);

// Encode/decode the record layout shared by packs and RECORDS frames.
struct IncomingRecord {
    Id id{};
    std::uint8_t kind = 0;
    std::vector<std::uint8_t> body;
};
std::optional<IncomingRecord> parse_record(Bytes data);
std::vector<std::uint8_t> make_record(const Id& id, std::uint8_t kind, Bytes body);

} // namespace vn::wire