general: Use deducation guides for std::lock_guard and std::unique_lock
Since C++17, the introduction of deduction guides for locking facilities means that we no longer need to hardcode the mutex type into the locks themselves, making it easier to switch mutex types, should it ever be necessary in the future.
This commit is contained in:
parent
d5bcb45b2a
commit
21c71d21ae
21 changed files with 124 additions and 122 deletions
|
@ -302,7 +302,7 @@ void Room::RoomImpl::StartLoop() {
|
|||
|
||||
void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
if (members.size() >= room_information.member_slots) {
|
||||
SendRoomIsFull(event->peer);
|
||||
return;
|
||||
|
@ -369,13 +369,13 @@ void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) {
|
|||
|
||||
std::string uid;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(verify_UID_mutex);
|
||||
std::lock_guard lock(verify_UID_mutex);
|
||||
uid = verify_UID;
|
||||
}
|
||||
member.user_data = verify_backend->LoadUserData(uid, token);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(ban_list_mutex);
|
||||
std::lock_guard lock(ban_list_mutex);
|
||||
|
||||
// Check username ban
|
||||
if (!member.user_data.username.empty() &&
|
||||
|
@ -401,7 +401,7 @@ void Room::RoomImpl::HandleJoinRequest(const ENetEvent* event) {
|
|||
SendStatusMessage(IdMemberJoin, member.nickname, member.user_data.username);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
members.push_back(std::move(member));
|
||||
}
|
||||
|
||||
|
@ -429,7 +429,7 @@ void Room::RoomImpl::HandleModKickPacket(const ENetEvent* event) {
|
|||
|
||||
std::string username;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
const auto target_member =
|
||||
std::find_if(members.begin(), members.end(),
|
||||
[&nickname](const auto& member) { return member.nickname == nickname; });
|
||||
|
@ -469,7 +469,7 @@ void Room::RoomImpl::HandleModBanPacket(const ENetEvent* event) {
|
|||
std::string ip;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
const auto target_member =
|
||||
std::find_if(members.begin(), members.end(),
|
||||
[&nickname](const auto& member) { return member.nickname == nickname; });
|
||||
|
@ -493,7 +493,7 @@ void Room::RoomImpl::HandleModBanPacket(const ENetEvent* event) {
|
|||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(ban_list_mutex);
|
||||
std::lock_guard lock(ban_list_mutex);
|
||||
|
||||
if (!username.empty()) {
|
||||
// Ban the forum username
|
||||
|
@ -530,7 +530,7 @@ void Room::RoomImpl::HandleModUnbanPacket(const ENetEvent* event) {
|
|||
|
||||
bool unbanned = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(ban_list_mutex);
|
||||
std::lock_guard lock(ban_list_mutex);
|
||||
|
||||
auto it = std::find(username_ban_list.begin(), username_ban_list.end(), address);
|
||||
if (it != username_ban_list.end()) {
|
||||
|
@ -568,28 +568,28 @@ bool Room::RoomImpl::IsValidNickname(const std::string& nickname) const {
|
|||
if (!std::regex_match(nickname, nickname_regex))
|
||||
return false;
|
||||
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
return std::all_of(members.begin(), members.end(),
|
||||
[&nickname](const auto& member) { return member.nickname != nickname; });
|
||||
}
|
||||
|
||||
bool Room::RoomImpl::IsValidMacAddress(const MacAddress& address) const {
|
||||
// A MAC address is valid if it is not already taken by anybody else in the room.
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
return std::all_of(members.begin(), members.end(),
|
||||
[&address](const auto& member) { return member.mac_address != address; });
|
||||
}
|
||||
|
||||
bool Room::RoomImpl::IsValidConsoleId(const std::string& console_id_hash) const {
|
||||
// A Console ID is valid if it is not already taken by anybody else in the room.
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
return std::all_of(members.begin(), members.end(), [&console_id_hash](const auto& member) {
|
||||
return member.console_id_hash != console_id_hash;
|
||||
});
|
||||
}
|
||||
|
||||
bool Room::RoomImpl::HasModPermission(const ENetPeer* client) const {
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
const auto sending_member =
|
||||
std::find_if(members.begin(), members.end(),
|
||||
[client](const auto& member) { return member.peer == client; });
|
||||
|
@ -734,7 +734,7 @@ void Room::RoomImpl::SendModBanListResponse(ENetPeer* client) {
|
|||
Packet packet;
|
||||
packet << static_cast<u8>(IdModBanListResponse);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(ban_list_mutex);
|
||||
std::lock_guard lock(ban_list_mutex);
|
||||
packet << username_ban_list;
|
||||
packet << ip_ban_list;
|
||||
}
|
||||
|
@ -748,7 +748,7 @@ void Room::RoomImpl::SendModBanListResponse(ENetPeer* client) {
|
|||
void Room::RoomImpl::SendCloseMessage() {
|
||||
Packet packet;
|
||||
packet << static_cast<u8>(IdCloseRoom);
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
if (!members.empty()) {
|
||||
ENetPacket* enet_packet =
|
||||
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
||||
|
@ -769,7 +769,7 @@ void Room::RoomImpl::SendStatusMessage(StatusMessageTypes type, const std::strin
|
|||
packet << static_cast<u8>(type);
|
||||
packet << nickname;
|
||||
packet << username;
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
if (!members.empty()) {
|
||||
ENetPacket* enet_packet =
|
||||
enet_packet_create(packet.GetData(), packet.GetDataSize(), ENET_PACKET_FLAG_RELIABLE);
|
||||
|
@ -792,7 +792,7 @@ void Room::RoomImpl::BroadcastRoomInformation() {
|
|||
|
||||
packet << static_cast<u32>(members.size());
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
for (const auto& member : members) {
|
||||
packet << member.nickname;
|
||||
packet << member.mac_address;
|
||||
|
@ -838,7 +838,7 @@ void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
|
|||
ENET_PACKET_FLAG_RELIABLE);
|
||||
|
||||
if (destination_address == BroadcastMac) { // Send the data to everyone except the sender
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
bool sent_packet = false;
|
||||
for (const auto& member : members) {
|
||||
if (member.peer != event->peer) {
|
||||
|
@ -851,7 +851,7 @@ void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
|
|||
enet_packet_destroy(enet_packet);
|
||||
}
|
||||
} else { // Send the data only to the destination client
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
auto member = std::find_if(members.begin(), members.end(),
|
||||
[destination_address](const Member& member) -> bool {
|
||||
return member.mac_address == destination_address;
|
||||
|
@ -881,7 +881,7 @@ void Room::RoomImpl::HandleChatPacket(const ENetEvent* event) {
|
|||
return member.peer == event->peer;
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
const auto sending_member = std::find_if(members.begin(), members.end(), CompareNetworkAddress);
|
||||
if (sending_member == members.end()) {
|
||||
return; // Received a chat message from a unknown sender
|
||||
|
@ -923,7 +923,7 @@ void Room::RoomImpl::HandleGameNamePacket(const ENetEvent* event) {
|
|||
in_packet >> game_info.id;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
auto member =
|
||||
std::find_if(members.begin(), members.end(), [event](const Member& member) -> bool {
|
||||
return member.peer == event->peer;
|
||||
|
@ -939,7 +939,7 @@ void Room::RoomImpl::HandleClientDisconnection(ENetPeer* client) {
|
|||
// Remove the client from the members list.
|
||||
std::string nickname, username;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(member_mutex);
|
||||
std::lock_guard lock(member_mutex);
|
||||
auto member = std::find_if(members.begin(), members.end(), [client](const Member& member) {
|
||||
return member.peer == client;
|
||||
});
|
||||
|
@ -1009,18 +1009,18 @@ const RoomInformation& Room::GetRoomInformation() const {
|
|||
}
|
||||
|
||||
std::string Room::GetVerifyUID() const {
|
||||
std::lock_guard<std::mutex> lock(room_impl->verify_UID_mutex);
|
||||
std::lock_guard lock(room_impl->verify_UID_mutex);
|
||||
return room_impl->verify_UID;
|
||||
}
|
||||
|
||||
Room::BanList Room::GetBanList() const {
|
||||
std::lock_guard<std::mutex> lock(room_impl->ban_list_mutex);
|
||||
std::lock_guard lock(room_impl->ban_list_mutex);
|
||||
return {room_impl->username_ban_list, room_impl->ip_ban_list};
|
||||
}
|
||||
|
||||
std::vector<Room::Member> Room::GetRoomMemberList() const {
|
||||
std::vector<Room::Member> member_list;
|
||||
std::lock_guard<std::mutex> lock(room_impl->member_mutex);
|
||||
std::lock_guard lock(room_impl->member_mutex);
|
||||
for (const auto& member_impl : room_impl->members) {
|
||||
Member member;
|
||||
member.nickname = member_impl.nickname;
|
||||
|
@ -1039,7 +1039,7 @@ bool Room::HasPassword() const {
|
|||
}
|
||||
|
||||
void Room::SetVerifyUID(const std::string& uid) {
|
||||
std::lock_guard<std::mutex> lock(room_impl->verify_UID_mutex);
|
||||
std::lock_guard lock(room_impl->verify_UID_mutex);
|
||||
room_impl->verify_UID = uid;
|
||||
}
|
||||
|
||||
|
@ -1054,7 +1054,7 @@ void Room::Destroy() {
|
|||
room_impl->room_information = {};
|
||||
room_impl->server = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(room_impl->member_mutex);
|
||||
std::lock_guard lock(room_impl->member_mutex);
|
||||
room_impl->members.clear();
|
||||
}
|
||||
room_impl->room_information.member_slots = 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue