Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
networkpacket.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
5#pragma once
6
7#include "util/pointer.h" // Buffer<T>
9#include "networkprotocol.h"
10#include <SColor.h>
11#include <vector>
12
14{
15public:
16 NetworkPacket(u16 command, u32 preallocate, session_t peer_id) :
17 m_command(command), m_peer_id(peer_id)
18 {
19 m_data.reserve(preallocate);
20 }
21 NetworkPacket(u16 command, u32 preallocate) :
22 m_command(command)
23 {
24 m_data.reserve(preallocate);
25 }
26 NetworkPacket() = default;
27
28 ~NetworkPacket() = default;
29
30 void putRawPacket(const u8 *data, u32 datasize, session_t peer_id);
31 void clear();
32
33 // Getters
34 u32 getSize() const { return m_datasize; }
35 session_t getPeerId() const { return m_peer_id; }
36 u16 getCommand() const { return m_command; }
37 u32 getRemainingBytes() const { return m_datasize - m_read_offset; }
38 const char *getRemainingString() { return getString(m_read_offset); }
39
40 // Returns a c-string without copying.
41 // A better name for this would be getRawString()
42 const char *getString(u32 from_offset) const;
43 // major difference to putCString(): doesn't write len into the buffer
44 void putRawString(const char *src, u32 len);
45 void putRawString(std::string_view src)
46 {
47 putRawString(src.data(), src.size());
48 }
49
50 NetworkPacket &operator>>(std::string &dst);
51 NetworkPacket &operator<<(std::string_view src);
52
53 void putLongString(std::string_view src);
54
55 NetworkPacket &operator>>(std::wstring &dst);
56 NetworkPacket &operator<<(std::wstring_view src);
57
58 std::string readLongString();
59
60 NetworkPacket &operator>>(char &dst);
61 NetworkPacket &operator<<(char src);
62
63 NetworkPacket &operator>>(bool &dst);
64 NetworkPacket &operator<<(bool src);
65
66 u8 getU8(u32 offset);
67
68 NetworkPacket &operator>>(u8 &dst);
70
71 u8 *getU8Ptr(u32 offset);
72
73 u16 getU16(u32 from_offset);
74 NetworkPacket &operator>>(u16 &dst);
75 NetworkPacket &operator<<(u16 src);
76
77 NetworkPacket &operator>>(u32 &dst);
78 NetworkPacket &operator<<(u32 src);
79
80 NetworkPacket &operator>>(u64 &dst);
81 NetworkPacket &operator<<(u64 src);
82
83 NetworkPacket &operator>>(float &dst);
84 NetworkPacket &operator<<(float src);
85
88
91
92 NetworkPacket &operator>>(s16 &dst);
93 NetworkPacket &operator<<(s16 src);
94
95 NetworkPacket &operator>>(s32 &dst);
96 NetworkPacket &operator<<(s32 src);
97
100
103
106
107 NetworkPacket &operator>>(video::SColor &dst);
108 NetworkPacket &operator<<(video::SColor src);
109
110 // Temp, we remove SharedBuffer when migration finished
111 // ^ this comment has been here for 7 years
113
114private:
115 void checkReadOffset(u32 from_offset, u32 field_size) const;
116
117 inline void checkDataSize(u32 field_size)
118 {
119 if (m_read_offset + field_size > m_datasize) {
120 m_datasize = m_read_offset + field_size;
121 m_data.resize(m_datasize);
122 }
123 }
124
125 std::vector<u8> m_data;
126 u32 m_datasize = 0;
128 u16 m_command = 0;
130};
Definition pointer.h:29
Definition networkpacket.h:14
const char * getRemainingString()
Definition networkpacket.h:38
void clear()
Definition networkpacket.cpp:39
std::string readLongString()
Definition networkpacket.cpp:188
u16 getU16(u32 from_offset)
Definition networkpacket.cpp:342
u32 getRemainingBytes() const
Definition networkpacket.h:37
void putRawPacket(const u8 *data, u32 datasize, session_t peer_id)
Definition networkpacket.cpp:21
void checkReadOffset(u32 from_offset, u32 field_size) const
Definition networkpacket.cpp:11
u16 m_command
Definition networkpacket.h:128
u32 m_read_offset
Definition networkpacket.h:127
void putRawString(std::string_view src)
Definition networkpacket.h:45
NetworkPacket & operator>>(std::string &dst)
Definition networkpacket.cpp:66
const char * getString(u32 from_offset) const
Definition networkpacket.cpp:48
NetworkPacket()=default
Buffer< u8 > oldForgePacket()
Definition networkpacket.cpp:519
std::vector< u8 > m_data
Definition networkpacket.h:125
u32 m_datasize
Definition networkpacket.h:126
u32 getSize() const
Definition networkpacket.h:34
~NetworkPacket()=default
u8 getU8(u32 offset)
Definition networkpacket.cpp:314
void checkDataSize(u32 field_size)
Definition networkpacket.h:117
u8 * getU8Ptr(u32 offset)
Definition networkpacket.cpp:321
NetworkPacket(u16 command, u32 preallocate, session_t peer_id)
Definition networkpacket.h:16
NetworkPacket & operator<<(std::string_view src)
Definition networkpacket.cpp:87
void putRawString(const char *src, u32 len)
Definition networkpacket.cpp:55
u16 getCommand() const
Definition networkpacket.h:36
NetworkPacket(u16 command, u32 preallocate)
Definition networkpacket.h:21
void putLongString(std::string_view src)
Definition networkpacket.cpp:102
session_t m_peer_id
Definition networkpacket.h:129
session_t getPeerId() const
Definition networkpacket.h:35
core::vector2d< s32 > v2s32
Definition irr_v2d.h:13
core::vector2d< f32 > v2f
Definition irr_v2d.h:11
core::vector3d< s32 > v3s32
Definition irr_v3d.h:15
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
core::vector3df v3f
Definition irr_v3d.h:11
u16 session_t
Definition networkprotocol.h:22