-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionmanager.cpp
More file actions
44 lines (38 loc) · 1.36 KB
/
Copy pathsessionmanager.cpp
File metadata and controls
44 lines (38 loc) · 1.36 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "sessionmanager.h"
void SessionManager::setOnline(const QString& username,int socketFd){
std::lock_guard<std::mutex> lk(mutex_);
Session s;
s.username=username;
s.socketDescriptor=socketFd;
s.lastActive=QDateTime::currentDateTime();
s.online=true;
sessions_[username]=s;
}
void SessionManager::setOffline(const QString& username){
std::lock_guard<std::mutex> lk(mutex_);
auto it = sessions_.find(username);
if (it != sessions_.end()) {
it->second.online = false;
it->second.socketDescriptor = -1;
it->second.lastActive = QDateTime::currentDateTimeUtc();
}
}
bool SessionManager::isOnline(const QString& username) {
std::lock_guard<std::mutex> lk(mutex_);
auto it = sessions_.find(username);
return it != sessions_.end() && it->second.online;
}
std::optional<Session> SessionManager::getSession(const QString& username) {
std::lock_guard<std::mutex> lk(mutex_);
auto it = sessions_.find(username);
if (it == sessions_.end()) return std::nullopt;
return it->second;
}
void SessionManager::removeSession(const QString& username) {
std::lock_guard<std::mutex> lk(mutex_);
sessions_.erase(username);
}
std::unordered_map<QString, Session> SessionManager::snapshot() {
std::lock_guard<std::mutex> lk(mutex_);
return sessions_;
}