FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_packet.hpp
1/*
2 * Copyright 2025 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_PACKET_HPP_INCLUDED_
18#define _FGE_C_PACKET_HPP_INCLUDED_
19
20/*
21 * Original from : https://github.com/SFML/SFML
22 * Copyright (C) 2007-2022 Laurent Gomila
23 *
24 * Altered/Modified by Guillaume Guillet
25 */
26
27#include "FastEngine/fge_extern.hpp"
28#include "FastEngine/C_matrix.hpp"
29#include "FastEngine/network/C_error.hpp"
30#include "tinyutf8.h"
31#include <cstdint>
32#include <forward_list>
33#include <list>
34#include <span>
35#include <string>
36#include <vector>
37
38#include "FastEngine/C_vector.hpp"
39#include "FastEngine/graphic/C_color.hpp"
40
41#define FGE_PACKET_DEFAULT_RESERVESIZE 4096
42
43namespace fge::net
44{
45
46class SocketTcp;
47class SocketUdp;
48
49using SizeType = uint16_t;
50
51class FGE_API Packet
52{
53public:
54 Packet();
55 Packet(Packet&& pck) noexcept;
56 Packet(Packet& pck) = default;
57 Packet(Packet const& pck) = default;
58 explicit Packet(std::size_t reserveSize);
59 virtual ~Packet() = default;
60
61 void clear();
62 void flush();
63 void reserve(std::size_t reserveSize);
64
65 Packet& append(std::size_t size); //Will push to host byte order without data (increasing size)
66 Packet& append(void const* data, std::size_t size); //Will push to host byte order
67 Packet& pack(void const* data, std::size_t size); //Will push and auto convert to network byte order
68
69 bool write(std::size_t pos, void const* data, std::size_t size); //Will write to host byte order
70 bool pack(std::size_t pos, void const* data, std::size_t size); //Will write and auto convert to network byte order
71
72 Packet const& read(void* buff, std::size_t size) const; //Will read to network byte order
73 Packet const& unpack(void* buff, std::size_t size) const; //Will read and auto convert to host byte order
74
75 bool read(std::size_t pos, void* buff, std::size_t size) const; //Will read to network byte order
76 bool unpack(std::size_t pos, void* buff, std::size_t size) const; //Will read and auto convert to host byte order
77
78 Packet& shrink(std::size_t size);
79 bool erase(std::size_t pos, std::size_t size);
80 Packet const& skip(std::size_t size) const;
81
82 void setReadPos(std::size_t pos) const;
83 [[nodiscard]] std::size_t getReadPos() const;
84 [[nodiscard]] bool isExtractable(std::size_t size) const;
85
86 [[nodiscard]] uint8_t const* getData(std::size_t pos) const; //Get data pointer to pos
87 [[nodiscard]] uint8_t* getData(std::size_t pos);
88 [[nodiscard]] uint8_t const* getData() const; //Get data pointer to pos 0
89 [[nodiscard]] uint8_t* getData();
90
91 [[nodiscard]] std::size_t getDataSize() const;
92 [[nodiscard]] uint32_t getLength() const; //Get length of a string or others at the read position
93 //can be useful to allocate a char buffer before reading
94 void invalidate() const;
95 void setValidity(bool validity) const;
96 [[nodiscard]] bool isValid() const;
97 [[nodiscard]] explicit operator bool() const;
98 [[nodiscard]] bool endReached() const;
99
100 [[nodiscard]] std::vector<uint8_t> const& getTransmitCache() const;
101 [[nodiscard]] std::size_t getTransmitPos() const;
102 [[nodiscard]] bool isTransmitCacheValid() const;
103 void invalidateTransmitCache();
104
105 inline Packet& operator<<(bool data);
106
107 inline Packet& operator<<(int8_t data);
108 inline Packet& operator<<(int16_t data);
109 inline Packet& operator<<(int32_t data);
110 inline Packet& operator<<(int64_t data);
111
112 inline Packet& operator<<(uint8_t data);
113 inline Packet& operator<<(uint16_t data);
114 inline Packet& operator<<(uint32_t data);
115 inline Packet& operator<<(uint64_t data);
116
117 inline Packet& operator<<(float data);
118 inline Packet& operator<<(double data);
119 inline Packet& operator<<(long double data);
120
121 Packet& operator<<(std::string_view const& data);
122 Packet& operator<<(char const* data);
123 Packet& operator<<(std::string const& data);
124 Packet& operator<<(tiny_utf8::string const& data);
125 Packet& operator<<(wchar_t const* data);
126 Packet& operator<<(std::wstring const& data);
127
128 template<typename T>
129 Packet& operator<<(std::forward_list<T> const& data);
130 template<typename T>
131 Packet& operator<<(std::list<T> const& data);
132 template<typename T>
133 Packet& operator<<(std::vector<T> const& data);
134
135 template<typename T>
136 Packet& operator<<(fge::Vector2<T> const& data);
137 template<typename T>
138 Packet& operator<<(fge::Vector3<T> const& data);
139
140 template<typename T>
141 Packet& operator<<(fge::Matrix<T> const& data);
142
143 inline Packet& operator<<(fge::Color const& data);
144
145 template<class TEnum, typename = std::enable_if_t<std::is_enum_v<TEnum>>>
146 inline Packet& operator<<(TEnum const& data);
147
148 template<class TData>
149 inline Packet& operator<<(std::unique_ptr<TData> const& data);
150
152
153 inline Packet const& operator>>(bool& data) const;
154
155 inline Packet const& operator>>(int8_t& data) const;
156 inline Packet const& operator>>(int16_t& data) const;
157 inline Packet const& operator>>(int32_t& data) const;
158 inline Packet const& operator>>(int64_t& data) const;
159
160 inline Packet const& operator>>(uint8_t& data) const;
161 inline Packet const& operator>>(uint16_t& data) const;
162 inline Packet const& operator>>(uint32_t& data) const;
163 inline Packet const& operator>>(uint64_t& data) const;
164
165 inline Packet const& operator>>(float& data) const;
166 inline Packet const& operator>>(double& data) const;
167 inline Packet const& operator>>(long double& data) const;
168
169 Packet const& operator>>(char* data) const;
170 Packet const& operator>>(std::string& data) const;
171 Packet const& operator>>(tiny_utf8::string& data) const;
172 Packet const& operator>>(wchar_t* data) const;
173 Packet const& operator>>(std::wstring& data) const;
174
175 template<typename T>
176 Packet const& operator>>(std::forward_list<T>& data) const;
177 template<typename T>
178 Packet const& operator>>(std::list<T>& data) const;
179 template<typename T>
180 Packet const& operator>>(std::vector<T>& data) const;
181
182 template<typename T>
183 Packet const& operator>>(fge::Vector2<T>& data) const;
184 template<typename T>
185 Packet const& operator>>(fge::Vector3<T>& data) const;
186
187 template<typename T>
188 Packet const& operator>>(fge::Matrix<T>& data) const;
189
190 inline Packet const& operator>>(fge::Color& data) const;
191
192 template<class TEnum, typename = std::enable_if_t<std::is_enum_v<TEnum>>>
193 inline Packet const& operator>>(TEnum& data) const;
194
195 template<class TData>
196 inline Packet const& operator>>(std::unique_ptr<TData>& data) const;
197
198 bool operator==(Packet const& right) const = delete;
199 bool operator!=(Packet const& right) const = delete;
200
201 [[nodiscard]] virtual bool onSend(std::size_t offset);
202 virtual void onReceive(std::span<uint8_t const> const& data);
203
204protected:
205 friend class SocketTcp;
206 friend class SocketUdp;
207
208 std::vector<uint8_t> _g_transmitCache;
209 std::size_t _g_transmitPos;
210 bool _g_transmitCacheValid;
211
212private:
213 std::vector<uint8_t> g_data;
214 mutable std::size_t g_readPos;
215 mutable bool g_valid;
216};
217
218} // namespace fge::net
219
220#include "C_packet.inl"
221
222#endif // _FGE_C_PACKET_HPP_INCLUDED_
Definition C_color.hpp:35
A container to store a 2D matrix of any type.
Definition C_matrix.hpp:40
A wrapper for TCP sockets inheriting from Socket.
Definition C_socket.hpp:493
A wrapper for UDP sockets inheriting from Socket.
Definition C_socket.hpp:348