KD SOAP API Documentation: KDSoapServerThread.cpp Source File

| KD SOAP API Documentation 2.2

Loading...
Searching...
No Matches
KDSoapServerThread.cpp
Go to the documentation of this file.
1/****************************************************************************
2**
3** This file is part of the KD Soap project.
4**
5** SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6**
7** SPDX-License-Identifier: MIT
8**
9****************************************************************************/
10#include "KDSoapServer.h"
11#include "KDSoapServerSocket_p.h"
12#include "KDSoapServerThread_p.h"
13#include "KDSoapSocketList_p.h"
14
15#include <QMetaType>
16
17KDSoapServerThread::KDSoapServerThread(QObject *parent)
18 : QThread(parent)
19 , d(nullptr)
20{
21 qRegisterMetaType<KDSoapServer *>("KDSoapServer*");
22 qRegisterMetaType<QSemaphore *>("QSemaphore*");
23}
24
25KDSoapServerThread::~KDSoapServerThread()
26{
27}
28
29 void KDSoapServerThread::run()
30{
31 KDSoapServerThreadImpl impl;
32 d = &impl;
33 m_semaphore.release();
34 exec();
35 d = nullptr;
36}
37
38 int KDSoapServerThread::socketCount() const
39{
40 if (d) {
41 return d->socketCount();
42 }
43 return 0;
44}
45
46 int KDSoapServerThread::socketCountForServer(const KDSoapServer *server) const
47{
48 if (d) {
49 return d->socketCountForServer(server);
50 }
51 return 0;
52}
53
54 int KDSoapServerThread::totalConnectionCountForServer(const KDSoapServer *server) const
55{
56 if (d) {
57 return d->totalConnectionCountForServer(server);
58 }
59 return 0;
60}
61
62 void KDSoapServerThread::resetTotalConnectionCountForServer(const KDSoapServer *server)
63{
64 if (d) {
65 d->resetTotalConnectionCountForServer(server);
66 }
67}
68
69 void KDSoapServerThread::disconnectSocketsForServer(KDSoapServer *server, QSemaphore &semaphore)
70{
71 if (d) {
72 // clang-format off
73 QMetaObject::invokeMethod(d, "disconnectSocketsForServer", Q_ARG(KDSoapServer*, server), Q_ARG(QSemaphore*, &semaphore));
74 // clang-format on
75 }
76}
77
78 void KDSoapServerThread::startThread()
79{
80 QThread::start();
81 m_semaphore.acquire(); // wait for init to be done
82}
83
84 void KDSoapServerThread::quitThread()
85{
86 QMetaObject::invokeMethod(d, "quit");
87}
88
89 void KDSoapServerThread::handleIncomingConnection(int socketDescriptor, KDSoapServer *server)
90{
91 d->addIncomingConnection();
92 // clang-format off
93 QMetaObject::invokeMethod(d, "handleIncomingConnection", Q_ARG(int, socketDescriptor), Q_ARG(KDSoapServer*, server));
94 // clang-format on
95}
96
98
99KDSoapServerThreadImpl::KDSoapServerThreadImpl()
100 : QObject(nullptr)
101 , m_incomingConnectionCount(0)
102{
103}
104
105KDSoapServerThreadImpl::~KDSoapServerThreadImpl()
106{
107 qDeleteAll(m_socketLists);
108}
109
110// Called from main thread!
111 int KDSoapServerThreadImpl::socketCount()
112{
113 QMutexLocker lock(&m_socketListMutex);
114 int sc = 0;
115 for (KDSoapSocketList *socketList : qAsConst(m_socketLists)) {
116 sc += socketList->socketCount();
117 }
118 sc += m_incomingConnectionCount.loadAcquire();
119 return sc;
120}
121
122 KDSoapSocketList *KDSoapServerThreadImpl::socketListForServer(KDSoapServer *server)
123{
124 KDSoapSocketList *sockets = m_socketLists.value(server);
125 if (sockets) {
126 return sockets;
127 }
128
129 sockets = new KDSoapSocketList(server); // creates the server object
130 m_socketLists.insert(server, sockets);
131 return sockets;
132}
133
134 void KDSoapServerThreadImpl::addIncomingConnection()
135{
136 m_incomingConnectionCount.fetchAndAddAcquire(1);
137}
138
139// Called in the thread itself so that the socket list and server object
140// are created in the thread.
141 void KDSoapServerThreadImpl::handleIncomingConnection(int socketDescriptor, KDSoapServer *server)
142{
143 QMutexLocker lock(&m_socketListMutex);
144 KDSoapSocketList *sockets = socketListForServer(server);
145 KDSoapServerSocket *socket = sockets->handleIncomingConnection(socketDescriptor);
146 Q_UNUSED(socket);
147 m_incomingConnectionCount.fetchAndAddAcquire(-1);
148}
149
150 void KDSoapServerThreadImpl::quit()
151{
152 thread()->quit();
153}
154
155 int KDSoapServerThreadImpl::socketCountForServer(const KDSoapServer *server)
156{
157 QMutexLocker lock(&m_socketListMutex);
158 KDSoapSocketList *sockets = m_socketLists.value(const_cast<KDSoapServer *>(server));
159 return sockets ? sockets->socketCount() : 0;
160}
161
162 void KDSoapServerThreadImpl::disconnectSocketsForServer(KDSoapServer *server, QSemaphore *semaphore)
163{
164 QMutexLocker lock(&m_socketListMutex);
165 KDSoapSocketList *sockets = m_socketLists.value(server);
166 if (sockets) {
167 sockets->disconnectAll();
168 }
169 semaphore->release();
170}
171
172 int KDSoapServerThreadImpl::totalConnectionCountForServer(const KDSoapServer *server)
173{
174 QMutexLocker lock(&m_socketListMutex);
175 KDSoapSocketList *sockets = m_socketLists.value(const_cast<KDSoapServer *>(server));
176 return sockets ? sockets->totalConnectionCount() : 0;
177}
178
179 void KDSoapServerThreadImpl::resetTotalConnectionCountForServer(const KDSoapServer *server)
180{
181 QMutexLocker lock(&m_socketListMutex);
182 KDSoapSocketList *sockets = m_socketLists.value(const_cast<KDSoapServer *>(server));
183 if (sockets) {
184 sockets->resetTotalConnectionCount();
185 }
186}
KDSoapServerSocket_p.h
KDSoapServerThread_p.h
KDSoapServer.h
KDSoapSocketList_p.h
KDSoapServerSocket
Definition KDSoapServerSocket_p.h:35
KDSoapServerThreadImpl
Definition KDSoapServerThread_p.h:22
KDSoapServerThreadImpl::~KDSoapServerThreadImpl
~KDSoapServerThreadImpl()
Definition KDSoapServerThread.cpp:105
KDSoapServerThreadImpl::KDSoapServerThreadImpl
KDSoapServerThreadImpl()
Definition KDSoapServerThread.cpp:99
KDSoapServerThreadImpl::totalConnectionCountForServer
int totalConnectionCountForServer(const KDSoapServer *server)
Definition KDSoapServerThread.cpp:172
KDSoapServerThreadImpl::disconnectSocketsForServer
void disconnectSocketsForServer(KDSoapServer *server, QSemaphore *semaphore)
Definition KDSoapServerThread.cpp:162
KDSoapServerThreadImpl::resetTotalConnectionCountForServer
void resetTotalConnectionCountForServer(const KDSoapServer *server)
Definition KDSoapServerThread.cpp:179
KDSoapServerThreadImpl::quit
void quit()
Definition KDSoapServerThread.cpp:150
KDSoapServerThreadImpl::socketCount
int socketCount()
Definition KDSoapServerThread.cpp:111
KDSoapServerThreadImpl::socketCountForServer
int socketCountForServer(const KDSoapServer *server)
Definition KDSoapServerThread.cpp:155
KDSoapServerThreadImpl::handleIncomingConnection
void handleIncomingConnection(int socketDescriptor, KDSoapServer *server)
Definition KDSoapServerThread.cpp:141
KDSoapServerThreadImpl::addIncomingConnection
void addIncomingConnection()
Definition KDSoapServerThread.cpp:134
KDSoapServerThread::socketCountForServer
int socketCountForServer(const KDSoapServer *server) const
Definition KDSoapServerThread.cpp:46
KDSoapServerThread::disconnectSocketsForServer
void disconnectSocketsForServer(KDSoapServer *server, QSemaphore &semaphore)
Definition KDSoapServerThread.cpp:69
KDSoapServerThread::KDSoapServerThread
KDSoapServerThread(QObject *parent=0)
Definition KDSoapServerThread.cpp:17
KDSoapServerThread::startThread
void startThread()
Definition KDSoapServerThread.cpp:78
KDSoapServerThread::totalConnectionCountForServer
int totalConnectionCountForServer(const KDSoapServer *server) const
Definition KDSoapServerThread.cpp:54
KDSoapServerThread::resetTotalConnectionCountForServer
void resetTotalConnectionCountForServer(const KDSoapServer *server)
Definition KDSoapServerThread.cpp:62
KDSoapServerThread::run
virtual void run() override
Definition KDSoapServerThread.cpp:29
KDSoapServerThread::~KDSoapServerThread
~KDSoapServerThread()
Definition KDSoapServerThread.cpp:25
KDSoapServerThread::handleIncomingConnection
void handleIncomingConnection(int socketDescriptor, KDSoapServer *server)
Definition KDSoapServerThread.cpp:89
KDSoapServerThread::quitThread
void quitThread()
Definition KDSoapServerThread.cpp:84
KDSoapServerThread::socketCount
int socketCount() const
Definition KDSoapServerThread.cpp:38
KDSoapServer
Definition KDSoapServer.h:30
KDSoapSocketList
Definition KDSoapSocketList_p.h:23
QAtomicInteger::fetchAndAddAcquire
T fetchAndAddAcquire(T valueToAdd)
QAtomicInteger::loadAcquire
T loadAcquire() const const
QHash::insert
QHash::iterator insert(const Key &key, const T &value)
QHash::value
const T value(const Key &key) const const
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QMutexLocker
QObject
QObject::qobject_cast
T qobject_cast(QObject *object)
QObject::thread
QThread * thread() const const
QSemaphore
QSemaphore::acquire
void acquire(int n)
QSemaphore::release
void release(int n)
QThread
QThread::exec
int exec()
QThread::quit
void quit()
QThread::start
void start(QThread::Priority priority)

© 2010-2024 Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-soap/
Generated on Sun Jan 7 2024 11:47:36 for KD SOAP API Documentation by doxygen 1.9.8