]> git.localhorst.tv Git - blank.git/blob - src/net/Packet.hpp
give unique IDs to entities
[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         } header;
31
32         static constexpr std::size_t MAX_PAYLOAD_LEN = 500 - sizeof(Header);
33
34         std::uint8_t payload[MAX_PAYLOAD_LEN];
35
36
37         void Tag() noexcept { header.tag = TAG; }
38
39         void Type(std::uint8_t t) noexcept { header.type = t; }
40         std::uint8_t Type() const noexcept { return header.type; }
41         const char *TypeString() const noexcept { return Type2String(Type()); }
42
43
44         struct Payload {
45                 std::size_t length;
46                 std::uint8_t *data;
47
48                 template<class T>
49                 void Write(const T &, size_t off) noexcept;
50                 template<class T>
51                 void Read(T &, size_t off) const noexcept;
52
53                 void WriteString(const std::string &src, std::size_t off, std::size_t maxlen) noexcept;
54                 void ReadString(std::string &dst, std::size_t off, std::size_t maxlen) const noexcept;
55         };
56
57         struct Ping : public Payload {
58                 static constexpr std::uint8_t TYPE = 0;
59                 static constexpr std::size_t MAX_LEN = 0;
60         };
61
62         struct Login : public Payload {
63                 static constexpr std::uint8_t TYPE = 1;
64                 static constexpr std::size_t MAX_LEN = 32;
65
66                 void WritePlayerName(const std::string &) noexcept;
67                 void ReadPlayerName(std::string &) const noexcept;
68         };
69
70         struct Join : public Payload {
71                 static constexpr std::uint8_t TYPE = 2;
72                 static constexpr std::size_t MAX_LEN = 100;
73
74                 void WritePlayer(const Entity &) noexcept;
75                 void ReadPlayerID(std::uint32_t &) const noexcept;
76                 void ReadPlayer(Entity &) const noexcept;
77                 void WriteWorldName(const std::string &) noexcept;
78                 void ReadWorldName(std::string &) const noexcept;
79         };
80
81         struct Part : public Payload {
82                 static constexpr std::uint8_t TYPE = 3;
83                 static constexpr std::size_t MAX_LEN = 0;
84         };
85
86
87         template<class PayloadType>
88         PayloadType As() {
89                 PayloadType result;
90                 result.length = PayloadType::MAX_LEN;
91                 result.data = &payload[0];
92                 return result;
93         }
94
95         template<class PayloadType>
96         static PayloadType As(const UDPpacket &pack) {
97                 PayloadType result;
98                 result.length = std::min(pack.len - sizeof(Header), PayloadType::MAX_LEN);
99                 result.data = pack.data + sizeof(Header);
100                 return result;
101         }
102
103         template<class PayloadType>
104         static PayloadType Make(UDPpacket &udp_pack) {
105                 Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
106                 pack.Tag();
107                 pack.Type(PayloadType::TYPE);
108
109                 udp_pack.len = sizeof(Header) + PayloadType::MAX_LEN;
110
111                 PayloadType result;
112                 result.length = PayloadType::MAX_LEN;
113                 result.data = pack.payload;
114                 return result;
115         }
116
117 };
118
119 }
120
121 #endif