My Project
Serialization.hpp
1 /*
2 ** EPITECH PROJECT, 2022
3 ** B-CPP-500-PAR-5-1-rtype-martin.vanaud
4 ** File description:
5 ** Serialization
6 */
7 
8 #ifndef SERIALIZATION_HPP_
9  #define SERIALIZATION_HPP_
10 
11  #include <vector>
12  #include <iostream>
13  #include <cstring>
14 
15  #include "Shoot.hpp"
16 
21  enum PACKET_TYPE {
22  SHOOT,
23  DIRECTION,
24  NEW_CONNECTION,
25  DISCONNECTION,
26  NEW_CLIENT,
27  GET_LOBBYS,
28  CREATE_LOBBY,
29  START_GAME,
30  JOIN_LOBBY
31  };
32 };
33 
38  enum PACKET_TYPE {
39  POSITION,
40  NEW_ENTITY,
41  KILL_ENTITY,
42  KILL_ENTITY_TYPE,
43  NEW_CLIENT_RESPONSE,
44  SEND_LOBBYS,
45  NUMBER_PLAYERS_IN_LOBBY,
46  UPDATE_ENTITY_HEALTH,
47  UPDATE_ENTITY_SCORE,
48  END_GAME,
49  NEW_LEVEL
50  };
51 };
52 
56 enum ENTITY_TYPE {
57  WALL,
58  PLAYER,
59  GREENSPACESHIP,
60  REDSPACESHIP,
61  PURPLESPACESHIP,
62  MARINESPACESHIP,
63  ENEMY,
64  BULLET,
65  PARALLAX,
66  BUTTON,
67  TEXT,
68  IMAGE,
69  UI,
70  NET,
71  ENEMY2,
72  INPUT_BOX,
73  ENEMY3
74 };
75 
76 using byte = unsigned char;
77 
83 template <class Serializable>
91  static std::vector<byte> serialize(Serializable const &obj) {
92  std::vector<byte> ret;
93 
94  ret.resize(sizeof(Serializable));
95  std::memcpy(ret.data(), std::addressof(obj), sizeof(Serializable));
96  return ret;
97  }
98 
105  static Serializable unserialize(std::vector<byte> const &v) {
106  Serializable s;
107 
108  std::memcpy(&s, v.data(), sizeof(Serializable));
109  return s;
110  }
111 };
112 
125  template<class Seriazable>
126  static std::vector<byte> serializeHeader(uint8_t id, Seriazable const &obj)
127  {
128  std::vector<byte> bytes;
129  bytes.resize(sizeof(uint8_t));
130 
131  std::memcpy(bytes.data(), &id, sizeof(uint8_t));
132  std::vector<byte> data = serializable_trait<Seriazable>::serialize(obj);
133  bytes.insert(bytes.end(), data.begin(), data.end());
134  return bytes;
135  };
136 
143  static uint8_t getId(std::vector<byte> const &bytes)
144  {
145  uint8_t id;
146  std::memcpy(&id, bytes.data(), sizeof(uint8_t));
147  return id;
148  };
149 };
150 
151 #endif /* !SERIALIZATION_HPP_ */
152 
serialize_header
Class to serialize and unserialize a packet (as a std::vector of bytes) with an id representing the t...
Definition: Serialization.hpp:116
serialize_header::serializeHeader
static std::vector< byte > serializeHeader(uint8_t id, Seriazable const &obj)
A method to serialize an object (the object passed as template argument) into a vector of byte with h...
Definition: Serialization.hpp:126
serializable_trait
A templated class to serialize and unserialize a packet (as a std::vector of bytes)
Definition: Serialization.hpp:84
NETWORK_CLIENT_TO_SERVER
Enum representing Type of request sent from Client to Network.
Definition: Serialization.hpp:20
serializable_trait::unserialize
static Serializable unserialize(std::vector< byte > const &v)
A method to unserialize a vector of byte into an object (passed as template argument)
Definition: Serialization.hpp:105
serializable_trait::serialize
static std::vector< byte > serialize(Serializable const &obj)
A method to serialize an object (the object passed as template argument) into a vector of byte.
Definition: Serialization.hpp:91
NETWORK_SERVER_TO_CLIENT
Enum representing Type of request sent from Network to Client.
Definition: Serialization.hpp:37
serialize_header::getId
static uint8_t getId(std::vector< byte > const &bytes)
Get the Id object from the bytes.
Definition: Serialization.hpp:143