vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
messages.h
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <deque>
6#include <functional>
7#include <vector>
8
9#include "net/wire/bit_stream.h"
10
11namespace Vkm::Engine {
12
14constexpr size_t NET_MESSAGE_MAX_BYTES = 255;
15
21constexpr size_t NET_MESSAGE_WINDOW = 64;
22
31class NetOutbox {
32 public:
34 static constexpr uint32_t NONE_BITS = 1;
35
36 public:
37 NetOutbox() = default;
38 ~NetOutbox() = default;
39
40 NetOutbox(const NetOutbox& other) = default;
41 NetOutbox& operator=(const NetOutbox& other) = default;
42
43 NetOutbox(NetOutbox && other) = default;
44 NetOutbox& operator=(NetOutbox && other) = default;
45
46 public:
54 bool push(const uint8_t* bytes, size_t size);
55
65 size_t write(BitWriter& out, size_t budgetBits) const;
66
72 void acknowledge(uint16_t newest);
73
75 size_t pending() const { return m_pending.size(); }
76
78 void clear();
79
80 private:
81 struct Message {
82 uint16_t sequence = 0;
83 std::vector<uint8_t> bytes;
84 };
85
86 private:
87 std::deque<Message> m_pending;
88 uint16_t m_next = 1;
89};
90
94class NetInbox {
95 public:
96 using Deliver = std::function<void(const uint8_t* bytes, size_t size)>;
97
98 public:
99 NetInbox() = default;
100 ~NetInbox() = default;
101
102 NetInbox(const NetInbox& other) = default;
103 NetInbox& operator=(const NetInbox& other) = default;
104
105 NetInbox(NetInbox && other) = default;
106 NetInbox& operator=(NetInbox && other) = default;
107
108 public:
122 bool read(BitReader& in, const Deliver& deliver);
123
125 uint16_t newest() const { return m_newest; }
126
128 uint16_t acknowledge() {
129 m_room = NET_MESSAGE_WINDOW;
130 return m_newest;
131 }
132
133 private:
134 uint16_t m_newest = 0;
135 size_t m_room = NET_MESSAGE_WINDOW;
136};
137
138} // namespace Vkm::Engine
Reads back what a BitWriter wrote, in the same order.
Definition bit_stream.h:145
Packs values to the bit into a byte buffer.
Definition bit_stream.h:19
uint16_t acknowledge()
newest(), as an acknowledgement sent: the sender may now be handed on as many again.
Definition messages.h:128
bool read(BitReader &in, const Deliver &deliver)
Read a packet's messages, handing on those not seen before.
uint16_t newest() const
The latest sequence handed on, for the acknowledgement; zero before any.
Definition messages.h:125
static constexpr uint32_t NONE_BITS
What write() writes when no message fits: the bit saying none follow.
Definition messages.h:34
void clear()
Forget every message, and number the next one from the start.
size_t pending() const
Messages waiting to be acknowledged.
Definition messages.h:75
bool push(const uint8_t *bytes, size_t size)
Keep size bytes to send.
size_t write(BitWriter &out, size_t budgetBits) const
Write the oldest messages kept that fit in budgetBits.
void acknowledge(uint16_t newest)
The other end has every message up to newest: stop sending them.