-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnlineManager.cpp
More file actions
27 lines (23 loc) · 911 Bytes
/
Copy pathOnlineManager.cpp
File metadata and controls
27 lines (23 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "OnlineManager.h"
#include <mutex>
OnlineManager::OnlineManager() : online_users() {}
void OnlineManager::addUser(int user_id, int user_socket) {
std::unique_lock<std::shared_mutex> lock(map_mutex); // Exclusive lock
online_users[user_id] = user_socket;
}
void OnlineManager::removeUser(int user_id) {
std::unique_lock<std::shared_mutex> lock(map_mutex); // Exclusive lock
online_users.erase(user_id);
}
bool OnlineManager::isOnline(int user_id) const {
std::shared_lock<std::shared_mutex> lock(map_mutex); // Shared lock
return online_users.find(user_id) != online_users.end();
}
int OnlineManager::getSocket(int user_id) const {
std::shared_lock<std::shared_mutex> lock(map_mutex); // Shared lock
auto it = online_users.find(user_id);
if (it != online_users.end()) {
return it->second;
}
return -1; // Return -1 if the user is not found
}