Wrath of Zeus
Made by Torchlight Games for CSE 125 SP24
Loading...
Searching...
No Matches
packet.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <vector>
6#include <utility>
7#include <ostream>
8#include <sstream>
9#include <iostream>
10
11#include <boost/asio.hpp>
12
13#include "shared/game/event.hpp"
18
19
20// Might want to move this later on to somewhere in the game code
21
30enum class PacketType: uint16_t {
31 // Setup
35
36 // Gameplay
37 Event = 2000,
38};
39
55
60 void to_network() {
61 this->size = htons(this->size);
62 this->type = static_cast<PacketType>(htons(static_cast<uint16_t>(this->type)));
63 }
64
74 explicit PacketHeader(void* buffer) {
75 PacketHeader* buf_hdr = static_cast<PacketHeader*>(buffer);
76 this->size = ntohs(buf_hdr->size);
77 this->type = static_cast<PacketType>(ntohs(static_cast<uint16_t>(buf_hdr->type)));
78 }
79
81 uint16_t size;
84};
85
92 std::string lobby_name;
97
98 DEF_SERIALIZE(Archive& ar, const unsigned int version) {
99 ar & this->lobby_name;
100 ar & this->slots_taken;
101 ar & this->slots_avail;
102 }
103};
104
111 std::string player_name;
112
113 DEF_SERIALIZE(Archive& ar, const unsigned int version) {
114 ar & this->player_name;
115 }
116};
117
126
127 DEF_SERIALIZE(Archive& ar, const unsigned int version) {
128 ar & eid & is_dungeon_master;
129 }
130};
131
134
135 DEF_SERIALIZE(Archive& ar, const unsigned int version) {
136 ar & event;
137 }
138};
139
146public:
150 ~PackagedPacket() = default;
151
158 template <class Packet>
159 static std::shared_ptr<PackagedPacket> make_shared(PacketType type, Packet packet) {
160 std::string data = serialize<Packet>(packet);
161
162 PacketHeader hdr(data.size(), type);
163
164 return std::shared_ptr<PackagedPacket>(new PackagedPacket(hdr, data));
165 }
166
173 std::array<boost::asio::const_buffer, 2> toBuffer() {
174 return {
175 boost::asio::buffer(&this->hdr, sizeof(PacketHeader)),
176 boost::asio::buffer(this->data)
177 };
178 }
179
180private:
196 PackagedPacket(PacketHeader hdr, const std::string& data)
197 :hdr(hdr), data(data)
198 {
199 this->hdr.size = data.size();
200 this->hdr.to_network();
201 }
202
207 PackagedPacket(const PackagedPacket& a) = delete;
208 PackagedPacket& operator=(const PackagedPacket& a) = delete;
209
211 PacketHeader hdr;
213 std::string data;
214};
Definition: packet.hpp:145
std::array< boost::asio::const_buffer, 2 > toBuffer()
Definition: packet.hpp:173
static std::shared_ptr< PackagedPacket > make_shared(PacketType type, Packet packet)
Definition: packet.hpp:159
~PackagedPacket()=default
GLboolean * data
Definition: glad.h:1600
GLuint buffer
Definition: glad.h:1791
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glad.h:1531
GLboolean GLboolean GLboolean GLboolean a
Definition: glad.h:2133
GLsizeiptr size
Definition: glad.h:1803
PacketType
Definition: packet.hpp:30
@ ServerLobbyBroadcast
Sent by the server via UDP broadcast saying it has a server open.
@ ClientDeclareInfo
Sent by the client after TCP handshake.
@ ServerAssignEID
Sent by the server after TCP handshake, giving client its EID.
Definition: packet.hpp:109
std::string player_name
Name that the client wishes to be called by.
Definition: packet.hpp:111
DEF_SERIALIZE(Archive &ar, const unsigned int version)
Definition: packet.hpp:113
Definition: packet.hpp:132
Event event
Definition: packet.hpp:133
DEF_SERIALIZE(Archive &ar, const unsigned int version)
Definition: packet.hpp:135
Definition: event.hpp:348
Definition: packet.hpp:47
PacketType type
What kind of packet this is, according to the Type enum class.
Definition: packet.hpp:83
PacketHeader(void *buffer)
Definition: packet.hpp:74
void to_network()
Definition: packet.hpp:60
uint16_t size
Size (in bytes) of the packet data (not including the header)
Definition: packet.hpp:81
PacketHeader(uint16_t size, PacketType type)
Definition: packet.hpp:54
Definition: packet.hpp:122
bool is_dungeon_master
Definition: packet.hpp:125
EntityID eid
ID that the server is assigning to the client.
Definition: packet.hpp:124
DEF_SERIALIZE(Archive &ar, const unsigned int version)
Definition: packet.hpp:127
Definition: packet.hpp:90
int slots_avail
How many more clients can join this lobby.
Definition: packet.hpp:96
std::string lobby_name
Name of the server lobby.
Definition: packet.hpp:92
DEF_SERIALIZE(Archive &ar, const unsigned int version)
Definition: packet.hpp:98
int slots_taken
How many clients are already in this lobby.
Definition: packet.hpp:94
uint32_t EntityID
Global Object ID (when the server or client references an object with a particular EntityID,...
Definition: typedefs.hpp:9