diff --git a/SQCSim-common/bullet.cpp b/SQCSim-common/bullet.cpp index 09dde11..9b26861 100644 --- a/SQCSim-common/bullet.cpp +++ b/SQCSim-common/bullet.cpp @@ -7,7 +7,7 @@ Bullet::Bullet(Vector3f pos, Vector3f dir, uint64_t tid): m_startpos(pos), m_cur Bullet::~Bullet() {} -bool Bullet::Update(World* world, float elapsedtime, int perframe, std::map mapPlayer) { +bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordered_map mapPlayer) { int max = 100 / perframe; float damage = 0.057f; for (int x = 0; x < max; ++x) { diff --git a/SQCSim-common/bullet.h b/SQCSim-common/bullet.h index cc04737..04bbfad 100644 --- a/SQCSim-common/bullet.h +++ b/SQCSim-common/bullet.h @@ -1,9 +1,9 @@ #ifndef BULLET_H__ #define BULLET_H__ +#include #include "define.h" #include "vector3.h" -#include #include "player.h" @@ -16,7 +16,7 @@ public: Bullet(Vector3f pos, Vector3f dir, uint64_t tid); ~Bullet(); - bool Update(World* world, float elapsedtime, int perframe, std::map m_mapPlayer); + bool Update(World* world, float elapsedtime, int perframe, std::unordered_map m_mapPlayer); void Transpose(int& x, int& z); Vector3f getPos() const; Vector3f getVel() const; diff --git a/SQCSim-common/define.h b/SQCSim-common/define.h index a5441d4..48a4f4c 100644 --- a/SQCSim-common/define.h +++ b/SQCSim-common/define.h @@ -46,6 +46,7 @@ typedef uint64_t Timestamp; #include #include +#define poll WSAPoll #define flag_t u_long #define addrlen_t int #define ioctl ioctlsocket diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index 1c4177c..fbc5fb5 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -950,7 +950,7 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) { return pck; } -std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { +std::vector netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf) { std::vector lsPck; int len = buf->tmp? buf->tmp - buf->ptr: 0, end = 0; @@ -958,8 +958,18 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { * next = buf->tmp ? buf->tmp + 1: buf->ptr, * last = buf->tmp ? buf->tmp: buf->ptr; bool ended = true; + struct pollfd fds[1]; + + fds[0].fd = sock; + fds[0].events = POLLIN; while (true) { + if (!poll(fds, 1, 0)) { + if (ended) + buf->tmp = nullptr; + return lsPck; + } + int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0); if (bytes <= 0) { // si recv() retourne -1 ou 0; ça veut dire qu'il y a plus rien a lire qui n'a pas déjà été traité. if (ended) @@ -983,15 +993,24 @@ std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) { cursor = (char*)memchr(next, '\r', end); if (cursor) { + next = cursor; cursor--; - next = cursor + 1; cmp = memcmp(cursor, Footer, sizeof(uint32_t)); if (cmp == 0) { - lsPck.push_back(last); - cursor += sizeof(uint32_t); - last = cursor; - next = cursor + 1; + if (!outbuf) { + lsPck.push_back(last); + cursor += sizeof(uint32_t); + last = cursor; + next = cursor + 1; + } + else { + memcpy(&outbuf->ptr[cursor - last], last, cursor - last); + lsPck.push_back(&outbuf->ptr[cursor - last]); + cursor += sizeof(uint32_t); + last = cursor; + next = cursor + 1; + } } } else { diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index 45c9f9e..432e31d 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -172,7 +172,7 @@ namespace netprot { template void sendPack(SOCKET sock, T* pack, Buffer* buf); template void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad); - std::vector recvPacks(SOCKET sock, Buffer* buf); + std::vector recvPacks(SOCKET sock, Buffer* buf, Buffer* oufbuf = nullptr); /* Templates */ diff --git a/SQCSim-srv/connection.h b/SQCSim-srv/connection.h index b7a69ee..9233273 100644 --- a/SQCSim-srv/connection.h +++ b/SQCSim-srv/connection.h @@ -2,7 +2,7 @@ #define CONNECTION_H__ #include -#include +#include #include "../SQCSim-common/player.h" #include "../SQCSim-common/vector3.h" #include "../SQCSim-common/netprotocol.h" @@ -34,9 +34,9 @@ public: void CleanInputManifest(Timestamp time); private: - std::map m_input_manifest; - std::map m_output_manifest; - std::map m_chatlog; + std::unordered_map m_input_manifest; + std::unordered_map m_output_manifest; + std::unordered_map m_chatlog; SOCKET m_sock; sockaddr_in m_addr; diff --git a/SQCSim-srv/server.h b/SQCSim-srv/server.h index 10b7709..ee14723 100644 --- a/SQCSim-srv/server.h +++ b/SQCSim-srv/server.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "../SQCSim-common/world.h" #include "../SQCSim-common/netprotocol.h" #include "define.h" @@ -35,8 +36,8 @@ private: Buffer m_buf; - std::map> m_players; - std::map m_chatlog; + std::unordered_map> m_players; + std::unordered_map m_chatlog; std::vector m_ids; GameInfo m_game; diff --git a/SQCSim2021/connector.h b/SQCSim2021/connector.h index 4fd883e..c5361ac 100644 --- a/SQCSim2021/connector.h +++ b/SQCSim2021/connector.h @@ -2,6 +2,7 @@ #define CONNECTOR_H__ #include +#include #include "../SQCSim-common/netprotocol.h" #include "define.h" @@ -26,13 +27,13 @@ public: // void updateRemotePlayers(std::map rplayers); - std::map m_players; + std::unordered_map m_players; private: #ifdef _WIN32 WSADATA m_wsaData; #endif - std::map m_inputmanifest; - std::map m_teams; + std::unordered_map m_inputmanifest; + std::unordered_map m_teams; netprot::LoginInfo m_loginfo; netprot::GameInfo m_gameinfo; diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h index efd8892..41b8a4f 100644 --- a/SQCSim2021/engine.h +++ b/SQCSim2021/engine.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "../SQCSim-common/array2d.h" #include "../SQCSim-common/blockinfo.h" #include "../SQCSim-common/bullet.h" @@ -88,7 +89,7 @@ private: Bullet* m_bullets[MAX_BULLETS]; - std::map m_players; + std::unordered_map m_players; netprot::Buffer m_buf; std::chrono::high_resolution_clock::time_point m_startTime;