Zigonnage dans le srv.

This commit is contained in:
MarcEricMartel 2023-09-24 11:07:03 -04:00
parent 51b0795c62
commit 2bb55e5bf6
5 changed files with 101 additions and 34 deletions

View File

@ -2,13 +2,13 @@
Connection::Connection(in_addr addr,
std::string name,
UINT64 hash,
UINT64 self_hash,
UINT64 team_hash):
UINT64 id,
UINT64 self_id,
UINT64 team_id):
m_addr(addr),
m_hash(hash),
m_shash(self_hash),
m_thash(team_hash),
m_id(id),
m_sid(self_id),
m_tid(team_id),
m_name(name) {
}
@ -19,13 +19,38 @@ Connection::~Connection() {
in_addr Connection::GetAddr() const { return m_addr; }
UINT64 Connection::GetHash(bool self) const { return self? m_shash: m_hash; }
UINT64 Connection::GetHash(bool self) const { return self? m_sid: m_id; }
UINT64 Connection::GetTeamHash() const { return m_thash; }
UINT64 Connection::GetTeamHash() const { return m_tid; }
std::string Connection::GetName() const { return m_name; }
void Connection::Clean(std::chrono::system_clock::time_point time) {
while (m_input_manifest.front().timestamp < time || !m_input_manifest.empty())
m_input_manifest.pop_front();
void Connection::AddInput(Input in) {
m_input_manifest.insert({in.timestamp, in});
}
Output* Connection::getOutput(Timestamp time) {
auto out = m_output_manifest.find(time);
if (out != m_output_manifest.end())
return &out->second;
return nullptr;
}
Sync Connection::getSync(Timestamp time) {
Sync sync;
auto out = m_output_manifest.find(time);
if (out != m_output_manifest.end()) {
sync.timestamp = out->second.timestamp;
sync.position = out->second.position;
sync.sid = m_sid;
}
return sync;
}
void Connection::CleanInputManifest(Timestamp time) {
auto wat = m_input_manifest.find(time);
while (wat != m_input_manifest.begin())
m_input_manifest.erase(wat--);
}

View File

@ -1,17 +1,30 @@
#ifndef _CONNECTION_H__
#define _CONNECTION_H__
#ifndef CONNECTION_H__
#define CONNECTION_H__
#include <deque>
#include <chrono>
#include <map>
#include "../SQCSim-common/player.h"
#include "../SQCSim-common/vector3.h"
#include "define.h"
struct Input {
std::chrono::system_clock::time_point timestamp;
Timestamp timestamp;
UINT8 keys; // 0bFBLRJS__
Vector3f direction;
};
struct Output {
Timestamp timestamp;
UINT64 id = 0;
Vector3f position, direction;
bool is_shooting, is_jumping;
};
struct Sync {
Timestamp timestamp;
UINT64 sid = 0;
Vector3f position;
};
class Connection {
public:
Connection(
@ -29,13 +42,18 @@ public:
UINT64 GetTeamHash() const;
std::string GetName() const;
void Clean(std::chrono::system_clock::time_point time);
void AddInput(Input in);
Output* getOutput(Timestamp time);
Sync getSync(Timestamp time);
void CleanInputManifest(Timestamp time);
private:
std::deque<Input> m_input_manifest;
std::map<Timestamp, Input> m_input_manifest;
std::map<Timestamp, Output> m_output_manifest;
in_addr m_addr;
UINT64 m_hash,
m_shash,
m_thash;
UINT64 m_id,
m_sid,
m_tid;
std::string m_name;
};

View File

@ -1,13 +1,18 @@
#ifndef _SRV_DEFINE_H__
#define _SRV_DEFINE_H__
#ifndef SRV_DEFINE_H__
#define SRV_DEFINE_H__
#include "../SQCSim-common/define.h"
#include <iostream>
#include <sstream>
#include <cctype>
#include <string>
#include <chrono>
#define MAX_CONNECTIONS 16
typedef unsigned char LogDest;
enum LOG_DEST { CONSOLE, LOGFILE, LOG_LAST };
typedef std::chrono::system_clock::time_point Timestamp;
enum LogDest { CONSOLE, LOGFILE, LOG_LAST };
#ifdef _WIN32

View File

@ -1,10 +1,11 @@
#include "server.h"
Server::Server(LogDest log) : m_log(log) {
if (log == LogDest::LOGFILE) {
Server::Server(LogDest log) {
m_log = log;
if (log == LOG_DEST::LOGFILE) {
m_logfile = std::ofstream("server.log", std::ofstream::out);
if (!m_logfile.is_open()) {
m_log = LogDest::CONSOLE; // Fallback console.
m_log = LOG_DEST::CONSOLE; // Fallback console.
Log("Ouverture fichier log: repli vers console.", true, false);
}
}
@ -60,11 +61,28 @@ int Server::Init() {
return 5;
}
for (auto& conn : m_conn)
conn = nullptr;
return 0;
}
int Server::Ready() {
Log("Prêt à démarrer...", false, false);
if (listen(m_sock_tcp, MAX_CONNECTIONS) < 0) {
Log("Écoute sur le port TCP.", true, true);
return 1;
}
char buffer[2048];
bool readystart = false;
Log("À l'écoute sur le port: " + std::to_string(SRV_PORT), false, false);
while (!readystart) {
Log("trololo", false, false);
readystart = true;
}
return 0;
}
@ -93,10 +111,10 @@ inline std::string Server::Timestamp() {
void Server::Log(std::string str, bool is_error = false, bool is_fatal = false) {
switch (m_log) {
case LogDest::LOGFILE:
case LOG_DEST::LOGFILE:
m_logfile << Timestamp() << (is_fatal ? "FATAL " : "") << (is_error ? "ERROR " : "") << str << std::endl;
break;
case LogDest::CONSOLE:
case LOG_DEST::CONSOLE:
default:
std::cout << Timestamp() << (is_fatal? "FATAL ": "") << (is_error ? "ERROR ": "") << str << std::endl;
break;

View File

@ -1,15 +1,16 @@
#ifndef _SERVER_H__
#define _SERVER_H__
#ifndef SERVER_H__
#define SERVER_H__
#include <fstream>
#include <vector>
#include <string>
#include "../SQCSim-common/world.h"
#include "define.h"
#include "connection.h"
class Server {
public:
Server(LogDest log = LogDest::CONSOLE);
Server(LogDest log = LOG_DEST::CONSOLE);
~Server();
int Init();
@ -23,10 +24,10 @@ private:
SOCKET m_sock_udp = 0,
m_sock_tcp = 0;
LogDest m_log;
std::ofstream m_logfile;
Connection* m_conn[MAX_CONNECTIONS];
World* m_world = nullptr;
std::vector<Player> m_players;
std::string Timestamp();
void Log(std::string str, bool is_error, bool is_fatal);