diff --git a/SQCSim-srv/connection.cpp b/SQCSim-srv/connection.cpp index 964cc07..2b93114 100644 --- a/SQCSim-srv/connection.cpp +++ b/SQCSim-srv/connection.cpp @@ -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--); + } diff --git a/SQCSim-srv/connection.h b/SQCSim-srv/connection.h index 198a39f..5b0134d 100644 --- a/SQCSim-srv/connection.h +++ b/SQCSim-srv/connection.h @@ -1,17 +1,30 @@ -#ifndef _CONNECTION_H__ -#define _CONNECTION_H__ +#ifndef CONNECTION_H__ +#define CONNECTION_H__ #include -#include +#include #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 m_input_manifest; + std::map m_input_manifest; + std::map 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; }; diff --git a/SQCSim-srv/define.h b/SQCSim-srv/define.h index 3a37bd4..74c1c26 100644 --- a/SQCSim-srv/define.h +++ b/SQCSim-srv/define.h @@ -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 #include #include #include +#include + +#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 diff --git a/SQCSim-srv/server.cpp b/SQCSim-srv/server.cpp index afe262d..e2740ba 100644 --- a/SQCSim-srv/server.cpp +++ b/SQCSim-srv/server.cpp @@ -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; diff --git a/SQCSim-srv/server.h b/SQCSim-srv/server.h index 246ad16..2cb0618 100644 --- a/SQCSim-srv/server.h +++ b/SQCSim-srv/server.h @@ -1,15 +1,16 @@ -#ifndef _SERVER_H__ -#define _SERVER_H__ +#ifndef SERVER_H__ +#define SERVER_H__ #include #include #include #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 m_players; std::string Timestamp(); void Log(std::string str, bool is_error, bool is_fatal);