+
+ // 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(socket->peerAddress().toString());
+ socket->close();
+ return;
+ }
+ }
+
+ // 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.
+ QCoreApplication::postEvent(this, new AddClientEvent(socket, uid));
+}
+
+void Core::customEvent(QEvent *event) {
+ if(event->type() == AddClientEventId) {
+ AddClientEvent *addClientEvent = static_cast<AddClientEvent *>(event);
+ addClientHelper(addClientEvent->socket, addClientEvent->userId);
+ return;
+ }
+}
+
+void Core::addClientHelper(QTcpSocket *socket, UserId uid) {
+ // Find or create session for validated user
+ if(!sessions.contains(uid)) {
+ qWarning() << qPrintable(tr("Could not find a session for client:")) << qPrintable(socket->peerAddress().toString());