mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-17 22:39:19 +02:00
Networking used to be reachable only through the eu07.ini network.server / network.client entries, and init_modes() refused to start at all unless a scenery file was already known. That made it impossible to join a session without knowing (and typing) the host's scenario up front, even though SERVER_HELLO already carries the scenario name. - add --host [address:port] and --connect address:port to the command line (default port 7420, --host defaults to 0.0.0.0), plus --scenario/--vehicle aliases and -h/--help; the existing ini settings keep working - drop the init_modes() restriction: a client goes straight to the scenario loader and waits there for the handshake, a host without -s still gets the launcher and simply refuses joins until a scenario is up - the loader screen reports the network state instead of sitting on a blank progress bar - a client that did not ask for a vehicle with -v starts as an observer rather than failing to find the default EU07-424; the lobby of stage 2 will take over the choice - handshake carries build identification and the server answers an impossible join with SERVER_REJECT and a readable reason instead of dropping the socket silently; protocol version bumped to 3 - deserialize_message() no longer dereferences a null message on an unknown message type
150 lines
4.1 KiB
C++
150 lines
4.1 KiB
C++
#include "stdafx.h"
|
|
#include "network/message.h"
|
|
#include "scene/sn_utils.h"
|
|
|
|
void network::client_hello::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_int32(stream, version);
|
|
sn_utils::ls_uint32(stream, start_packet);
|
|
sn_utils::s_str(stream, app_version);
|
|
}
|
|
|
|
void network::client_hello::deserialize(std::istream &stream)
|
|
{
|
|
version = sn_utils::ld_int32(stream);
|
|
start_packet = sn_utils::ld_uint32(stream);
|
|
app_version = sn_utils::d_str(stream);
|
|
}
|
|
|
|
void network::server_reject::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::s_str(stream, reason);
|
|
}
|
|
|
|
void network::server_reject::deserialize(std::istream &stream)
|
|
{
|
|
reason = sn_utils::d_str(stream);
|
|
}
|
|
|
|
void network::server_hello::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint32(stream, seed);
|
|
sn_utils::ls_int64(stream, timestamp);
|
|
sn_utils::ls_int64(stream, config);
|
|
sn_utils::s_str(stream, scenario);
|
|
sn_utils::s_str(stream, app_version);
|
|
}
|
|
|
|
void network::server_hello::deserialize(std::istream &stream)
|
|
{
|
|
seed = sn_utils::ld_uint32(stream);
|
|
timestamp = sn_utils::ld_int64(stream);
|
|
config = sn_utils::ld_int64(stream);
|
|
scenario = sn_utils::d_str(stream);
|
|
app_version = sn_utils::d_str(stream);
|
|
}
|
|
|
|
void ::network::request_command::serialize(std::ostream &stream) const
|
|
{
|
|
sn_utils::ls_uint32(stream, commands.size());
|
|
for (auto const &kv : commands)
|
|
{
|
|
sn_utils::ls_uint32(stream, kv.first);
|
|
sn_utils::ls_uint32(stream, kv.second.size());
|
|
for (command_data const &data : kv.second)
|
|
{
|
|
sn_utils::ls_uint32(stream, (uint32_t)data.command);
|
|
sn_utils::ls_int32(stream, data.action);
|
|
sn_utils::ls_float64(stream, data.param1);
|
|
sn_utils::ls_float64(stream, data.param2);
|
|
sn_utils::ls_float64(stream, data.time_delta);
|
|
|
|
sn_utils::s_bool(stream, data.freefly);
|
|
sn_utils::s_vec3(stream, data.location);
|
|
|
|
sn_utils::s_str(stream, data.payload);
|
|
}
|
|
}
|
|
}
|
|
|
|
void network::request_command::deserialize(std::istream &stream)
|
|
{
|
|
uint32_t commands_size = sn_utils::ld_uint32(stream);
|
|
for (uint32_t i = 0; i < commands_size; i++)
|
|
{
|
|
uint32_t recipient = sn_utils::ld_uint32(stream);
|
|
uint32_t sequence_size = sn_utils::ld_uint32(stream);
|
|
|
|
command_queue::commanddata_sequence sequence;
|
|
for (uint32_t i = 0; i < sequence_size; i++)
|
|
{
|
|
command_data data;
|
|
data.command = (user_command)sn_utils::ld_uint32(stream);
|
|
data.action = sn_utils::ld_int32(stream);
|
|
data.param1 = sn_utils::ld_float64(stream);
|
|
data.param2 = sn_utils::ld_float64(stream);
|
|
data.time_delta = sn_utils::ld_float64(stream);
|
|
|
|
data.freefly = sn_utils::d_bool(stream);
|
|
data.location = sn_utils::d_vec3(stream);
|
|
|
|
data.payload = sn_utils::d_str(stream);
|
|
|
|
sequence.emplace_back(data);
|
|
}
|
|
|
|
commands.emplace(recipient, sequence);
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
request_command::serialize(stream);
|
|
}
|
|
|
|
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);
|
|
|
|
request_command::deserialize(stream);
|
|
}
|
|
|
|
std::shared_ptr<network::message> network::deserialize_message(std::istream &stream)
|
|
{
|
|
message::type_e type = (message::type_e)sn_utils::ld_uint16(stream);
|
|
|
|
std::shared_ptr<message> msg;
|
|
|
|
if (type == message::CLIENT_HELLO)
|
|
msg = std::make_shared<client_hello>();
|
|
else if (type == message::SERVER_HELLO)
|
|
msg = std::make_shared<server_hello>();
|
|
else if (type == message::FRAME_INFO)
|
|
msg = std::make_shared<frame_info>();
|
|
else if (type == message::REQUEST_COMMAND)
|
|
msg = std::make_shared<request_command>();
|
|
else if (type == message::SERVER_REJECT)
|
|
msg = std::make_shared<server_reject>();
|
|
|
|
if (!msg) {
|
|
// unknown message type; hand back a marker the peer handlers treat as a protocol error
|
|
return std::make_shared<message>(message::TYPE_MAX);
|
|
}
|
|
|
|
msg->deserialize(stream);
|
|
|
|
return msg;
|
|
}
|
|
|
|
void network::serialize_message(const message &msg, std::ostream &stream)
|
|
{
|
|
sn_utils::ls_uint16(stream, (uint16_t)msg.type);
|
|
msg.serialize(stream);
|
|
}
|