summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-05-20 19:00:37 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-05-20 19:00:37 +0800
commit9e3a7d5238d1b1c7a00e457f67a8108108909f01 (patch)
tree713628aff287107182d1bd2a0f23f0327618b3c1
parent092965b583bd0c377ed5d9f2af0e0f4bb0548354 (diff)
Revert "internal: use smart pointer for everything, no more naked pointer"
This reverts commit 72cf95bc9b1eb338457a0815eb1fe4f70b42ebb3. segfault/IOT instruct on client exit...
-rw-r--r--src/session.cpp6
-rw-r--r--src/session.hpp18
-rw-r--r--src/sessionManager.cpp29
-rw-r--r--src/sessionManager.hpp10
4 files changed, 32 insertions, 31 deletions
diff --git a/src/session.cpp b/src/session.cpp
index 8167a91..2814c41 100644
--- a/src/session.cpp
+++ b/src/session.cpp
@@ -151,7 +151,7 @@ void CSession::run() {
} else
log(LOG, "Client {} exited without even registering", m_ip);
- g_pSessionManager->kick(std::move(self));
+ g_pSessionManager->kick(self);
}
bool CSession::isMuted() const {
@@ -266,6 +266,6 @@ const std::string &CSession::getIp() const {
return m_ip;
}
-void CSession::setSelf(std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>> self) {
- this->self = std::move(self);
+void CSession::setSelf(std::pair<std::jthread, std::shared_ptr<CSession>> *self) {
+ this->self = self;
}
diff --git a/src/session.hpp b/src/session.hpp
index c3aecc1..1669964 100644
--- a/src/session.hpp
+++ b/src/session.hpp
@@ -24,7 +24,7 @@ class CSession {
const std::string &getName() const;
const std::string &getIp() const;
- void setSelf(std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>> self);
+ void setSelf(std::pair<std::jthread, std::shared_ptr<CSession>> *self);
std::unique_ptr<SRecvData> read();
std::unique_ptr<SRecvData> read(const std::string &msg, bool bypassDeaf = false);
@@ -52,14 +52,14 @@ class CSession {
WRITE
};
- std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>> self;
- Hyprutils::OS::CFileDescriptor m_sockfd;
- sockaddr_in m_addr;
- socklen_t m_addrLen = sizeof(m_addr);
- std::string m_name;
- std::string m_ip;
- int m_port;
- bool m_isAdmin = false;
+ std::pair<std::jthread, std::shared_ptr<CSession>> *self;
+ Hyprutils::OS::CFileDescriptor m_sockfd;
+ sockaddr_in m_addr;
+ socklen_t m_addrLen = sizeof(m_addr);
+ std::string m_name;
+ std::string m_ip;
+ int m_port;
+ bool m_isAdmin = false;
bool m_isReading = false;
bool m_bMuted = false;
diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp
index 6d9df28..b81aeaf 100644
--- a/src/sessionManager.cpp
+++ b/src/sessionManager.cpp
@@ -12,7 +12,7 @@ void CSessionManager::shutdownSessions() {
return;
for (auto &session : m_vSessions)
- kick(std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>>(&session), false, "Server shutting down");
+ kick(&session, false, "Server shutting down");
}
CSessionManager::CSessionManager() {
@@ -35,10 +35,11 @@ CSessionManager::~CSessionManager() {
log(SYS, "SessionManager: bye");
}
-void CSessionManager::newSession() {
+std::pair<std::jthread, std::shared_ptr<CSession>> *CSessionManager::newSession() {
std::shared_ptr session = std::make_shared<CSession>();
- auto instance = std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>>(&m_vSessions.emplace_back(std::jthread(&CSession::run, session.get()), session));
- instance->second->setSelf(std::move(instance));
+ const auto instance = &m_vSessions.emplace_back(std::jthread(&CSession::run, session.get()), session);
+ instance->second->setSelf(instance);
+ return instance;
}
void CSessionManager::run() {
@@ -65,7 +66,7 @@ bool CSessionManager::nameExists(const std::string &name) {
if (it != m_vSessions.end()) {
if (!it->second->isValid()) {
- kick(it->second, true, "Connection lost");
+ kick(it->second.get(), true, "Connection lost");
return false;
}
return true;
@@ -73,28 +74,28 @@ bool CSessionManager::nameExists(const std::string &name) {
return false;
}
-std::shared_ptr<CSession> CSessionManager::getByName(const std::string &name) const {
+CSession *CSessionManager::getByName(const std::string &name) const {
auto it = std::ranges::find_if(m_vSessions, [&name](const auto &s) { return s.second->getName() == name; });
- return it != m_vSessions.end() ? it->second : nullptr;
+ return it != m_vSessions.end() ? it->second.get() : nullptr;
}
-std::shared_ptr<CSession> CSessionManager::getByIp(const std::string &ip) const {
+CSession *CSessionManager::getByIp(const std::string &ip) const {
auto it = std::ranges::find_if(m_vSessions, [&ip](const auto &s) { log(LOG, s.second->m_ip); return s.second->m_ip == ip; });
- return it != m_vSessions.end() ? it->second : nullptr;
+ return it != m_vSessions.end() ? it->second.get() : nullptr;
}
std::vector<std::shared_ptr<CSession>> CSessionManager::getSessions() const {
return m_vSessions | std::views::transform([](const auto &s) { return s.second; }) | std::ranges::to<std::vector>();
}
-void CSessionManager::kick(std::shared_ptr<CSession> session, const bool kill, const std::string &reason) {
+void CSessionManager::kick(CSession *session, const bool kill, const std::string &reason) {
for (auto &_session : m_vSessions)
- if (_session.second == session)
- kick(std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>>(&_session), kill, reason);
+ if (_session.second.get() == session)
+ kick(&_session, kill, reason);
}
-void CSessionManager::kick(std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>> session, const bool kill, const std::string &reason) {
- auto it = std::ranges::find_if(m_vSessions, [&session](const auto &s) { return s.second.get() == session->second.get(); });
+void CSessionManager::kick(std::pair<std::jthread, std::shared_ptr<CSession>> *session, const bool kill, const std::string &reason) {
+ auto it = std::ranges::find_if(m_vSessions, [session](const auto &s) { return s.second.get() == session->second.get(); });
if (it != m_vSessions.end()) {
if (!reason.empty()) {
diff --git a/src/sessionManager.hpp b/src/sessionManager.hpp
index e969bd9..8e02a7d 100644
--- a/src/sessionManager.hpp
+++ b/src/sessionManager.hpp
@@ -12,19 +12,19 @@ class CSessionManager {
CSessionManager();
~CSessionManager();
- void newSession();
+ std::pair<std::jthread, std::shared_ptr<CSession>> *newSession();
void run();
void broadcast(const std::string &msg) const;
- void kick(std::shared_ptr<CSession> session, const bool kill = false, const std::string &reason = "");
- void kick(std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>> session, const bool kill = false, const std::string &reason = "");
+ void kick(CSession *session, const bool kill = false, const std::string &reason = "");
+ void kick(std::pair<std::jthread, std::shared_ptr<CSession>> *session, const bool kill = false, const std::string &reason = "");
void addSession(const CSession &session);
bool nameExists(const std::string &name);
void shutdownSessions();
- std::shared_ptr<CSession> getByName(const std::string &name) const;
- std::shared_ptr<CSession> getByIp(const std::string &ip) const;
+ CSession *getByName(const std::string &name) const;
+ CSession *getByIp(const std::string &ip) const;
std::vector<std::shared_ptr<CSession>> getSessions() const;
private: