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() {}
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;
float damage = 0.057f;
for (int x = 0; x < max; ++x) {

View File

@ -1,9 +1,9 @@
#ifndef BULLET_H__
#define BULLET_H__
#include <unordered_map>
#include "define.h"
#include "vector3.h"
#include <map>
#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<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);
Vector3f getPos() const;
Vector3f getVel() const;

View File

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

View File

@ -950,7 +950,7 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) {
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;
int len = buf->tmp? buf->tmp - buf->ptr: 0,
end = 0;
@ -958,8 +958,18 @@ std::vector<char*> 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<char*> 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 {

View File

@ -172,7 +172,7 @@ namespace netprot {
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);
std::vector<char*> recvPacks(SOCKET sock, Buffer* buf);
std::vector<char*> recvPacks(SOCKET sock, Buffer* buf, Buffer* oufbuf = nullptr);
/* Templates */

View File

@ -2,7 +2,7 @@
#define CONNECTION_H__
#include <deque>
#include <map>
#include <unordered_map>
#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<Timestamp, Input> m_input_manifest;
std::map<Timestamp, Output> m_output_manifest;
std::map<Timestamp, Chat> m_chatlog;
std::unordered_map<Timestamp, Input> m_input_manifest;
std::unordered_map<Timestamp, Output> m_output_manifest;
std::unordered_map<Timestamp, Chat> m_chatlog;
SOCKET m_sock;
sockaddr_in m_addr;

View File

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

View File

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

View File

@ -4,6 +4,7 @@
#include <algorithm>
#include <chrono>
#include <cmath>
#include <unordered_map>
#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<uint64_t, Player*> m_players;
std::unordered_map<uint64_t, Player*> m_players;
netprot::Buffer m_buf;
std::chrono::high_resolution_clock::time_point m_startTime;