diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-06-22 15:09:42 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-06-22 15:09:42 +0800 |
| commit | 9b61c218b665f94f5cc383a5e607b951ce6af121 (patch) | |
| tree | 1de1bec4da34170a15737117a02023a39f673ade /src/server/client.cpp | |
| parent | 47375901d5158aa82c3178fe0840c5b77f142720 (diff) | |
refactor(server): properly use weak pointer
API!(server/client:create): require manually specifying server
API!(server/client:newClient): now returns a SP instead of WP
Diffstat (limited to 'src/server/client.cpp')
| -rw-r--r-- | src/server/client.cpp | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/server/client.cpp b/src/server/client.cpp index 6e42b69..e84a654 100644 --- a/src/server/client.cpp +++ b/src/server/client.cpp @@ -16,16 +16,16 @@ using namespace LibSock::Server; Client::Client(SP<Server> server, SP<Clients> clients, bool track, bool oneShot) : m_track(track) , m_oneShot(oneShot) { - pServer = std::move(server); - pClients = std::move(clients); + m_wpServer = std::move(server); + m_wpClients = std::move(clients); - if (!pClients) + if (m_wpClients.expired()) throw std::runtime_error("server:client: ClientManager doesn't exist"); - if (!pServer) + if (m_wpServer.expired()) throw std::runtime_error("server:client: Server doesn't exist"); - m_sockfd = std::make_shared<LibSock::CFileDescriptor>(accept(pServer->getSocket()->get(), reinterpret_cast<sockaddr *>(&m_addr), &m_addrLen)); // if this is in the init list, it will run before - // m_addrLen, so it won't work :/ + m_sockfd = std::make_shared<LibSock::CFileDescriptor>(accept(m_wpServer.lock()->getSocket()->get(), reinterpret_cast<sockaddr *>(&m_addr), &m_addrLen)); // if this is in the init list, it will run before + // m_addrLen, so it won't work :/ if (!m_sockfd->isValid()) throw std::runtime_error("server:client: Failed to create socket"); @@ -91,7 +91,7 @@ UP<SRecvData> Client::read(std::optional<std::function<bool(const SRecvData &)>> } if (m_track) - pClients->m_vDatas.push_back({recvData->data, self}); + m_wpClients.lock()->m_vDatas.push_back({recvData->data, m_self}); return recvData; } @@ -108,11 +108,11 @@ void Client::runLoop(bool resumeHist, std::optional<std::function<bool(const SRe recvLoop(cb); - pClients->kick(self); + m_wpClients.lock()->kick(m_self); } void Client::resumeHistory() { - for (const auto &chat : pClients->getDatas()) + for (const auto &chat : m_wpClients.lock()->getDatas()) write(chat.msg); } |
