FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_packet.hpp
1/*
2 * Copyright 2024 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 "tinyutf8.h"
30#include <cstdint>
31#include <forward_list>
32#include <list>
33#include <string>
34#include <vector>
35
36#include "FastEngine/C_vector.hpp"
37#include "FastEngine/graphic/C_color.hpp"
38
39#define FGE_PACKET_DEFAULT_RESERVESIZE 4096
40
41namespace fge::net
42{
43
44struct FGE_API Error
45{
46 enum class Types
47 {
48 ERR_NONE,
49 ERR_ALREADY_INVALID,
50 ERR_EXTRACT,
51 ERR_RULE,
52
53 ERR_SCENE_OLD_PACKET
54 };
55
56 Types _type{Types::ERR_NONE};
57 std::size_t _readPos{0};
58 char const* _error{nullptr};
59 char const* _function{nullptr};
60
61 void dump(std::ostream& os) const;
62};
63
64class SocketTcp;
65class SocketUdp;
66
67using SizeType = uint16_t;
68
69class FGE_API Packet
70{
71public:
72 Packet();
73 Packet(fge::net::Packet&& pck) noexcept;
74 Packet(fge::net::Packet& pck) = default;
75 Packet(fge::net::Packet const& pck) = default;
76 explicit Packet(std::size_t reserveSize);
77 virtual ~Packet() = default;
78
79 void clear();
80 void flush();
81 void reserve(std::size_t reserveSize);
82
83 fge::net::Packet& append(std::size_t size); //Will push to host byte order without data (increasing size)
84 fge::net::Packet& append(void const* data, std::size_t size); //Will push to host byte order
85 fge::net::Packet& pack(void const* data, std::size_t size); //Will push and auto convert to network byte order
86
87 bool write(std::size_t pos, void const* data, std::size_t size); //Will write to host byte order
88 bool pack(std::size_t pos, void const* data, std::size_t size); //Will write and auto convert to network byte order
89
90 fge::net::Packet const& read(void* buff, std::size_t size) const; //Will read to network byte order
91 fge::net::Packet const& unpack(void* buff, std::size_t size) const; //Will read and auto convert to host byte order
92
93 bool read(std::size_t pos, void* buff, std::size_t size) const; //Will read to network byte order
94 bool unpack(std::size_t pos, void* buff, std::size_t size) const; //Will read and auto convert to host byte order
95
96 fge::net::Packet& shrink(std::size_t size);
97 bool erase(std::size_t pos, std::size_t size);
98 fge::net::Packet const& skip(std::size_t size) const;
99
100 void setReadPos(std::size_t pos) const;
101 [[nodiscard]] std::size_t getReadPos() const;
102 [[nodiscard]] bool isExtractable(std::size_t size) const;
103
104 [[nodiscard]] uint8_t const* getData(std::size_t pos) const; //Get data pointer to pos
105 [[nodiscard]] uint8_t* getData(std::size_t pos);
106 [[nodiscard]] uint8_t const* getData() const; //Get data pointer to pos 0
107 [[nodiscard]] uint8_t* getData();
108
109 [[nodiscard]] std::size_t getDataSize() const;
110 [[nodiscard]] uint32_t getLength() const; //Get length of a string or others at the read position
111 //can be useful to allocate a char buffer before reading
112 void invalidate() const;
113 void setValidity(bool validity) const;
114 [[nodiscard]] bool isValid() const;
115 [[nodiscard]] explicit operator bool() const;
116 [[nodiscard]] bool endReached() const;
117
118 inline fge::net::Packet& operator<<(bool data);
119
120 inline fge::net::Packet& operator<<(int8_t data);
121 inline fge::net::Packet& operator<<(int16_t data);
122 inline fge::net::Packet& operator<<(int32_t data);
123 inline fge::net::Packet& operator<<(int64_t data);
124
125 inline fge::net::Packet& operator<<(uint8_t data);
126 inline fge::net::Packet& operator<<(uint16_t data);
127 inline fge::net::Packet& operator<<(uint32_t data);
128 inline fge::net::Packet& operator<<(uint64_t data);
129
130 inline fge::net::Packet& operator<<(float data);
131 inline fge::net::Packet& operator<<(double data);
132 inline fge::net::Packet& operator<<(long double data);
133
134 fge::net::Packet& operator<<(std::string_view const& data);
135 fge::net::Packet& operator<<(char const* data);
136 fge::net::Packet& operator<<(std::string const& data);
137 fge::net::Packet& operator<<(tiny_utf8::string const& data);
138 fge::net::Packet& operator<<(wchar_t const* data);
139 fge::net::Packet& operator<<(std::wstring const& data);
140
141 template<typename T>
142 fge::net::Packet& operator<<(std::forward_list<T> const& data);
143 template<typename T>
144 fge::net::Packet& operator<<(std::list<T> const& data);
145 template<typename T>
146 fge::net::Packet& operator<<(std::vector<T> const& data);
147
148 template<typename T>
149 fge::net::Packet& operator<<(fge::Vector2<T> const& data);
150 template<typename T>
151 fge::net::Packet& operator<<(fge::Vector3<T> const& data);
152
153 template<typename T>
154 fge::net::Packet& operator<<(fge::Matrix<T> const& data);
155
156 inline fge::net::Packet& operator<<(fge::Color const& data);
157
158 template<class TEnum, typename = std::enable_if_t<std::is_enum_v<TEnum>>>
159 inline fge::net::Packet& operator<<(TEnum const& data);
160
161 template<class TData>
162 inline fge::net::Packet& operator<<(std::unique_ptr<TData> const& data);
163
165
166 inline fge::net::Packet const& operator>>(bool& data) const;
167
168 inline fge::net::Packet const& operator>>(int8_t& data) const;
169 inline fge::net::Packet const& operator>>(int16_t& data) const;
170 inline fge::net::Packet const& operator>>(int32_t& data) const;
171 inline fge::net::Packet const& operator>>(int64_t& data) const;
172
173 inline fge::net::Packet const& operator>>(uint8_t& data) const;
174 inline fge::net::Packet const& operator>>(uint16_t& data) const;
175 inline fge::net::Packet const& operator>>(uint32_t& data) const;
176 inline fge::net::Packet const& operator>>(uint64_t& data) const;
177
178 inline fge::net::Packet const& operator>>(float& data) const;
179 inline fge::net::Packet const& operator>>(double& data) const;
180 inline fge::net::Packet const& operator>>(long double& data) const;
181
182 fge::net::Packet const& operator>>(char* data) const;
183 fge::net::Packet const& operator>>(std::string& data) const;
184 fge::net::Packet const& operator>>(tiny_utf8::string& data) const;
185 fge::net::Packet const& operator>>(wchar_t* data) const;
186 fge::net::Packet const& operator>>(std::wstring& data) const;
187
188 template<typename T>
189 fge::net::Packet const& operator>>(std::forward_list<T>& data) const;
190 template<typename T>
191 fge::net::Packet const& operator>>(std::list<T>& data) const;
192 template<typename T>
193 fge::net::Packet const& operator>>(std::vector<T>& data) const;
194
195 template<typename T>
196 fge::net::Packet const& operator>>(fge::Vector2<T>& data) const;
197 template<typename T>
198 fge::net::Packet const& operator>>(fge::Vector3<T>& data) const;
199
200 template<typename T>
201 fge::net::Packet const& operator>>(fge::Matrix<T>& data) const;
202
203 inline fge::net::Packet const& operator>>(fge::Color& data) const;
204
205 template<class TEnum, typename = std::enable_if_t<std::is_enum_v<TEnum>>>
206 inline fge::net::Packet const& operator>>(TEnum& data) const;
207
208 template<class TData>
209 inline fge::net::Packet const& operator>>(std::unique_ptr<TData>& data) const;
210
211 bool operator==(Packet const& right) const = delete;
212 bool operator!=(Packet const& right) const = delete;
213
214 static std::size_t _defaultReserveSize;
215
216 virtual void onSend(std::vector<uint8_t>& buffer, std::size_t offset);
217 virtual void onReceive(void* data, std::size_t size);
218
219protected:
220 friend class fge::net::SocketTcp;
221 friend class fge::net::SocketUdp;
222
223 std::size_t _g_sendPos;
224 std::vector<uint8_t> _g_lastData;
225 bool _g_lastDataValidity;
226
227 std::vector<uint8_t> _g_data;
228 mutable std::size_t _g_readPos;
229 mutable bool _g_valid;
230};
231
232} // namespace fge::net
233
234#include "C_packet.inl"
235
236#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
Definition C_packet.hpp:70
A wrapper for TCP sockets inheriting from Socket.
Definition C_socket.hpp:407
A wrapper for UDP sockets inheriting from Socket.
Definition C_socket.hpp:267
Definition C_packet.hpp:45