16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-18 03:09:18 +02:00
This commit is contained in:
milek7
2019-01-18 00:26:48 +01:00
parent aa82349aee
commit f63bd04bfe
17 changed files with 542 additions and 463 deletions

View File

@@ -1,5 +1,4 @@
#pragma once
#include <asio.hpp>
#include <memory>
#include <functional>
#include <optional>
@@ -10,52 +9,73 @@
namespace network
{
class connection : public std::enable_shared_from_this<connection>
class connection
{
friend class server;
friend class client;
private:
void message_received(std::shared_ptr<message> &msg);
/*
std::queue<
std::pair<std::chrono::high_resolution_clock::time_point,
std::shared_ptr<delta_message>>> delta_queue;
std::shared_ptr<frame_info>>> delta_queue;
command_queue::commands_map client_commands_queue;
bool is_client;
//std::chrono::high_resolution_clock::time_point last_time;
//double accum = -1.0;
*/
bool is_client;
protected:
std::function<void(const message &msg)> message_handler;
public:
virtual void connected() = 0;
virtual void send_message(const message &msg) = 0;
connection(bool client = false);
void set_handler(std::function<void(const message &msg)> handler);
virtual void disconnect() = 0;
virtual void send_data(std::shared_ptr<std::string> buffer) = 0;
void data_received(std::string &buffer);
public:
connection(bool client = false);
void send_message(std::shared_ptr<message> msg);
virtual void connected();
std::tuple<double, double, command_queue::commands_map> get_next_delta();
command_queue::commands_map pop_commands();
enum peer_state {
AWAITING_HELLO,
CATCHING_UP,
ACTIVE,
DEAD
};
peer_state state;
};
class server
{
protected:
void handle_message(connection &conn, const message &msg);
std::vector<std::shared_ptr<connection>> clients;
std::fstream recorder;
command_queue::commands_map client_commands_queue;
public:
server();
void push_delta(double dt, double sync, command_queue::commands_map commands);
void push_delta(double dt, double sync, const command_queue::commands_map &commands);
command_queue::commands_map pop_commands();
};
class client
{
protected:
void handle_message(connection &conn, const message &msg);
std::shared_ptr<connection> conn;
std::queue<
std::pair<std::chrono::high_resolution_clock::time_point,
frame_info>> delta_queue;
public:
std::tuple<double, double, command_queue::commands_map> get_next_delta();
void send_commands(command_queue::commands_map commands);