Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
networkpacket.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include "util/pointer.h"
23#include "networkprotocol.h"
24#include <SColor.h>
25
27{
28public:
29 NetworkPacket(u16 command, u32 preallocate, session_t peer_id) :
30 m_command(command), m_peer_id(peer_id)
31 {
32 m_data.reserve(preallocate);
33 }
34 NetworkPacket(u16 command, u32 preallocate) :
35 m_command(command)
36 {
37 m_data.reserve(preallocate);
38 }
39 NetworkPacket() = default;
40
41 ~NetworkPacket() = default;
42
43 void putRawPacket(const u8 *data, u32 datasize, session_t peer_id);
44 void clear();
45
46 // Getters
47 u32 getSize() const { return m_datasize; }
48 session_t getPeerId() const { return m_peer_id; }
49 u16 getCommand() const { return m_command; }
50 u32 getRemainingBytes() const { return m_datasize - m_read_offset; }
51 const char *getRemainingString() { return getString(m_read_offset); }
52
53 // Returns a c-string without copying.
54 // A better name for this would be getRawString()
55 const char *getString(u32 from_offset) const;
56 // major difference to putCString(): doesn't write len into the buffer
57 void putRawString(const char *src, u32 len);
58 void putRawString(std::string_view src)
59 {
60 putRawString(src.data(), src.size());
61 }
62
63 NetworkPacket &operator>>(std::string &dst);
64 NetworkPacket &operator<<(std::string_view src);
65
66 void putLongString(std::string_view src);
67
68 NetworkPacket &operator>>(std::wstring &dst);
69 NetworkPacket &operator<<(std::wstring_view src);
70
71 std::string readLongString();
72
73 NetworkPacket &operator>>(char &dst);
74 NetworkPacket &operator<<(char src);
75
76 NetworkPacket &operator>>(bool &dst);
77 NetworkPacket &operator<<(bool src);
78
79 u8 getU8(u32 offset);
80
81 NetworkPacket &operator>>(u8 &dst);
83
84 u8 *getU8Ptr(u32 offset);
85
86 u16 getU16(u32 from_offset);
87 NetworkPacket &operator>>(u16 &dst);
88 NetworkPacket &operator<<(u16 src);
89
90 NetworkPacket &operator>>(u32 &dst);
91 NetworkPacket &operator<<(u32 src);
92
93 NetworkPacket &operator>>(u64 &dst);
94 NetworkPacket &operator<<(u64 src);
95
96 NetworkPacket &operator>>(float &dst);
97 NetworkPacket &operator<<(float src);
98
101
104
105 NetworkPacket &operator>>(s16 &dst);
106 NetworkPacket &operator<<(s16 src);
107
108 NetworkPacket &operator>>(s32 &dst);
109 NetworkPacket &operator<<(s32 src);
110
113
116
119
120 NetworkPacket &operator>>(video::SColor &dst);
121 NetworkPacket &operator<<(video::SColor src);
122
123 // Temp, we remove SharedBuffer when migration finished
124 // ^ this comment has been here for 7 years
126
127private:
128 void checkReadOffset(u32 from_offset, u32 field_size) const;
129
130 inline void checkDataSize(u32 field_size)
131 {
132 if (m_read_offset + field_size > m_datasize) {
133 m_datasize = m_read_offset + field_size;
134 m_data.resize(m_datasize);
135 }
136 }
137
138 std::vector<u8> m_data;
139 u32 m_datasize = 0;
141 u16 m_command = 0;
143};
Definition: pointer.h:44
Definition: networkpacket.h:27
const char * getRemainingString()
Definition: networkpacket.h:51
void clear()
Definition: networkpacket.cpp:54
std::string readLongString()
Definition: networkpacket.cpp:203
u16 getU16(u32 from_offset)
Definition: networkpacket.cpp:357
u32 getRemainingBytes() const
Definition: networkpacket.h:50
void putRawPacket(const u8 *data, u32 datasize, session_t peer_id)
Definition: networkpacket.cpp:36
void checkReadOffset(u32 from_offset, u32 field_size) const
Definition: networkpacket.cpp:26
u16 m_command
Definition: networkpacket.h:141
u32 m_read_offset
Definition: networkpacket.h:140
void putRawString(std::string_view src)
Definition: networkpacket.h:58
NetworkPacket & operator>>(std::string &dst)
Definition: networkpacket.cpp:81
const char * getString(u32 from_offset) const
Definition: networkpacket.cpp:63
NetworkPacket()=default
Buffer< u8 > oldForgePacket()
Definition: networkpacket.cpp:534
std::vector< u8 > m_data
Definition: networkpacket.h:138
u32 m_datasize
Definition: networkpacket.h:139
u32 getSize() const
Definition: networkpacket.h:47
~NetworkPacket()=default
u8 getU8(u32 offset)
Definition: networkpacket.cpp:329
void checkDataSize(u32 field_size)
Definition: networkpacket.h:130
u8 * getU8Ptr(u32 offset)
Definition: networkpacket.cpp:336
NetworkPacket(u16 command, u32 preallocate, session_t peer_id)
Definition: networkpacket.h:29
NetworkPacket & operator<<(std::string_view src)
Definition: networkpacket.cpp:102
void putRawString(const char *src, u32 len)
Definition: networkpacket.cpp:70
u16 getCommand() const
Definition: networkpacket.h:49
NetworkPacket(u16 command, u32 preallocate)
Definition: networkpacket.h:34
void putLongString(std::string_view src)
Definition: networkpacket.cpp:117
session_t m_peer_id
Definition: networkpacket.h:142
session_t getPeerId() const
Definition: networkpacket.h:48
core::vector2d< s32 > v2s32
Definition: irr_v2d.h:28
core::vector2d< f32 > v2f
Definition: irr_v2d.h:26
core::vector3d< s32 > v3s32
Definition: irr_v3d.h:30
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
core::vector3df v3f
Definition: irr_v3d.h:26
u16 session_t
Definition: networkprotocol.h:251