mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-17 23:39:18 +02:00
network improvements
This commit is contained in:
@@ -16,12 +16,13 @@ command_queue::commands_map network::server_manager::pop_commands()
|
||||
return map;
|
||||
}
|
||||
|
||||
void network::server_manager::push_delta(double dt, double sync, const command_queue::commands_map &commands)
|
||||
void network::server_manager::push_delta(double render_dt, double dt, double sync, const command_queue::commands_map &commands)
|
||||
{
|
||||
if (dt == 0.0 && commands.empty())
|
||||
return;
|
||||
|
||||
frame_info msg;
|
||||
msg.render_dt = render_dt;
|
||||
msg.dt = dt;
|
||||
msg.sync = sync;
|
||||
msg.commands = commands;
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace network
|
||||
public:
|
||||
server_manager();
|
||||
|
||||
void push_delta(double dt, double sync, const command_queue::commands_map &commands);
|
||||
void push_delta(double render_dt, double dt, double sync, const command_queue::commands_map &commands);
|
||||
command_queue::commands_map pop_commands();
|
||||
void create_server(const std::string &backend, const std::string &conf);
|
||||
};
|
||||
|
||||
@@ -74,6 +74,7 @@ void network::request_command::deserialize(std::istream &stream)
|
||||
|
||||
void network::frame_info::serialize(std::ostream &stream) const
|
||||
{
|
||||
sn_utils::ls_float64(stream, render_dt);
|
||||
sn_utils::ls_float64(stream, dt);
|
||||
sn_utils::ls_float64(stream, sync);
|
||||
|
||||
@@ -82,6 +83,7 @@ void network::frame_info::serialize(std::ostream &stream) const
|
||||
|
||||
void network::frame_info::deserialize(std::istream &stream)
|
||||
{
|
||||
render_dt = sn_utils::ld_float64(stream);
|
||||
dt = sn_utils::ld_float64(stream);
|
||||
sync = sn_utils::ld_float64(stream);
|
||||
|
||||
|
||||
@@ -59,6 +59,7 @@ struct frame_info : public request_command
|
||||
{
|
||||
frame_info() : request_command(FRAME_INFO) {}
|
||||
|
||||
double render_dt;
|
||||
double dt;
|
||||
double sync;
|
||||
|
||||
|
||||
@@ -74,37 +74,7 @@ void network::connection::send_complete(std::shared_ptr<std::string> buf)
|
||||
|
||||
// --------------
|
||||
|
||||
/*
|
||||
std::tuple<double, double, command_queue::commands_map> network::connection::get_next_delta()
|
||||
{
|
||||
if (delta_queue.empty()) {
|
||||
return std::tuple<double, double,
|
||||
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
|
||||
}
|
||||
|
||||
///auto now = std::chrono::high_resolution_clock::now();
|
||||
|
||||
//double last_frame = std::chrono::duration_cast<std::chrono::seconds>(now - last_time).count();
|
||||
//last_time = now;
|
||||
//accum += last_frame;
|
||||
|
||||
auto entry = delta_queue.front();
|
||||
|
||||
//if (accum > remote_dt) {
|
||||
//accum -= remote_dt;
|
||||
|
||||
delta_queue.pop();
|
||||
return std::make_tuple(entry.second->dt, entry.second->sync, entry.second->commands);
|
||||
//}
|
||||
//else {
|
||||
//return 0.0;
|
||||
//}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
// server
|
||||
|
||||
network::server::server(std::shared_ptr<std::istream> buf) : backbuffer(buf)
|
||||
{
|
||||
|
||||
@@ -186,18 +156,49 @@ void network::client::update()
|
||||
}
|
||||
|
||||
// client
|
||||
std::tuple<double, double, command_queue::commands_map> network::client::get_next_delta()
|
||||
std::tuple<double, double, command_queue::commands_map> network::client::get_next_delta(int counter)
|
||||
{
|
||||
if (counter == 1) {
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
frame_time = now - last_frame;
|
||||
last_frame = now;
|
||||
}
|
||||
|
||||
if (delta_queue.empty()) {
|
||||
// buffer underflow
|
||||
return std::tuple<double, double,
|
||||
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
|
||||
}
|
||||
|
||||
auto entry = delta_queue.front();
|
||||
delta_queue.pop();
|
||||
|
||||
auto &delta = entry.second;
|
||||
return std::make_tuple(entry.second.dt, entry.second.sync, entry.second.commands);
|
||||
float size = delta_queue.size() - consume_counter;
|
||||
auto entry = delta_queue.front();
|
||||
float mult = entry.render_dt / std::chrono::duration_cast<std::chrono::duration<float>>(frame_time).count();
|
||||
|
||||
if (counter == 1 && size < MAX_BUFFER_SIZE * 2.0f) {
|
||||
last_target = last_target * TARGET_MIX +
|
||||
(std::min(TARGET_MIN + jitteriness * JITTERINESS_MULTIPIER, MAX_BUFFER_SIZE)) * (1.0f - TARGET_MIX);
|
||||
float diff = size - last_target;
|
||||
jitteriness = std::max(jitteriness * JITTERINESS_MIX, std::abs(diff));
|
||||
|
||||
float speed = 1.0f + diff * CONSUME_MULTIPIER;
|
||||
|
||||
consume_counter += speed;
|
||||
}
|
||||
|
||||
if (size > MAX_BUFFER_SIZE || consume_counter > mult) {
|
||||
if (consume_counter > mult) {
|
||||
consume_counter = std::clamp(consume_counter - mult, -MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
delta_queue.pop();
|
||||
|
||||
return std::make_tuple(entry.dt, entry.sync, entry.commands);
|
||||
} else {
|
||||
// nothing to push
|
||||
return std::tuple<double, double,
|
||||
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
|
||||
}
|
||||
}
|
||||
|
||||
void network::client::send_commands(command_queue::commands_map commands)
|
||||
@@ -240,8 +241,7 @@ void network::client::handle_message(std::shared_ptr<connection> conn, const mes
|
||||
resume_frame_counter++;
|
||||
|
||||
auto delta = dynamic_cast<const frame_info&>(msg);
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
delta_queue.push(std::make_pair(now, delta));
|
||||
delta_queue.push(delta);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,17 +18,6 @@ namespace network
|
||||
private:
|
||||
const int CATCHUP_PACKETS = 300;
|
||||
|
||||
/*
|
||||
std::queue<
|
||||
std::pair<std::chrono::high_resolution_clock::time_point,
|
||||
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:
|
||||
@@ -88,14 +77,25 @@ namespace network
|
||||
size_t reconnect_delay = 0;
|
||||
|
||||
const size_t RECONNECT_DELAY_FRAMES = 60;
|
||||
const float MAX_BUFFER_SIZE = 60.0f;
|
||||
const float JITTERINESS_MIX = 0.998f;
|
||||
const float TARGET_MIN = 2.0f;
|
||||
const float TARGET_MIX = 0.98f;
|
||||
const float JITTERINESS_MULTIPIER = 2.0f;
|
||||
const float CONSUME_MULTIPIER = 0.05f;
|
||||
|
||||
std::queue<
|
||||
std::pair<std::chrono::high_resolution_clock::time_point,
|
||||
frame_info>> delta_queue;
|
||||
std::queue<frame_info> delta_queue;
|
||||
|
||||
float last_target = 20.0f;
|
||||
float jitteriness = 1.0f;
|
||||
float consume_counter = 0.0f;
|
||||
|
||||
std::chrono::high_resolution_clock::time_point last_frame;
|
||||
std::chrono::high_resolution_clock::duration frame_time;
|
||||
|
||||
public:
|
||||
void update();
|
||||
std::tuple<double, double, command_queue::commands_map> get_next_delta();
|
||||
std::tuple<double, double, command_queue::commands_map> get_next_delta(int counter);
|
||||
void send_commands(command_queue::commands_map commands);
|
||||
int get_frame_counter() {
|
||||
return resume_frame_counter;
|
||||
|
||||
Reference in New Issue
Block a user