qlocalserver.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/network/socket/qlocalserver.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtNetwork module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include "qlocalserver.h"-
41#include "qlocalserver_p.h"-
42#include "qlocalsocket.h"-
43-
44QT_BEGIN_NAMESPACE-
45-
46#ifndef QT_NO_LOCALSERVER-
47-
48/*!-
49 \class QLocalServer-
50 \since 4.4-
51 \inmodule QtNetwork-
52-
53 \brief The QLocalServer class provides a local socket based server.-
54-
55 This class makes it possible to accept incoming local socket-
56 connections.-
57-
58 Call listen() to have the server start listening-
59 for incoming connections on a specified key. The-
60 newConnection() signal is then emitted each time a client-
61 connects to the server.-
62-
63 Call nextPendingConnection() to accept the pending connection-
64 as a connected QLocalSocket. The function returns a pointer to a-
65 QLocalSocket that can be used for communicating with the client.-
66-
67 If an error occurs, serverError() returns the type of error, and-
68 errorString() can be called to get a human readable description-
69 of what happened.-
70-
71 When listening for connections, the name which the server is-
72 listening on is available through serverName().-
73-
74 Calling close() makes QLocalServer stop listening for incoming connections.-
75-
76 Although QLocalServer is designed for use with an event loop, it's possible-
77 to use it without one. In that case, you must use waitForNewConnection(),-
78 which blocks until either a connection is available or a timeout expires.-
79-
80 \sa QLocalSocket, QTcpServer-
81*/-
82-
83/*!-
84 \enum QLocalServer::SocketOption-
85 \since 5.0-
86-
87 This enum describes the possible options that can be used to create the-
88 socket. This changes the access permissions on platforms (Linux, Windows)-
89 that support access permissions on the socket. Both GroupAccess and OtherAccess-
90 may vary slightly in meanings depending on the platform.-
91-
92 \value NoOptions No access restrictions have been set.-
93 \value UserAccessOption-
94 Access is restricted to the same user as the process that created the socket.-
95 \value GroupAccessOption-
96 Access is restricted to the same group but not the user that created the socket on Linux.-
97 Access is restricted to the primary group of the process on Windows-
98 \value OtherAccessOption-
99 Access is available to everyone but the user and group that created the socket on Linux.-
100 Access is available to everyone on Windows.-
101 \value WorldAccessOption-
102 No access restrictions.-
103-
104 \sa socketOptions-
105*/-
106-
107-
108/*!-
109 Create a new local socket server with the given \a parent.-
110-
111 \sa listen()-
112 */-
113QLocalServer::QLocalServer(QObject *parent)-
114 : QObject(*new QLocalServerPrivate, parent)-
115{-
116 Q_D(QLocalServer);-
117 d->init();-
118}
executed 57 times by 3 tests: end of block
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
57
119-
120/*!-
121 Destroys the QLocalServer object. If the server is listening for-
122 connections, it is automatically closed.-
123-
124 Any client QLocalSockets that are still connected must either-
125 disconnect or be reparented before the server is deleted.-
126-
127 \sa close()-
128 */-
129QLocalServer::~QLocalServer()-
130{-
131 if (isListening())
isListening()Description
TRUEevaluated 40 times by 3 tests
Evaluated by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
FALSEevaluated 17 times by 1 test
Evaluated by:
  • tst_qlocalsocket - unknown status
17-40
132 close();
executed 40 times by 3 tests: close();
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
40
133}
executed 57 times by 3 tests: end of block
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
57
134-
135/*!-
136 \property QLocalServer::socketOptions-
137 \since 5.0-
138-
139 The setSocketOptions method controls how the socket operates.-
140 For example the socket may restrict access to what user ids can-
141 connect to the socket.-
142-
143 These options must be set before listen() is called.-
144-
145 In some cases, such as with Unix domain sockets on Linux, the-
146 access to the socket will be determined by file system permissions,-
147 and are created based on the umask. Setting the access flags will-
148 overide this and will restrict or permit access as specified.-
149-
150 Other Unix-based operating systems, such as \macos, do not-
151 honor file permissions for Unix domain sockets and by default-
152 have WorldAccess and these permission flags will have no effect.-
153-
154 On Windows, UserAccessOption is sufficient to allow a non-
155 elevated process to connect to a local server created by an-
156 elevated process run by the same user. GroupAccessOption-
157 refers to the primary group of the process (see TokenPrimaryGroup-
158 in the Windows documentation). OtherAccessOption refers to-
159 the well known "Everyone" group.-
160-
161 By default none of the flags are set, access permissions-
162 are the platform default.-
163-
164 \sa listen()-
165*/-
166void QLocalServer::setSocketOptions(SocketOptions options)-
167{-
168 Q_D(QLocalServer);-
169-
170 d->socketOptions = options;-
171}
executed 4 times by 1 test: end of block
Executed by:
  • tst_qlocalsocket - unknown status
4
172-
173/*!-
174 \since 5.0-
175 Returns the socket options set on the socket.-
176-
177 \sa setSocketOptions()-
178 */-
179QLocalServer::SocketOptions QLocalServer::socketOptions() const-
180{-
181 Q_D(const QLocalServer);-
182 return d->socketOptions;
never executed: return d->socketOptions;
0
183}-
184-
185/*!-
186 Stop listening for incoming connections. Existing connections are not-
187 affected, but any new connections will be refused.-
188-
189 \sa isListening(), listen()-
190 */-
191void QLocalServer::close()-
192{-
193 Q_D(QLocalServer);-
194 if (!isListening())
!isListening()Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • tst_qlocalsocket - unknown status
FALSEevaluated 50 times by 3 tests
Evaluated by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
5-50
195 return;
executed 5 times by 1 test: return;
Executed by:
  • tst_qlocalsocket - unknown status
5
196 qDeleteAll(d->pendingConnections);-
197 d->pendingConnections.clear();-
198 d->closeServer();-
199 d->serverName.clear();-
200 d->fullServerName.clear();-
201 d->errorString.clear();-
202 d->error = QAbstractSocket::UnknownSocketError;-
203}
executed 50 times by 3 tests: end of block
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
50
204-
205/*!-
206 Returns the human-readable message appropriate to the current error-
207 reported by serverError(). If no suitable string is available, an empty-
208 string is returned.-
209-
210 \sa serverError()-
211 */-
212QString QLocalServer::errorString() const-
213{-
214 Q_D(const QLocalServer);-
215 return d->errorString;
executed 42 times by 2 tests: return d->errorString;
Executed by:
  • tst_QNetworkReply
  • tst_qlocalsocket - unknown status
42
216}-
217-
218/*!-
219 Returns \c true if the server has a pending connection; otherwise-
220 returns \c false.-
221-
222 \sa nextPendingConnection(), setMaxPendingConnections()-
223 */-
224bool QLocalServer::hasPendingConnections() const-
225{-
226 Q_D(const QLocalServer);-
227 return !(d->pendingConnections.isEmpty());
executed 79 times by 2 tests: return !(d->pendingConnections.isEmpty());
Executed by:
  • tst_QNetworkReply
  • tst_qlocalsocket - unknown status
79
228}-
229-
230/*!-
231 This virtual function is called by QLocalServer when a new connection-
232 is available. \a socketDescriptor is the native socket descriptor for-
233 the accepted connection.-
234-
235 The base implementation creates a QLocalSocket, sets the socket descriptor-
236 and then stores the QLocalSocket in an internal list of pending-
237 connections. Finally newConnection() is emitted.-
238-
239 Reimplement this function to alter the server's behavior-
240 when a connection is available.-
241-
242 \sa newConnection(), nextPendingConnection(),-
243 QLocalSocket::setSocketDescriptor()-
244 */-
245void QLocalServer::incomingConnection(quintptr socketDescriptor)-
246{-
247 Q_D(QLocalServer);-
248 QLocalSocket *socket = new QLocalSocket(this);-
249 socket->setSocketDescriptor(socketDescriptor);-
250 d->pendingConnections.enqueue(socket);-
251 emit newConnection();-
252}
executed 77 times by 3 tests: end of block
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
77
253-
254/*!-
255 Returns \c true if the server is listening for incoming connections-
256 otherwise false.-
257-
258 \sa listen(), close()-
259 */-
260bool QLocalServer::isListening() const-
261{-
262 Q_D(const QLocalServer);-
263 return !(d->serverName.isEmpty());
executed 277 times by 3 tests: return !(d->serverName.isEmpty());
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
277
264}-
265-
266/*!-
267 Tells the server to listen for incoming connections on \a name.-
268 If the server is currently listening then it will return false.-
269 Return true on success otherwise false.-
270-
271 \a name can be a single name and QLocalServer will determine-
272 the correct platform specific path. serverName() will return-
273 the name that is passed into listen.-
274-
275 Usually you would just pass in a name like "foo", but on Unix this-
276 could also be a path such as "/tmp/foo" and on Windows this could-
277 be a pipe path such as "\\\\.\\pipe\\foo"-
278-
279 \note On Unix if the server crashes without closing listen will fail-
280 with AddressInUseError. To create a new server the file should be removed.-
281 On Windows two local servers can listen to the same pipe at the same-
282 time, but any connections will go to one of the server.-
283-
284 \sa serverName(), isListening(), close()-
285 */-
286bool QLocalServer::listen(const QString &name)-
287{-
288 Q_D(QLocalServer);-
289 if (isListening()) {
isListening()Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qlocalsocket - unknown status
FALSEevaluated 54 times by 3 tests
Evaluated by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
2-54
290 qWarning("QLocalServer::listen() called when already listening");-
291 return false;
executed 2 times by 1 test: return false;
Executed by:
  • tst_qlocalsocket - unknown status
2
292 }-
293-
294 if (name.isEmpty()) {
name.isEmpty()Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • tst_qlocalsocket - unknown status
FALSEevaluated 48 times by 3 tests
Evaluated by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
6-48
295 d->error = QAbstractSocket::HostNotFoundError;-
296 QString function = QLatin1String("QLocalServer::listen");-
297 d->errorString = tr("%1: Name error").arg(function);-
298 return false;
executed 6 times by 1 test: return false;
Executed by:
  • tst_qlocalsocket - unknown status
6
299 }-
300-
301 if (!d->listen(name)) {
!d->listen(name)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_qlocalsocket - unknown status
FALSEevaluated 46 times by 3 tests
Evaluated by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
2-46
302 d->serverName.clear();-
303 d->fullServerName.clear();-
304 return false;
executed 2 times by 1 test: return false;
Executed by:
  • tst_qlocalsocket - unknown status
2
305 }-
306-
307 d->serverName = name;-
308 return true;
executed 46 times by 3 tests: return true;
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
46
309}-
310-
311/*!-
312 \since 5.0-
313-
314 Instructs the server to listen for incoming connections on-
315 \a socketDescriptor. The property returns \c false if the server is-
316 currently listening. It returns \c true on success; otherwise,-
317 it returns \c false. The socket must be ready to accept-
318 new connections with no extra platform-specific functions-
319 called. The socket is set into non-blocking mode.-
320-
321 serverName(), fullServerName() may return a string with-
322 a name if this option is supported by the platform;-
323 otherwise, they return an empty QString.-
324-
325 \sa isListening(), close()-
326 */-
327bool QLocalServer::listen(qintptr socketDescriptor)-
328{-
329 Q_D(QLocalServer);-
330 if (isListening()) {
isListening()Description
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_qlocalsocket - unknown status
0-4
331 qWarning("QLocalServer::listen() called when already listening");-
332 return false;
never executed: return false;
0
333 }-
334-
335 d->serverName.clear();-
336 d->fullServerName.clear();-
337-
338 if (!d->listen(socketDescriptor)) {
!d->listen(socketDescriptor)Description
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • tst_qlocalsocket - unknown status
0-4
339 return false;
never executed: return false;
0
340 }-
341-
342 return true;
executed 4 times by 1 test: return true;
Executed by:
  • tst_qlocalsocket - unknown status
4
343}-
344-
345/*!-
346 Returns the maximum number of pending accepted connections.-
347 The default is 30.-
348-
349 \sa setMaxPendingConnections(), hasPendingConnections()-
350 */-
351int QLocalServer::maxPendingConnections() const-
352{-
353 Q_D(const QLocalServer);-
354 return d->maxPendingConnections;
executed 8 times by 1 test: return d->maxPendingConnections;
Executed by:
  • tst_qlocalsocket - unknown status
8
355}-
356-
357/*!-
358 \fn void QLocalServer::newConnection()-
359-
360 This signal is emitted every time a new connection is available.-
361-
362 \sa hasPendingConnections(), nextPendingConnection()-
363*/-
364-
365/*!-
366 Returns the next pending connection as a connected QLocalSocket object.-
367-
368 The socket is created as a child of the server, which means that it is-
369 automatically deleted when the QLocalServer object is destroyed. It is-
370 still a good idea to delete the object explicitly when you are done with-
371 it, to avoid wasting memory.-
372-
373 0 is returned if this function is called when there are no pending-
374 connections.-
375-
376 \sa hasPendingConnections(), newConnection(), incomingConnection()-
377 */-
378QLocalSocket *QLocalServer::nextPendingConnection()-
379{-
380 Q_D(QLocalServer);-
381 if (d->pendingConnections.isEmpty())
d->pendingConn...ions.isEmpty()Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • tst_qlocalsocket - unknown status
FALSEevaluated 71 times by 3 tests
Evaluated by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
10-71
382 return 0;
executed 10 times by 1 test: return 0;
Executed by:
  • tst_qlocalsocket - unknown status
10
383 QLocalSocket *nextSocket = d->pendingConnections.dequeue();-
384#ifndef QT_LOCALSOCKET_TCP-
385 if (d->pendingConnections.size() <= d->maxPendingConnections)
d->pendingConn...ingConnectionsDescription
TRUEevaluated 71 times by 3 tests
Evaluated by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
FALSEnever evaluated
0-71
386#ifndef Q_OS_WIN-
387 d->socketNotifier->setEnabled(true);
executed 71 times by 3 tests: d->socketNotifier->setEnabled(true);
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
71
388#else-
389 d->connectionEventNotifier->setEnabled(true);-
390#endif-
391#endif-
392 return nextSocket;
executed 71 times by 3 tests: return nextSocket;
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
71
393}-
394-
395/*!-
396 \since 4.5-
397-
398 Removes any server instance that might cause a call to listen() to fail-
399 and returns \c true if successful; otherwise returns \c false.-
400 This function is meant to recover from a crash, when the previous server-
401 instance has not been cleaned up.-
402-
403 On Windows, this function does nothing; on Unix, it removes the socket file-
404 given by \a name.-
405-
406 \warning Be careful to avoid removing sockets of running instances.-
407*/-
408bool QLocalServer::removeServer(const QString &name)-
409{-
410 return QLocalServerPrivate::removeServer(name);
executed 35 times by 2 tests: return QLocalServerPrivate::removeServer(name);
Executed by:
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
35
411}-
412-
413/*!-
414 Returns the server name if the server is listening for connections;-
415 otherwise returns QString()-
416-
417 \sa listen(), fullServerName()-
418 */-
419QString QLocalServer::serverName() const-
420{-
421 Q_D(const QLocalServer);-
422 return d->serverName;
executed 21 times by 1 test: return d->serverName;
Executed by:
  • tst_qlocalsocket - unknown status
21
423}-
424-
425/*!-
426 Returns the full path that the server is listening on.-
427-
428 Note: This is platform specific-
429-
430 \sa listen(), serverName()-
431 */-
432QString QLocalServer::fullServerName() const-
433{-
434 Q_D(const QLocalServer);-
435 return d->fullServerName;
executed 21 times by 1 test: return d->fullServerName;
Executed by:
  • tst_qlocalsocket - unknown status
21
436}-
437-
438/*!-
439 Returns the type of error that occurred last or NoError.-
440-
441 \sa errorString()-
442 */-
443QAbstractSocket::SocketError QLocalServer::serverError() const-
444{-
445 Q_D(const QLocalServer);-
446 return d->error;
executed 40 times by 1 test: return d->error;
Executed by:
  • tst_qlocalsocket - unknown status
40
447}-
448-
449/*!-
450 Sets the maximum number of pending accepted connections to-
451 \a numConnections. QLocalServer will accept no more than-
452 \a numConnections incoming connections before nextPendingConnection()-
453 is called.-
454-
455 Note: Even though QLocalServer will stop accepting new connections-
456 after it has reached its maximum number of pending connections,-
457 the operating system may still keep them in queue which will result-
458 in clients signaling that it is connected.-
459-
460 \sa maxPendingConnections(), hasPendingConnections()-
461 */-
462void QLocalServer::setMaxPendingConnections(int numConnections)-
463{-
464 Q_D(QLocalServer);-
465 d->maxPendingConnections = numConnections;-
466}
executed 10 times by 1 test: end of block
Executed by:
  • tst_qlocalsocket - unknown status
10
467-
468/*!-
469 Waits for at most \a msec milliseconds or until an incoming connection-
470 is available. Returns \c true if a connection is available; otherwise-
471 returns \c false. If the operation timed out and \a timedOut is not 0,-
472 *timedOut will be set to true.-
473-
474 This is a blocking function call. Its use is ill-advised in a-
475 single-threaded GUI application, since the whole application will stop-
476 responding until the function returns. waitForNewConnection() is mostly-
477 useful when there is no event loop available.-
478-
479 The non-blocking alternative is to connect to the newConnection() signal.-
480-
481 If msec is -1, this function will not time out.-
482-
483 \sa hasPendingConnections(), nextPendingConnection()-
484 */-
485bool QLocalServer::waitForNewConnection(int msec, bool *timedOut)-
486{-
487 Q_D(QLocalServer);-
488 if (timedOut)
timedOutDescription
TRUEevaluated 63 times by 1 test
Evaluated by:
  • tst_qlocalsocket - unknown status
FALSEevaluated 25 times by 3 tests
Evaluated by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
25-63
489 *timedOut = false;
executed 63 times by 1 test: *timedOut = false;
Executed by:
  • tst_qlocalsocket - unknown status
63
490-
491 if (!isListening())
!isListening()Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • tst_qlocalsocket - unknown status
FALSEevaluated 79 times by 3 tests
Evaluated by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
9-79
492 return false;
executed 9 times by 1 test: return false;
Executed by:
  • tst_qlocalsocket - unknown status
9
493-
494 d->waitForNewConnection(msec, timedOut);-
495-
496 return !d->pendingConnections.isEmpty();
executed 79 times by 3 tests: return !d->pendingConnections.isEmpty();
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
79
497}-
498-
499#endif-
500-
501QT_END_NAMESPACE-
502-
503#include "moc_qlocalserver.cpp"-
504-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9