About a lucky man who made the grade

This commit is contained in:
MarcEricMartel 2023-10-28 12:03:22 -04:00
parent 42d612ad33
commit 3f8d860b93
2 changed files with 56 additions and 75 deletions

View File

@ -74,29 +74,39 @@ namespace netprot {
uint16_t ammo = 0;
uint8_t hp = 0;
Vector3f position;
Sync() {}
Sync(Sync* sync) : timestamp(sync->timestamp), sid(sync->sid), timer(sync->timer), ammo(sync->ammo), hp(sync->hp), position(sync->position) {}
};
struct TeamInfo { // cli <-> srv TCP once
char name[32];
uint64_t id = 0;
TeamInfo() {}
TeamInfo(TeamInfo* tem) : id(tem->id) { strcpy_s(tem->name, name); }
};
struct LoginInfo { // cli <-> srv TCP once
char name[32];
uint64_t sid = 0,
tid = 0;
LoginInfo() {}
LoginInfo(LoginInfo* ply): sid(ply->sid), tid(ply->tid) { strcpy_s(ply->name, name); }
};
struct PlayerInfo { // cli <-> srv TCP once
char name[32];
uint64_t id = 0,
tid = 0;
PlayerInfo() {}
PlayerInfo(PlayerInfo* log) : id(log->id), tid(log->tid) { strcpy_s(log->name, name); }
};
struct GameInfo { // cli <-> srv TCP event (before game start)/ once
uint64_t seed;
uint32_t countdown;
uint8_t gameType; // TOOD: enum.
uint8_t gameType; // TODD: enum.
GameInfo() {}
GameInfo(GameInfo* gam) : seed(gam->seed), countdown(gam->countdown), gameType(gam->gameType) {}
};
struct Chat { // cli <-> srv TCP event
@ -104,11 +114,15 @@ namespace netprot {
dest_id = 0,
dest_team_id = 0;
char mess[140]; // Good 'nough for twitr, good 'nough for me.
Chat() {}
Chat(Chat* cha) : src_id(cha->src_id), dest_id(cha->dest_id), dest_team_id(cha->dest_team_id) { strcpy_s(cha->mess, mess); }
};
struct ErrorLog { // srv -> cli TCP event
char mess[140];
bool is_fatal;
ErrorLog() {};
ErrorLog(ErrorLog* err) : is_fatal(err->is_fatal) { strcpy_s(err->mess, mess); }
};
void Serialize(Input* in, char* buf[], uint32_t* buflen); // cli
@ -138,7 +152,7 @@ namespace netprot {
bool emptyPack(Packet pck);
Packet makePack(void* ptr, PACKET_TYPE type); // Pour pouvoir faire une liste de stock à supprimer sans avoir à en faire une pour chaque type.
Packet makePack(void* ptr, PACKET_TYPE type); // Pour pouvoir faire une liste de stock a supprimer sans avoir a en faire une pour chaque type.
template <class T> void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen);
template <class T> void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad);
@ -151,8 +165,8 @@ namespace netprot {
template <class T>
void sendPack(SOCKET sock, T* pack, char** buf, uint32_t* buflen) {
netprot::Serialize(pack, buf, buflen);
memset(buf->ptr[buf->len], , '\0', sizeof(uint64_t) + sizeof(uint8_t));
buf->len += sizeof(uint64_t) + sizeof(uint8_t)
memset(&buf[*buflen], '\0', sizeof(uint64_t) + sizeof(uint8_t));
buflen += sizeof(uint64_t) + sizeof(uint8_t);
send(sock, *buf, *buflen, 0);
*buflen = BUFFER_LENGTH;
}
@ -161,8 +175,8 @@ namespace netprot {
void sendPackTo(SOCKET sock, T* pack, char** buf, uint32_t* buflen, sockaddr_in* sockad) {
sockaddr_in addr = *sockad;
netprot::Serialize(pack, buf, buflen);
memset(buf->ptr[buf->len], , '\0', sizeof(uint64_t) + sizeof(uint8_t));
buf->len += sizeof(uint64_t) + sizeof(uint8_t)
memset(&buf[*buflen], '\0', sizeof(uint64_t) + sizeof(uint8_t));
buflen += sizeof(uint64_t) + sizeof(uint8_t);
sendto(sock, *buf, *buflen, 0, (sockaddr*)&addr, sizeof(addr));
*buflen = BUFFER_LENGTH;
}
@ -170,8 +184,8 @@ namespace netprot {
template <class T>
void sendPack(SOCKET sock, T* pack, Buffer* buf) {
netprot::Serialize(pack, &buf->ptr, &buf->len);
memset(buf->ptr[buf->len], , '\0', sizeof(uint64_t) + sizeof(uint8_t));
buf->len += sizeof(uint64_t) + sizeof(uint8_t)
memset(&buf->ptr[buf->len], '\0', sizeof(uint64_t) + sizeof(uint8_t));
buf->len += sizeof(uint64_t) + sizeof(uint8_t);
send(sock, buf->ptr, buf->len, 0);
buf->rstLen();
}
@ -180,8 +194,8 @@ namespace netprot {
void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad) {
sockaddr_in addr = *sockad;
netprot::Serialize(pack, &buf->ptr, &buf->len);
memset(buf->ptr[buf->len], ,'\0', sizeof(uint64_t) + sizeof(uint8_t));
buf->len += sizeof(uint64_t) + sizeof(uint8_t)
memset(&buf->ptr[buf->len], '\0', sizeof(uint64_t) + sizeof(uint8_t));
buf->len += sizeof(uint64_t) + sizeof(uint8_t);
sendto(sock, buf->ptr, buf->len, 0, (sockaddr*)&addr, sizeof(addr));
buf->rstLen();
}

View File

@ -62,87 +62,54 @@ int Connector::Connect(const char* srv_addr, std::string name) {
return 3;
}
char* buf = new char[1500];
uint32_t buflen = 1500;
netprot::Buffer bf;
netprot::LoginInfo log;
memcpy(&log.name, name.c_str(), name.size() + 1);
strcpy_s(log.name, name.c_str());
netprot::Serialize(&log, &buf, &buflen);
netprot::sendPack(m_sock_tcp, &log, &bf);
int se = send(m_sock_tcp, buf, buflen, 0);
delete[] buf;
buf = new char[150] {0};
buflen = 150;
bool ready = false;
int errors = 0;
int bytes = 0, ptrbuf = 0;
char* currbuf = new char[150];
netprot::PlayerInfo pinfo{};
std::vector<netprot::Packet> lsPck;
while (!ready) {
int len = 0;
bool is_fullpack = false;
lsPck = netprot::recvPacks(m_sock_tcp, &bf);
do {
len = recv(m_sock_tcp, &buf[bytes], buflen, 0);
bytes += len;
buflen -= bytes;
if (lsPck.empty())
errors++;
for (int i = 0; i < buflen; ++i) {
// TODOTODOTODOSAINT-TROPEZ
}
} while (!is_fullpack);
switch (netprot::getType(currbuf, buflen)) {
case netprot::PACKET_TYPE::LOGINF:
if (bytes < sizeof(netprot::LoginInfo))
for (auto& pck : lsPck) {
bool bypass_delete = false;
netprot::PlayerInfo* pl = nullptr;
switch (pck.type) {
using enum netprot::PACKET_TYPE;
case LOGINF:
m_loginfo = netprot::LoginInfo((netprot::LoginInfo*)pck.ptr);
break;
if (!netprot::Deserialize(&m_loginfo, currbuf, buflen)) {
std::cout << "Packet LoginInfo invalide." << std::endl;
return 2;
}
currbuf = buf + sizeof(netprot::LoginInfo) + 2;
//ptrbuf += sizeof(netprot::LoginInfo) + 2;
case GAMEINFO:
m_gameinfo = netprot::GameInfo((netprot::GameInfo*)pck.ptr);
break;
case netprot::PACKET_TYPE::GAMEINFO:
if (bytes < sizeof(netprot::GameInfo))
case PLAYINF:
pl = (netprot::PlayerInfo*)pck.ptr;
m_players[pl->id] = pl;
bypass_delete = true;
break;
if (!netprot::Deserialize(&m_gameinfo, currbuf, buflen)) {
std::cout << "Packet GameInfo invalide." << std::endl;
return 4;
}
currbuf = buf + sizeof(netprot::GameInfo) + 2;
//ptrbuf += sizeof(netprot::GameInfo) + 2;
case TEAMINF:
// TODO: Faire dequoi avec TeamInfo si on fini par avoir des teams.
break;
case netprot::PACKET_TYPE::PLAYINF:
if (bytes < sizeof(netprot::PlayerInfo))
break;
if (!netprot::Deserialize(&pinfo, currbuf, buflen)) {
std::cout << "Packet PlayerInfo invalide." << std::endl;
return 4;
}
m_players[pinfo.id] = pinfo;
currbuf = buf + sizeof(netprot::PlayerInfo) + 2;
//ptrbuf += sizeof(netprot::PlayerInfo) + 2;
break;
case netprot::PACKET_TYPE::SYNC:
if (bytes < sizeof(netprot::Sync))
break;
if (!netprot::Deserialize(&m_origin, currbuf, buflen)) {
std::cout << "Packet Sync invalide." << std::endl;
return 4;
}
case SYNC:
m_origin = netprot::Sync((netprot::Sync*)pck.ptr);
ready = true;
break;
default:
++errors;
//std::cout << "Packet invalide." << std::endl;
errors++;
break;
}
if (!bypass_delete)
netprot::emptyPack(pck);
}
lsPck.clear();
if (errors > 100)
return 5;
return 4;
}
return 0;
}