Poll McSocket

This commit is contained in:
MarcEricMartel 2023-11-06 13:56:12 -05:00
parent ac9174f793
commit 9b9e2b3f1f
9 changed files with 43 additions and 20 deletions

View File

@ -7,7 +7,7 @@ Bullet::Bullet(Vector3f pos, Vector3f dir, uint64_t tid): m_startpos(pos), m_cur
Bullet::~Bullet() {} Bullet::~Bullet() {}
bool Bullet::Update(World* world, float elapsedtime, int perframe, std::map<uint64_t, Player*> mapPlayer) { bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordered_map<uint64_t, Player*> mapPlayer) {
int max = 100 / perframe; int max = 100 / perframe;
float damage = 0.057f; float damage = 0.057f;
for (int x = 0; x < max; ++x) { for (int x = 0; x < max; ++x) {

View File

@ -1,9 +1,9 @@
#ifndef BULLET_H__ #ifndef BULLET_H__
#define BULLET_H__ #define BULLET_H__
#include <unordered_map>
#include "define.h" #include "define.h"
#include "vector3.h" #include "vector3.h"
#include <map>
#include "player.h" #include "player.h"
@ -16,7 +16,7 @@ public:
Bullet(Vector3f pos, Vector3f dir, uint64_t tid); Bullet(Vector3f pos, Vector3f dir, uint64_t tid);
~Bullet(); ~Bullet();
bool Update(World* world, float elapsedtime, int perframe, std::map<uint64_t, Player*> m_mapPlayer); bool Update(World* world, float elapsedtime, int perframe, std::unordered_map<uint64_t, Player*> m_mapPlayer);
void Transpose(int& x, int& z); void Transpose(int& x, int& z);
Vector3f getPos() const; Vector3f getPos() const;
Vector3f getVel() const; Vector3f getVel() const;

View File

@ -46,6 +46,7 @@ typedef uint64_t Timestamp;
#include <cstdio> #include <cstdio>
#include <ctime> #include <ctime>
#define poll WSAPoll
#define flag_t u_long #define flag_t u_long
#define addrlen_t int #define addrlen_t int
#define ioctl ioctlsocket #define ioctl ioctlsocket

View File

@ -950,7 +950,7 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) {
return pck; return pck;
} }
std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) { std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf) {
std::vector<char*> lsPck; std::vector<char*> lsPck;
int len = buf->tmp? buf->tmp - buf->ptr: 0, int len = buf->tmp? buf->tmp - buf->ptr: 0,
end = 0; end = 0;
@ -958,8 +958,18 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
* next = buf->tmp ? buf->tmp + 1: buf->ptr, * next = buf->tmp ? buf->tmp + 1: buf->ptr,
* last = buf->tmp ? buf->tmp: buf->ptr; * last = buf->tmp ? buf->tmp: buf->ptr;
bool ended = true; bool ended = true;
struct pollfd fds[1];
fds[0].fd = sock;
fds[0].events = POLLIN;
while (true) { 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); 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 (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) if (ended)
@ -983,15 +993,24 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
cursor = (char*)memchr(next, '\r', end); cursor = (char*)memchr(next, '\r', end);
if (cursor) { if (cursor) {
next = cursor;
cursor--; cursor--;
next = cursor + 1;
cmp = memcmp(cursor, Footer, sizeof(uint32_t)); cmp = memcmp(cursor, Footer, sizeof(uint32_t));
if (cmp == 0) { if (cmp == 0) {
lsPck.push_back(last); if (!outbuf) {
cursor += sizeof(uint32_t); lsPck.push_back(last);
last = cursor; cursor += sizeof(uint32_t);
next = cursor + 1; 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 { else {

View File

@ -172,7 +172,7 @@ namespace netprot {
template <class T> void sendPack(SOCKET sock, T* pack, Buffer* buf); template <class T> void sendPack(SOCKET sock, T* pack, Buffer* buf);
template <class T> void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad); template <class T> void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad);
std::vector<char*> recvPacks(SOCKET sock, Buffer* buf); std::vector<char*> recvPacks(SOCKET sock, Buffer* buf, Buffer* oufbuf = nullptr);
/* Templates */ /* Templates */

View File

@ -2,7 +2,7 @@
#define CONNECTION_H__ #define CONNECTION_H__
#include <deque> #include <deque>
#include <map> #include <unordered_map>
#include "../SQCSim-common/player.h" #include "../SQCSim-common/player.h"
#include "../SQCSim-common/vector3.h" #include "../SQCSim-common/vector3.h"
#include "../SQCSim-common/netprotocol.h" #include "../SQCSim-common/netprotocol.h"
@ -34,9 +34,9 @@ public:
void CleanInputManifest(Timestamp time); void CleanInputManifest(Timestamp time);
private: private:
std::map<Timestamp, Input> m_input_manifest; std::unordered_map<Timestamp, Input> m_input_manifest;
std::map<Timestamp, Output> m_output_manifest; std::unordered_map<Timestamp, Output> m_output_manifest;
std::map<Timestamp, Chat> m_chatlog; std::unordered_map<Timestamp, Chat> m_chatlog;
SOCKET m_sock; SOCKET m_sock;
sockaddr_in m_addr; sockaddr_in m_addr;

View File

@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include <set> #include <set>
#include <string> #include <string>
#include <unordered_map>
#include "../SQCSim-common/world.h" #include "../SQCSim-common/world.h"
#include "../SQCSim-common/netprotocol.h" #include "../SQCSim-common/netprotocol.h"
#include "define.h" #include "define.h"
@ -35,8 +36,8 @@ private:
Buffer m_buf; Buffer m_buf;
std::map<uint64_t, std::unique_ptr<Connection>> m_players; std::unordered_map<uint64_t, std::unique_ptr<Connection>> m_players;
std::map <Timestamp, Chat> m_chatlog; std::unordered_map<Timestamp, Chat> m_chatlog;
std::vector<uint64_t> m_ids; std::vector<uint64_t> m_ids;
GameInfo m_game; GameInfo m_game;

View File

@ -2,6 +2,7 @@
#define CONNECTOR_H__ #define CONNECTOR_H__
#include <stdlib.h> #include <stdlib.h>
#include <unordered_map>
#include "../SQCSim-common/netprotocol.h" #include "../SQCSim-common/netprotocol.h"
#include "define.h" #include "define.h"
@ -26,13 +27,13 @@ public:
// void updateRemotePlayers(std::map<RemotePlayers> rplayers); // void updateRemotePlayers(std::map<RemotePlayers> rplayers);
std::map<uint64_t, netprot::PlayerInfo> m_players; std::unordered_map<uint64_t, netprot::PlayerInfo> m_players;
private: private:
#ifdef _WIN32 #ifdef _WIN32
WSADATA m_wsaData; WSADATA m_wsaData;
#endif #endif
std::map<Timestamp, netprot::Input> m_inputmanifest; std::unordered_map<Timestamp, netprot::Input> m_inputmanifest;
std::map<uint64_t, netprot::TeamInfo> m_teams; std::unordered_map<uint64_t, netprot::TeamInfo> m_teams;
netprot::LoginInfo m_loginfo; netprot::LoginInfo m_loginfo;
netprot::GameInfo m_gameinfo; netprot::GameInfo m_gameinfo;

View File

@ -4,6 +4,7 @@
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
#include <cmath> #include <cmath>
#include <unordered_map>
#include "../SQCSim-common/array2d.h" #include "../SQCSim-common/array2d.h"
#include "../SQCSim-common/blockinfo.h" #include "../SQCSim-common/blockinfo.h"
#include "../SQCSim-common/bullet.h" #include "../SQCSim-common/bullet.h"
@ -88,7 +89,7 @@ private:
Bullet* m_bullets[MAX_BULLETS]; Bullet* m_bullets[MAX_BULLETS];
std::map<uint64_t, Player*> m_players; std::unordered_map<uint64_t, Player*> m_players;
netprot::Buffer m_buf; netprot::Buffer m_buf;
std::chrono::high_resolution_clock::time_point m_startTime; std::chrono::high_resolution_clock::time_point m_startTime;