This commit is contained in:
MarcEricMartel 2023-09-27 11:24:41 -04:00
parent 8bc74624c3
commit 4dbcb08a11
8 changed files with 112 additions and 66 deletions

View File

@ -28,7 +28,8 @@ typedef uint64_t Timestamp;
#ifdef _WIN32 #ifdef _WIN32
#pragma comment(lib,"wsock32.lib") // Pour pouvoir faire fonctionner le linker sans le vcxproject #pragma comment(lib,"wsock32.lib") // Pour pouvoir faire fonctionner le linker sans le vcxproject
#pragma comment(lib,"ws2_32.lib")
#include <ws2tcpip.h>
#include <Windows.h> #include <Windows.h>
#include <cstdio> #include <cstdio>
#include <ctime> #include <ctime>

View File

@ -18,7 +18,6 @@ void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) {
void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) { void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
*buf[0] = netprot::PACKET_TYPE::LOGINF; *buf[0] = netprot::PACKET_TYPE::LOGINF;
void* nom = &linfo->name;
char* buff = *buf + 1; char* buff = *buf + 1;
size_t namesize = 1; size_t namesize = 1;
@ -28,21 +27,23 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
++namesize; ++namesize;
else break; else break;
memcpy(buff, nom, sizeof(linfo->name)); memcpy(*buf + 1, &linfo->name, namesize);
uint64_t sid = linfo->sid; uint64_t sid = linfo->sid;
char diff[sizeof(uint64_t)] = { (sid >> 56) & 0xFF, uint8_t diff[sizeof(uint64_t)] = {
(sid >> 56) & 0xFF,
(sid >> 48) & 0xFF, (sid >> 48) & 0xFF,
(sid >> 40) & 0xFF, (sid >> 40) & 0xFF,
(sid >> 32) & 0xFF, (sid >> 32) & 0xFF,
(sid >> 24) & 0xFF, (sid >> 24) & 0xFF,
(sid >> 16) & 0xFF, (sid >> 16) & 0xFF,
(sid >> 8) & 0xFF, (sid >> 8) & 0xFF,
sid & 0xFF }; sid & 0xFF
};
void* ptrsid = *buf + namesize; char* ptrsid = *buf + namesize + 2;
memcpy(ptrsid, &diff, sizeof(uint64_t)); memcpy(ptrsid, diff, sizeof(uint64_t));
*buflen = sizeof(LoginInfo) + 1; *buflen = namesize + sizeof(uint64_t) + 2;
} }
void netprot::Serialize(SelfInfo* sinfo, char* buf[], uint32_t* buflen) { void netprot::Serialize(SelfInfo* sinfo, char* buf[], uint32_t* buflen) {
@ -84,10 +85,10 @@ bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) {
} }
bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) { bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) {
if (buflen < sizeof(sizeof(LoginInfo)) + 1) if (buflen < sizeof(LoginInfo) + 3)
return false; return false;
size_t namesize = 0; size_t namesize = 1;
for (int x = 0; x < sizeof(linfo->name); ++x) for (int x = 0; x < sizeof(linfo->name); ++x)
if (buf[x] != (char)'\0') if (buf[x] != (char)'\0')
@ -97,8 +98,9 @@ bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) {
memcpy((void*)&linfo->name, (void*)&buf[1], namesize); memcpy((void*)&linfo->name, (void*)&buf[1], namesize);
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(&diff, &buf[namesize], sizeof(uint64_t)); memcpy(diff, &buf[namesize + 1], sizeof(uint64_t));
linfo->sid = (uint64_t)diff[0] << 56 | linfo->sid =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 | (uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 | (uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 | (uint64_t)diff[3] << 32 |

View File

@ -73,17 +73,42 @@ int Server::Ready() {
return 1; return 1;
} }
char buffer[2048]; char* buf = new char[150];
uint32_t buflen = 150;
bool readystart = false; bool readystart = false;
Log("À l'écoute sur le port: " + std::to_string(SRV_PORT), false, false); Log("À l'écoute sur le port: " + std::to_string(SRV_PORT), false, false);
while (!readystart) { while (!readystart) {
sockaddr_in sockad;
int addrlen = sizeof(sockad);
SOCKET sock = accept(m_sock_tcp, (sockaddr*)&sockad, &addrlen);
// Listen/accept ici. if (sock < 0)
Log("Erreur de connexion", true, false);
else if (sock > 0) {
char* strbuf = new char[150];
uint32_t strbuflen = 150;
std::cout << inet_ntop(AF_INET, &sockad.sin_addr, strbuf, strbuflen) << ':' << std::to_string(sockad.sin_port) << std::endl;;
if (recv(sock, buf, buflen, 0) > 0) {
netprot::LoginInfo log;
if (netprot::Deserialize(&log, buf, buflen)) {
std::string str;
log.sid = 8675309; // EIGHT SIX SEVENFIVE THREE AUGHT NIIIIIIIIIiiIIIIiINE!
str.append(log.name).append(": ").append(std::to_string(log.sid));
Log(str, false, false);
netprot::Serialize(&log, &buf, &buflen);
send(sock, buf, buflen, 0);
std::cin.getline(nullptr, 1);
readystart = true; readystart = true;
} }
}
}
}
return 0; return 0;
} }
@ -118,7 +143,7 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false)
break; break;
case LOG_DEST::CONSOLE: case LOG_DEST::CONSOLE:
default: default:
std::cout << Timestamp() << (is_fatal? "FATAL ": "") << (is_error ? "ERROR ": "") << str << std::endl; std::cout << Timestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break; break;
} }
@ -135,30 +160,24 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false)
// Test serialize/deserialize: // Test serialize/deserialize:
/* /*
netprot::LoginInfo* log = new netprot::LoginInfo(); netprot::LoginInfo log, log2;
char nom[] = "Jean Dujardin"; std::cout << "Nom? ";
memcpy(log->name, &nom, sizeof(nom)); std::cin.getline(log.name, 32); // NO! STD::CIN >> VARIABLE;! EVEEEEEERRRR!!!
log->sid = 12345; log.sid = 12345;
char* buf = new char[150]; char* buf = new char[150];
uint32_t buflen = 150; uint32_t buflen = 150;
netprot::Serialize(log, &buf, &buflen); netprot::Serialize(&log, &buf, &buflen);
delete log; bool is_work = netprot::Deserialize(&log2, buf, buflen);
log = new netprot::LoginInfo();
bool is_work = netprot::Deserialize(log, buf, buflen);
std::string str; std::string str;
str.append(is_work ? "Y " : "N ").append(log2.name).append(": ").append(std::to_string(log2.sid));
str.append(is_work ? "Y " : "N ").append(log->name).append(": ").append(std::to_string(log->sid));
Log(str, false, false); Log(str, false, false);
*/ */
///* Recevoir paquet */ ///* Recevoir paquet */
//while (true) { //while (true) {
// char buffer[2048]; // char buffer[2048];

View File

@ -5,9 +5,9 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include "../SQCSim-common/world.h" #include "../SQCSim-common/world.h"
#include "../SQCSim-common/netprotocol.h"
#include "define.h" #include "define.h"
#include "connection.h" #include "connection.h"
#include "../SQCSim-common/netprotocol.h"
class Server { class Server {
public: public:

View File

@ -27,7 +27,7 @@ int Connector::Init() {
/* Creation structure donnes descripteur du socket serveur */ /* Creation structure donnes descripteur du socket serveur */
sockaddr_in addr; sockaddr_in addr;
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(SRV_PORT); addr.sin_port = htons(CLI_PORT);
addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(m_sock_udp, (sockaddr*)&addr, sizeof(addr)) != 0) { /* Associer le socket UDP au port */ if (bind(m_sock_udp, (sockaddr*)&addr, sizeof(addr)) != 0) { /* Associer le socket UDP au port */
@ -39,16 +39,49 @@ int Connector::Init() {
std::cout << "Association Socket TCP." << std::endl; std::cout << "Association Socket TCP." << std::endl;
return 5; return 5;
} }
std::cout << "It is the voork!" << std::endl;
return 0; return 0;
} }
int Connector::Connect(sockaddr_in srv_addr, std::string name) { int Connector::Connect(char* srv_addr, std::string name) {
sockaddr_in add;
add.sin_family = AF_INET;
add.sin_port = htons(SRV_PORT);
if (inet_pton(AF_INET, srv_addr, &add.sin_addr) <= 0) {
std::cout << "Addresse serveur invalide." << std::endl;
return 1;
}
if (connect(m_sock_tcp, (sockaddr*)&add, sizeof(add)) < 0) {
std::cout << "Échec de la connexion." << std::endl;
}
char* buf = new char[150];
uint32_t buflen = 150;
netprot::LoginInfo log, retlog;
log.sid = 0;
memcpy(&log.name, name.c_str(), name.size() + 1);
netprot::Serialize(&log, &buf, &buflen);
int se = send(m_sock_tcp, buf, buflen, 0);
delete[] buf;
buf = new char[150] {0};
buflen = 150;
while (recv(m_sock_tcp, buf, buflen, 0) < se) {}
if (!netprot::Deserialize(&retlog, buf, buflen)) {
std::cout << "Packet invalide." << std::endl;
return 2;
}
m_name = retlog.name;
m_sid = retlog.sid;
return 0; return 0;
} }
UINT64 Connector::getId() const { return m_sid; } uint64_t Connector::getId() const { return m_sid; }
unsigned int Connector::getSeed() const { return m_seed; } unsigned int Connector::getSeed() const { return m_seed; }

View File

@ -3,6 +3,7 @@
#include "define.h" #include "define.h"
#include "../SQCSim-common/netprotocol.h" #include "../SQCSim-common/netprotocol.h"
#include <stdlib.h>
class Connector { class Connector {
public: public:
@ -10,8 +11,8 @@ public:
~Connector(); ~Connector();
int Init(); int Init();
int Connect(sockaddr_in srv_addr, std::string name); int Connect(char* srv_addr, std::string name);
UINT64 getId() const; uint64_t getId() const;
unsigned int getSeed() const; unsigned int getSeed() const;
//void SendInput(); //void SendInput();
@ -23,9 +24,9 @@ private:
SOCKET m_sock_udp = 0, SOCKET m_sock_udp = 0,
m_sock_tcp = 0; m_sock_tcp = 0;
std::string m_name = ""; std::string m_name = "";
UINT64 m_sid = 0, uint64_t m_sid = 0,
m_tid = 0; m_tid = 0;
unsigned int m_seed = 12345; unsigned int m_seed = 0;
}; };
#endif #endif

View File

@ -1,23 +1,20 @@
#ifndef CLI_DEFINE_H__ #ifndef CLI_DEFINE_H__
#define CLI_DEFINE_H__ #define CLI_DEFINE_H__
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "../SQCSim-common/define.h" #include "../SQCSim-common/define.h"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h>
#include <GL/glew.h> #include <GL/glew.h>
#include <gl/GL.h> #include <gl/GL.h>
#include <gl/GLU.h> #include <gl/GLU.h>
#endif #endif
#define NETWORK_TEST false
#define NETWORK_TEST true
#define SRV_ADDR "127.0.0.1" #define SRV_ADDR "127.0.0.1"
#define COUNTDOWN 300 #define COUNTDOWN 300

View File

@ -40,26 +40,20 @@ void Engine::Init() {
m_skybox.Init(0.2f); m_skybox.Init(0.2f);
// Objet de musique! // Objet de musique!
m_audio.ToggleMusicState(); //m_audio.ToggleMusicState();
// Array pour les balles. // Array pour les balles.
for (int x = 0; x < MAX_BULLETS; ++x) for (int x = 0; x < MAX_BULLETS; ++x)
m_bullets[x] = nullptr; m_bullets[x] = nullptr;
uint64_t seed = 12345; uint64_t seed = 0;
std::string playname = "John Test"; std::string playname = "La Chienne à Jacques";
const char srvaddr[] = "127.0.0.1";
if (NETWORK_TEST) { // Test connexion réseau. if (NETWORK_TEST) { // Test connexion réseau.
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(SRV_PORT);
addr.sin_addr.s_addr = inet_addr(srvaddr);
if (!m_conn.Init()) { if (!m_conn.Init()) {
if (m_conn.Connect(addr, playname)) { if (!m_conn.Connect(SRV_ADDR, playname)) {
// setup jeu en réseau. // setup jeu en réseau.
seed = m_conn.getSeed();
std::cout << "ID reçu du serveur: " << std::to_string(m_conn.getId()) << "!" << std::endl; std::cout << "ID reçu du serveur: " << std::to_string(m_conn.getId()) << "!" << std::endl;
//seed = m_conn.getSeed();
} }
else std::cout << "Erreur de connexion." << std::endl; else std::cout << "Erreur de connexion." << std::endl;
} }
@ -210,7 +204,6 @@ void Engine::DisplayHud(int timer) {
ss << "Time: " << (int)(timer / 60) << ":" << std::setw(2) << std::setfill('0') << timer % 60; ss << "Time: " << (int)(timer / 60) << ":" << std::setw(2) << std::setfill('0') << timer % 60;
PrintText(Width() - Width() * 0.15, Height() - (Height() / 19.2), scale, ss.str()); PrintText(Width() - Width() * 0.15, Height() - (Height() / 19.2), scale, ss.str());
} }
void Engine::DisplayInfo(float elapsedTime, BlockType bloc) { void Engine::DisplayInfo(float elapsedTime, BlockType bloc) {