mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-03 16:25:35 +00:00
refactor identification to its own method
This commit is contained in:
parent
ff33f1d42f
commit
e9805c3679
@ -105,4 +105,5 @@ private:
|
|||||||
thread_pool m_threadpool {};
|
thread_pool m_threadpool {};
|
||||||
Sync<bool> m_shutdown { false };
|
Sync<bool> m_shutdown { false };
|
||||||
ip::udp::socket m_udp_socket { m_io };
|
ip::udp::socket m_udp_socket { m_io };
|
||||||
|
void handle_identification(ClientID id, const Packet& packet, std::shared_ptr<Client>& client);
|
||||||
};
|
};
|
||||||
|
137
src/Network.cpp
137
src/Network.cpp
@ -208,73 +208,7 @@ void Network::handle_packet(ClientID id, const Packet& packet) {
|
|||||||
// and fall through
|
// and fall through
|
||||||
[[fallthrough]];
|
[[fallthrough]];
|
||||||
case bmp::State::Identification:
|
case bmp::State::Identification:
|
||||||
switch (packet.purpose) {
|
handle_identification(id, packet, client);
|
||||||
case bmp::Purpose::ProtocolVersion: {
|
|
||||||
struct bmp::ProtocolVersion protocol_version { };
|
|
||||||
protocol_version.deserialize_from(packet.data);
|
|
||||||
if (protocol_version.version.major != 1) {
|
|
||||||
beammp_debugf("{}: Protocol version bad", id);
|
|
||||||
// version bad
|
|
||||||
Packet protocol_v_bad_packet {
|
|
||||||
.purpose = bmp::ProtocolVersionBad,
|
|
||||||
};
|
|
||||||
client->tcp_write(protocol_v_bad_packet);
|
|
||||||
disconnect(id, fmt::format("bad protocol version: {}.{}.{}", protocol_version.version.major, protocol_version.version.minor, protocol_version.version.patch));
|
|
||||||
} else {
|
|
||||||
beammp_debugf("{}: Protocol version ok", id);
|
|
||||||
// version ok
|
|
||||||
Packet protocol_v_ok_packet {
|
|
||||||
.purpose = bmp::ProtocolVersionOk,
|
|
||||||
};
|
|
||||||
client->tcp_write(protocol_v_ok_packet);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case bmp::Purpose::ClientInfo: {
|
|
||||||
struct bmp::ClientInfo cinfo { };
|
|
||||||
cinfo.deserialize_from(packet.data);
|
|
||||||
beammp_debugf("{} is running game version: v{}.{}.{}, mod version: v{}.{}.{}, client implementation '{}' v{}.{}.{}",
|
|
||||||
id,
|
|
||||||
cinfo.game_version.major,
|
|
||||||
cinfo.game_version.minor,
|
|
||||||
cinfo.game_version.patch,
|
|
||||||
cinfo.mod_version.major,
|
|
||||||
cinfo.mod_version.minor,
|
|
||||||
cinfo.mod_version.patch,
|
|
||||||
cinfo.implementation.value,
|
|
||||||
cinfo.program_version.major,
|
|
||||||
cinfo.program_version.minor,
|
|
||||||
cinfo.program_version.patch);
|
|
||||||
// respond with server info
|
|
||||||
auto version = Application::ServerVersion();
|
|
||||||
struct bmp::ServerInfo sinfo {
|
|
||||||
.program_version = {
|
|
||||||
.major = version.major,
|
|
||||||
.minor = version.minor,
|
|
||||||
.patch = version.patch,
|
|
||||||
},
|
|
||||||
.implementation = {
|
|
||||||
.value = "Official BeamMP Server (BeamMP Ltd.)",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
Packet sinfo_packet {
|
|
||||||
.purpose = bmp::Purpose::ServerInfo,
|
|
||||||
.data = std::vector<uint8_t>(1024),
|
|
||||||
};
|
|
||||||
sinfo.serialize_to(sinfo_packet.data);
|
|
||||||
client->tcp_write(sinfo_packet);
|
|
||||||
// now transfer to next state
|
|
||||||
Packet auth_state {
|
|
||||||
.purpose = bmp::Purpose::StateChangeAuthentication,
|
|
||||||
};
|
|
||||||
client->tcp_write(auth_state);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
beammp_errorf("Got 0x{:x} in state {}. This is not allowed disconnecting the client", uint16_t(packet.purpose), int(client->state));
|
|
||||||
disconnect(id, "invalid purpose in current state");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case bmp::State::Authentication:
|
case bmp::State::Authentication:
|
||||||
break;
|
break;
|
||||||
@ -288,3 +222,72 @@ void Network::handle_packet(ClientID id, const Packet& packet) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void Network::handle_identification(ClientID id, const Packet& packet, std::shared_ptr<Client>& client) {
|
||||||
|
switch (packet.purpose) {
|
||||||
|
case bmp::ProtocolVersion: {
|
||||||
|
struct bmp::ProtocolVersion protocol_version { };
|
||||||
|
protocol_version.deserialize_from(packet.data);
|
||||||
|
if (protocol_version.version.major != 1) {
|
||||||
|
beammp_debugf("{}: Protocol version bad", id);
|
||||||
|
// version bad
|
||||||
|
Packet protocol_v_bad_packet {
|
||||||
|
.purpose = bmp::ProtocolVersionBad,
|
||||||
|
};
|
||||||
|
client->tcp_write(protocol_v_bad_packet);
|
||||||
|
disconnect(id, fmt::format("bad protocol version: {}.{}.{}", protocol_version.version.major, protocol_version.version.minor, protocol_version.version.patch));
|
||||||
|
} else {
|
||||||
|
beammp_debugf("{}: Protocol version ok", id);
|
||||||
|
// version ok
|
||||||
|
Packet protocol_v_ok_packet {
|
||||||
|
.purpose = bmp::ProtocolVersionOk,
|
||||||
|
};
|
||||||
|
client->tcp_write(protocol_v_ok_packet);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case bmp::ClientInfo: {
|
||||||
|
struct bmp::ClientInfo cinfo { };
|
||||||
|
cinfo.deserialize_from(packet.data);
|
||||||
|
beammp_debugf("{} is running game version: v{}.{}.{}, mod version: v{}.{}.{}, client implementation '{}' v{}.{}.{}",
|
||||||
|
id,
|
||||||
|
cinfo.game_version.major,
|
||||||
|
cinfo.game_version.minor,
|
||||||
|
cinfo.game_version.patch,
|
||||||
|
cinfo.mod_version.major,
|
||||||
|
cinfo.mod_version.minor,
|
||||||
|
cinfo.mod_version.patch,
|
||||||
|
cinfo.implementation.value,
|
||||||
|
cinfo.program_version.major,
|
||||||
|
cinfo.program_version.minor,
|
||||||
|
cinfo.program_version.patch);
|
||||||
|
// respond with server info
|
||||||
|
auto version = Application::ServerVersion();
|
||||||
|
struct bmp::ServerInfo sinfo {
|
||||||
|
.program_version = {
|
||||||
|
.major = version.major,
|
||||||
|
.minor = version.minor,
|
||||||
|
.patch = version.patch,
|
||||||
|
},
|
||||||
|
.implementation = {
|
||||||
|
.value = "Official BeamMP Server (BeamMP Ltd.)",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
Packet sinfo_packet {
|
||||||
|
.purpose = bmp::ServerInfo,
|
||||||
|
.data = std::vector<uint8_t>(1024),
|
||||||
|
};
|
||||||
|
sinfo.serialize_to(sinfo_packet.data);
|
||||||
|
client->tcp_write(sinfo_packet);
|
||||||
|
// now transfer to next state
|
||||||
|
Packet auth_state {
|
||||||
|
.purpose = bmp::StateChangeAuthentication,
|
||||||
|
};
|
||||||
|
client->tcp_write(auth_state);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
beammp_errorf("Got 0x{:x} in state {}. This is not allowed disconnecting the client", uint16_t(packet.purpose), int(client->state));
|
||||||
|
disconnect(id, "invalid purpose in current state");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user