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

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