Redressage des classes; seed est reçue du serveur

This commit is contained in:
MarcEricMartel 2023-09-27 17:34:25 -04:00
parent 7eabee38ad
commit df52d4a084
8 changed files with 198 additions and 77 deletions

View File

@ -23,7 +23,7 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
memcpy(*buf + 1, &linfo->name, namesize);
uint64_t sid = linfo->sid;
uint8_t diff[sizeof(uint64_t)] = {
uint8_t sid8[sizeof(uint64_t)] = {
(sid >> 56) & 0xFF,
(sid >> 48) & 0xFF,
(sid >> 40) & 0xFF,
@ -34,13 +34,23 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
sid & 0xFF
};
memcpy(*buf + namesize + 2, diff, sizeof(uint64_t));
memcpy(*buf + namesize + 2, sid8, sizeof(uint64_t));
*buflen = namesize + sizeof(uint64_t) + 2;
}
uint64_t tid = linfo->tid;
uint8_t tid8[sizeof(uint64_t)] = {
(tid >> 56) & 0xFF,
(tid >> 48) & 0xFF,
(tid >> 40) & 0xFF,
(tid >> 32) & 0xFF,
(tid >> 24) & 0xFF,
(tid >> 16) & 0xFF,
(tid >> 8) & 0xFF,
tid & 0xFF
};
void netprot::Serialize(SelfInfo* sinfo, char* buf[], uint32_t* buflen) {
memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
}
void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) {
@ -48,7 +58,51 @@ void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) {
}
void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::GAMEINFO;
uint64_t game = ginfo->seed;
uint8_t seed8[sizeof(uint64_t)] = {
(game >> 56) & 0xFF,
(game >> 48) & 0xFF,
(game >> 40) & 0xFF,
(game >> 32) & 0xFF,
(game >> 24) & 0xFF,
(game >> 16) & 0xFF,
(game >> 8) & 0xFF,
game & 0xFF
};
memcpy(*buf + 1, seed8, sizeof(uint64_t));
game = ginfo->countdown;
uint8_t count8[sizeof(uint64_t)] = {
(game >> 56) & 0xFF,
(game >> 48) & 0xFF,
(game >> 40) & 0xFF,
(game >> 32) & 0xFF,
(game >> 24) & 0xFF,
(game >> 16) & 0xFF,
(game >> 8) & 0xFF,
game & 0xFF
};
memcpy(*buf + sizeof(uint64_t) + 1, count8, sizeof(uint64_t));
game = ginfo->countdown;
uint8_t gtype8[sizeof(uint64_t)] = {
(game >> 56) & 0xFF,
(game >> 48) & 0xFF,
(game >> 40) & 0xFF,
(game >> 32) & 0xFF,
(game >> 24) & 0xFF,
(game >> 16) & 0xFF,
(game >> 8) & 0xFF,
game & 0xFF
};
memcpy(*buf + sizeof(uint64_t) + 1, gtype8, sizeof(uint64_t));
*buflen = sizeof(uint64_t) * 3 + 1;
}
void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) {
@ -100,11 +154,18 @@ bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) {
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
return true;
}
memcpy(diff, &buf[namesize + sizeof(uint64_t) + 1], sizeof(uint64_t));
linfo->tid =
(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];
bool netprot::Deserialize(SelfInfo* sinfo, char* buf, const uint32_t buflen) {
return false;
return true;
}
bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) {
@ -112,7 +173,44 @@ bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, const uint32_t buflen) {
}
bool netprot::Deserialize(GameInfo* ginfo, char* buf, const uint32_t buflen) {
return false;
if (buflen < sizeof(GameInfo) + 1)
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[1], sizeof(uint64_t));
ginfo->seed =
(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];
memcpy(diff, &buf[sizeof(uint64_t) + 1], sizeof(uint64_t));
ginfo->countdown =
(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];
memcpy(diff, &buf[sizeof(uint64_t) * 2 + 1], sizeof(uint64_t));
ginfo->gameType =
(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(Chat* chat, char* buf, const uint32_t buflen) {

View File

@ -48,12 +48,8 @@ namespace netprot {
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;
uint64_t sid = 0,
tid = 0;
};
struct PlayerInfo { // cli <-> srv TCP once
@ -85,7 +81,6 @@ namespace netprot {
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
@ -96,7 +91,6 @@ namespace netprot {
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

View File

@ -1,29 +1,27 @@
#include "connection.h"
Connection::Connection(in_addr addr,
std::string name,
uint64_t id,
uint64_t self_id,
uint64_t team_id):
m_addr(addr),
m_id(id),
m_sid(self_id),
m_tid(team_id),
m_name(name) {
Connection::Connection(SOCKET sock,
sockaddr_in sockaddr,
netprot::LoginInfo log,
netprot::PlayerInfo play):
m_sock(sock),
m_addr(sockaddr),
m_loginfo(log),
m_playinfo(play) {
}
Connection::~Connection() {
closesocket(m_sock);
}
in_addr Connection::GetAddr() const { return m_addr; }
uint64_t Connection::GetHash(bool self) const { return self? m_loginfo.sid: m_playinfo.id; }
uint64_t Connection::GetHash(bool self) const { return self? m_sid: m_id; }
uint64_t Connection::GetTeamHash() const { return m_loginfo.tid; }
uint64_t Connection::GetTeamHash() const { return m_tid; }
std::string Connection::GetName() const { return m_name; }
std::string Connection::GetName() const { return m_loginfo.name; }
void Connection::AddInput(netprot::Input in) {
m_input_manifest.insert({ in.timestamp, in });
@ -42,7 +40,7 @@ netprot::Sync Connection::getSync(Timestamp time) {
if (out != m_output_manifest.end()) {
sync.timestamp = out->second.timestamp;
sync.position = out->second.position;
sync.sid = m_sid;
sync.sid = m_loginfo.sid;
}
return sync;
}

View File

@ -10,16 +10,14 @@
class Connection {
public:
Connection(
in_addr addr,
std::string name,
uint64_t hash,
uint64_t self_hash,
uint64_t team_hash);
SOCKET sock,
sockaddr_in sockaddr,
netprot::LoginInfo log,
netprot::PlayerInfo play);
~Connection();
Player* player = nullptr;
in_addr GetAddr() const;
uint64_t GetHash(bool self = true) const;
uint64_t GetTeamHash() const;
std::string GetName() const;
@ -32,11 +30,12 @@ public:
private:
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,
m_tid;
std::string m_name;
std::map<Timestamp, netprot::Chat> m_chatlog;
SOCKET m_sock;
sockaddr_in m_addr;
netprot::LoginInfo m_loginfo;
netprot::PlayerInfo m_playinfo;
};
#endif

View File

@ -76,6 +76,7 @@ int Server::Ready() {
char* buf = new char[150];
uint32_t buflen = 150;
bool readystart = false;
srand(time(NULL));
Log("À l'écoute sur le port: " + std::to_string(SRV_PORT), false, false);
@ -87,23 +88,35 @@ int Server::Ready() {
if (sock < 0)
Log("Erreur de connexion", true, false);
else if (sock > 0) {
std::string str = "Nouvelle connection provenant de: ";
char* strbuf = new char[150];
uint32_t strbuflen = 150;
std::cout << inet_ntop(AF_INET, &sockad.sin_addr, strbuf, strbuflen) << ':' << std::to_string(sockad.sin_port) << std::endl;;
str.append(inet_ntop(AF_INET, &sockad.sin_addr, strbuf, strbuflen)).append(": ").append(std::to_string(sockad.sin_port));
if (recv(sock, buf, buflen, 0) > 0) {
netprot::LoginInfo log;
if (netprot::Deserialize(&log, buf, buflen)) {
std::string str;
log.sid = 8675309; // EIGHT SIX SEVENFIVE THREE AUGHT NIIIIIIIIIiiIIIIiINE!
log.sid = ((uint64_t)rand() << 25) % 8675309; // EIGHT SIX SEVENFIVE THREE AUGHT NIIIIIIIIIiiIIIIiINE!
log.tid = 123456789;
str.append(" Nom: ").append(log.name);
Log(str, false, false);
str = "";
str.append(log.name).append(": ").append(std::to_string(log.sid));
str.append(log.name).append(" SID: [").append(std::to_string(log.sid).append("]"));
Log(str, false, false);
netprot::Serialize(&log, &buf, &buflen);
send(sock, buf, buflen, 0);
std::cin.getline(nullptr, 1);
buflen = 150;
Sleep(300);
netprot::GameInfo ginfo;
ginfo.countdown = 360;
ginfo.gameType = 1;
ginfo.seed = 9370707;
netprot::Serialize(&ginfo, &buf, &buflen);
send(sock, buf, buflen, 0);
Sleep(10000);
readystart = true;
}
}

View File

@ -44,44 +44,58 @@ int Connector::Init() {
int Connector::Connect(char* srv_addr, std::string name) {
sockaddr_in add;
add.sin_family = AF_INET;
add.sin_port = htons(SRV_PORT);
m_srvsockaddr.sin_family = AF_INET;
m_srvsockaddr.sin_port = htons(SRV_PORT);
if (inet_pton(AF_INET, srv_addr, &add.sin_addr) <= 0) {
if (inet_pton(AF_INET, srv_addr, &m_srvsockaddr.sin_addr) <= 0) {
std::cout << "Addresse serveur invalide." << std::endl;
return 1;
}
if (connect(m_sock_tcp, (sockaddr*)&add, sizeof(add)) < 0) {
if (connect(m_sock_tcp, (sockaddr*)&m_srvsockaddr, sizeof(m_srvsockaddr)) < 0) {
std::cout << "Échec de la connexion." << std::endl;
}
char* buf = new char[150];
uint32_t buflen = 150;
netprot::LoginInfo log, retlog;
log.sid = 0;
netprot::LoginInfo log;
memcpy(&log.name, name.c_str(), name.size() + 1);
netprot::Serialize(&log, &buf, &buflen);
int se = send(m_sock_tcp, buf, buflen, 0);
delete[] buf;
buf = new char[150] {0};
buflen = 150;
int rpack = 0;
while (recv(m_sock_tcp, buf, buflen, 0) < se) {}
while (rpack < 2) {
recv(m_sock_tcp, buf, buflen, 0);
if (!netprot::Deserialize(&retlog, buf, buflen)) {
std::cout << "Packet invalide." << std::endl;
return 2;
switch (netprot::getType(buf, buflen)) {
case netprot::PACKET_TYPE::LOGINF:
if (!netprot::Deserialize(&m_loginfo, buf, buflen)) {
std::cout << "Packet LoginInfo invalide." << std::endl;
return 2;
}
++rpack;
break;
case netprot::PACKET_TYPE::GAMEINFO:
if (!netprot::Deserialize(&m_gameinfo, buf, buflen)) {
std::cout << "Packet GameInfo invalide." << std::endl;
return 3;
}
++rpack;
break;
default:
std::cout << "Packet invalide." << std::endl;
break;
}
}
m_name = retlog.name;
m_sid = retlog.sid;
return 0;
}
uint64_t Connector::getId() const { return m_sid; }
uint64_t Connector::getId() const { return m_loginfo.sid; }
unsigned int Connector::getSeed() const { return m_seed; }
unsigned int Connector::getSeed() const { return m_gameinfo.seed; }

View File

@ -21,12 +21,16 @@ private:
#ifdef _WIN32
WSADATA m_wsaData;
#endif
std::map<Timestamp, netprot::Input> m_inputmanifest;
std::map<uint64_t, netprot::PlayerInfo> m_players;
std::map<uint64_t, netprot::TeamInfo> m_teams;
netprot::LoginInfo m_loginfo;
netprot::GameInfo m_gameinfo;
sockaddr_in m_srvsockaddr;
SOCKET m_sock_udp = 0,
m_sock_tcp = 0;
std::string m_name = "";
uint64_t m_sid = 0,
m_tid = 0;
unsigned int m_seed = 0;
m_sock_tcp = 0;
};
#endif

View File

@ -53,7 +53,8 @@ void Engine::Init() {
if (!m_conn.Connect(SRV_ADDR, playname)) {
// setup jeu en réseau.
std::cout << "ID reçu du serveur: " << std::to_string(m_conn.getId()) << "!" << std::endl;
//seed = m_conn.getSeed();
std::cout << "Seed reçu du serveur: " << std::to_string(m_conn.getSeed()) << "!" << std::endl;
seed = m_conn.getSeed();
}
else std::cout << "Erreur de connexion." << std::endl;
}