diff --git a/SQCSim-common/define.h b/SQCSim-common/define.h index bafb39e..968b71d 100644 --- a/SQCSim-common/define.h +++ b/SQCSim-common/define.h @@ -28,7 +28,8 @@ typedef uint64_t Timestamp; #ifdef _WIN32 #pragma comment(lib,"wsock32.lib") // Pour pouvoir faire fonctionner le linker sans le vcxproject - +#pragma comment(lib,"ws2_32.lib") +#include #include #include #include diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index 5f8aef1..5e95427 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -18,7 +18,6 @@ void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) { void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) { *buf[0] = netprot::PACKET_TYPE::LOGINF; - void* nom = &linfo->name; char* buff = *buf + 1; size_t namesize = 1; @@ -27,22 +26,24 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) { if (linfo->name[x] != (char)'\0') ++namesize; else break; - - memcpy(buff, nom, sizeof(linfo->name)); + + memcpy(*buf + 1, &linfo->name, namesize); 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 >> 40) & 0xFF, (sid >> 32) & 0xFF, (sid >> 24) & 0xFF, (sid >> 16) & 0xFF, (sid >> 8) & 0xFF, - sid & 0xFF }; + sid & 0xFF + }; - void* ptrsid = *buf + namesize; - memcpy(ptrsid, &diff, sizeof(uint64_t)); + char* ptrsid = *buf + namesize + 2; + 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) { @@ -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) { - if (buflen < sizeof(sizeof(LoginInfo)) + 1) + if (buflen < sizeof(LoginInfo) + 3) return false; - size_t namesize = 0; + size_t namesize = 1; for (int x = 0; x < sizeof(linfo->name); ++x) if (buf[x] != (char)'\0') @@ -97,15 +98,16 @@ bool netprot::Deserialize(LoginInfo* linfo, char* buf, const uint32_t buflen) { memcpy((void*)&linfo->name, (void*)&buf[1], namesize); uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 }; - memcpy(&diff, &buf[namesize], sizeof(uint64_t)); - linfo->sid = (uint64_t)diff[0] << 56 | - (uint64_t)diff[1] << 48 | - (uint64_t)diff[2] << 40 | - (uint64_t)diff[3] << 32 | - (uint64_t)diff[4] << 24 | - (uint64_t)diff[5] << 16 | - (uint64_t)diff[6] << 8 | - (uint64_t)diff[7]; + memcpy(diff, &buf[namesize + 1], sizeof(uint64_t)); + linfo->sid = + (uint64_t)diff[0] << 56 | + (uint64_t)diff[1] << 48 | + (uint64_t)diff[2] << 40 | + (uint64_t)diff[3] << 32 | + (uint64_t)diff[4] << 24 | + (uint64_t)diff[5] << 16 | + (uint64_t)diff[6] << 8 | + (uint64_t)diff[7]; return true; } diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index 5d391c8..7e7171a 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -73,16 +73,41 @@ int Server::Ready() { return 1; } - char buffer[2048]; + char* buf = new char[150]; + uint32_t buflen = 150; bool readystart = false; Log("À l'écoute sur le port: " + std::to_string(SRV_PORT), false, false); 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); - readystart = true; + netprot::Serialize(&log, &buf, &buflen); + send(sock, buf, buflen, 0); + + std::cin.getline(nullptr, 1); + readystart = true; + } + } + } } return 0; } @@ -118,7 +143,7 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false) break; case LOG_DEST::CONSOLE: 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; } @@ -135,30 +160,24 @@ void Server::Log(std::string str, bool is_error = false, bool is_fatal = false) // Test serialize/deserialize: /* - netprot::LoginInfo* log = new netprot::LoginInfo(); - char nom[] = "Jean Dujardin"; - memcpy(log->name, &nom, sizeof(nom)); - log->sid = 12345; + netprot::LoginInfo log, log2; + std::cout << "Nom? "; + std::cin.getline(log.name, 32); // NO! STD::CIN >> VARIABLE;! EVEEEEEERRRR!!! + log.sid = 12345; char* buf = new char[150]; uint32_t buflen = 150; - netprot::Serialize(log, &buf, &buflen); + netprot::Serialize(&log, &buf, &buflen); - delete log; - log = new netprot::LoginInfo(); - - bool is_work = netprot::Deserialize(log, buf, buflen); + bool is_work = netprot::Deserialize(&log2, buf, buflen); std::string str; - - str.append(is_work ? "Y " : "N ").append(log->name).append(": ").append(std::to_string(log->sid)); + str.append(is_work ? "Y " : "N ").append(log2.name).append(": ").append(std::to_string(log2.sid)); Log(str, false, false); */ - - ///* Recevoir paquet */ //while (true) { // char buffer[2048]; diff --git a/SQCSim-srv/server.h b/SQCSim-srv/server.h index 298fc0f..b79b805 100644 --- a/SQCSim-srv/server.h +++ b/SQCSim-srv/server.h @@ -5,9 +5,9 @@ #include #include #include "../SQCSim-common/world.h" +#include "../SQCSim-common/netprotocol.h" #include "define.h" #include "connection.h" -#include "../SQCSim-common/netprotocol.h" class Server { public: diff --git a/SQCSim2021/connector.cpp b/SQCSim2021/connector.cpp index 4ccc9a0..a2fd522 100644 --- a/SQCSim2021/connector.cpp +++ b/SQCSim2021/connector.cpp @@ -27,7 +27,7 @@ int Connector::Init() { /* Creation structure donnes descripteur du socket serveur */ sockaddr_in addr; 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); 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; return 5; } - - std::cout << "It is the voork!" << std::endl; - 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; } -UINT64 Connector::getId() const { return m_sid; } +uint64_t Connector::getId() const { return m_sid; } unsigned int Connector::getSeed() const { return m_seed; } diff --git a/SQCSim2021/connector.h b/SQCSim2021/connector.h index b9653ad..f3ed6bd 100644 --- a/SQCSim2021/connector.h +++ b/SQCSim2021/connector.h @@ -3,6 +3,7 @@ #include "define.h" #include "../SQCSim-common/netprotocol.h" +#include class Connector { public: @@ -10,8 +11,8 @@ public: ~Connector(); int Init(); - int Connect(sockaddr_in srv_addr, std::string name); - UINT64 getId() const; + int Connect(char* srv_addr, std::string name); + uint64_t getId() const; unsigned int getSeed() const; //void SendInput(); @@ -23,9 +24,9 @@ private: SOCKET m_sock_udp = 0, m_sock_tcp = 0; std::string m_name = ""; - UINT64 m_sid = 0, + uint64_t m_sid = 0, m_tid = 0; - unsigned int m_seed = 12345; + unsigned int m_seed = 0; }; #endif diff --git a/SQCSim2021/define.h b/SQCSim2021/define.h index 533d6dc..ceeb08c 100644 --- a/SQCSim2021/define.h +++ b/SQCSim2021/define.h @@ -1,23 +1,20 @@ #ifndef CLI_DEFINE_H__ #define CLI_DEFINE_H__ -#include -#include #include #include #include +#include +#include #include "../SQCSim-common/define.h" #ifdef _WIN32 -#include #include #include #include #endif - - -#define NETWORK_TEST true +#define NETWORK_TEST false #define SRV_ADDR "127.0.0.1" #define COUNTDOWN 300 diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index bd618ae..b30818f 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -40,26 +40,20 @@ void Engine::Init() { m_skybox.Init(0.2f); // Objet de musique! - m_audio.ToggleMusicState(); + //m_audio.ToggleMusicState(); // Array pour les balles. for (int x = 0; x < MAX_BULLETS; ++x) m_bullets[x] = nullptr; - uint64_t seed = 12345; - std::string playname = "John Test"; - const char srvaddr[] = "127.0.0.1"; + uint64_t seed = 0; + std::string playname = "La Chienne à Jacques"; 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.Connect(addr, playname)) { + if (!m_conn.Connect(SRV_ADDR, playname)) { // setup jeu en réseau. - seed = m_conn.getSeed(); 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; } @@ -210,7 +204,6 @@ void Engine::DisplayHud(int timer) { 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()); - } void Engine::DisplayInfo(float elapsedTime, BlockType bloc) {