From bcf5b100646fb19b15ab5ba3ef72e707659e7208 Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Sat, 7 Feb 2015 17:25:43 +0100 Subject: [PATCH] Simplify session thread handling in Core Whenever we want to get a session thread for a given user, we first would check if we already have one and get that, or create a new one. Instead of doing this several times in the code, this commit just turns the former createSession() method into sessionForUser(), which will create a thread if needed. This also removes a dead assignment clang-analyzer complained about. --- src/core/core.cpp | 56 ++++++++++++++--------------------------------- src/core/core.h | 4 ++-- 2 files changed, 18 insertions(+), 42 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index a86424f4..e7b56392 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -222,7 +222,7 @@ Core::~Core() foreach(CoreAuthHandler *handler, _connectingClients) { handler->deleteLater(); // disconnect non authed clients } - qDeleteAll(sessions); + qDeleteAll(_sessions); qDeleteAll(_storageBackends); } @@ -234,7 +234,8 @@ void Core::saveState() CoreSettings s; QVariantMap state; QVariantList activeSessions; - foreach(UserId user, instance()->sessions.keys()) activeSessions << QVariant::fromValue(user); + foreach(UserId user, instance()->_sessions.keys()) + activeSessions << QVariant::fromValue(user); state["CoreStateVersion"] = 1; state["ActiveSessions"] = activeSessions; s.setCoreState(state); @@ -247,7 +248,7 @@ void Core::restoreState() // qWarning() << qPrintable(tr("Cannot restore a state for an unconfigured core!")); return; } - if (instance()->sessions.count()) { + if (instance()->_sessions.count()) { qWarning() << qPrintable(tr("Calling restoreState() even though active sessions exist!")); return; } @@ -265,7 +266,7 @@ void Core::restoreState() quInfo() << "Restoring previous core state..."; foreach(QVariant v, activeSessions) { UserId user = v.value(); - instance()->createSession(user, true); + instance()->sessionForUser(user, true); } } } @@ -577,19 +578,7 @@ void Core::setupClientSession(RemotePeer *peer, UserId uid) handler->deleteLater(); // Find or create session for validated user - SessionThread *session; - if (sessions.contains(uid)) { - session = sessions[uid]; - } - else { - session = createSession(uid); - if (!session) { - qWarning() << qPrintable(tr("Could not initialize session for client:")) << qPrintable(peer->description()); - peer->close(); - peer->deleteLater(); - return; - } - } + sessionForUser(uid); // as we are currently handling an event triggered by incoming data on this socket // it is unsafe to directly move the socket to the client thread. @@ -610,14 +599,7 @@ void Core::customEvent(QEvent *event) void Core::addClientHelper(RemotePeer *peer, UserId uid) { // Find or create session for validated user - if (!sessions.contains(uid)) { - qWarning() << qPrintable(tr("Could not find a session for client:")) << qPrintable(peer->description()); - peer->close(); - peer->deleteLater(); - return; - } - - SessionThread *session = sessions[uid]; + SessionThread *session = sessionForUser(uid); session->addClient(peer); } @@ -643,26 +625,20 @@ void Core::setupInternalClientSession(InternalPeer *clientPeer) clientPeer->setPeer(corePeer); // Find or create session for validated user - SessionThread *sessionThread; - if (sessions.contains(uid)) - sessionThread = sessions[uid]; - else - sessionThread = createSession(uid); - + SessionThread *sessionThread = sessionForUser(uid); sessionThread->addClient(corePeer); } -SessionThread *Core::createSession(UserId uid, bool restore) +SessionThread *Core::sessionForUser(UserId uid, bool restore) { - if (sessions.contains(uid)) { - qWarning() << "Calling createSession() when a session for the user already exists!"; - return 0; - } - SessionThread *sess = new SessionThread(uid, restore, this); - sessions[uid] = sess; - sess->start(); - return sess; + if (_sessions.contains(uid)) + return _sessions[uid]; + + SessionThread *session = new SessionThread(uid, restore, this); + _sessions[uid] = session; + session->start(); + return session; } diff --git a/src/core/core.h b/src/core/core.h index d544744a..a17211d8 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -530,7 +530,7 @@ private: void init(); static Core *instanceptr; - SessionThread *createSession(UserId userId, bool restoreState = false); + SessionThread *sessionForUser(UserId userId, bool restoreState = false); void addClientHelper(RemotePeer *peer, UserId uid); //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg); QString setupCoreForInternalUsage(); @@ -547,7 +547,7 @@ private: private: QSet _connectingClients; - QHash sessions; + QHash _sessions; Storage *_storage; QTimer _storageSyncTimer; -- 2.20.1