navid/veins / 9d43af5809a4086fe4899784b3f2c54d2b901ca262a85d903cebc534b826ed2d / xveins/src/wire.h

wire.h

← xveins/src · 1902 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,
};

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