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

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