/
Shymer123
/
Messenger
Обзор
Документация
Войти
/
Shymer123
/
Messenger
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Server/SourceFiles/ServerCache/ServerCache.cpp
61 строка
1 KB
Shymer123
The server architecture has been changed and the server side has been adapted to json requests.
09 янв 2025, 09:12
09 янв 2025, 09:12
c1fb01d
Код
Авторство
О чём код?
#include "ServerCache.h" ServerCache& ServerCache::getInstance() { static ServerCache instance; return instance; } void ServerCache::addUserInServerCache(const int clientSocket, const int userID) { std::lock_guard<std::mutex> lock(cacheMutex); socketToID[clientSocket] = userID; IDToSocket[userID] = clientSocket; } void ServerCache::removeUserInServerCache(const int clientSocket) { std::lock_guard<std::mutex> lock(cacheMutex); auto it = socketToID.find(clientSocket); if(it != socketToID.end()) { int userID = it->second; socketToID.erase(clientSocket); IDToSocket.erase(userID); } } bool ServerCache::isUserInServerCache(const int userID) { std::lock_guard<std::mutex> lock(cacheMutex); return IDToSocket.find(userID) != IDToSocket.end(); } int ServerCache::getSocketByUserID(const int userID) { std::lock_guard<std::mutex> lock(cacheMutex); auto it = IDToSocket.find(userID); if(it != IDToSocket.end()) { return it->second; } return -1; } int ServerCache::getUserIDBySocket(const int clientSocket) { std::lock_guard<std::mutex> lock(cacheMutex); auto it = socketToID.find(clientSocket); if(it != socketToID.end()) { return it->second; } return -1; } const std::unordered_map<int, int>& ServerCache::getSocketToID() { std::lock_guard<std::mutex> lock(cacheMutex); return socketToID; }