serialization
This commit is contained in:
parent
035436c639
commit
4c2e8a12ad
@ -145,6 +145,7 @@
|
||||
<ClCompile Include="blockinfo.cpp" />
|
||||
<ClCompile Include="bullet.cpp" />
|
||||
<ClCompile Include="chunk.cpp" />
|
||||
<ClCompile Include="netprotocol.cpp" />
|
||||
<ClCompile Include="opensimplex.cpp" />
|
||||
<ClCompile Include="player.cpp" />
|
||||
<ClCompile Include="world.cpp" />
|
||||
|
@ -71,5 +71,8 @@
|
||||
<ClCompile Include="player.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="netprotocol.cpp">
|
||||
<Filter>Fichiers sources</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -22,7 +22,7 @@
|
||||
|
||||
typedef uint8_t BlockType;
|
||||
enum BLOCK_TYPE { BTYPE_AIR, BTYPE_DIRT, BTYPE_GRASS, BTYPE_METAL, BTYPE_ICE, BTYPE_LAST };
|
||||
typedef std::chrono::system_clock::time_point Timestamp;
|
||||
typedef uint64_t Timestamp;
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
|
139
SQCSim-common/netprotocol.cpp
Normal file
139
SQCSim-common/netprotocol.cpp
Normal file
@ -0,0 +1,139 @@
|
||||
#include "netprotocol.h"
|
||||
|
||||
void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
|
||||
|
||||
}
|
||||
|
||||
void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) {
|
||||
|
||||
}
|
||||
|
||||
void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) {
|
||||
|
||||
}
|
||||
|
||||
void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) {
|
||||
|
||||
}
|
||||
|
||||
void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
|
||||
*buf[0] = netprot::PACKET_TYPE::LOGINF;
|
||||
void* nom = &linfo->name;
|
||||
char* buff = *buf + 1;
|
||||
|
||||
size_t namesize = 1;
|
||||
|
||||
for (int x = 0; x < sizeof(linfo->name); ++x)
|
||||
if (linfo->name[x] != (char)'\0')
|
||||
++namesize;
|
||||
else break;
|
||||
|
||||
memcpy(buff, nom, sizeof(linfo->name));
|
||||
uint64_t sid = linfo->sid;
|
||||
char diff[sizeof(uint64_t)] = { (sid >> 56) & 0xFF,
|
||||
(sid >> 48) & 0xFF,
|
||||
(sid >> 40) & 0xFF,
|
||||
(sid >> 32) & 0xFF,
|
||||
(sid >> 24) & 0xFF,
|
||||
(sid >> 16) & 0xFF,
|
||||
(sid >> 8) & 0xFF,
|
||||
sid & 0xFF };
|
||||
|
||||
void* ptrsid = *buf + namesize;
|
||||
memcpy(ptrsid, &diff, sizeof(uint64_t));
|
||||
|
||||
*buflen = sizeof(LoginInfo) + 1;
|
||||
}
|
||||
|
||||
void netprot::Serialize(SelfInfo* sinfo, char* buf[], uint32_t* buflen) {
|
||||
|
||||
}
|
||||
|
||||
void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) {
|
||||
|
||||
}
|
||||
|
||||
void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) {
|
||||
|
||||
}
|
||||
|
||||
void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) {
|
||||
|
||||
}
|
||||
|
||||
void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool netprot::Deserialize(Output* out, char* buf, const uint32_t buflen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool netprot::Deserialize(Sync* sync, char* buf, const uint32_t buflen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) {
|
||||
if (buflen < sizeof(sizeof(LoginInfo)) + 1)
|
||||
return false;
|
||||
|
||||
size_t namesize = 0;
|
||||
|
||||
for (int x = 0; x < sizeof(linfo->name); ++x)
|
||||
if (buf[x] != (char)'\0')
|
||||
++namesize;
|
||||
else break;
|
||||
|
||||
memcpy((void*)&linfo->name, (void*)&buf[1], namesize);
|
||||
|
||||
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
|
||||
memcpy(&diff, &buf[namesize], sizeof(uint64_t));
|
||||
linfo->sid = (uint64_t)diff[0] << 56 |
|
||||
(uint64_t)diff[1] << 48 |
|
||||
(uint64_t)diff[2] << 40 |
|
||||
(uint64_t)diff[3] << 32 |
|
||||
(uint64_t)diff[4] << 24 |
|
||||
(uint64_t)diff[5] << 16 |
|
||||
(uint64_t)diff[6] << 8 |
|
||||
(uint64_t)diff[7];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool netprot::Deserialize(SelfInfo* sinfo, char* buf, const uint32_t buflen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool netprot::Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool netprot::Deserialize(Chat* chat, char* buf, const uint32_t buflen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) {
|
||||
return false;
|
||||
}
|
||||
|
||||
netprot::PacketType netprot::getType(char* buf, const uint32_t buflen) {
|
||||
if (buflen < 1 ||
|
||||
buf[0] >= netprot::PACKET_TYPE::LAST_PACK ||
|
||||
buf[0] <= netprot::PACKET_TYPE::ERR)
|
||||
return netprot::PACKET_TYPE::ERR;
|
||||
return buf[0];
|
||||
}
|
@ -1,98 +1,107 @@
|
||||
#ifndef NETPROTOCOL_H__
|
||||
#define NETPROTOCOL_H__
|
||||
#include "define.h"
|
||||
#include <string>
|
||||
#include "vector3.h"
|
||||
|
||||
/* Protocole Particulier de Partie à 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
|
||||
|
@ -25,19 +25,19 @@ uint64_t Connection::GetTeamHash() const { return m_tid; }
|
||||
|
||||
std::string Connection::GetName() const { return m_name; }
|
||||
|
||||
void Connection::AddInput(Input in) {
|
||||
m_input_manifest.insert({in.timestamp, in});
|
||||
void Connection::AddInput(netprot::Input in) {
|
||||
m_input_manifest.insert({ in.timestamp, in });
|
||||
}
|
||||
|
||||
Output* Connection::getOutput(Timestamp time) {
|
||||
netprot::Output* Connection::getOutput(Timestamp time) {
|
||||
auto out = m_output_manifest.find(time);
|
||||
if (out != m_output_manifest.end())
|
||||
return &out->second;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Sync Connection::getSync(Timestamp time) {
|
||||
Sync sync;
|
||||
netprot::Sync Connection::getSync(Timestamp time) {
|
||||
netprot::Sync sync;
|
||||
auto out = m_output_manifest.find(time);
|
||||
if (out != m_output_manifest.end()) {
|
||||
sync.timestamp = out->second.timestamp;
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <map>
|
||||
#include "../SQCSim-common/player.h"
|
||||
#include "../SQCSim-common/vector3.h"
|
||||
#include "../SQCSim-common/serialization.h"
|
||||
#include "../SQCSim-common/netprotocol.h"
|
||||
#include "define.h"
|
||||
|
||||
class Connection {
|
||||
@ -24,14 +24,14 @@ public:
|
||||
uint64_t GetTeamHash() const;
|
||||
std::string GetName() const;
|
||||
|
||||
void AddInput(Input in);
|
||||
Output* getOutput(Timestamp time);
|
||||
Sync getSync(Timestamp time);
|
||||
void AddInput(netprot::Input in);
|
||||
netprot::Output* getOutput(Timestamp time);
|
||||
netprot::Sync getSync(Timestamp time);
|
||||
|
||||
void CleanInputManifest(Timestamp time);
|
||||
private:
|
||||
std::map<Timestamp, Input> m_input_manifest;
|
||||
std::map<Timestamp, Output> m_output_manifest;
|
||||
std::map<Timestamp, netprot::Input> m_input_manifest;
|
||||
std::map<Timestamp, netprot::Output> m_output_manifest;
|
||||
in_addr m_addr;
|
||||
uint64_t m_id,
|
||||
m_sid,
|
||||
|
@ -1,4 +1,3 @@
|
||||
#include "define.h"
|
||||
#include "server.h"
|
||||
|
||||
int main() {
|
||||
|
@ -79,14 +79,16 @@ int Server::Ready() {
|
||||
Log("À l'écoute sur le port: " + std::to_string(SRV_PORT), false, false);
|
||||
|
||||
while (!readystart) {
|
||||
|
||||
Log("trololo", false, false);
|
||||
|
||||
// Listen/accept ici.
|
||||
|
||||
readystart = true;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Server::Run() {
|
||||
|
||||
Log("Partie en cours...", false, false);
|
||||
}
|
||||
|
||||
@ -131,6 +133,32 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false)
|
||||
}
|
||||
}
|
||||
|
||||
// Test serialize/deserialize:
|
||||
/*
|
||||
netprot::LoginInfo* log = new netprot::LoginInfo();
|
||||
char nom[] = "Jean Dujardin";
|
||||
memcpy(log->name, &nom, sizeof(nom));
|
||||
log->sid = 12345;
|
||||
char* buf = new char[150];
|
||||
uint32_t buflen = 150;
|
||||
|
||||
netprot::Serialize(log, &buf, &buflen);
|
||||
|
||||
delete log;
|
||||
log = new netprot::LoginInfo();
|
||||
|
||||
bool is_work = netprot::Deserialize(log, buf, buflen);
|
||||
|
||||
std::string str;
|
||||
|
||||
str.append(is_work ? "Y " : "N ").append(log->name).append(": ").append(std::to_string(log->sid));
|
||||
|
||||
Log(str, false, false);
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
///* Recevoir paquet */
|
||||
//while (true) {
|
||||
// char buffer[2048];
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "../SQCSim-common/world.h"
|
||||
#include "define.h"
|
||||
#include "connection.h"
|
||||
#include "../SQCSim-common/netprotocol.h"
|
||||
|
||||
class Server {
|
||||
public:
|
||||
|
Loading…
Reference in New Issue
Block a user