]> git.localhorst.tv Git - blank.git/blob - src/net/Packet.hpp
documented packet structure
[blank.git] / src / net / Packet.hpp
1 #ifndef BLANK_NET_PACKET_HPP_
2 #define BLANK_NET_PACKET_HPP_
3
4 #include <cstdint>
5 #include <ostream>
6 #include <string>
7 #include <SDL_net.h>
8
9
10 namespace blank {
11
12 class Entity;
13
14 struct Packet {
15
16         static constexpr std::uint32_t TAG = 0xFB1AB1AF;
17
18         static const char *Type2String(std::uint8_t) noexcept;
19
20         struct TControl {
21                 std::uint16_t seq;
22                 std::uint16_t ack;
23                 std::uint32_t hist;
24         };
25
26         struct Header {
27                 std::uint32_t tag;
28                 TControl ctrl;
29                 std::uint8_t type;
30                 std::uint8_t reserved1;
31                 std::uint8_t reserved2;
32                 std::uint8_t reserved3;
33         } header;
34
35         static constexpr std::size_t MAX_PAYLOAD_LEN = 500 - sizeof(Header);
36
37         std::uint8_t payload[MAX_PAYLOAD_LEN];
38
39
40         void Tag() noexcept { header.tag = TAG; }
41
42         void Type(std::uint8_t t) noexcept { header.type = t; }
43         std::uint8_t Type() const noexcept { return header.type; }
44         const char *TypeString() const noexcept { return Type2String(Type()); }
45
46
47         struct Payload {
48                 std::size_t length;
49                 std::uint8_t *data;
50
51                 template<class T>
52                 void Write(const T &, size_t off) noexcept;
53                 template<class T>
54                 void Read(T &, size_t off) const noexcept;
55
56                 void WriteString(const std::string &src, std::size_t off, std::size_t maxlen) noexcept;
57                 void ReadString(std::string &dst, std::size_t off, std::size_t maxlen) const noexcept;
58         };
59
60         struct Ping : public Payload {
61                 static constexpr std::uint8_t TYPE = 0;
62                 static constexpr std::size_t MAX_LEN = 0;
63         };
64
65         struct Login : public Payload {
66                 static constexpr std::uint8_t TYPE = 1;
67                 static constexpr std::size_t MAX_LEN = 32;
68
69                 void WritePlayerName(const std::string &) noexcept;
70                 void ReadPlayerName(std::string &) const noexcept;
71         };
72
73         struct Join : public Payload {
74                 static constexpr std::uint8_t TYPE = 2;
75                 static constexpr std::size_t MAX_LEN = 100;
76
77                 void WritePlayer(const Entity &) noexcept;
78                 void ReadPlayerID(std::uint32_t &) const noexcept;
79                 void ReadPlayer(Entity &) const noexcept;
80                 void WriteWorldName(const std::string &) noexcept;
81                 void ReadWorldName(std::string &) const noexcept;
82         };
83
84         struct Part : public Payload {
85                 static constexpr std::uint8_t TYPE = 3;
86                 static constexpr std::size_t MAX_LEN = 0;
87         };
88
89
90         template<class PayloadType>
91         PayloadType As() {
92                 PayloadType result;
93                 result.length = PayloadType::MAX_LEN;
94                 result.data = &payload[0];
95                 return result;
96         }
97
98         template<class PayloadType>
99         static PayloadType As(const UDPpacket &pack) {
100                 PayloadType result;
101                 result.length = std::min(pack.len - sizeof(Header), PayloadType::MAX_LEN);
102                 result.data = pack.data + sizeof(Header);
103                 return result;
104         }
105
106         template<class PayloadType>
107         static PayloadType Make(UDPpacket &udp_pack) {
108                 Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
109                 pack.Tag();
110                 pack.Type(PayloadType::TYPE);
111
112                 udp_pack.len = sizeof(Header) + PayloadType::MAX_LEN;
113
114                 PayloadType result;
115                 result.length = PayloadType::MAX_LEN;
116                 result.data = pack.payload;
117                 return result;
118         }
119
120 };
121
122 }
123
124 #endif