Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/socket/qlocalsocket_unix.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 | #include "qnet_unix_p.h" | - | ||||||||||||
37 | - | |||||||||||||
38 | #ifndef QT_NO_LOCALSOCKET | - | ||||||||||||
39 | - | |||||||||||||
40 | #include <sys/types.h> | - | ||||||||||||
41 | #include <sys/socket.h> | - | ||||||||||||
42 | #include <sys/un.h> | - | ||||||||||||
43 | #include <unistd.h> | - | ||||||||||||
44 | #include <fcntl.h> | - | ||||||||||||
45 | #include <errno.h> | - | ||||||||||||
46 | - | |||||||||||||
47 | #include <qdir.h> | - | ||||||||||||
48 | #include <qdebug.h> | - | ||||||||||||
49 | #include <qelapsedtimer.h> | - | ||||||||||||
50 | - | |||||||||||||
51 | #ifdef Q_OS_VXWORKS | - | ||||||||||||
52 | # include <selectLib.h> | - | ||||||||||||
53 | #endif | - | ||||||||||||
54 | - | |||||||||||||
55 | #define QT_CONNECT_TIMEOUT 30000 | - | ||||||||||||
56 | - | |||||||||||||
57 | QT_BEGIN_NAMESPACE | - | ||||||||||||
58 | - | |||||||||||||
59 | QLocalSocketPrivate::QLocalSocketPrivate() : QIODevicePrivate(), | - | ||||||||||||
60 | delayConnect(0), | - | ||||||||||||
61 | connectTimer(0), | - | ||||||||||||
62 | connectingSocket(-1), | - | ||||||||||||
63 | connectingOpenMode(0), | - | ||||||||||||
64 | state(QLocalSocket::UnconnectedState) | - | ||||||||||||
65 | { | - | ||||||||||||
66 | } executed 165 times by 3 tests: end of block Executed by:
| 165 | ||||||||||||
67 | - | |||||||||||||
68 | void QLocalSocketPrivate::init() | - | ||||||||||||
69 | { | - | ||||||||||||
70 | Q_Q(QLocalSocket); | - | ||||||||||||
71 | // QIODevice signals | - | ||||||||||||
72 | q->connect(&unixSocket, SIGNAL(aboutToClose()), q, SIGNAL(aboutToClose())); | - | ||||||||||||
73 | q->connect(&unixSocket, SIGNAL(bytesWritten(qint64)), | - | ||||||||||||
74 | q, SIGNAL(bytesWritten(qint64))); | - | ||||||||||||
75 | q->connect(&unixSocket, SIGNAL(readyRead()), q, SIGNAL(readyRead())); | - | ||||||||||||
76 | // QAbstractSocket signals | - | ||||||||||||
77 | q->connect(&unixSocket, SIGNAL(connected()), q, SIGNAL(connected())); | - | ||||||||||||
78 | q->connect(&unixSocket, SIGNAL(disconnected()), q, SIGNAL(disconnected())); | - | ||||||||||||
79 | q->connect(&unixSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), | - | ||||||||||||
80 | q, SLOT(_q_stateChanged(QAbstractSocket::SocketState))); | - | ||||||||||||
81 | q->connect(&unixSocket, SIGNAL(error(QAbstractSocket::SocketError)), | - | ||||||||||||
82 | q, SLOT(_q_error(QAbstractSocket::SocketError))); | - | ||||||||||||
83 | q->connect(&unixSocket, SIGNAL(readChannelFinished()), q, SIGNAL(readChannelFinished())); | - | ||||||||||||
84 | unixSocket.setParent(q); | - | ||||||||||||
85 | } executed 165 times by 3 tests: end of block Executed by:
| 165 | ||||||||||||
86 | - | |||||||||||||
87 | void QLocalSocketPrivate::_q_error(QAbstractSocket::SocketError socketError) | - | ||||||||||||
88 | { | - | ||||||||||||
89 | Q_Q(QLocalSocket); | - | ||||||||||||
90 | QString function = QLatin1String("QLocalSocket"); | - | ||||||||||||
91 | QLocalSocket::LocalSocketError error = (QLocalSocket::LocalSocketError)socketError; | - | ||||||||||||
92 | QString errorString = generateErrorString(error, function); | - | ||||||||||||
93 | q->setErrorString(errorString); | - | ||||||||||||
94 | emit q->error(error); | - | ||||||||||||
95 | } executed 25 times by 3 tests: end of block Executed by:
| 25 | ||||||||||||
96 | - | |||||||||||||
97 | void QLocalSocketPrivate::_q_stateChanged(QAbstractSocket::SocketState newState) | - | ||||||||||||
98 | { | - | ||||||||||||
99 | Q_Q(QLocalSocket); | - | ||||||||||||
100 | QLocalSocket::LocalSocketState currentState = state; | - | ||||||||||||
101 | switch(newState) { | - | ||||||||||||
102 | case QAbstractSocket::UnconnectedState: executed 157 times by 3 tests: case QAbstractSocket::UnconnectedState: Executed by:
| 157 | ||||||||||||
103 | state = QLocalSocket::UnconnectedState; | - | ||||||||||||
104 | serverName.clear(); | - | ||||||||||||
105 | fullServerName.clear(); | - | ||||||||||||
106 | break; executed 157 times by 3 tests: break; Executed by:
| 157 | ||||||||||||
107 | case QAbstractSocket::ConnectingState: never executed: case QAbstractSocket::ConnectingState: | 0 | ||||||||||||
108 | state = QLocalSocket::ConnectingState; | - | ||||||||||||
109 | break; never executed: break; | 0 | ||||||||||||
110 | case QAbstractSocket::ConnectedState: executed 158 times by 3 tests: case QAbstractSocket::ConnectedState: Executed by:
| 158 | ||||||||||||
111 | state = QLocalSocket::ConnectedState; | - | ||||||||||||
112 | break; executed 158 times by 3 tests: break; Executed by:
| 158 | ||||||||||||
113 | case QAbstractSocket::ClosingState: executed 154 times by 3 tests: case QAbstractSocket::ClosingState: Executed by:
| 154 | ||||||||||||
114 | state = QLocalSocket::ClosingState; | - | ||||||||||||
115 | break; executed 154 times by 3 tests: break; Executed by:
| 154 | ||||||||||||
116 | default: never executed: default: | 0 | ||||||||||||
117 | #if defined QLOCALSOCKET_DEBUG | - | ||||||||||||
118 | qWarning() << "QLocalSocket::Unhandled socket state change:" << newState; | - | ||||||||||||
119 | #endif | - | ||||||||||||
120 | return; never executed: return; | 0 | ||||||||||||
121 | } | - | ||||||||||||
122 | if (currentState != state)
| 77-392 | ||||||||||||
123 | emit q->stateChanged(state); executed 392 times by 3 tests: q->stateChanged(state); Executed by:
| 392 | ||||||||||||
124 | } executed 469 times by 3 tests: end of block Executed by:
| 469 | ||||||||||||
125 | - | |||||||||||||
126 | QString QLocalSocketPrivate::generateErrorString(QLocalSocket::LocalSocketError error, const QString &function) const | - | ||||||||||||
127 | { | - | ||||||||||||
128 | QString errorString; | - | ||||||||||||
129 | switch (error) { | - | ||||||||||||
130 | case QLocalSocket::ConnectionRefusedError: never executed: case QLocalSocket::ConnectionRefusedError: | 0 | ||||||||||||
131 | errorString = QLocalSocket::tr("%1: Connection refused").arg(function); | - | ||||||||||||
132 | break; never executed: break; | 0 | ||||||||||||
133 | case QLocalSocket::PeerClosedError: executed 19 times by 3 tests: case QLocalSocket::PeerClosedError: Executed by:
| 19 | ||||||||||||
134 | errorString = QLocalSocket::tr("%1: Remote closed").arg(function); | - | ||||||||||||
135 | break; executed 19 times by 3 tests: break; Executed by:
| 19 | ||||||||||||
136 | case QLocalSocket::ServerNotFoundError: executed 8 times by 1 test: case QLocalSocket::ServerNotFoundError: Executed by:
| 8 | ||||||||||||
137 | errorString = QLocalSocket::tr("%1: Invalid name").arg(function); | - | ||||||||||||
138 | break; executed 8 times by 1 test: break; Executed by:
| 8 | ||||||||||||
139 | case QLocalSocket::SocketAccessError: never executed: case QLocalSocket::SocketAccessError: | 0 | ||||||||||||
140 | errorString = QLocalSocket::tr("%1: Socket access error").arg(function); | - | ||||||||||||
141 | break; never executed: break; | 0 | ||||||||||||
142 | case QLocalSocket::SocketResourceError: never executed: case QLocalSocket::SocketResourceError: | 0 | ||||||||||||
143 | errorString = QLocalSocket::tr("%1: Socket resource error").arg(function); | - | ||||||||||||
144 | break; never executed: break; | 0 | ||||||||||||
145 | case QLocalSocket::SocketTimeoutError: executed 6 times by 1 test: case QLocalSocket::SocketTimeoutError: Executed by:
| 6 | ||||||||||||
146 | errorString = QLocalSocket::tr("%1: Socket operation timed out").arg(function); | - | ||||||||||||
147 | break; executed 6 times by 1 test: break; Executed by:
| 6 | ||||||||||||
148 | case QLocalSocket::DatagramTooLargeError: never executed: case QLocalSocket::DatagramTooLargeError: | 0 | ||||||||||||
149 | errorString = QLocalSocket::tr("%1: Datagram too large").arg(function); | - | ||||||||||||
150 | break; never executed: break; | 0 | ||||||||||||
151 | case QLocalSocket::ConnectionError: never executed: case QLocalSocket::ConnectionError: | 0 | ||||||||||||
152 | errorString = QLocalSocket::tr("%1: Connection error").arg(function); | - | ||||||||||||
153 | break; never executed: break; | 0 | ||||||||||||
154 | case QLocalSocket::UnsupportedSocketOperationError: never executed: case QLocalSocket::UnsupportedSocketOperationError: | 0 | ||||||||||||
155 | errorString = QLocalSocket::tr("%1: The socket operation is not supported").arg(function); | - | ||||||||||||
156 | break; never executed: break; | 0 | ||||||||||||
157 | case QLocalSocket::OperationError: never executed: case QLocalSocket::OperationError: | 0 | ||||||||||||
158 | errorString = QLocalSocket::tr("%1: Operation not permitted when socket is in this state").arg(function); | - | ||||||||||||
159 | break; never executed: break; | 0 | ||||||||||||
160 | case QLocalSocket::UnknownSocketError: never executed: case QLocalSocket::UnknownSocketError: | 0 | ||||||||||||
161 | default: never executed: default: | 0 | ||||||||||||
162 | errorString = QLocalSocket::tr("%1: Unknown error %2").arg(function).arg(errno); | - | ||||||||||||
163 | } never executed: end of block | 0 | ||||||||||||
164 | return errorString; executed 33 times by 3 tests: return errorString; Executed by:
| 33 | ||||||||||||
165 | } | - | ||||||||||||
166 | - | |||||||||||||
167 | void QLocalSocketPrivate::errorOccurred(QLocalSocket::LocalSocketError error, const QString &function) | - | ||||||||||||
168 | { | - | ||||||||||||
169 | Q_Q(QLocalSocket); | - | ||||||||||||
170 | switch (error) { | - | ||||||||||||
171 | case QLocalSocket::ConnectionRefusedError: never executed: case QLocalSocket::ConnectionRefusedError: | 0 | ||||||||||||
172 | unixSocket.setSocketError(QAbstractSocket::ConnectionRefusedError); | - | ||||||||||||
173 | break; never executed: break; | 0 | ||||||||||||
174 | case QLocalSocket::PeerClosedError: never executed: case QLocalSocket::PeerClosedError: | 0 | ||||||||||||
175 | unixSocket.setSocketError(QAbstractSocket::RemoteHostClosedError); | - | ||||||||||||
176 | break; never executed: break; | 0 | ||||||||||||
177 | case QLocalSocket::ServerNotFoundError: executed 8 times by 1 test: case QLocalSocket::ServerNotFoundError: Executed by:
| 8 | ||||||||||||
178 | unixSocket.setSocketError(QAbstractSocket::HostNotFoundError); | - | ||||||||||||
179 | break; executed 8 times by 1 test: break; Executed by:
| 8 | ||||||||||||
180 | case QLocalSocket::SocketAccessError: never executed: case QLocalSocket::SocketAccessError: | 0 | ||||||||||||
181 | unixSocket.setSocketError(QAbstractSocket::SocketAccessError); | - | ||||||||||||
182 | break; never executed: break; | 0 | ||||||||||||
183 | case QLocalSocket::SocketResourceError: never executed: case QLocalSocket::SocketResourceError: | 0 | ||||||||||||
184 | unixSocket.setSocketError(QAbstractSocket::SocketResourceError); | - | ||||||||||||
185 | break; never executed: break; | 0 | ||||||||||||
186 | case QLocalSocket::SocketTimeoutError: never executed: case QLocalSocket::SocketTimeoutError: | 0 | ||||||||||||
187 | unixSocket.setSocketError(QAbstractSocket::SocketTimeoutError); | - | ||||||||||||
188 | break; never executed: break; | 0 | ||||||||||||
189 | case QLocalSocket::DatagramTooLargeError: never executed: case QLocalSocket::DatagramTooLargeError: | 0 | ||||||||||||
190 | unixSocket.setSocketError(QAbstractSocket::DatagramTooLargeError); | - | ||||||||||||
191 | break; never executed: break; | 0 | ||||||||||||
192 | case QLocalSocket::ConnectionError: never executed: case QLocalSocket::ConnectionError: | 0 | ||||||||||||
193 | unixSocket.setSocketError(QAbstractSocket::NetworkError); | - | ||||||||||||
194 | break; never executed: break; | 0 | ||||||||||||
195 | case QLocalSocket::UnsupportedSocketOperationError: never executed: case QLocalSocket::UnsupportedSocketOperationError: | 0 | ||||||||||||
196 | unixSocket.setSocketError(QAbstractSocket::UnsupportedSocketOperationError); | - | ||||||||||||
197 | break; never executed: break; | 0 | ||||||||||||
198 | case QLocalSocket::UnknownSocketError: never executed: case QLocalSocket::UnknownSocketError: | 0 | ||||||||||||
199 | default: never executed: default: | 0 | ||||||||||||
200 | unixSocket.setSocketError(QAbstractSocket::UnknownSocketError); | - | ||||||||||||
201 | } never executed: end of block | 0 | ||||||||||||
202 | - | |||||||||||||
203 | QString errorString = generateErrorString(error, function); | - | ||||||||||||
204 | q->setErrorString(errorString); | - | ||||||||||||
205 | emit q->error(error); | - | ||||||||||||
206 | - | |||||||||||||
207 | // errors cause a disconnect | - | ||||||||||||
208 | unixSocket.setSocketState(QAbstractSocket::UnconnectedState); | - | ||||||||||||
209 | bool stateChanged = (state != QLocalSocket::UnconnectedState); | - | ||||||||||||
210 | state = QLocalSocket::UnconnectedState; | - | ||||||||||||
211 | q->close(); | - | ||||||||||||
212 | if (stateChanged)
| 0-8 | ||||||||||||
213 | q->emit stateChanged(state); executed 8 times by 1 test: q-> stateChanged(state); Executed by:
| 8 | ||||||||||||
214 | } executed 8 times by 1 test: end of block Executed by:
| 8 | ||||||||||||
215 | - | |||||||||||||
216 | void QLocalSocket::connectToServer(OpenMode openMode) | - | ||||||||||||
217 | { | - | ||||||||||||
218 | Q_D(QLocalSocket); | - | ||||||||||||
219 | if (state() == ConnectedState || state() == ConnectingState) {
| 0-89 | ||||||||||||
220 | QString errorString = d->generateErrorString(QLocalSocket::OperationError, QLatin1String("QLocalSocket::connectToserver")); | - | ||||||||||||
221 | setErrorString(errorString); | - | ||||||||||||
222 | emit error(QLocalSocket::OperationError); | - | ||||||||||||
223 | return; never executed: return; | 0 | ||||||||||||
224 | } | - | ||||||||||||
225 | - | |||||||||||||
226 | d->errorString.clear(); | - | ||||||||||||
227 | d->unixSocket.setSocketState(QAbstractSocket::ConnectingState); | - | ||||||||||||
228 | d->state = ConnectingState; | - | ||||||||||||
229 | emit stateChanged(d->state); | - | ||||||||||||
230 | - | |||||||||||||
231 | if (d->serverName.isEmpty()) {
| 7-82 | ||||||||||||
232 | d->errorOccurred(ServerNotFoundError, | - | ||||||||||||
233 | QLatin1String("QLocalSocket::connectToServer")); | - | ||||||||||||
234 | return; executed 7 times by 1 test: return; Executed by:
| 7 | ||||||||||||
235 | } | - | ||||||||||||
236 | - | |||||||||||||
237 | // create the socket | - | ||||||||||||
238 | if (-1 == (d->connectingSocket = qt_safe_socket(PF_UNIX, SOCK_STREAM, 0, O_NONBLOCK))) {
| 0-82 | ||||||||||||
239 | d->errorOccurred(UnsupportedSocketOperationError, | - | ||||||||||||
240 | QLatin1String("QLocalSocket::connectToServer")); | - | ||||||||||||
241 | return; never executed: return; | 0 | ||||||||||||
242 | } | - | ||||||||||||
243 | - | |||||||||||||
244 | // _q_connectToSocket does the actual connecting | - | ||||||||||||
245 | d->connectingName = d->serverName; | - | ||||||||||||
246 | d->connectingOpenMode = openMode; | - | ||||||||||||
247 | d->_q_connectToSocket(); | - | ||||||||||||
248 | return; executed 82 times by 2 tests: return; Executed by:
| 82 | ||||||||||||
249 | } | - | ||||||||||||
250 | - | |||||||||||||
251 | /*! | - | ||||||||||||
252 | \internal | - | ||||||||||||
253 | - | |||||||||||||
254 | Tries to connect connectingName and connectingOpenMode | - | ||||||||||||
255 | - | |||||||||||||
256 | \sa connectToServer(), waitForConnected() | - | ||||||||||||
257 | */ | - | ||||||||||||
258 | void QLocalSocketPrivate::_q_connectToSocket() | - | ||||||||||||
259 | { | - | ||||||||||||
260 | Q_Q(QLocalSocket); | - | ||||||||||||
261 | QString connectingPathName; | - | ||||||||||||
262 | - | |||||||||||||
263 | // determine the full server path | - | ||||||||||||
264 | if (connectingName.startsWith(QLatin1Char('/'))) {
| 1-81 | ||||||||||||
265 | connectingPathName = connectingName; | - | ||||||||||||
266 | } else { executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
267 | connectingPathName = QDir::tempPath(); | - | ||||||||||||
268 | connectingPathName += QLatin1Char('/') + connectingName; | - | ||||||||||||
269 | } executed 81 times by 2 tests: end of block Executed by:
| 81 | ||||||||||||
270 | - | |||||||||||||
271 | const QByteArray encodedConnectingPathName = QFile::encodeName(connectingPathName); | - | ||||||||||||
272 | struct sockaddr_un name; | - | ||||||||||||
273 | name.sun_family = PF_UNIX; | - | ||||||||||||
274 | if (sizeof(name.sun_path) < (uint)encodedConnectingPathName.size() + 1) {
| 1-81 | ||||||||||||
275 | QString function = QLatin1String("QLocalSocket::connectToServer"); | - | ||||||||||||
276 | errorOccurred(QLocalSocket::ServerNotFoundError, function); | - | ||||||||||||
277 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||
278 | } | - | ||||||||||||
279 | ::memcpy(name.sun_path, encodedConnectingPathName.constData(), | - | ||||||||||||
280 | encodedConnectingPathName.size() + 1); | - | ||||||||||||
281 | if (-1 == qt_safe_connect(connectingSocket, (struct sockaddr *)&name, sizeof(name))) {
| 0-81 | ||||||||||||
282 | QString function = QLatin1String("QLocalSocket::connectToServer"); | - | ||||||||||||
283 | switch (errno) | - | ||||||||||||
284 | { | - | ||||||||||||
285 | case EINVAL: never executed: case 22: | 0 | ||||||||||||
286 | case ECONNREFUSED: never executed: case 111: | 0 | ||||||||||||
287 | errorOccurred(QLocalSocket::ConnectionRefusedError, function); | - | ||||||||||||
288 | break; never executed: break; | 0 | ||||||||||||
289 | case ENOENT: never executed: case 2: | 0 | ||||||||||||
290 | errorOccurred(QLocalSocket::ServerNotFoundError, function); | - | ||||||||||||
291 | break; never executed: break; | 0 | ||||||||||||
292 | case EACCES: never executed: case 13: | 0 | ||||||||||||
293 | case EPERM: never executed: case 1: | 0 | ||||||||||||
294 | errorOccurred(QLocalSocket::SocketAccessError, function); | - | ||||||||||||
295 | break; never executed: break; | 0 | ||||||||||||
296 | case ETIMEDOUT: never executed: case 110: | 0 | ||||||||||||
297 | errorOccurred(QLocalSocket::SocketTimeoutError, function); | - | ||||||||||||
298 | break; never executed: break; | 0 | ||||||||||||
299 | case EAGAIN: never executed: case 11: | 0 | ||||||||||||
300 | // Try again later, all of the sockets listening are full | - | ||||||||||||
301 | if (!delayConnect) {
| 0 | ||||||||||||
302 | delayConnect = new QSocketNotifier(connectingSocket, QSocketNotifier::Write, q); | - | ||||||||||||
303 | q->connect(delayConnect, SIGNAL(activated(int)), q, SLOT(_q_connectToSocket())); | - | ||||||||||||
304 | } never executed: end of block | 0 | ||||||||||||
305 | if (!connectTimer) {
| 0 | ||||||||||||
306 | connectTimer = new QTimer(q); | - | ||||||||||||
307 | q->connect(connectTimer, SIGNAL(timeout()), | - | ||||||||||||
308 | q, SLOT(_q_abortConnectionAttempt()), | - | ||||||||||||
309 | Qt::DirectConnection); | - | ||||||||||||
310 | connectTimer->start(QT_CONNECT_TIMEOUT); | - | ||||||||||||
311 | } never executed: end of block | 0 | ||||||||||||
312 | delayConnect->setEnabled(true); | - | ||||||||||||
313 | break; never executed: break; | 0 | ||||||||||||
314 | default: never executed: default: | 0 | ||||||||||||
315 | errorOccurred(QLocalSocket::UnknownSocketError, function); | - | ||||||||||||
316 | } never executed: end of block | 0 | ||||||||||||
317 | return; never executed: return; | 0 | ||||||||||||
318 | } | - | ||||||||||||
319 | - | |||||||||||||
320 | // connected! | - | ||||||||||||
321 | cancelDelayedConnect(); | - | ||||||||||||
322 | - | |||||||||||||
323 | serverName = connectingName; | - | ||||||||||||
324 | fullServerName = connectingPathName; | - | ||||||||||||
325 | if (unixSocket.setSocketDescriptor(connectingSocket,
| 0-81 | ||||||||||||
326 | QAbstractSocket::ConnectedState, connectingOpenMode)) {
| 0-81 | ||||||||||||
327 | q->QIODevice::open(connectingOpenMode | QIODevice::Unbuffered); | - | ||||||||||||
328 | q->emit connected(); | - | ||||||||||||
329 | } else { executed 81 times by 2 tests: end of block Executed by:
| 81 | ||||||||||||
330 | QString function = QLatin1String("QLocalSocket::connectToServer"); | - | ||||||||||||
331 | errorOccurred(QLocalSocket::UnknownSocketError, function); | - | ||||||||||||
332 | } never executed: end of block | 0 | ||||||||||||
333 | connectingSocket = -1; | - | ||||||||||||
334 | connectingName.clear(); | - | ||||||||||||
335 | connectingOpenMode = 0; | - | ||||||||||||
336 | } executed 81 times by 2 tests: end of block Executed by:
| 81 | ||||||||||||
337 | - | |||||||||||||
338 | bool QLocalSocket::setSocketDescriptor(qintptr socketDescriptor, | - | ||||||||||||
339 | LocalSocketState socketState, OpenMode openMode) | - | ||||||||||||
340 | { | - | ||||||||||||
341 | Q_D(QLocalSocket); | - | ||||||||||||
342 | QAbstractSocket::SocketState newSocketState = QAbstractSocket::UnconnectedState; | - | ||||||||||||
343 | switch (socketState) { | - | ||||||||||||
344 | case ConnectingState: executed 1 time by 1 test: case ConnectingState: Executed by:
| 1 | ||||||||||||
345 | newSocketState = QAbstractSocket::ConnectingState; | - | ||||||||||||
346 | break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||
347 | case ConnectedState: executed 77 times by 3 tests: case ConnectedState: Executed by:
| 77 | ||||||||||||
348 | newSocketState = QAbstractSocket::ConnectedState; | - | ||||||||||||
349 | break; executed 77 times by 3 tests: break; Executed by:
| 77 | ||||||||||||
350 | case ClosingState: never executed: case ClosingState: | 0 | ||||||||||||
351 | newSocketState = QAbstractSocket::ClosingState; | - | ||||||||||||
352 | break; never executed: break; | 0 | ||||||||||||
353 | case UnconnectedState: never executed: case UnconnectedState: | 0 | ||||||||||||
354 | newSocketState = QAbstractSocket::UnconnectedState; | - | ||||||||||||
355 | break; never executed: break; | 0 | ||||||||||||
356 | } | - | ||||||||||||
357 | QIODevice::open(openMode); | - | ||||||||||||
358 | d->state = socketState; | - | ||||||||||||
359 | return d->unixSocket.setSocketDescriptor(socketDescriptor, executed 78 times by 3 tests: return d->unixSocket.setSocketDescriptor(socketDescriptor, newSocketState, openMode); Executed by:
| 78 | ||||||||||||
360 | newSocketState, openMode); executed 78 times by 3 tests: return d->unixSocket.setSocketDescriptor(socketDescriptor, newSocketState, openMode); Executed by:
| 78 | ||||||||||||
361 | } | - | ||||||||||||
362 | - | |||||||||||||
363 | void QLocalSocketPrivate::_q_abortConnectionAttempt() | - | ||||||||||||
364 | { | - | ||||||||||||
365 | Q_Q(QLocalSocket); | - | ||||||||||||
366 | q->close(); | - | ||||||||||||
367 | } never executed: end of block | 0 | ||||||||||||
368 | - | |||||||||||||
369 | void QLocalSocketPrivate::cancelDelayedConnect() | - | ||||||||||||
370 | { | - | ||||||||||||
371 | if (delayConnect) {
| 0-305 | ||||||||||||
372 | delayConnect->setEnabled(false); | - | ||||||||||||
373 | delete delayConnect; | - | ||||||||||||
374 | delayConnect = 0; | - | ||||||||||||
375 | connectTimer->stop(); | - | ||||||||||||
376 | delete connectTimer; | - | ||||||||||||
377 | connectTimer = 0; | - | ||||||||||||
378 | } never executed: end of block | 0 | ||||||||||||
379 | } executed 305 times by 3 tests: end of block Executed by:
| 305 | ||||||||||||
380 | - | |||||||||||||
381 | qintptr QLocalSocket::socketDescriptor() const | - | ||||||||||||
382 | { | - | ||||||||||||
383 | Q_D(const QLocalSocket); | - | ||||||||||||
384 | return d->unixSocket.socketDescriptor(); executed 1 time by 1 test: return d->unixSocket.socketDescriptor(); Executed by:
| 1 | ||||||||||||
385 | } | - | ||||||||||||
386 | - | |||||||||||||
387 | qint64 QLocalSocket::readData(char *data, qint64 c) | - | ||||||||||||
388 | { | - | ||||||||||||
389 | Q_D(QLocalSocket); | - | ||||||||||||
390 | return d->unixSocket.read(data, c); executed 4330 times by 2 tests: return d->unixSocket.read(data, c); Executed by:
| 4330 | ||||||||||||
391 | } | - | ||||||||||||
392 | - | |||||||||||||
393 | qint64 QLocalSocket::writeData(const char *data, qint64 c) | - | ||||||||||||
394 | { | - | ||||||||||||
395 | Q_D(QLocalSocket); | - | ||||||||||||
396 | return d->unixSocket.writeData(data, c); executed 2573 times by 2 tests: return d->unixSocket.writeData(data, c); Executed by:
| 2573 | ||||||||||||
397 | } | - | ||||||||||||
398 | - | |||||||||||||
399 | void QLocalSocket::abort() | - | ||||||||||||
400 | { | - | ||||||||||||
401 | Q_D(QLocalSocket); | - | ||||||||||||
402 | d->unixSocket.abort(); | - | ||||||||||||
403 | } executed 5 times by 1 test: end of block Executed by:
| 5 | ||||||||||||
404 | - | |||||||||||||
405 | qint64 QLocalSocket::bytesAvailable() const | - | ||||||||||||
406 | { | - | ||||||||||||
407 | Q_D(const QLocalSocket); | - | ||||||||||||
408 | return QIODevice::bytesAvailable() + d->unixSocket.bytesAvailable(); executed 5112 times by 2 tests: return QIODevice::bytesAvailable() + d->unixSocket.bytesAvailable(); Executed by:
| 5112 | ||||||||||||
409 | } | - | ||||||||||||
410 | - | |||||||||||||
411 | qint64 QLocalSocket::bytesToWrite() const | - | ||||||||||||
412 | { | - | ||||||||||||
413 | Q_D(const QLocalSocket); | - | ||||||||||||
414 | return d->unixSocket.bytesToWrite(); executed 4018 times by 1 test: return d->unixSocket.bytesToWrite(); Executed by:
| 4018 | ||||||||||||
415 | } | - | ||||||||||||
416 | - | |||||||||||||
417 | bool QLocalSocket::canReadLine() const | - | ||||||||||||
418 | { | - | ||||||||||||
419 | Q_D(const QLocalSocket); | - | ||||||||||||
420 | return QIODevice::canReadLine() || d->unixSocket.canReadLine(); executed 14 times by 1 test: return QIODevice::canReadLine() || d->unixSocket.canReadLine(); Executed by:
| 0-14 | ||||||||||||
421 | } | - | ||||||||||||
422 | - | |||||||||||||
423 | void QLocalSocket::close() | - | ||||||||||||
424 | { | - | ||||||||||||
425 | Q_D(QLocalSocket); | - | ||||||||||||
426 | d->unixSocket.close(); | - | ||||||||||||
427 | d->cancelDelayedConnect(); | - | ||||||||||||
428 | if (d->connectingSocket != -1)
| 1-223 | ||||||||||||
429 | ::close(d->connectingSocket); executed 1 time by 1 test: ::close(d->connectingSocket); Executed by:
| 1 | ||||||||||||
430 | d->connectingSocket = -1; | - | ||||||||||||
431 | d->connectingName.clear(); | - | ||||||||||||
432 | d->connectingOpenMode = 0; | - | ||||||||||||
433 | d->serverName.clear(); | - | ||||||||||||
434 | d->fullServerName.clear(); | - | ||||||||||||
435 | QIODevice::close(); | - | ||||||||||||
436 | } executed 224 times by 3 tests: end of block Executed by:
| 224 | ||||||||||||
437 | - | |||||||||||||
438 | bool QLocalSocket::waitForBytesWritten(int msecs) | - | ||||||||||||
439 | { | - | ||||||||||||
440 | Q_D(QLocalSocket); | - | ||||||||||||
441 | return d->unixSocket.waitForBytesWritten(msecs); executed 2042 times by 1 test: return d->unixSocket.waitForBytesWritten(msecs); Executed by:
| 2042 | ||||||||||||
442 | } | - | ||||||||||||
443 | - | |||||||||||||
444 | bool QLocalSocket::flush() | - | ||||||||||||
445 | { | - | ||||||||||||
446 | Q_D(QLocalSocket); | - | ||||||||||||
447 | return d->unixSocket.flush(); executed 17 times by 1 test: return d->unixSocket.flush(); Executed by:
| 17 | ||||||||||||
448 | } | - | ||||||||||||
449 | - | |||||||||||||
450 | void QLocalSocket::disconnectFromServer() | - | ||||||||||||
451 | { | - | ||||||||||||
452 | Q_D(QLocalSocket); | - | ||||||||||||
453 | d->unixSocket.disconnectFromHost(); | - | ||||||||||||
454 | } executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
455 | - | |||||||||||||
456 | QLocalSocket::LocalSocketError QLocalSocket::error() const | - | ||||||||||||
457 | { | - | ||||||||||||
458 | Q_D(const QLocalSocket); | - | ||||||||||||
459 | switch (d->unixSocket.error()) { | - | ||||||||||||
460 | case QAbstractSocket::ConnectionRefusedError: never executed: case QAbstractSocket::ConnectionRefusedError: | 0 | ||||||||||||
461 | return QLocalSocket::ConnectionRefusedError; never executed: return QLocalSocket::ConnectionRefusedError; | 0 | ||||||||||||
462 | case QAbstractSocket::RemoteHostClosedError: executed 1 time by 1 test: case QAbstractSocket::RemoteHostClosedError: Executed by:
| 1 | ||||||||||||
463 | return QLocalSocket::PeerClosedError; executed 1 time by 1 test: return QLocalSocket::PeerClosedError; Executed by:
| 1 | ||||||||||||
464 | case QAbstractSocket::HostNotFoundError: executed 14 times by 1 test: case QAbstractSocket::HostNotFoundError: Executed by:
| 14 | ||||||||||||
465 | return QLocalSocket::ServerNotFoundError; executed 14 times by 1 test: return QLocalSocket::ServerNotFoundError; Executed by:
| 14 | ||||||||||||
466 | case QAbstractSocket::SocketAccessError: never executed: case QAbstractSocket::SocketAccessError: | 0 | ||||||||||||
467 | return QLocalSocket::SocketAccessError; never executed: return QLocalSocket::SocketAccessError; | 0 | ||||||||||||
468 | case QAbstractSocket::SocketResourceError: never executed: case QAbstractSocket::SocketResourceError: | 0 | ||||||||||||
469 | return QLocalSocket::SocketResourceError; never executed: return QLocalSocket::SocketResourceError; | 0 | ||||||||||||
470 | case QAbstractSocket::SocketTimeoutError: executed 6 times by 1 test: case QAbstractSocket::SocketTimeoutError: Executed by:
| 6 | ||||||||||||
471 | return QLocalSocket::SocketTimeoutError; executed 6 times by 1 test: return QLocalSocket::SocketTimeoutError; Executed by:
| 6 | ||||||||||||
472 | case QAbstractSocket::DatagramTooLargeError: never executed: case QAbstractSocket::DatagramTooLargeError: | 0 | ||||||||||||
473 | return QLocalSocket::DatagramTooLargeError; never executed: return QLocalSocket::DatagramTooLargeError; | 0 | ||||||||||||
474 | case QAbstractSocket::NetworkError: never executed: case QAbstractSocket::NetworkError: | 0 | ||||||||||||
475 | return QLocalSocket::ConnectionError; never executed: return QLocalSocket::ConnectionError; | 0 | ||||||||||||
476 | case QAbstractSocket::UnsupportedSocketOperationError: never executed: case QAbstractSocket::UnsupportedSocketOperationError: | 0 | ||||||||||||
477 | return QLocalSocket::UnsupportedSocketOperationError; never executed: return QLocalSocket::UnsupportedSocketOperationError; | 0 | ||||||||||||
478 | case QAbstractSocket::UnknownSocketError: executed 7 times by 1 test: case QAbstractSocket::UnknownSocketError: Executed by:
| 7 | ||||||||||||
479 | return QLocalSocket::UnknownSocketError; executed 7 times by 1 test: return QLocalSocket::UnknownSocketError; Executed by:
| 7 | ||||||||||||
480 | default: never executed: default: | 0 | ||||||||||||
481 | #if defined QLOCALSOCKET_DEBUG | - | ||||||||||||
482 | qWarning() << "QLocalSocket error not handled:" << d->unixSocket.error(); | - | ||||||||||||
483 | #endif | - | ||||||||||||
484 | break; never executed: break; | 0 | ||||||||||||
485 | } | - | ||||||||||||
486 | return UnknownSocketError; never executed: return UnknownSocketError; | 0 | ||||||||||||
487 | } | - | ||||||||||||
488 | - | |||||||||||||
489 | bool QLocalSocket::isValid() const | - | ||||||||||||
490 | { | - | ||||||||||||
491 | Q_D(const QLocalSocket); | - | ||||||||||||
492 | return d->unixSocket.isValid(); executed 20 times by 1 test: return d->unixSocket.isValid(); Executed by:
| 20 | ||||||||||||
493 | } | - | ||||||||||||
494 | - | |||||||||||||
495 | qint64 QLocalSocket::readBufferSize() const | - | ||||||||||||
496 | { | - | ||||||||||||
497 | Q_D(const QLocalSocket); | - | ||||||||||||
498 | return d->unixSocket.readBufferSize(); executed 14 times by 1 test: return d->unixSocket.readBufferSize(); Executed by:
| 14 | ||||||||||||
499 | } | - | ||||||||||||
500 | - | |||||||||||||
501 | void QLocalSocket::setReadBufferSize(qint64 size) | - | ||||||||||||
502 | { | - | ||||||||||||
503 | Q_D(QLocalSocket); | - | ||||||||||||
504 | d->unixSocket.setReadBufferSize(size); | - | ||||||||||||
505 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
506 | - | |||||||||||||
507 | bool QLocalSocket::waitForConnected(int msec) | - | ||||||||||||
508 | { | - | ||||||||||||
509 | Q_D(QLocalSocket); | - | ||||||||||||
510 | if (state() != ConnectingState)
| 0-78 | ||||||||||||
511 | return (state() == ConnectedState); executed 78 times by 2 tests: return (state() == ConnectedState); Executed by:
| 78 | ||||||||||||
512 | - | |||||||||||||
513 | fd_set fds; | - | ||||||||||||
514 | FD_ZERO(&fds); | - | ||||||||||||
515 | FD_SET(d->connectingSocket, &fds); | - | ||||||||||||
516 | - | |||||||||||||
517 | timeval timeout; | - | ||||||||||||
518 | timeout.tv_sec = msec / 1000; | - | ||||||||||||
519 | timeout.tv_usec = (msec % 1000) * 1000; | - | ||||||||||||
520 | - | |||||||||||||
521 | // timeout can not be 0 or else select will return an error. | - | ||||||||||||
522 | if (0 == msec)
| 0 | ||||||||||||
523 | timeout.tv_usec = 1000; never executed: timeout.tv_usec = 1000; | 0 | ||||||||||||
524 | - | |||||||||||||
525 | int result = -1; | - | ||||||||||||
526 | // on Linux timeout will be updated by select, but _not_ on other systems. | - | ||||||||||||
527 | QElapsedTimer timer; | - | ||||||||||||
528 | timer.start(); | - | ||||||||||||
529 | while (state() == ConnectingState
| 0 | ||||||||||||
530 | && (-1 == msec || timer.elapsed() < msec)) {
| 0 | ||||||||||||
531 | result = ::select(d->connectingSocket + 1, &fds, 0, 0, &timeout); | - | ||||||||||||
532 | if (-1 == result && errno != EINTR) {
| 0 | ||||||||||||
533 | d->errorOccurred( QLocalSocket::UnknownSocketError, | - | ||||||||||||
534 | QLatin1String("QLocalSocket::waitForConnected")); | - | ||||||||||||
535 | break; never executed: break; | 0 | ||||||||||||
536 | } | - | ||||||||||||
537 | if (result > 0)
| 0 | ||||||||||||
538 | d->_q_connectToSocket(); never executed: d->_q_connectToSocket(); | 0 | ||||||||||||
539 | } never executed: end of block | 0 | ||||||||||||
540 | - | |||||||||||||
541 | return (state() == ConnectedState); never executed: return (state() == ConnectedState); | 0 | ||||||||||||
542 | } | - | ||||||||||||
543 | - | |||||||||||||
544 | bool QLocalSocket::waitForDisconnected(int msecs) | - | ||||||||||||
545 | { | - | ||||||||||||
546 | Q_D(QLocalSocket); | - | ||||||||||||
547 | if (state() == UnconnectedState) {
| 1-5 | ||||||||||||
548 | qWarning("QLocalSocket::waitForDisconnected() is not allowed in UnconnectedState"); | - | ||||||||||||
549 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||
550 | } | - | ||||||||||||
551 | return (d->unixSocket.waitForDisconnected(msecs)); executed 5 times by 1 test: return (d->unixSocket.waitForDisconnected(msecs)); Executed by:
| 5 | ||||||||||||
552 | } | - | ||||||||||||
553 | - | |||||||||||||
554 | bool QLocalSocket::waitForReadyRead(int msecs) | - | ||||||||||||
555 | { | - | ||||||||||||
556 | Q_D(QLocalSocket); | - | ||||||||||||
557 | if (state() == QLocalSocket::UnconnectedState)
| 7-2049 | ||||||||||||
558 | return false; executed 7 times by 1 test: return false; Executed by:
| 7 | ||||||||||||
559 | return (d->unixSocket.waitForReadyRead(msecs)); executed 2049 times by 2 tests: return (d->unixSocket.waitForReadyRead(msecs)); Executed by:
| 2049 | ||||||||||||
560 | } | - | ||||||||||||
561 | - | |||||||||||||
562 | QT_END_NAMESPACE | - | ||||||||||||
563 | - | |||||||||||||
564 | #endif | - | ||||||||||||
Source code | Switch to Preprocessed file |