qlocalsocket.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/network/socket/qlocalsocket.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 "qlocalsocket.h"-
41#include "qlocalsocket_p.h"-
42-
43#ifndef QT_NO_LOCALSOCKET-
44-
45QT_BEGIN_NAMESPACE-
46-
47/*!-
48 \class QLocalSocket-
49 \since 4.4-
50 \inmodule QtNetwork-
51-
52 \brief The QLocalSocket class provides a local socket.-
53-
54 On Windows this is a named pipe and on Unix this is a local domain socket.-
55-
56 If an error occurs, socketError() returns the type of error, and-
57 errorString() can be called to get a human readable description-
58 of what happened.-
59-
60 Although QLocalSocket is designed for use with an event loop, it's possible-
61 to use it without one. In that case, you must use waitForConnected(),-
62 waitForReadyRead(), waitForBytesWritten(), and waitForDisconnected()-
63 which blocks until the operation is complete or the timeout expires.-
64-
65 \sa QLocalServer-
66*/-
67-
68/*!-
69 \fn void QLocalSocket::connectToServer(OpenMode openMode)-
70 \since 5.1-
71-
72 Attempts to make a connection to serverName().-
73 setServerName() must be called before you open the connection.-
74 Alternatively you can use connectToServer(const QString &name, OpenMode openMode);-
75-
76 The socket is opened in the given \a openMode and first enters ConnectingState.-
77 If a connection is established, QLocalSocket enters ConnectedState and emits connected().-
78-
79 After calling this function, the socket can emit error() to signal that an error occurred.-
80-
81 \sa state(), serverName(), waitForConnected()-
82*/-
83-
84/*!-
85 \fn void QLocalSocket::open(OpenMode openMode)-
86-
87 Equivalent to connectToServer(OpenMode mode).-
88 The socket is opened in the given \a openMode to the server defined by setServerName().-
89-
90 Note that unlike in most other QIODevice subclasses, open() may not open the device directly.-
91 The function return false if the socket was already connected or if the server to connect-
92 to was not defined and true in any other case. The connected() or error() signals will be-
93 emitted once the device is actualy open (or the connection failed).-
94-
95 See connectToServer() for more details.-
96*/-
97-
98/*!-
99 \fn void QLocalSocket::connected()-
100-
101 This signal is emitted after connectToServer() has been called and-
102 a connection has been successfully established.-
103-
104 \sa connectToServer(), disconnected()-
105*/-
106-
107/*!-
108 \fn bool QLocalSocket::setSocketDescriptor(qintptr socketDescriptor,-
109 LocalSocketState socketState, OpenMode openMode)-
110-
111 Initializes QLocalSocket with the native socket descriptor-
112 \a socketDescriptor. Returns \c true if socketDescriptor is accepted-
113 as a valid socket descriptor; otherwise returns \c false. The socket is-
114 opened in the mode specified by \a openMode, and enters the socket state-
115 specified by \a socketState.-
116-
117 \note It is not possible to initialize two local sockets with the same-
118 native socket descriptor.-
119-
120 \sa socketDescriptor(), state(), openMode()-
121*/-
122-
123/*!-
124 \fn qintptr QLocalSocket::socketDescriptor() const-
125-
126 Returns the native socket descriptor of the QLocalSocket object if-
127 this is available; otherwise returns -1.-
128-
129 The socket descriptor is not available when QLocalSocket-
130 is in UnconnectedState.-
131-
132 \sa setSocketDescriptor()-
133*/-
134-
135/*!-
136 \fn qint64 QLocalSocket::readData(char *data, qint64 c)-
137 \reimp-
138*/-
139-
140/*!-
141 \fn qint64 QLocalSocket::writeData(const char *data, qint64 c)-
142 \reimp-
143*/-
144-
145/*!-
146 \fn void QLocalSocket::abort()-
147-
148 Aborts the current connection and resets the socket.-
149 Unlike disconnectFromServer(), this function immediately closes the socket,-
150 clearing any pending data in the write buffer.-
151-
152 \sa disconnectFromServer(), close()-
153*/-
154-
155/*!-
156 \fn qint64 QLocalSocket::bytesAvailable() const-
157 \reimp-
158*/-
159-
160/*!-
161 \fn qint64 QLocalSocket::bytesToWrite() const-
162 \reimp-
163*/-
164-
165/*!-
166 \fn bool QLocalSocket::canReadLine() const-
167 \reimp-
168*/-
169-
170/*!-
171 \fn void QLocalSocket::close()-
172 \reimp-
173*/-
174-
175/*!-
176 \fn bool QLocalSocket::waitForBytesWritten(int msecs)-
177 \reimp-
178*/-
179-
180/*!-
181 \fn bool QLocalSocket::flush()-
182-
183 This function writes as much as possible from the internal write buffer-
184 to the socket, without blocking. If any data was written, this function-
185 returns \c true; otherwise false is returned.-
186-
187 Call this function if you need QLocalSocket to start sending buffered data-
188 immediately. The number of bytes successfully written depends on the-
189 operating system. In most cases, you do not need to call this function,-
190 because QLocalSocket will start sending data automatically once control-
191 goes back to the event loop. In the absence of an event loop, call-
192 waitForBytesWritten() instead.-
193-
194 \sa write(), waitForBytesWritten()-
195*/-
196-
197/*!-
198 \fn void QLocalSocket::disconnectFromServer()-
199-
200 Attempts to close the socket. If there is pending data waiting to be-
201 written, QLocalSocket will enter ClosingState and wait until all data-
202 has been written. Eventually, it will enter UnconnectedState and emit-
203 the disconnectedFromServer() signal.-
204-
205 \sa connectToServer()-
206*/-
207-
208/*!-
209 \fn QLocalSocket::LocalSocketError QLocalSocket::error() const-
210-
211 Returns the type of error that last occurred.-
212-
213 \sa state(), errorString()-
214*/-
215-
216/*!-
217 \fn bool QLocalSocket::isValid() const-
218-
219 Returns \c true if the socket is valid and ready for use; otherwise-
220 returns \c false.-
221-
222 \note The socket's state must be ConnectedState before reading-
223 and writing can occur.-
224-
225 \sa state(), connectToServer()-
226*/-
227-
228/*!-
229 \fn qint64 QLocalSocket::readBufferSize() const-
230-
231 Returns the size of the internal read buffer. This limits the amount of-
232 data that the client can receive before you call read() or readAll().-
233 A read buffer size of 0 (the default) means that the buffer has no size-
234 limit, ensuring that no data is lost.-
235-
236 \sa setReadBufferSize(), read()-
237*/-
238-
239/*!-
240 \fn void QLocalSocket::setReadBufferSize(qint64 size)-
241-
242 Sets the size of QLocalSocket's internal read buffer to be \a size bytes.-
243-
244 If the buffer size is limited to a certain size, QLocalSocket won't-
245 buffer more than this size of data. Exceptionally, a buffer size of 0-
246 means that the read buffer is unlimited and all incoming data is buffered.-
247 This is the default.-
248-
249 This option is useful if you only read the data at certain points in-
250 time (e.g., in a real-time streaming application) or if you want to-
251 protect your socket against receiving too much data, which may eventually-
252 cause your application to run out of memory.-
253-
254 \sa readBufferSize(), read()-
255*/-
256-
257/*!-
258 \fn bool QLocalSocket::waitForConnected(int msecs)-
259-
260 Waits until the socket is connected, up to \a msecs milliseconds. If the-
261 connection has been established, this function returns \c true; otherwise-
262 it returns \c false. In the case where it returns \c false, you can call-
263 error() to determine the cause of the error.-
264-
265 The following example waits up to one second for a connection-
266 to be established:-
267-
268 \snippet code/src_network_socket_qlocalsocket_unix.cpp 0-
269-
270 If \a msecs is -1, this function will not time out.-
271-
272 \sa connectToServer(), connected()-
273*/-
274-
275/*!-
276 \fn bool QLocalSocket::waitForDisconnected(int msecs)-
277-
278 Waits until the socket has disconnected, up to \a msecs-
279 milliseconds. If the connection has been disconnected, this-
280 function returns \c true; otherwise it returns \c false. In the case-
281 where it returns \c false, you can call error() to determine-
282 the cause of the error.-
283-
284 The following example waits up to one second for a connection-
285 to be closed:-
286-
287 \snippet code/src_network_socket_qlocalsocket_unix.cpp 1-
288-
289 If \a msecs is -1, this function will not time out.-
290-
291 \sa disconnectFromServer(), close()-
292*/-
293-
294/*!-
295 \fn bool QLocalSocket::waitForReadyRead(int msecs)-
296-
297 This function blocks until data is available for reading and the-
298 \l{QIODevice::}{readyRead()} signal has been emitted. The function-
299 will timeout after \a msecs milliseconds; the default timeout is-
300 30000 milliseconds.-
301-
302 The function returns \c true if data is available for reading;-
303 otherwise it returns \c false (if an error occurred or the-
304 operation timed out).-
305-
306 \sa waitForBytesWritten()-
307*/-
308-
309/*!-
310 \fn void QLocalSocket::disconnected()-
311-
312 This signal is emitted when the socket has been disconnected.-
313-
314 \sa connectToServer(), disconnectFromServer(), abort(), connected()-
315*/-
316-
317/*!-
318 \fn void QLocalSocket::error(QLocalSocket::LocalSocketError socketError)-
319-
320 This signal is emitted after an error occurred. The \a socketError-
321 parameter describes the type of error that occurred.-
322-
323 QLocalSocket::LocalSocketError is not a registered metatype, so for queued-
324 connections, you will have to register it with Q_DECLARE_METATYPE() and-
325 qRegisterMetaType().-
326-
327 \sa error(), errorString(), {Creating Custom Qt Types}-
328*/-
329-
330/*!-
331 \fn void QLocalSocket::stateChanged(QLocalSocket::LocalSocketState socketState)-
332-
333 This signal is emitted whenever QLocalSocket's state changes.-
334 The \a socketState parameter is the new state.-
335-
336 QLocalSocket::SocketState is not a registered metatype, so for queued-
337 connections, you will have to register it with Q_DECLARE_METATYPE() and-
338 qRegisterMetaType().-
339-
340 \sa state(), {Creating Custom Qt Types}-
341*/-
342-
343/*!-
344 Creates a new local socket. The \a parent argument is passed to-
345 QObject's constructor.-
346 */-
347QLocalSocket::QLocalSocket(QObject * parent)-
348 : QIODevice(*new QLocalSocketPrivate, parent)-
349{-
350 Q_D(QLocalSocket);-
351 d->init();-
352}
executed 165 times by 3 tests: end of block
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
165
353-
354/*!-
355 Destroys the socket, closing the connection if necessary.-
356 */-
357QLocalSocket::~QLocalSocket()-
358{-
359 close();-
360#if !defined(Q_OS_WIN) && !defined(QT_LOCALSOCKET_TCP)-
361 Q_D(QLocalSocket);-
362 d->unixSocket.setParent(0);-
363#endif-
364}
executed 165 times by 3 tests: end of block
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
165
365-
366bool QLocalSocket::open(OpenMode openMode)-
367{-
368 connectToServer(openMode);-
369 return isOpen();
executed 1 time by 1 test: return isOpen();
Executed by:
  • tst_qlocalsocket - unknown status
1
370}-
371-
372/*! \overload-
373-
374 Set the server \a name and attempts to make a connection to it.-
375-
376 The socket is opened in the given \a openMode and first enters ConnectingState.-
377 If a connection is established, QLocalSocket enters ConnectedState and emits connected().-
378-
379 After calling this function, the socket can emit error() to signal that an error occurred.-
380-
381 \sa state(), serverName(), waitForConnected()-
382*/-
383void QLocalSocket::connectToServer(const QString &name, OpenMode openMode)-
384{-
385 setServerName(name);-
386 connectToServer(openMode);-
387}
executed 88 times by 2 tests: end of block
Executed by:
  • tst_QNetworkReply
  • tst_qlocalsocket - unknown status
88
388-
389/*!-
390 \since 5.1-
391-
392 Set the \a name of the peer to connect to.-
393 On Windows name is the name of a named pipe; on Unix name is the name of a local domain socket.-
394-
395 This function must be called when the socket is not connected.-
396*/-
397void QLocalSocket::setServerName(const QString & name)-
398{-
399 Q_D(QLocalSocket);-
400 if (d->state != UnconnectedState) {
d->state != UnconnectedStateDescription
TRUEnever evaluated
FALSEevaluated 89 times by 2 tests
Evaluated by:
  • tst_QNetworkReply
  • tst_qlocalsocket - unknown status
0-89
401 qWarning("QLocalSocket::setServerName() called while not in unconnected state");-
402 return;
never executed: return;
0
403 }-
404 d->serverName = name;-
405}
executed 89 times by 2 tests: end of block
Executed by:
  • tst_QNetworkReply
  • tst_qlocalsocket - unknown status
89
406-
407/*!-
408 Returns the name of the peer as specified by setServerName(), or an-
409 empty QString if setServerName() has not been called or connectToServer() failed.-
410-
411 \sa connectToServer(), fullServerName()-
412-
413 */-
414QString QLocalSocket::serverName() const-
415{-
416 Q_D(const QLocalSocket);-
417 return d->serverName;
executed 15 times by 1 test: return d->serverName;
Executed by:
  • tst_qlocalsocket - unknown status
15
418}-
419-
420/*!-
421 Returns the server path that the socket is connected to.-
422-
423 \note The return value of this function is platform specific.-
424-
425 \sa connectToServer(), serverName()-
426 */-
427QString QLocalSocket::fullServerName() const-
428{-
429 Q_D(const QLocalSocket);-
430 return d->fullServerName;
executed 15 times by 1 test: return d->fullServerName;
Executed by:
  • tst_qlocalsocket - unknown status
15
431}-
432-
433/*!-
434 Returns the state of the socket.-
435-
436 \sa error()-
437 */-
438QLocalSocket::LocalSocketState QLocalSocket::state() const-
439{-
440 Q_D(const QLocalSocket);-
441 return d->state;
executed 2780 times by 3 tests: return d->state;
Executed by:
  • tst_QNetworkReply
  • tst_QNetworkSession
  • tst_qlocalsocket - unknown status
2780
442}-
443-
444/*! \reimp-
445*/-
446bool QLocalSocket::isSequential() const-
447{-
448 return true;
executed 306 times by 2 tests: return true;
Executed by:
  • tst_QNetworkReply
  • tst_qlocalsocket - unknown status
306
449}-
450-
451/*!-
452 \enum QLocalSocket::LocalSocketError-
453-
454 The LocalServerError enumeration represents the errors that can occur.-
455 The most recent error can be retrieved through a call to-
456 \l QLocalSocket::error().-
457-
458 \value ConnectionRefusedError The connection was refused by-
459 the peer (or timed out).-
460 \value PeerClosedError The remote socket closed the connection.-
461 Note that the client socket (i.e., this socket) will be closed-
462 after the remote close notification has been sent.-
463 \value ServerNotFoundError The local socket name was not found.-
464 \value SocketAccessError The socket operation failed because the-
465 application lacked the required privileges.-
466 \value SocketResourceError The local system ran out of resources-
467 (e.g., too many sockets).-
468 \value SocketTimeoutError The socket operation timed out.-
469 \value DatagramTooLargeError The datagram was larger than the operating-
470 system's limit (which can be as low as 8192 bytes).-
471 \value ConnectionError An error occurred with the connection.-
472 \value UnsupportedSocketOperationError The requested socket operation-
473 is not supported by the local operating system.-
474 \value OperationError An operation was attempted while the socket was in a state that-
475 did not permit it.-
476 \value UnknownSocketError An unidentified error occurred.-
477 */-
478-
479/*!-
480 \enum QLocalSocket::LocalSocketState-
481-
482 This enum describes the different states in which a socket can be.-
483-
484 \sa QLocalSocket::state()-
485-
486 \value UnconnectedState The socket is not connected.-
487 \value ConnectingState The socket has started establishing a connection.-
488 \value ConnectedState A connection is established.-
489 \value ClosingState The socket is about to close-
490 (data may still be waiting to be written).-
491 */-
492-
493#ifndef QT_NO_DEBUG_STREAM-
494QDebug operator<<(QDebug debug, QLocalSocket::LocalSocketError error)-
495{-
496 QDebugStateSaver saver(debug);-
497 debug.resetFormat().nospace();-
498 switch (error) {-
499 case QLocalSocket::ConnectionRefusedError:
executed 1 time by 1 test: case QLocalSocket::ConnectionRefusedError:
Executed by:
  • tst_qlocalsocket - unknown status
1
500 debug << "QLocalSocket::ConnectionRefusedError";-
501 break;
executed 1 time by 1 test: break;
Executed by:
  • tst_qlocalsocket - unknown status
1
502 case QLocalSocket::PeerClosedError:
never executed: case QLocalSocket::PeerClosedError:
0
503 debug << "QLocalSocket::PeerClosedError";-
504 break;
never executed: break;
0
505 case QLocalSocket::ServerNotFoundError:
never executed: case QLocalSocket::ServerNotFoundError:
0
506 debug << "QLocalSocket::ServerNotFoundError";-
507 break;
never executed: break;
0
508 case QLocalSocket::SocketAccessError:
never executed: case QLocalSocket::SocketAccessError:
0
509 debug << "QLocalSocket::SocketAccessError";-
510 break;
never executed: break;
0
511 case QLocalSocket::SocketResourceError:
never executed: case QLocalSocket::SocketResourceError:
0
512 debug << "QLocalSocket::SocketResourceError";-
513 break;
never executed: break;
0
514 case QLocalSocket::SocketTimeoutError:
never executed: case QLocalSocket::SocketTimeoutError:
0
515 debug << "QLocalSocket::SocketTimeoutError";-
516 break;
never executed: break;
0
517 case QLocalSocket::DatagramTooLargeError:
never executed: case QLocalSocket::DatagramTooLargeError:
0
518 debug << "QLocalSocket::DatagramTooLargeError";-
519 break;
never executed: break;
0
520 case QLocalSocket::ConnectionError:
never executed: case QLocalSocket::ConnectionError:
0
521 debug << "QLocalSocket::ConnectionError";-
522 break;
never executed: break;
0
523 case QLocalSocket::UnsupportedSocketOperationError:
never executed: case QLocalSocket::UnsupportedSocketOperationError:
0
524 debug << "QLocalSocket::UnsupportedSocketOperationError";-
525 break;
never executed: break;
0
526 case QLocalSocket::UnknownSocketError:
never executed: case QLocalSocket::UnknownSocketError:
0
527 debug << "QLocalSocket::UnknownSocketError";-
528 break;
never executed: break;
0
529 default:
never executed: default:
0
530 debug << "QLocalSocket::SocketError(" << int(error) << ')';-
531 break;
never executed: break;
0
532 }-
533 return debug;
executed 1 time by 1 test: return debug;
Executed by:
  • tst_qlocalsocket - unknown status
1
534}-
535-
536QDebug operator<<(QDebug debug, QLocalSocket::LocalSocketState state)-
537{-
538 QDebugStateSaver saver(debug);-
539 debug.resetFormat().nospace();-
540 switch (state) {-
541 case QLocalSocket::UnconnectedState:
executed 1 time by 1 test: case QLocalSocket::UnconnectedState:
Executed by:
  • tst_qlocalsocket - unknown status
1
542 debug << "QLocalSocket::UnconnectedState";-
543 break;
executed 1 time by 1 test: break;
Executed by:
  • tst_qlocalsocket - unknown status
1
544 case QLocalSocket::ConnectingState:
never executed: case QLocalSocket::ConnectingState:
0
545 debug << "QLocalSocket::ConnectingState";-
546 break;
never executed: break;
0
547 case QLocalSocket::ConnectedState:
never executed: case QLocalSocket::ConnectedState:
0
548 debug << "QLocalSocket::ConnectedState";-
549 break;
never executed: break;
0
550 case QLocalSocket::ClosingState:
never executed: case QLocalSocket::ClosingState:
0
551 debug << "QLocalSocket::ClosingState";-
552 break;
never executed: break;
0
553 default:
never executed: default:
0
554 debug << "QLocalSocket::SocketState(" << int(state) << ')';-
555 break;
never executed: break;
0
556 }-
557 return debug;
executed 1 time by 1 test: return debug;
Executed by:
  • tst_qlocalsocket - unknown status
1
558}-
559#endif-
560-
561QT_END_NAMESPACE-
562-
563#endif-
564-
565#include "moc_qlocalsocket.cpp"-
Source codeSwitch to Preprocessed file

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