]> git.localhorst.tv Git - blank.git/blob - src/net/Packet.hpp
reorganized client state
[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 ReadPlayer(Entity &) const noexcept;
76                 void WriteWorldName(const std::string &) noexcept;
77                 void ReadWorldName(std::string &) const noexcept;
78         };
79
80         struct Part : public Payload {
81                 static constexpr std::uint8_t TYPE = 3;
82                 static constexpr std::size_t MAX_LEN = 0;
83         };
84
85
86         template<class PayloadType>
87         PayloadType As() {
88                 PayloadType result;
89                 result.length = PayloadType::MAX_LEN;
90                 result.data = &payload[0];
91                 return result;
92         }
93
94         template<class PayloadType>
95         static PayloadType As(const UDPpacket &pack) {
96                 PayloadType result;
97                 result.length = std::min(pack.len - sizeof(Header), PayloadType::MAX_LEN);
98                 result.data = pack.data + sizeof(Header);
99                 return result;
100         }
101
102         template<class PayloadType>
103         static PayloadType Make(UDPpacket &udp_pack) {
104                 Packet &pack = *reinterpret_cast<Packet *>(udp_pack.data);
105                 pack.Tag();
106                 pack.Type(PayloadType::TYPE);
107
108                 udp_pack.len = sizeof(Header) + PayloadType::TYPE;
109
110                 PayloadType result;
111                 result.length = PayloadType::MAX_LEN;
112                 result.data = pack.payload;
113                 return result;
114         }
115
116 };
117
118 }
119
120 #endif