Network Layer 7 rework (Packet handling)
[oweals/minetest.git] / src / network / networkpacket.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #ifndef NETWORKPACKET_HEADER
22 #define NETWORKPACKET_HEADER
23
24 #include "util/numeric.h"
25 #include "networkprotocol.h"
26
27 class NetworkPacket
28 {
29
30 public:
31                 NetworkPacket(u8 *data, u32 datasize, u16 peer_id);
32                 ~NetworkPacket();
33
34                 // Getters
35                 u32 getSize() { return m_datasize; }
36                 u16 getPeerId() { return m_peer_id; }
37
38                 // Data extractors
39                 char* getString(u32 from_offset);
40                 NetworkPacket& operator>>(std::string& dst);
41                 NetworkPacket& operator>>(std::wstring& dst);
42                 std::string readLongString();
43
44                 char getChar(u32 offset);
45                 NetworkPacket& operator>>(char& dst);
46
47                 NetworkPacket& operator>>(bool& dst);
48
49                 u8 getU8(u32 offset);
50                 NetworkPacket& operator>>(u8& dst);
51
52                 u8* getU8Ptr(u32 offset);
53                 u16 getU16(u32 from_offset);
54                 NetworkPacket& operator>>(u16& dst);
55                 u32 getU32(u32 from_offset);
56                 NetworkPacket& operator>>(u32& dst);
57                 u64 getU64(u32 from_offset);
58                 NetworkPacket& operator>>(u64& dst);
59
60                 float getF1000(u32 offset);
61                 NetworkPacket& operator>>(float& dst);
62                 NetworkPacket& operator>>(v2f& dst);
63                 NetworkPacket& operator>>(v3f& dst);
64
65                 s16 getS16(u32 from_offset);
66                 NetworkPacket& operator>>(s16& dst);
67                 s32 getS32(u32 from_offset);
68                 NetworkPacket& operator>>(s32& dst);
69
70                 NetworkPacket& operator>>(v2s32& dst);
71
72                 v3s16 getV3S16(u32 from_offset);
73                 NetworkPacket& operator>>(v3s16& dst);
74
75                 v3s32 getV3S32(u32 from_offset);
76                 NetworkPacket& operator>>(v3s32& dst);
77
78 protected:
79                 u8 *m_data;
80                 u32 m_datasize;
81                 u32 m_read_offset;
82 private:
83                 u16 m_peer_id;
84 };
85
86 #endif