Connection::Receive(): receive Network Packet instead of SharedBuffer<u8>.
[oweals/minetest.git] / src / network / networkpacket.h
1 /*
2 Minetest
3 Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef NETWORKPACKET_HEADER
21 #define NETWORKPACKET_HEADER
22
23 #include "util/pointer.h"
24 #include "util/numeric.h"
25 #include "networkprotocol.h"
26
27 class NetworkPacket
28 {
29
30 public:
31                 NetworkPacket(u16 command, u32 datasize, u16 peer_id);
32                 NetworkPacket(u16 command, u32 datasize);
33                 NetworkPacket(): m_datasize(0), m_read_offset(0), m_command(0),
34                                 m_peer_id(0) {}
35                 ~NetworkPacket();
36
37                 void putRawPacket(u8 *data, u32 datasize, u16 peer_id);
38
39                 // Getters
40                 u32 getSize() { return m_datasize; }
41                 u16 getPeerId() { return m_peer_id; }
42                 u16 getCommand() { return m_command; }
43
44                 // Data extractors
45                 char* getString(u32 from_offset);
46                 void putRawString(const char* src, u32 len);
47
48                 NetworkPacket& operator>>(std::string& dst);
49                 NetworkPacket& operator<<(std::string src);
50
51                 void putLongString(std::string src);
52
53                 NetworkPacket& operator>>(std::wstring& dst);
54                 NetworkPacket& operator<<(std::wstring src);
55
56                 std::string readLongString();
57
58                 char getChar(u32 offset);
59                 NetworkPacket& operator>>(char& dst);
60                 NetworkPacket& operator<<(char src);
61
62                 NetworkPacket& operator>>(bool& dst);
63                 NetworkPacket& operator<<(bool src);
64
65                 u8 getU8(u32 offset);
66
67                 NetworkPacket& operator>>(u8& dst);
68                 NetworkPacket& operator<<(u8 src);
69
70                 u8* getU8Ptr(u32 offset);
71
72                 u16 getU16(u32 from_offset);
73                 NetworkPacket& operator>>(u16& dst);
74                 NetworkPacket& operator<<(u16 src);
75
76                 NetworkPacket& operator>>(u32& dst);
77                 NetworkPacket& operator<<(u32 src);
78
79                 NetworkPacket& operator>>(u64& dst);
80                 NetworkPacket& operator<<(u64 src);
81
82                 NetworkPacket& operator>>(float& dst);
83                 NetworkPacket& operator<<(float src);
84
85                 NetworkPacket& operator>>(v2f& dst);
86                 NetworkPacket& operator<<(v2f src);
87
88                 NetworkPacket& operator>>(v3f& dst);
89                 NetworkPacket& operator<<(v3f src);
90
91                 NetworkPacket& operator>>(s16& dst);
92                 NetworkPacket& operator<<(s16 src);
93
94                 NetworkPacket& operator>>(s32& dst);
95                 NetworkPacket& operator<<(s32 src);
96
97                 NetworkPacket& operator>>(v2s32& dst);
98                 NetworkPacket& operator<<(v2s32 src);
99
100                 NetworkPacket& operator>>(v3s16& dst);
101                 NetworkPacket& operator<<(v3s16 src);
102
103                 NetworkPacket& operator>>(v3s32& dst);
104                 NetworkPacket& operator<<(v3s32 src);
105
106                 NetworkPacket& operator>>(video::SColor& dst);
107                 NetworkPacket& operator<<(video::SColor src);
108
109                 // Temp, we remove SharedBuffer when migration finished
110                 Buffer<u8> oldForgePacket();
111 private:
112                 template<typename T> void checkDataSize()
113                 {
114                         if (m_read_offset + sizeof(T) > m_datasize) {
115                                 m_datasize += sizeof(T);
116                                 m_data.resize(m_datasize);
117                         }
118                 }
119
120                 template<typename T> void incrOffset()
121                 {
122                         m_read_offset += sizeof(T);
123                 }
124
125                 std::vector<u8> m_data;
126                 u32 m_datasize;
127                 u32 m_read_offset;
128                 u16 m_command;
129                 u16 m_peer_id;
130 };
131
132 #endif