à tester

This commit is contained in:
MarcEricMartel 2023-10-25 12:16:14 -04:00
parent dd2396e5e4
commit 9b4defba2a
6 changed files with 56 additions and 46 deletions

View File

@ -4,8 +4,8 @@
Connection::Connection(SOCKET sock,
sockaddr_in sockaddr,
netprot::LoginInfo log,
netprot::PlayerInfo play):
LoginInfo log,
PlayerInfo play):
m_sock(sock),
m_addr(sockaddr),
m_loginfo(log),
@ -13,9 +13,7 @@ Connection::Connection(SOCKET sock,
}
Connection::~Connection() {
closesocket(m_sock);
}
Connection::~Connection() { closesocket(m_sock); }
uint64_t Connection::GetHash(bool self) const { return self? m_loginfo.sid: m_playinfo.id; }
@ -23,19 +21,17 @@ uint64_t Connection::GetTeamHash() const { return m_loginfo.tid; }
std::string Connection::GetName() const { return m_loginfo.name; }
void Connection::AddInput(netprot::Input in) {
m_input_manifest.insert({ in.timestamp, in });
}
void Connection::AddInput(Input in) { m_input_manifest.insert({ in.timestamp, in }); }
netprot::Output* Connection::getOutput(Timestamp time) {
Output* Connection::getOutput(Timestamp time) {
auto out = m_output_manifest.find(time);
if (out != m_output_manifest.end())
return &out->second;
return nullptr;
}
netprot::Sync Connection::getSync(Timestamp time) {
netprot::Sync sync;
Sync Connection::getSync(Timestamp time) {
Sync sync;
auto out = m_output_manifest.find(time);
if (out != m_output_manifest.end()) {
sync.timestamp = out->second.timestamp;
@ -45,10 +41,13 @@ netprot::Sync Connection::getSync(Timestamp time) {
return sync;
}
SOCKET Connection::getSock() const { return m_sock; }
PlayerInfo* Connection::getInfo() const { return (PlayerInfo*)&m_playinfo; }
void Connection::CleanInputManifest(Timestamp time) {
auto wat = m_input_manifest.find(time);
while (wat != m_input_manifest.begin())
m_input_manifest.erase(wat--);
}

View File

@ -8,13 +8,15 @@
#include "../SQCSim-common/netprotocol.h"
#include "define.h"
using namespace netprot;
class Connection {
public:
Connection(
SOCKET sock,
sockaddr_in sockaddr,
netprot::LoginInfo log,
netprot::PlayerInfo play);
LoginInfo log,
PlayerInfo play);
~Connection();
Player* player = nullptr;
@ -23,20 +25,22 @@ public:
uint64_t GetTeamHash() const;
std::string GetName() const;
void AddInput(netprot::Input in);
netprot::Output* getOutput(Timestamp time);
netprot::Sync getSync(Timestamp time);
void AddInput(Input in);
Output* getOutput(Timestamp time);
Sync getSync(Timestamp time);
SOCKET getSock() const;
PlayerInfo* getInfo() const;
void CleanInputManifest(Timestamp time);
private:
std::map<Timestamp, netprot::Input> m_input_manifest;
std::map<Timestamp, netprot::Output> m_output_manifest;
std::map<Timestamp, netprot::Chat> m_chatlog;
std::map<Timestamp, Input> m_input_manifest;
std::map<Timestamp, Output> m_output_manifest;
std::map<Timestamp, Chat> m_chatlog;
SOCKET m_sock;
sockaddr_in m_addr;
netprot::LoginInfo m_loginfo;
netprot::PlayerInfo m_playinfo;
LoginInfo m_loginfo;
PlayerInfo m_playinfo;
};
#endif

View File

@ -114,45 +114,49 @@ int Server::Ready() {
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;
netprot::PlayerInfo play;
LoginInfo* log;
PlayerInfo play;
netprot::Packet pck = netprot::getPack(buf, buflen);
if (pck.type != netprot::PACKET_TYPE::LOGINF) {
if (pck.type != netprot::PACKET_TYPE::ERR)
netprot::emptyPack(pck);
continue;
Packet pck = getPack(buf, buflen);
if (pck.type != PACKET_TYPE::LOGINF) {
Log("Paquet invalide.", true, false);
if (pck.type != PACKET_TYPE::ERR)
emptyPack(pck);
continue; // Passer au prochain appel si c'est pas un LoginInfo ou un LoginInfo invalide qui rentre.
}
log = (netprot::LoginInfo*)pck.ptr;
log = (LoginInfo*)pck.ptr;
log->sid = getUniqueId();
log->tid = 0;
str.append(" Nom: ").append(log->name);
Log(str, false, false);
str = "";
Log(str.append(" Nom: ").append(log->name), false, false);
str.clear();
str.append(log->name).append(" SID: [").append(std::to_string(log->sid).append("]"));
Log(str, false, false);
Log(str.append(log->name).append(" SID: [").append(std::to_string(log->sid).append("]")), false, false);
netprot::sendPack<netprot::LoginInfo>(sock, log, &buf, &buflen);
sendPack<LoginInfo>(sock, log, &buf, &buflen);
play.id = getUniqueId();
memcpy(play.name, log->name, std::strlen(log->name) + 1);
strcpy_s(play.name, log->name);
play.tid = log->tid;
netprot::sendPack<netprot::GameInfo>(sock, &m_game, &buf, &buflen);
sendPack<GameInfo>(sock, &m_game, &buf, &buflen);
Connection* conn = new Connection(sock, sockad, *log, play);
// TODO: Envoyer les infos de joueur distant aux joueurs déjà connectés
// et envoyer les infos des joueurs distants au nouveau joueur.
for (auto& player : m_players) {
sendPack<PlayerInfo>(player.second->getSock(), &play, &buf, &buflen); // Envoyer les infos de joueur distant aux joueurs déjà connectés
buflen = BUFFER_LENGTH;
sendPack<PlayerInfo>(sock, player.second->getInfo(), &buf, &buflen); // et envoyer les infos des joueurs distants au nouveau joueur.
buflen = BUFFER_LENGTH;
}
m_players[log->sid] = conn;
delete log; // le pck va se supprimer tout seul, mais le pointer du log qui vient de lui, non.
if (++nbrconn >= nbrjoueurs)
readystart = true;
}
}
}
@ -166,12 +170,12 @@ void Server::Run() {
sockaddr_in sockad;
int socklen = sizeof(sockad);
Log("Partie en cours...", false, false);
Log("Debut de la partie...", false, false);
for (auto& conn : m_players) // Gérer le point de spawn des joueurs.
conn.second->player = new Player(Vector3f(64., 128., 64.));
// TODO: Faire un premier sync pour que le joueur parte à la bonne place.
// TODO: Faire un premier sync pour que les joueurs partent à la bonne place.
while (true) {
if (recvfrom(m_sock_udp, buf, BUFFER_LENGTH, 0, (sockaddr*)&sockad, &socklen) > 0) {

View File

@ -10,6 +10,8 @@
#include "define.h"
#include "connection.h"
using namespace netprot;
class Server {
public:
Server(LogDest log = LOG_DEST::CONSOLE);

View File

@ -88,6 +88,9 @@ int Connector::Connect(const char* srv_addr, std::string name) {
}
++rpack;
break;
case netprot::PACKET_TYPE::PLAYINF:
// TODO: Populer un remotePlayer avec ça.
break;
default:
++errors;
//std::cout << "Packet invalide." << std::endl;

View File

@ -32,7 +32,5 @@ private:
netprot::LoginInfo m_loginfo;
netprot::GameInfo m_gameinfo;
};
#endif