]> git.localhorst.tv Git - blank.git/blob - src/net/Packet.hpp
defined and implemented join and part packets
[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
8
9 namespace blank {
10
11 class Entity;
12
13 struct Packet {
14
15         static constexpr std::uint32_t TAG = 0xFB1AB1AF;
16
17         enum Type {
18                 PING = 0,
19                 LOGIN = 1,
20                 JOIN = 2,
21                 PART = 3,
22         };
23
24         static const char *Type2String(Type) noexcept;
25
26         struct TControl {
27                 std::uint16_t seq;
28                 std::uint16_t ack;
29                 std::uint32_t hist;
30         };
31
32         struct Header {
33                 std::uint32_t tag;
34                 TControl ctrl;
35                 std::uint8_t type;
36         } header;
37
38         std::uint8_t payload[500 - sizeof(Header)];
39
40
41         Type GetType() const noexcept { return Type(header.type); }
42
43         void Tag() noexcept;
44
45         std::size_t MakePing() noexcept;
46         std::size_t MakeLogin(const std::string &name) noexcept;
47         std::size_t MakeJoin(const Entity &player, const std::string &world_name) noexcept;
48         std::size_t MakePart() noexcept;
49
50 };
51
52 inline std::ostream &operator <<(std::ostream &out, Packet::Type t) {
53         return out << Packet::Type2String(t);
54 }
55
56 }
57
58 #endif