diff --git a/SQCSim-srv/connection.cpp b/SQCSim-srv/connection.cpp index c721255..4ac9968 100644 --- a/SQCSim-srv/connection.cpp +++ b/SQCSim-srv/connection.cpp @@ -47,19 +47,19 @@ PlayerInfo* Connection::getInfo() const { return (PlayerInfo*)&m_playinfo; } sockaddr_in* Connection::getAddr() const { return (sockaddr_in*)&m_addr; } -void Connection::getPacks() { +void Connection::getPacks(SOCKET sock) { std::vector lsPck; Input in; while (true) { - lsPck = recvPacksFrom(m_sock, m_buf, m_addr); + lsPck = recvPacksFrom(sock, &m_buf, m_addr); for (auto& pck : lsPck) { - uint32_t bsize = m_buf->len - (pck - m_buf->ptr); + uint32_t bsize = m_buf.len - (pck - m_buf.ptr); switch (netprot::getType(pck, 1)) { using enum netprot::PACKET_TYPE; case INPUT: if (Deserialize(&in, pck, &bsize)) - m_input_manifest.insert({ in.timestamp, in }); + m_input_manifest[in.timestamp] = in; break; default: break; } @@ -68,7 +68,17 @@ void Connection::getPacks() { } } -std::thread Connection::Start(){ return std::thread(getPacks); } +void Connection::sendPacks(SOCKET sock, std::unordered_map conns) { + while (m_last_out < m_output_manifest.size()) { + Output out = m_output_manifest.at(m_last_out++); + + for (auto& [key, conn] : conns) { + if (m_playinfo.id == conn->GetHash(true)) + continue; + sendPackTo(sock, &out, &m_bufout, conn->getAddr()); + } + } +} void Connection::Run(World* world) { Input in, last; @@ -78,17 +88,27 @@ void Connection::Run(World* world) { if (m_input_manifest.size() < 2) return; - in = m_input_manifest.at(m_input_manifest.size()); - last = m_input_manifest.at(m_input_manifest.size() - 1); + while (m_last_in < m_input_manifest.size()) { + in = m_input_manifest.at(m_last_in + 1); + last = m_input_manifest.at(m_last_in); - el = (float)(in.timestamp - last.timestamp) / 1000.; + el = (float)(in.timestamp - last.timestamp) / 1000.; + player.get()->SetDirection(in.direction); + player.get()->ApplyPhysics(player.get()->GetInput(in.keys.forward, + in.keys.backward, + in.keys.left, + in.keys.right, + in.keys.jump, false, el), world, el); - player.get()->SetDirection(in.direction); - player.get()->ApplyPhysics(player.get()->GetInput(in.keys.forward, in.keys.backward, in.keys.left, in.keys.right, in.keys.jump, false, el), world, el); + out.position = player.get()->GetPosition(); + out.direction = in.direction; + out.timestamp = in.timestamp; + out.id = m_playinfo.id; - out.position = player.get()->GetPosition(); - out.direction = in.direction; - out.timestamp = in.timestamp; + m_output_manifest[out.timestamp] = out; + + ++m_last_in; + } } void Connection::CleanInputManifest(Timestamp time) { diff --git a/SQCSim-srv/connection.h b/SQCSim-srv/connection.h index 0d96bba..af9c2df 100644 --- a/SQCSim-srv/connection.h +++ b/SQCSim-srv/connection.h @@ -33,9 +33,8 @@ public: PlayerInfo* getInfo() const; sockaddr_in* getAddr() const; - void getPacks(); - - std::thread Start(); + void getPacks(SOCKET sock); + void sendPacks(SOCKET sock, std::unordered_map conns); void Run(World* world); @@ -50,6 +49,11 @@ private: LoginInfo m_loginfo; PlayerInfo m_playinfo; - Buffer* m_buf; + Timestamp m_startsync; + + uint64_t m_last_in = 0, + m_last_out = 0; + Buffer m_buf, + m_bufout; }; #endif diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index 72d53b4..e724269 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -155,7 +155,7 @@ int Server::Ready() { play.tid = log->tid; sendPack(sock, &m_game, &m_buf); - std::unique_ptr conn = std::make_unique(sock, sockad, *log, play); + Connection* conn = new Connection(sock, sockad, *log, play); for (auto& [key, player] : m_players) { sendPack(player->getSock(), &play, &m_buf); // Envoyer les infos de joueur distant aux joueurs d�j� connect�s @@ -183,16 +183,12 @@ void Server::Run() { Log("Debut de la partie...", false, false); int players = m_players.size(); - std::thread* getPacksThreads = new std::thread[players]; - m_world = std::make_unique(); + m_world = new World(); m_world->SetSeed(m_game.seed); m_world->GetChunks().Reset(nullptr); m_world->BuildWorld(); - - for (int x = 0; x < players; ++x) - getPacksThreads[x] = m_players[x].get()->start(); - + for (auto& [key, conn] : m_players) { // Creation des instances de joueurs et premier sync. conn->player = std::make_unique(Vector3f(8.5f, CHUNK_SIZE_Y + 1.8f, 8.5f)); Sync sync; @@ -207,7 +203,9 @@ void Server::Run() { while (!endgame) { for (auto& [key, conn] : m_players) { - conn->player->GetInput + conn->getPacks(m_sock_udp); + conn->Run(m_world); + conn->sendPacks(m_sock_udp, m_players); } } diff --git a/SQCSim-srv/server.h b/SQCSim-srv/server.h index ee14723..dd5a397 100644 --- a/SQCSim-srv/server.h +++ b/SQCSim-srv/server.h @@ -36,12 +36,12 @@ private: Buffer m_buf; - std::unordered_map> m_players; + std::unordered_map m_players; std::unordered_map m_chatlog; std::vector m_ids; GameInfo m_game; - std::unique_ptr m_world = nullptr; + World* m_world = nullptr; const bool m_manual_setup = SRV_MANUAL_SETUP; std::string LogTimestamp(); diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index b5d02ad..c89c304 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -1111,6 +1111,7 @@ void Engine::Render(float elapsedTime) { using namespace std::chrono; using namespace netprot; Input input; + static std::vector lsPck; input.sid = m_conn.getId(); input.direction = m_player.GetDirection(); @@ -1123,10 +1124,27 @@ void Engine::Render(float elapsedTime) { input.keys.block = m_mouseR; input.keys.shoot = m_mouseL; - sendPackTo(m_conn.m_sock_udp, &input, &m_buf, &m_conn.m_srvsockaddr); + sendPackTo(m_conn.m_sock_udp, &input, &m_bufout, &m_conn.m_srvsockaddr); - // TODO: Faire la gestion de la réception de paquets. + lsPck = recvPacks(m_conn.m_sock_udp, &m_buf); + for (auto& pck : lsPck) { + uint32_t bsize = m_buf.len - (pck - m_buf.ptr); + netprot::Sync sync; + netprot::Output out; + switch (netprot::getType(pck, 1)) { + using enum netprot::PACKET_TYPE; + case SYNC: + if (!netprot::Deserialize(&sync, pck, &bsize)) {} + break; + case OUTPUT: + if (!netprot::Deserialize(&out, pck, &bsize)) {} + break; + default: + break; + } + } + lsPck.clear(); } } else if (m_gamestate == GameState::MAIN_MENU || m_gamestate == GameState::OPTIONS) diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h index 72f7986..49263b3 100644 --- a/SQCSim2021/engine.h +++ b/SQCSim2021/engine.h @@ -90,7 +90,7 @@ private: Bullet* m_bullets[MAX_BULLETS]; std::unordered_map m_players; - netprot::Buffer m_buf; + netprot::Buffer m_buf, m_bufout; std::chrono::high_resolution_clock::time_point m_startTime; //Menu