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