network: Address review comments

This commit is contained in:
FearlessTobi 2022-07-25 19:16:59 +02:00
parent 61ce57b524
commit a41baaa181
5 changed files with 201 additions and 197 deletions

View file

@ -65,80 +65,80 @@ Packet::operator bool() const {
return is_valid; return is_valid;
} }
Packet& Packet::operator>>(bool& out_data) { Packet& Packet::Read(bool& out_data) {
u8 value{}; u8 value{};
if (*this >> value) { if (Read(value)) {
out_data = (value != 0); out_data = (value != 0);
} }
return *this; return *this;
} }
Packet& Packet::operator>>(s8& out_data) { Packet& Packet::Read(s8& out_data) {
Read(&out_data, sizeof(out_data)); Read(&out_data, sizeof(out_data));
return *this; return *this;
} }
Packet& Packet::operator>>(u8& out_data) { Packet& Packet::Read(u8& out_data) {
Read(&out_data, sizeof(out_data)); Read(&out_data, sizeof(out_data));
return *this; return *this;
} }
Packet& Packet::operator>>(s16& out_data) { Packet& Packet::Read(s16& out_data) {
s16 value{}; s16 value{};
Read(&value, sizeof(value)); Read(&value, sizeof(value));
out_data = ntohs(value); out_data = ntohs(value);
return *this; return *this;
} }
Packet& Packet::operator>>(u16& out_data) { Packet& Packet::Read(u16& out_data) {
u16 value{}; u16 value{};
Read(&value, sizeof(value)); Read(&value, sizeof(value));
out_data = ntohs(value); out_data = ntohs(value);
return *this; return *this;
} }
Packet& Packet::operator>>(s32& out_data) { Packet& Packet::Read(s32& out_data) {
s32 value{}; s32 value{};
Read(&value, sizeof(value)); Read(&value, sizeof(value));
out_data = ntohl(value); out_data = ntohl(value);
return *this; return *this;
} }
Packet& Packet::operator>>(u32& out_data) { Packet& Packet::Read(u32& out_data) {
u32 value{}; u32 value{};
Read(&value, sizeof(value)); Read(&value, sizeof(value));
out_data = ntohl(value); out_data = ntohl(value);
return *this; return *this;
} }
Packet& Packet::operator>>(s64& out_data) { Packet& Packet::Read(s64& out_data) {
s64 value{}; s64 value{};
Read(&value, sizeof(value)); Read(&value, sizeof(value));
out_data = ntohll(value); out_data = ntohll(value);
return *this; return *this;
} }
Packet& Packet::operator>>(u64& out_data) { Packet& Packet::Read(u64& out_data) {
u64 value{}; u64 value{};
Read(&value, sizeof(value)); Read(&value, sizeof(value));
out_data = ntohll(value); out_data = ntohll(value);
return *this; return *this;
} }
Packet& Packet::operator>>(float& out_data) { Packet& Packet::Read(float& out_data) {
Read(&out_data, sizeof(out_data)); Read(&out_data, sizeof(out_data));
return *this; return *this;
} }
Packet& Packet::operator>>(double& out_data) { Packet& Packet::Read(double& out_data) {
Read(&out_data, sizeof(out_data)); Read(&out_data, sizeof(out_data));
return *this; return *this;
} }
Packet& Packet::operator>>(char* out_data) { Packet& Packet::Read(char* out_data) {
// First extract string length // First extract string length
u32 length = 0; u32 length = 0;
*this >> length; Read(length);
if ((length > 0) && CheckSize(length)) { if ((length > 0) && CheckSize(length)) {
// Then extract characters // Then extract characters
@ -152,10 +152,10 @@ Packet& Packet::operator>>(char* out_data) {
return *this; return *this;
} }
Packet& Packet::operator>>(std::string& out_data) { Packet& Packet::Read(std::string& out_data) {
// First extract string length // First extract string length
u32 length = 0; u32 length = 0;
*this >> length; Read(length);
out_data.clear(); out_data.clear();
if ((length > 0) && CheckSize(length)) { if ((length > 0) && CheckSize(length)) {
@ -169,71 +169,71 @@ Packet& Packet::operator>>(std::string& out_data) {
return *this; return *this;
} }
Packet& Packet::operator<<(bool in_data) { Packet& Packet::Write(bool in_data) {
*this << static_cast<u8>(in_data); Write(static_cast<u8>(in_data));
return *this; return *this;
} }
Packet& Packet::operator<<(s8 in_data) { Packet& Packet::Write(s8 in_data) {
Append(&in_data, sizeof(in_data)); Append(&in_data, sizeof(in_data));
return *this; return *this;
} }
Packet& Packet::operator<<(u8 in_data) { Packet& Packet::Write(u8 in_data) {
Append(&in_data, sizeof(in_data)); Append(&in_data, sizeof(in_data));
return *this; return *this;
} }
Packet& Packet::operator<<(s16 in_data) { Packet& Packet::Write(s16 in_data) {
s16 toWrite = htons(in_data); s16 toWrite = htons(in_data);
Append(&toWrite, sizeof(toWrite)); Append(&toWrite, sizeof(toWrite));
return *this; return *this;
} }
Packet& Packet::operator<<(u16 in_data) { Packet& Packet::Write(u16 in_data) {
u16 toWrite = htons(in_data); u16 toWrite = htons(in_data);
Append(&toWrite, sizeof(toWrite)); Append(&toWrite, sizeof(toWrite));
return *this; return *this;
} }
Packet& Packet::operator<<(s32 in_data) { Packet& Packet::Write(s32 in_data) {
s32 toWrite = htonl(in_data); s32 toWrite = htonl(in_data);
Append(&toWrite, sizeof(toWrite)); Append(&toWrite, sizeof(toWrite));
return *this; return *this;
} }
Packet& Packet::operator<<(u32 in_data) { Packet& Packet::Write(u32 in_data) {
u32 toWrite = htonl(in_data); u32 toWrite = htonl(in_data);
Append(&toWrite, sizeof(toWrite)); Append(&toWrite, sizeof(toWrite));
return *this; return *this;
} }
Packet& Packet::operator<<(s64 in_data) { Packet& Packet::Write(s64 in_data) {
s64 toWrite = htonll(in_data); s64 toWrite = htonll(in_data);
Append(&toWrite, sizeof(toWrite)); Append(&toWrite, sizeof(toWrite));
return *this; return *this;
} }
Packet& Packet::operator<<(u64 in_data) { Packet& Packet::Write(u64 in_data) {
u64 toWrite = htonll(in_data); u64 toWrite = htonll(in_data);
Append(&toWrite, sizeof(toWrite)); Append(&toWrite, sizeof(toWrite));
return *this; return *this;
} }
Packet& Packet::operator<<(float in_data) { Packet& Packet::Write(float in_data) {
Append(&in_data, sizeof(in_data)); Append(&in_data, sizeof(in_data));
return *this; return *this;
} }
Packet& Packet::operator<<(double in_data) { Packet& Packet::Write(double in_data) {
Append(&in_data, sizeof(in_data)); Append(&in_data, sizeof(in_data));
return *this; return *this;
} }
Packet& Packet::operator<<(const char* in_data) { Packet& Packet::Write(const char* in_data) {
// First insert string length // First insert string length
u32 length = static_cast<u32>(std::strlen(in_data)); u32 length = static_cast<u32>(std::strlen(in_data));
*this << length; Write(length);
// Then insert characters // Then insert characters
Append(in_data, length * sizeof(char)); Append(in_data, length * sizeof(char));
@ -241,10 +241,10 @@ Packet& Packet::operator<<(const char* in_data) {
return *this; return *this;
} }
Packet& Packet::operator<<(const std::string& in_data) { Packet& Packet::Write(const std::string& in_data) {
// First insert string length // First insert string length
u32 length = static_cast<u32>(in_data.size()); u32 length = static_cast<u32>(in_data.size());
*this << length; Write(length);
// Then insert characters // Then insert characters
if (length > 0) if (length > 0)

View file

@ -63,43 +63,43 @@ public:
explicit operator bool() const; explicit operator bool() const;
/// Overloads of operator >> to read data from the packet /// Overloads of read function to read data from the packet
Packet& operator>>(bool& out_data); Packet& Read(bool& out_data);
Packet& operator>>(s8& out_data); Packet& Read(s8& out_data);
Packet& operator>>(u8& out_data); Packet& Read(u8& out_data);
Packet& operator>>(s16& out_data); Packet& Read(s16& out_data);
Packet& operator>>(u16& out_data); Packet& Read(u16& out_data);
Packet& operator>>(s32& out_data); Packet& Read(s32& out_data);
Packet& operator>>(u32& out_data); Packet& Read(u32& out_data);
Packet& operator>>(s64& out_data); Packet& Read(s64& out_data);
Packet& operator>>(u64& out_data); Packet& Read(u64& out_data);
Packet& operator>>(float& out_data); Packet& Read(float& out_data);
Packet& operator>>(double& out_data); Packet& Read(double& out_data);
Packet& operator>>(char* out_data); Packet& Read(char* out_data);
Packet& operator>>(std::string& out_data); Packet& Read(std::string& out_data);
template <typename T> template <typename T>
Packet& operator>>(std::vector<T>& out_data); Packet& Read(std::vector<T>& out_data);
template <typename T, std::size_t S> template <typename T, std::size_t S>
Packet& operator>>(std::array<T, S>& out_data); Packet& Read(std::array<T, S>& out_data);
/// Overloads of operator << to write data into the packet /// Overloads of write function to write data into the packet
Packet& operator<<(bool in_data); Packet& Write(bool in_data);
Packet& operator<<(s8 in_data); Packet& Write(s8 in_data);
Packet& operator<<(u8 in_data); Packet& Write(u8 in_data);
Packet& operator<<(s16 in_data); Packet& Write(s16 in_data);
Packet& operator<<(u16 in_data); Packet& Write(u16 in_data);
Packet& operator<<(s32 in_data); Packet& Write(s32 in_data);
Packet& operator<<(u32 in_data); Packet& Write(u32 in_data);
Packet& operator<<(s64 in_data); Packet& Write(s64 in_data);
Packet& operator<<(u64 in_data); Packet& Write(u64 in_data);
Packet& operator<<(float in_data); Packet& Write(float in_data);
Packet& operator<<(double in_data); Packet& Write(double in_data);
Packet& operator<<(const char* in_data); Packet& Write(const char* in_data);
Packet& operator<<(const std::string& in_data); Packet& Write(const std::string& in_data);
template <typename T> template <typename T>
Packet& operator<<(const std::vector<T>& in_data); Packet& Write(const std::vector<T>& in_data);
template <typename T, std::size_t S> template <typename T, std::size_t S>
Packet& operator<<(const std::array<T, S>& data); Packet& Write(const std::array<T, S>& data);
private: private:
/** /**
@ -117,47 +117,47 @@ private:
}; };
template <typename T> template <typename T>
Packet& Packet::operator>>(std::vector<T>& out_data) { Packet& Packet::Read(std::vector<T>& out_data) {
// First extract the size // First extract the size
u32 size = 0; u32 size = 0;
*this >> size; Read(size);
out_data.resize(size); out_data.resize(size);
// Then extract the data // Then extract the data
for (std::size_t i = 0; i < out_data.size(); ++i) { for (std::size_t i = 0; i < out_data.size(); ++i) {
T character; T character;
*this >> character; Read(character);
out_data[i] = character; out_data[i] = character;
} }
return *this; return *this;
} }
template <typename T, std::size_t S> template <typename T, std::size_t S>
Packet& Packet::operator>>(std::array<T, S>& out_data) { Packet& Packet::Read(std::array<T, S>& out_data) {
for (std::size_t i = 0; i < out_data.size(); ++i) { for (std::size_t i = 0; i < out_data.size(); ++i) {
T character; T character;
*this >> character; Read(character);
out_data[i] = character; out_data[i] = character;
} }
return *this; return *this;
} }
template <typename T> template <typename T>
Packet& Packet::operator<<(const std::vector<T>& in_data) { Packet& Packet::Write(const std::vector<T>& in_data) {
// First insert the size // First insert the size
*this << static_cast<u32>(in_data.size()); Write(static_cast<u32>(in_data.size()));
// Then insert the data // Then insert the data
for (std::size_t i = 0; i < in_data.size(); ++i) { for (std::size_t i = 0; i < in_data.size(); ++i) {
*this << in_data[i]; Write(in_data[i]);
} }
return *this; return *this;
} }
template <typename T, std::size_t S> template <typename T, std::size_t S>
Packet& Packet::operator<<(const std::array<T, S>& in_data) { Packet& Packet::Write(const std::array<T, S>& in_data) {
for (std::size_t i = 0; i < in_data.size(); ++i) { for (std::size_t i = 0; i < in_data.size(); ++i) {
*this << in_data[i]; Write(in_data[i]);
} }
return *this; return *this;
} }

View file

@ -7,6 +7,7 @@
#include <mutex> #include <mutex>
#include <random> #include <random>
#include <regex> #include <regex>
#include <shared_mutex>
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#include "common/logging/log.h" #include "common/logging/log.h"
@ -43,9 +44,8 @@ public:
ENetPeer* peer; ///< The remote peer. ENetPeer* peer; ///< The remote peer.
}; };
using MemberList = std::vector<Member>; using MemberList = std::vector<Member>;
MemberList members; ///< Information about the members of this room MemberList members; ///< Information about the members of this room
mutable std::mutex member_mutex; ///< Mutex for locking the members list mutable std::shared_mutex member_mutex; ///< Mutex for locking the members list
/// This should be a std::shared_mutex as soon as C++17 is supported
UsernameBanList username_ban_list; ///< List of banned usernames UsernameBanList username_ban_list; ///< List of banned usernames
IPBanList ip_ban_list; ///< List of banned IP addresses IPBanList ip_ban_list; ///< List of banned IP addresses
@ -311,22 +311,22 @@ void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) {
packet.Append(event->packet->data, event->packet->dataLength); packet.Append(event->packet->data, event->packet->dataLength);
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
std::string nickname; std::string nickname;
packet >> nickname; packet.Read(nickname);
std::string console_id_hash; std::string console_id_hash;
packet >> console_id_hash; packet.Read(console_id_hash);
MacAddress preferred_mac; MacAddress preferred_mac;
packet >> preferred_mac; packet.Read(preferred_mac);
u32 client_version; u32 client_version;
packet >> client_version; packet.Read(client_version);
std::string pass; std::string pass;
packet >> pass; packet.Read(pass);
std::string token; std::string token;
packet >> token; packet.Read(token);
if (pass != password) { if (pass != password) {
SendWrongPassword(event->peer); SendWrongPassword(event->peer);
@ -387,9 +387,9 @@ void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) {
} }
// Check IP ban // Check IP ban
char ip_raw[256]; std::array<char, 256> ip_raw{};
enet_address_get_host_ip(&event->peer->address, ip_raw, sizeof(ip_raw) - 1); enet_address_get_host_ip(&event->peer->address, ip_raw.data(), sizeof(ip_raw) - 1);
ip = ip_raw; ip = ip_raw.data();
if (std::find(ip_ban_list.begin(), ip_ban_list.end(), ip) != ip_ban_list.end()) { if (std::find(ip_ban_list.begin(), ip_ban_list.end(), ip) != ip_ban_list.end()) {
SendUserBanned(event->peer); SendUserBanned(event->peer);
@ -425,7 +425,7 @@ void Room::RoomImpl::HandleModKickPacket(const ENetEvent* event) {
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
std::string nickname; std::string nickname;
packet >> nickname; packet.Read(nickname);
std::string username, ip; std::string username, ip;
{ {
@ -443,9 +443,9 @@ void Room::RoomImpl::HandleModKickPacket(const ENetEvent* event) {
username = target_member->user_data.username; username = target_member->user_data.username;
char ip_raw[256]; std::array<char, 256> ip_raw{};
enet_address_get_host_ip(&target_member->peer->address, ip_raw, sizeof(ip_raw) - 1); enet_address_get_host_ip(&target_member->peer->address, ip_raw.data(), sizeof(ip_raw) - 1);
ip = ip_raw; ip = ip_raw.data();
enet_peer_disconnect(target_member->peer, 0); enet_peer_disconnect(target_member->peer, 0);
members.erase(target_member); members.erase(target_member);
@ -467,7 +467,7 @@ void Room::RoomImpl::HandleModBanPacket(const ENetEvent* event) {
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
std::string nickname; std::string nickname;
packet >> nickname; packet.Read(nickname);
std::string username, ip; std::string username, ip;
{ {
@ -486,9 +486,9 @@ void Room::RoomImpl::HandleModBanPacket(const ENetEvent* event) {
nickname = target_member->nickname; nickname = target_member->nickname;
username = target_member->user_data.username; username = target_member->user_data.username;
char ip_raw[256]; std::array<char, 256> ip_raw{};
enet_address_get_host_ip(&target_member->peer->address, ip_raw, sizeof(ip_raw) - 1); enet_address_get_host_ip(&target_member->peer->address, ip_raw.data(), sizeof(ip_raw) - 1);
ip = ip_raw; ip = ip_raw.data();
enet_peer_disconnect(target_member->peer, 0); enet_peer_disconnect(target_member->peer, 0);
members.erase(target_member); members.erase(target_member);
@ -528,7 +528,7 @@ void Room::RoomImpl::HandleModUnbanPacket(const ENetEvent* event) {
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
std::string address; std::string address;
packet >> address; packet.Read(address);
bool unbanned = false; bool unbanned = false;
{ {
@ -613,7 +613,7 @@ bool Room::RoomImpl::HasModPermission(const ENetPeer* client) const {
void Room::RoomImpl::SendNameCollision(ENetPeer* client) { void Room::RoomImpl::SendNameCollision(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdNameCollision); packet.Write(static_cast<u8>(IdNameCollision));
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@ -623,7 +623,7 @@ void Room::RoomImpl::SendNameCollision(ENetPeer* client) {
void Room::RoomImpl::SendMacCollision(ENetPeer* client) { void Room::RoomImpl::SendMacCollision(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdMacCollision); packet.Write(static_cast<u8>(IdMacCollision));
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@ -633,7 +633,7 @@ void Room::RoomImpl::SendMacCollision(ENetPeer* client) {
void Room::RoomImpl::SendConsoleIdCollision(ENetPeer* client) { void Room::RoomImpl::SendConsoleIdCollision(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdConsoleIdCollision); packet.Write(static_cast<u8>(IdConsoleIdCollision));
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@ -643,7 +643,7 @@ void Room::RoomImpl::SendConsoleIdCollision(ENetPeer* client) {
void Room::RoomImpl::SendWrongPassword(ENetPeer* client) { void Room::RoomImpl::SendWrongPassword(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdWrongPassword); packet.Write(static_cast<u8>(IdWrongPassword));
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@ -653,7 +653,7 @@ void Room::RoomImpl::SendWrongPassword(ENetPeer* client) {
void Room::RoomImpl::SendRoomIsFull(ENetPeer* client) { void Room::RoomImpl::SendRoomIsFull(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdRoomIsFull); packet.Write(static_cast<u8>(IdRoomIsFull));
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@ -663,8 +663,8 @@ void Room::RoomImpl::SendRoomIsFull(ENetPeer* client) {
void Room::RoomImpl::SendVersionMismatch(ENetPeer* client) { void Room::RoomImpl::SendVersionMismatch(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdVersionMismatch); packet.Write(static_cast<u8>(IdVersionMismatch));
packet << network_version; packet.Write(network_version);
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@ -674,8 +674,8 @@ void Room::RoomImpl::SendVersionMismatch(ENetPeer* client) {
void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) { void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdJoinSuccess); packet.Write(static_cast<u8>(IdJoinSuccess));
packet << mac_address; packet.Write(mac_address);
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(client, 0, enet_packet); enet_peer_send(client, 0, enet_packet);
@ -684,8 +684,8 @@ void Room::RoomImpl::SendJoinSuccess(ENetPeer* client, MacAddress mac_address) {
void Room::RoomImpl::SendJoinSuccessAsMod(ENetPeer* client, MacAddress mac_address) { void Room::RoomImpl::SendJoinSuccessAsMod(ENetPeer* client, MacAddress mac_address) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdJoinSuccessAsMod); packet.Write(static_cast<u8>(IdJoinSuccessAsMod));
packet << mac_address; packet.Write(mac_address);
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(client, 0, enet_packet); enet_peer_send(client, 0, enet_packet);
@ -694,7 +694,7 @@ void Room::RoomImpl::SendJoinSuccessAsMod(ENetPeer* client, MacAddress mac_addre
void Room::RoomImpl::SendUserKicked(ENetPeer* client) { void Room::RoomImpl::SendUserKicked(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdHostKicked); packet.Write(static_cast<u8>(IdHostKicked));
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@ -704,7 +704,7 @@ void Room::RoomImpl::SendUserKicked(ENetPeer* client) {
void Room::RoomImpl::SendUserBanned(ENetPeer* client) { void Room::RoomImpl::SendUserBanned(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdHostBanned); packet.Write(static_cast<u8>(IdHostBanned));
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@ -714,7 +714,7 @@ void Room::RoomImpl::SendUserBanned(ENetPeer* client) {
void Room::RoomImpl::SendModPermissionDenied(ENetPeer* client) { void Room::RoomImpl::SendModPermissionDenied(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdModPermissionDenied); packet.Write(static_cast<u8>(IdModPermissionDenied));
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@ -724,7 +724,7 @@ void Room::RoomImpl::SendModPermissionDenied(ENetPeer* client) {
void Room::RoomImpl::SendModNoSuchUser(ENetPeer* client) { void Room::RoomImpl::SendModNoSuchUser(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdModNoSuchUser); packet.Write(static_cast<u8>(IdModNoSuchUser));
ENetPacket* enet_packet = ENetPacket* enet_packet =
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE); enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
@ -734,11 +734,11 @@ void Room::RoomImpl::SendModNoSuchUser(ENetPeer* client) {
void Room::RoomImpl::SendModBanListResponse(ENetPeer* client) { void Room::RoomImpl::SendModBanListResponse(ENetPeer* client) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdModBanListResponse); packet.Write(static_cast<u8>(IdModBanListResponse));
{ {
std::lock_guard lock(ban_list_mutex); std::lock_guard lock(ban_list_mutex);
packet << username_ban_list; packet.Write(username_ban_list);
packet << ip_ban_list; packet.Write(ip_ban_list);
} }
ENetPacket* enet_packet = ENetPacket* enet_packet =
@ -749,7 +749,7 @@ void Room::RoomImpl::SendModBanListResponse(ENetPeer* client) {
void Room::RoomImpl::SendCloseMessage() { void Room::RoomImpl::SendCloseMessage() {
Packet packet; Packet packet;
packet << static_cast<u8>(IdCloseRoom); packet.Write(static_cast<u8>(IdCloseRoom));
std::lock_guard lock(member_mutex); std::lock_guard lock(member_mutex);
if (!members.empty()) { if (!members.empty()) {
ENetPacket* enet_packet = ENetPacket* enet_packet =
@ -767,10 +767,10 @@ void Room::RoomImpl::SendCloseMessage() {
void Room::RoomImpl::SendStatusMessage(StatusMessageTypes type, const std::string& nickname, void Room::RoomImpl::SendStatusMessage(StatusMessageTypes type, const std::string& nickname,
const std::string& username, const std::string& ip) { const std::string& username, const std::string& ip) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdStatusMessage); packet.Write(static_cast<u8>(IdStatusMessage));
packet << static_cast<u8>(type); packet.Write(static_cast<u8>(type));
packet << nickname; packet.Write(nickname);
packet << username; packet.Write(username);
std::lock_guard lock(member_mutex); std::lock_guard lock(member_mutex);
if (!members.empty()) { if (!members.empty()) {
ENetPacket* enet_packet = ENetPacket* enet_packet =
@ -805,25 +805,25 @@ void Room::RoomImpl::SendStatusMessage(StatusMessageTypes type, const std::strin
void Room::RoomImpl::BroadcastRoomInformation() { void Room::RoomImpl::BroadcastRoomInformation() {
Packet packet; Packet packet;
packet << static_cast<u8>(IdRoomInformation); packet.Write(static_cast<u8>(IdRoomInformation));
packet << room_information.name; packet.Write(room_information.name);
packet << room_information.description; packet.Write(room_information.description);
packet << room_information.member_slots; packet.Write(room_information.member_slots);
packet << room_information.port; packet.Write(room_information.port);
packet << room_information.preferred_game.name; packet.Write(room_information.preferred_game.name);
packet << room_information.host_username; packet.Write(room_information.host_username);
packet << static_cast<u32>(members.size()); packet.Write(static_cast<u32>(members.size()));
{ {
std::lock_guard lock(member_mutex); std::lock_guard lock(member_mutex);
for (const auto& member : members) { for (const auto& member : members) {
packet << member.nickname; packet.Write(member.nickname);
packet << member.mac_address; packet.Write(member.mac_address);
packet << member.game_info.name; packet.Write(member.game_info.name);
packet << member.game_info.id; packet.Write(member.game_info.id);
packet << member.user_data.username; packet.Write(member.user_data.username);
packet << member.user_data.display_name; packet.Write(member.user_data.display_name);
packet << member.user_data.avatar_url; packet.Write(member.user_data.avatar_url);
} }
} }
@ -853,7 +853,7 @@ void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
in_packet.IgnoreBytes(sizeof(u8)); // WifiPacket Channel in_packet.IgnoreBytes(sizeof(u8)); // WifiPacket Channel
in_packet.IgnoreBytes(sizeof(MacAddress)); // WifiPacket Transmitter Address in_packet.IgnoreBytes(sizeof(MacAddress)); // WifiPacket Transmitter Address
MacAddress destination_address; MacAddress destination_address;
in_packet >> destination_address; in_packet.Read(destination_address);
Packet out_packet; Packet out_packet;
out_packet.Append(event->packet->data, event->packet->dataLength); out_packet.Append(event->packet->data, event->packet->dataLength);
@ -899,7 +899,7 @@ void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) {
in_packet.IgnoreBytes(sizeof(u8)); // Ignore the message type in_packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
std::string message; std::string message;
in_packet >> message; in_packet.Read(message);
auto CompareNetworkAddress = [event](const Member member) -> bool { auto CompareNetworkAddress = [event](const Member member) -> bool {
return member.peer == event->peer; return member.peer == event->peer;
}; };
@ -914,10 +914,10 @@ void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) {
message.resize(std::min(static_cast<u32>(message.size()), MaxMessageSize)); message.resize(std::min(static_cast<u32>(message.size()), MaxMessageSize));
Packet out_packet; Packet out_packet;
out_packet << static_cast<u8>(IdChatMessage); out_packet.Write(static_cast<u8>(IdChatMessage));
out_packet << sending_member->nickname; out_packet.Write(sending_member->nickname);
out_packet << sending_member->user_data.username; out_packet.Write(sending_member->user_data.username);
out_packet << message; out_packet.Write(message);
ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(), ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
ENET_PACKET_FLAG_RELIABLE); ENET_PACKET_FLAG_RELIABLE);
@ -949,8 +949,8 @@ void Room::RoomImpl::HandleGameNamePacket(const ENetEvent* event) {
in_packet.IgnoreBytes(sizeof(u8)); // Ignore the message type in_packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
GameInfo game_info; GameInfo game_info;
in_packet >> game_info.name; in_packet.Read(game_info.name);
in_packet >> game_info.id; in_packet.Read(game_info.id);
{ {
std::lock_guard lock(member_mutex); std::lock_guard lock(member_mutex);
@ -989,9 +989,9 @@ void Room::RoomImpl::HandleClientDisconnection(ENetPeer* client) {
nickname = member->nickname; nickname = member->nickname;
username = member->user_data.username; username = member->user_data.username;
char ip_raw[256]; std::array<char, 256> ip_raw{};
enet_address_get_host_ip(&member->peer->address, ip_raw, sizeof(ip_raw) - 1); enet_address_get_host_ip(&member->peer->address, ip_raw.data(), sizeof(ip_raw) - 1);
ip = ip_raw; ip = ip_raw.data();
members.erase(member); members.erase(member);
} }

View file

@ -280,13 +280,13 @@ void RoomMember::RoomMemberImpl::SendJoinRequest(const std::string& nickname_,
const std::string& password, const std::string& password,
const std::string& token) { const std::string& token) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdJoinRequest); packet.Write(static_cast<u8>(IdJoinRequest));
packet << nickname_; packet.Write(nickname_);
packet << console_id_hash; packet.Write(console_id_hash);
packet << preferred_mac; packet.Write(preferred_mac);
packet << network_version; packet.Write(network_version);
packet << password; packet.Write(password);
packet << token; packet.Write(token);
Send(std::move(packet)); Send(std::move(packet));
} }
@ -298,12 +298,12 @@ void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* ev
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
RoomInformation info{}; RoomInformation info{};
packet >> info.name; packet.Read(info.name);
packet >> info.description; packet.Read(info.description);
packet >> info.member_slots; packet.Read(info.member_slots);
packet >> info.port; packet.Read(info.port);
packet >> info.preferred_game.name; packet.Read(info.preferred_game.name);
packet >> info.host_username; packet.Read(info.host_username);
room_information.name = info.name; room_information.name = info.name;
room_information.description = info.description; room_information.description = info.description;
room_information.member_slots = info.member_slots; room_information.member_slots = info.member_slots;
@ -312,17 +312,17 @@ void RoomMember::RoomMemberImpl::HandleRoomInformationPacket(const ENetEvent* ev
room_information.host_username = info.host_username; room_information.host_username = info.host_username;
u32 num_members; u32 num_members;
packet >> num_members; packet.Read(num_members);
member_information.resize(num_members); member_information.resize(num_members);
for (auto& member : member_information) { for (auto& member : member_information) {
packet >> member.nickname; packet.Read(member.nickname);
packet >> member.mac_address; packet.Read(member.mac_address);
packet >> member.game_info.name; packet.Read(member.game_info.name);
packet >> member.game_info.id; packet.Read(member.game_info.id);
packet >> member.username; packet.Read(member.username);
packet >> member.display_name; packet.Read(member.display_name);
packet >> member.avatar_url; packet.Read(member.avatar_url);
{ {
std::lock_guard lock(username_mutex); std::lock_guard lock(username_mutex);
@ -342,7 +342,7 @@ void RoomMember::RoomMemberImpl::HandleJoinPacket(const ENetEvent* event) {
packet.IgnoreBytes(sizeof(u8)); // Ignore the message type packet.IgnoreBytes(sizeof(u8)); // Ignore the message type
// Parse the MAC Address from the packet // Parse the MAC Address from the packet
packet >> mac_address; packet.Read(mac_address);
} }
void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) { void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) {
@ -355,14 +355,14 @@ void RoomMember::RoomMemberImpl::HandleWifiPackets(const ENetEvent* event) {
// Parse the WifiPacket from the packet // Parse the WifiPacket from the packet
u8 frame_type; u8 frame_type;
packet >> frame_type; packet.Read(frame_type);
WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(frame_type); WifiPacket::PacketType type = static_cast<WifiPacket::PacketType>(frame_type);
wifi_packet.type = type; wifi_packet.type = type;
packet >> wifi_packet.channel; packet.Read(wifi_packet.channel);
packet >> wifi_packet.transmitter_address; packet.Read(wifi_packet.transmitter_address);
packet >> wifi_packet.destination_address; packet.Read(wifi_packet.destination_address);
packet >> wifi_packet.data; packet.Read(wifi_packet.data);
Invoke<WifiPacket>(wifi_packet); Invoke<WifiPacket>(wifi_packet);
} }
@ -375,9 +375,9 @@ void RoomMember::RoomMemberImpl::HandleChatPacket(const ENetEvent* event) {
packet.IgnoreBytes(sizeof(u8)); packet.IgnoreBytes(sizeof(u8));
ChatEntry chat_entry{}; ChatEntry chat_entry{};
packet >> chat_entry.nickname; packet.Read(chat_entry.nickname);
packet >> chat_entry.username; packet.Read(chat_entry.username);
packet >> chat_entry.message; packet.Read(chat_entry.message);
Invoke<ChatEntry>(chat_entry); Invoke<ChatEntry>(chat_entry);
} }
@ -390,10 +390,10 @@ void RoomMember::RoomMemberImpl::HandleStatusMessagePacket(const ENetEvent* even
StatusMessageEntry status_message_entry{}; StatusMessageEntry status_message_entry{};
u8 type{}; u8 type{};
packet >> type; packet.Read(type);
status_message_entry.type = static_cast<StatusMessageTypes>(type); status_message_entry.type = static_cast<StatusMessageTypes>(type);
packet >> status_message_entry.nickname; packet.Read(status_message_entry.nickname);
packet >> status_message_entry.username; packet.Read(status_message_entry.username);
Invoke<StatusMessageEntry>(status_message_entry); Invoke<StatusMessageEntry>(status_message_entry);
} }
@ -405,8 +405,8 @@ void RoomMember::RoomMemberImpl::HandleModBanListResponsePacket(const ENetEvent*
packet.IgnoreBytes(sizeof(u8)); packet.IgnoreBytes(sizeof(u8));
Room::BanList ban_list = {}; Room::BanList ban_list = {};
packet >> ban_list.first; packet.Read(ban_list.first);
packet >> ban_list.second; packet.Read(ban_list.second);
Invoke<Room::BanList>(ban_list); Invoke<Room::BanList>(ban_list);
} }
@ -586,19 +586,19 @@ bool RoomMember::IsConnected() const {
void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) { void RoomMember::SendWifiPacket(const WifiPacket& wifi_packet) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdWifiPacket); packet.Write(static_cast<u8>(IdWifiPacket));
packet << static_cast<u8>(wifi_packet.type); packet.Write(static_cast<u8>(wifi_packet.type));
packet << wifi_packet.channel; packet.Write(wifi_packet.channel);
packet << wifi_packet.transmitter_address; packet.Write(wifi_packet.transmitter_address);
packet << wifi_packet.destination_address; packet.Write(wifi_packet.destination_address);
packet << wifi_packet.data; packet.Write(wifi_packet.data);
room_member_impl->Send(std::move(packet)); room_member_impl->Send(std::move(packet));
} }
void RoomMember::SendChatMessage(const std::string& message) { void RoomMember::SendChatMessage(const std::string& message) {
Packet packet; Packet packet;
packet << static_cast<u8>(IdChatMessage); packet.Write(static_cast<u8>(IdChatMessage));
packet << message; packet.Write(message);
room_member_impl->Send(std::move(packet)); room_member_impl->Send(std::move(packet));
} }
@ -608,9 +608,9 @@ void RoomMember::SendGameInfo(const GameInfo& game_info) {
return; return;
Packet packet; Packet packet;
packet << static_cast<u8>(IdSetGameInfo); packet.Write(static_cast<u8>(IdSetGameInfo));
packet << game_info.name; packet.Write(game_info.name);
packet << game_info.id; packet.Write(game_info.id);
room_member_impl->Send(std::move(packet)); room_member_impl->Send(std::move(packet));
} }
@ -621,8 +621,8 @@ void RoomMember::SendModerationRequest(RoomMessageTypes type, const std::string&
return; return;
Packet packet; Packet packet;
packet << static_cast<u8>(type); packet.Write(static_cast<u8>(type));
packet << nickname; packet.Write(nickname);
room_member_impl->Send(std::move(packet)); room_member_impl->Send(std::move(packet));
} }
@ -631,7 +631,7 @@ void RoomMember::RequestBanList() {
return; return;
Packet packet; Packet packet;
packet << static_cast<u8>(IdModGetBanList); packet.Write(static_cast<u8>(IdModGetBanList));
room_member_impl->Send(std::move(packet)); room_member_impl->Send(std::move(packet));
} }

View file

@ -74,14 +74,18 @@ MultiplayerState::~MultiplayerState() {
} }
void MultiplayerState::Close() { void MultiplayerState::Close() {
if (host_room) if (host_room) {
host_room->close(); host_room->close();
if (direct_connect) }
if (direct_connect) {
direct_connect->close(); direct_connect->close();
if (client_room) }
if (client_room) {
client_room->close(); client_room->close();
if (lobby) }
if (lobby) {
lobby->close(); lobby->close();
}
} }
void MultiplayerState::retranslateUi() { void MultiplayerState::retranslateUi() {