serialization
This commit is contained in:
		| @@ -1,98 +1,107 @@ | ||||
| #ifndef NETPROTOCOL_H__ | ||||
| #define NETPROTOCOL_H__ | ||||
| #include "define.h" | ||||
| #include <string> | ||||
| #include "vector3.h" | ||||
|  | ||||
| /* Protocole Particulier de Partie <20> Plusieurs Personnes (PPPPP) */ | ||||
|  | ||||
| // Packet: packet[0] = PacketType, packet[1..n-1] = {packet} | ||||
|  | ||||
| namespace netprot { | ||||
| 	typedef uint8_t PacketType; | ||||
| 	enum PACKET_TYPE { | ||||
| 				ERR, INPUT, OUTPUT, SYNC, | ||||
| 				TEAMINF, SELFINF, PLAYINF, | ||||
| 				TEAMINF, SELFINF, PLAYINF, LOGINF, | ||||
| 				CHUNKMOD, PLAYERMOD, PICKUPMOD, | ||||
| 				GAMEINFO, ENDINFO , CHAT, ERRLOG | ||||
| 				GAMEINFO, ENDINFO , CHAT, ERRLOG, | ||||
| 				LAST_PACK | ||||
| 			}; | ||||
|  | ||||
| 	typedef struct {					// cli -> srv			UDP			~frame | ||||
| 	struct Input {										// cli -> srv			UDP			~frame | ||||
| 		Timestamp timestamp; | ||||
| 		uint8_t keys; // 0bFBLRJS__ | ||||
| 		uint8_t keys; // 0bFBLRJS__ bit-packing de bool. | ||||
| 		Vector3f direction; | ||||
| 	} Input; | ||||
| 	}; | ||||
|  | ||||
| 	typedef struct {					// srv -> cli			UDP			~frame | ||||
| 	struct Output {										// srv -> cli			UDP			~frame | ||||
| 		Timestamp timestamp; | ||||
| 		uint64_t id = 0; | ||||
| 		Vector3f position, | ||||
| 				 direction; | ||||
| 		bool is_shooting, | ||||
| 			 is_jumping; | ||||
| 	} Output; | ||||
| 			  direction; | ||||
| 		uint8_t states; // 0bJSH_____ bit-packing de bool. | ||||
| 	}; | ||||
|  | ||||
| 	typedef struct {					// srv -> cli			TCP			~second | ||||
| 	struct Sync {										// srv -> cli			TCP			~second | ||||
| 		Timestamp timestamp; | ||||
| 		uint64_t sid = 0; | ||||
| 		uint32_t timer = 0; | ||||
| 		uint16_t ammo = 0; | ||||
| 		uint8_t hp = 0; | ||||
| 		Vector3f position; | ||||
| 	} Sync; | ||||
| 	}; | ||||
|  | ||||
| 	typedef struct {					// cli <-> srv			TCP			once | ||||
| 	struct TeamInfo {									// cli <-> srv			TCP			once | ||||
| 		char name[32]; | ||||
| 		uint64_t id = 0; | ||||
| 	} TeamInfo; | ||||
| 	}; | ||||
|  | ||||
| 	typedef struct {					// cli <-> srv			TCP			once | ||||
| 	struct LoginInfo {									// cli <-> srv			TCP			once | ||||
| 		char name[32]; | ||||
| 		uint64_t sid = 0; | ||||
| 	}; | ||||
|  | ||||
| 	struct SelfInfo {									// cli <-> srv			TCP			once | ||||
| 		uint64_t sid = 0, | ||||
| 				 tid = 0; | ||||
| 	} SelfInfo; | ||||
| 	}; | ||||
|  | ||||
| 	typedef struct {					// cli <-> srv			TCP			once | ||||
| 	struct PlayerInfo {									// cli <-> srv			TCP			once | ||||
| 		char name[32]; | ||||
| 		uint64_t id = 0, | ||||
| 				 tid = 0; | ||||
| 	} PlayerInfo; | ||||
| 	 | ||||
| 	typedef struct {					// cli <-> srv			TCP			event (before game start)/ once | ||||
| 	}; | ||||
|  | ||||
| 	struct GameInfo {									// cli <-> srv			TCP			event (before game start)/ once | ||||
| 		uint64_t seed; | ||||
| 		uint32_t countdown; | ||||
| 		uint8_t gameType; // TOOD: enum. | ||||
| 	} GameInfo; | ||||
| 	}; | ||||
|  | ||||
| 	typedef struct {					// cli <-> srv			TCP			event | ||||
| 	struct Chat {										// cli <-> srv			TCP			event | ||||
| 		uint64_t src_id = 0, | ||||
| 			dest_id = 0, | ||||
| 			dest_team_id = 0; | ||||
| 		char mess[140]; // Good 'nough for twitr, good 'nough for me. | ||||
| 	} Chat; | ||||
| 	}; | ||||
|  | ||||
| 	typedef struct {					// srv -> cli			TCP			event | ||||
| 	struct ErrorLog {									// srv -> cli			TCP			event | ||||
| 		char mess[140]; | ||||
| 		bool is_fatal; | ||||
| 	} ErrorLog; | ||||
| 	}; | ||||
|  | ||||
| 	inline void Serialize(Input* in, char* buf, uint32_t* buflen); // cli | ||||
| 	inline void Serialize(Output* out, char* buf, uint32_t* buflen); // srv | ||||
| 	inline void Serialize(Sync* sync, char* buf, uint32_t* buflen); // srv | ||||
| 	inline void Serialize(TeamInfo* tinfo, char* buf, uint32_t* buflen); // cli/srv | ||||
| 	inline void Serialize(SelfInfo* sinfo, char* buf, uint32_t* buflen); // cli/srv | ||||
| 	inline void Serialize(PlayerInfo* pinfo, char* buf, uint32_t* buflen); // srv | ||||
| 	inline void Serialize(GameInfo* ginfo, char* buf, uint32_t* buflen); // cli/srv | ||||
| 	inline void Serialize(Chat* chat, char* buf, uint32_t* buflen); // cli/srv | ||||
| 	inline void Serialize(ErrorLog* errlog, char* buf, uint32_t* buflen); // srv | ||||
| 	void Serialize(Input* in, char* buf[], uint32_t* buflen); // cli | ||||
| 	void Serialize(Output* out, char* buf[], uint32_t* buflen); // srv | ||||
| 	void Serialize(Sync* sync, char* buf[], uint32_t* buflen); // srv | ||||
| 	void Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen); // cli/srv | ||||
| 	void Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen); // cli/srv | ||||
| 	void Serialize(SelfInfo* sinfo, char* buf[], uint32_t* buflen); // cli/srv | ||||
| 	void Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen); // srv | ||||
| 	void Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen); // cli/srv | ||||
| 	void Serialize(Chat* chat, char* buf[], uint32_t* buflen); // cli/srv | ||||
| 	void Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen); // srv | ||||
|  | ||||
| 	inline void Deserialize(Input* in, char* buf, uint32_t* buflen); // srv | ||||
| 	inline void Deserialize(Output* out, char* buf, uint32_t* buflen); // cli | ||||
| 	inline void Deserialize(Sync* sync, char* buf, uint32_t* buflen); // cli | ||||
| 	inline void Deserialize(TeamInfo* tinfo, char* buf, uint32_t* buflen); // cli/srv | ||||
| 	inline void Deserialize(SelfInfo* sinfo, char* buf, uint32_t* buflen); // cli/srv | ||||
| 	inline void Deserialize(PlayerInfo* pinfo, char* buf, uint32_t* buflen); // cli | ||||
| 	inline void Deserialize(GameInfo* ginfo, char* buf, uint32_t* buflen); // cli | ||||
| 	inline void Deserialize(Chat* chat, char* buf, uint32_t* buflen); // srv/cli | ||||
| 	inline void Deserialize(ErrorLog* errlog, char* buf, uint32_t* buflen); // srv | ||||
| 	bool Deserialize(Input* in, char* buf, const uint32_t buflen); // srv | ||||
| 	bool Deserialize(Output* out, char* buf, const uint32_t buflen); // cli | ||||
| 	bool Deserialize(Sync* sync, char* buf, const uint32_t buflen); // cli | ||||
| 	bool Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen); // cli/srv | ||||
| 	bool Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen); // cli/srv | ||||
| 	bool Deserialize(SelfInfo* sinfo, char* buf, const uint32_t buflen); // cli/srv | ||||
| 	bool Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen); // cli | ||||
| 	bool Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen); // cli | ||||
| 	bool Deserialize(Chat* chat, char* buf, const uint32_t buflen); // srv/cli | ||||
| 	bool Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen); // srv | ||||
|  | ||||
| 	inline PacketType getType(char* buf, uint32_t* buflen); // srv/cli | ||||
| 	PacketType getType(char* buf, uint32_t buflen); // srv/cli | ||||
| }  | ||||
| #endif | ||||
|   | ||||
		Reference in New Issue
	
	Block a user