Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
binary.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/net/export.hpp>
5
6#include <cstddef>
7#include <cstdint>
8#include <optional>
9#include <span>
10#include <string>
11#include <string_view>
12#include <vector>
13
14namespace aegis2d::net {
15
21class AEGIS2D_NET_EXPORT BinaryWriter {
22public:
24 static constexpr std::size_t max_string_size = 0xFFFF;
25
28 void write_u8(std::uint8_t value);
31 void write_u16(std::uint16_t value);
34 void write_u32(std::uint32_t value);
37 void write_u64(std::uint64_t value);
40 void write_i8(std::int8_t value);
43 void write_i16(std::int16_t value);
46 void write_i32(std::int32_t value);
49 void write_i64(std::int64_t value);
52 void write_f32(float value);
55 void write_f64(double value);
58 void write_bool(bool value);
61 void write_bytes(std::span<const std::byte> bytes);
65 void write_string(std::string_view text);
66
69 [[nodiscard]] std::span<const std::byte> data() const noexcept;
72 [[nodiscard]] std::size_t size() const noexcept;
74 void clear() noexcept;
75
76private:
77 std::vector<std::byte> buffer_;
78};
79
88class AEGIS2D_NET_EXPORT BinaryReader {
89public:
92 explicit BinaryReader(std::span<const std::byte> bytes) noexcept;
93
96 [[nodiscard]] std::optional<std::uint8_t> read_u8() noexcept;
99 [[nodiscard]] std::optional<std::uint16_t> read_u16() noexcept;
102 [[nodiscard]] std::optional<std::uint32_t> read_u32() noexcept;
105 [[nodiscard]] std::optional<std::uint64_t> read_u64() noexcept;
108 [[nodiscard]] std::optional<std::int8_t> read_i8() noexcept;
111 [[nodiscard]] std::optional<std::int16_t> read_i16() noexcept;
114 [[nodiscard]] std::optional<std::int32_t> read_i32() noexcept;
117 [[nodiscard]] std::optional<std::int64_t> read_i64() noexcept;
122 [[nodiscard]] std::optional<float> read_f32() noexcept;
126 [[nodiscard]] std::optional<double> read_f64() noexcept;
130 [[nodiscard]] std::optional<bool> read_bool() noexcept;
135 [[nodiscard]] std::optional<std::span<const std::byte>> read_bytes(std::size_t count) noexcept;
141 [[nodiscard]] std::optional<std::string> read_string();
142
145 [[nodiscard]] bool failed() const noexcept;
149 [[nodiscard]] bool at_end() const noexcept;
152 [[nodiscard]] std::size_t position() const noexcept;
155 [[nodiscard]] std::size_t remaining() const noexcept;
156
157private:
158 [[nodiscard]] std::optional<std::span<const std::byte>> take(std::size_t count) noexcept;
159
160 std::span<const std::byte> bytes_;
161 std::size_t position_ = 0;
162 bool failed_ = false;
163};
164
165} // namespace aegis2d::net
Reads values written by BinaryWriter from untrusted bytes, never past their end.
Definition binary.hpp:88
BinaryReader(std::span< const std::byte > bytes) noexcept
Starts reading at the beginning of bytes.
std::optional< std::uint8_t > read_u8() noexcept
Reads an unsigned 8-bit integer.
Writes values field by field into a growing byte buffer.
Definition binary.hpp:21
void write_u64(std::uint64_t value)
Appends an unsigned 64-bit integer (8 bytes).
void write_i64(std::int64_t value)
Appends a signed 64-bit integer (8 bytes, two's complement).
std::span< const std::byte > data() const noexcept
Bytes written so far.
void write_string(std::string_view text)
Appends a string as its byte length on 16 bits, then its bytes (UTF-8).
void write_i32(std::int32_t value)
Appends a signed 32-bit integer (4 bytes, two's complement).
void write_u8(std::uint8_t value)
Appends an unsigned 8-bit integer (1 byte).
void write_bytes(std::span< const std::byte > bytes)
Appends raw bytes, without length: the reader must know how many to read.
void write_bool(bool value)
Appends a boolean as one byte, 0 or 1.
void write_f64(double value)
Appends a 64-bit float (8 bytes, IEEE 754 bits).
void write_i8(std::int8_t value)
Appends a signed 8-bit integer (1 byte, two's complement).
void write_i16(std::int16_t value)
Appends a signed 16-bit integer (2 bytes, two's complement).
void write_u16(std::uint16_t value)
Appends an unsigned 16-bit integer (2 bytes).
void write_f32(float value)
Appends a 32-bit float (4 bytes, IEEE 754 bits).
void write_u32(std::uint32_t value)
Appends an unsigned 32-bit integer (4 bytes).
Binary serialization, UDP/TCP sockets and the network thread.
Definition binary.hpp:14
Definition endpoint.hpp:85