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

@@ -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 << "<EFBFBD>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; }

View File

@@ -3,6 +3,7 @@
#include "define.h"
#include "../SQCSim-common/netprotocol.h"
#include <stdlib.h>
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

View File

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

View File

@@ -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 <20> 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<72>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) {