1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qlocalsocket.h"
43 #include "qlocalsocket_p.h"
45 #ifndef QT_NO_LOCALSOCKET
54 \brief The QLocalSocket class provides a local socket.
56 On Windows this is a named pipe and on Unix this is a local domain socket.
58 If an error occurs, socketError() returns the type of error, and
59 errorString() can be called to get a human readable description
62 Although QLocalSocket is designed for use with an event loop, it's possible
63 to use it without one. In that case, you must use waitForConnected(),
64 waitForReadyRead(), waitForBytesWritten(), and waitForDisconnected()
65 which blocks until the operation is complete or the timeout expires.
67 Note that this feature is not supported on versions of Windows earlier than
74 \fn void QLocalSocket::connectToServer(const QString &name, OpenMode openMode)
76 Attempts to make a connection to \a name.
78 The socket is opened in the given \a openMode and first enters ConnectingState.
79 It then attempts to connect to the address or addresses returned by the lookup.
80 Finally, if a connection is established, QLocalSocket enters ConnectedState
81 and emits connected().
83 At any point, the socket can emit error() to signal that an error occurred.
85 See also state(), serverName(), and waitForConnected().
89 \fn void QLocalSocket::connected()
91 This signal is emitted after connectToServer() has been called and
92 a connection has been successfully established.
94 \sa connectToServer(), disconnected()
98 \fn bool QLocalSocket::setSocketDescriptor(qintptr socketDescriptor,
99 LocalSocketState socketState, OpenMode openMode)
101 Initializes QLocalSocket with the native socket descriptor
102 \a socketDescriptor. Returns true if socketDescriptor is accepted
103 as a valid socket descriptor; otherwise returns false. The socket is
104 opened in the mode specified by \a openMode, and enters the socket state
105 specified by \a socketState.
107 \note It is not possible to initialize two local sockets with the same
108 native socket descriptor.
110 \sa socketDescriptor(), state(), openMode()
114 \fn qintptr QLocalSocket::socketDescriptor() const
116 Returns the native socket descriptor of the QLocalSocket object if
117 this is available; otherwise returns -1.
119 The socket descriptor is not available when QLocalSocket
120 is in UnconnectedState.
122 \sa setSocketDescriptor()
126 \fn qint64 QLocalSocket::readData(char *data, qint64 c)
131 \fn qint64 QLocalSocket::writeData(const char *data, qint64 c)
136 \fn void QLocalSocket::abort()
138 Aborts the current connection and resets the socket.
139 Unlike disconnectFromServer(), this function immediately closes the socket,
140 clearing any pending data in the write buffer.
142 \sa disconnectFromServer(), close()
146 \fn qint64 QLocalSocket::bytesAvailable() const
151 \fn qint64 QLocalSocket::bytesToWrite() const
156 \fn bool QLocalSocket::canReadLine() const
161 \fn void QLocalSocket::close()
166 \fn bool QLocalSocket::waitForBytesWritten(int msecs)
171 \fn bool QLocalSocket::flush()
173 This function writes as much as possible from the internal write buffer
174 to the socket, without blocking. If any data was written, this function
175 returns true; otherwise false is returned.
177 Call this function if you need QLocalSocket to start sending buffered data
178 immediately. The number of bytes successfully written depends on the
179 operating system. In most cases, you do not need to call this function,
180 because QLocalSocket will start sending data automatically once control
181 goes back to the event loop. In the absence of an event loop, call
182 waitForBytesWritten() instead.
184 \sa write(), waitForBytesWritten()
188 \fn void QLocalSocket::disconnectFromServer()
190 Attempts to close the socket. If there is pending data waiting to be
191 written, QLocalSocket will enter ClosingState and wait until all data
192 has been written. Eventually, it will enter UnconnectedState and emit
193 the disconnectedFromServer() signal.
195 \sa connectToServer()
199 \fn QLocalSocket::LocalSocketError QLocalSocket::error() const
201 Returns the type of error that last occurred.
203 \sa state(), errorString()
207 \fn bool QLocalSocket::isValid() const
209 Returns true if the socket is valid and ready for use; otherwise
212 \note The socket's state must be ConnectedState before reading
213 and writing can occur.
215 \sa state(), connectToServer()
219 \fn qint64 QLocalSocket::readBufferSize() const
221 Returns the size of the internal read buffer. This limits the amount of
222 data that the client can receive before you call read() or readAll().
223 A read buffer size of 0 (the default) means that the buffer has no size
224 limit, ensuring that no data is lost.
226 \sa setReadBufferSize(), read()
230 \fn void QLocalSocket::setReadBufferSize(qint64 size)
232 Sets the size of QLocalSocket's internal read buffer to be \a size bytes.
234 If the buffer size is limited to a certain size, QLocalSocket won't
235 buffer more than this size of data. Exceptionally, a buffer size of 0
236 means that the read buffer is unlimited and all incoming data is buffered.
239 This option is useful if you only read the data at certain points in
240 time (e.g., in a real-time streaming application) or if you want to
241 protect your socket against receiving too much data, which may eventually
242 cause your application to run out of memory.
244 \sa readBufferSize(), read()
248 \fn bool QLocalSocket::waitForConnected(int msecs)
250 Waits until the socket is connected, up to \a msecs milliseconds. If the
251 connection has been established, this function returns true; otherwise
252 it returns false. In the case where it returns false, you can call
253 error() to determine the cause of the error.
255 The following example waits up to one second for a connection
258 \snippet code/src_network_socket_qlocalsocket_unix.cpp 0
260 If \a msecs is -1, this function will not time out.
262 \sa connectToServer(), connected()
266 \fn bool QLocalSocket::waitForDisconnected(int msecs)
268 Waits until the socket has disconnected, up to \a msecs
269 milliseconds. If the connection has been disconnected, this
270 function returns true; otherwise it returns false. In the case
271 where it returns false, you can call error() to determine
272 the cause of the error.
274 The following example waits up to one second for a connection
277 \snippet code/src_network_socket_qlocalsocket_unix.cpp 1
279 If \a msecs is -1, this function will not time out.
281 \sa disconnectFromServer(), close()
285 \fn bool QLocalSocket::waitForReadyRead(int msecs)
287 This function blocks until data is available for reading and the
288 \l{QIODevice::}{readyRead()} signal has been emitted. The function
289 will timeout after \a msecs milliseconds; the default timeout is
292 The function returns true if data is available for reading;
293 otherwise it returns false (if an error occurred or the
294 operation timed out).
296 \sa waitForBytesWritten()
300 \fn void QLocalSocket::disconnected()
302 This signal is emitted when the socket has been disconnected.
304 \sa connectToServer(), disconnectFromServer(), abort(), connected()
308 \fn void QLocalSocket::error(QLocalSocket::LocalSocketError socketError)
310 This signal is emitted after an error occurred. The \a socketError
311 parameter describes the type of error that occurred.
313 QLocalSocket::LocalSocketError is not a registered metatype, so for queued
314 connections, you will have to register it with Q_DECLARE_METATYPE() and
317 \sa error(), errorString(), {Creating Custom Qt Types}
321 \fn void QLocalSocket::stateChanged(QLocalSocket::LocalSocketState socketState)
323 This signal is emitted whenever QLocalSocket's state changes.
324 The \a socketState parameter is the new state.
326 QLocalSocket::SocketState is not a registered metatype, so for queued
327 connections, you will have to register it with Q_DECLARE_METATYPE() and
330 \sa state(), {Creating Custom Qt Types}
334 Creates a new local socket. The \a parent argument is passed to
335 QObject's constructor.
337 QLocalSocket::QLocalSocket(QObject * parent)
338 : QIODevice(*new QLocalSocketPrivate, parent)
345 Destroys the socket, closing the connection if necessary.
347 QLocalSocket::~QLocalSocket()
350 #if !defined(Q_OS_WIN) && !defined(QT_LOCALSOCKET_TCP)
352 d->unixSocket.setParent(0);
357 Returns the name of the peer as specified by connectToServer(), or an
358 empty QString if connectToServer() has not been called or it failed.
360 \sa connectToServer(), fullServerName()
363 QString QLocalSocket::serverName() const
365 Q_D(const QLocalSocket);
366 return d->serverName;
370 Returns the server path that the socket is connected to.
372 \note The return value of this function is platform specific.
374 \sa connectToServer(), serverName()
376 QString QLocalSocket::fullServerName() const
378 Q_D(const QLocalSocket);
379 return d->fullServerName;
383 Returns the state of the socket.
387 QLocalSocket::LocalSocketState QLocalSocket::state() const
389 Q_D(const QLocalSocket);
395 bool QLocalSocket::isSequential() const
401 \enum QLocalSocket::LocalSocketError
403 The LocalServerError enumeration represents the errors that can occur.
404 The most recent error can be retrieved through a call to
405 \l QLocalSocket::error().
407 \value ConnectionRefusedError The connection was refused by
408 the peer (or timed out).
409 \value PeerClosedError The remote socket closed the connection.
410 Note that the client socket (i.e., this socket) will be closed
411 after the remote close notification has been sent.
412 \value ServerNotFoundError The local socket name was not found.
413 \value SocketAccessError The socket operation failed because the
414 application lacked the required privileges.
415 \value SocketResourceError The local system ran out of resources
416 (e.g., too many sockets).
417 \value SocketTimeoutError The socket operation timed out.
418 \value DatagramTooLargeError The datagram was larger than the operating
419 system's limit (which can be as low as 8192 bytes).
420 \value ConnectionError An error occurred with the connection.
421 \value UnsupportedSocketOperationError The requested socket operation
422 is not supported by the local operating system.
423 \value OperationError An operation was attempted while the socket was in a state that
425 \value UnknownSocketError An unidentified error occurred.
429 \enum QLocalSocket::LocalSocketState
431 This enum describes the different states in which a socket can be.
433 \sa QLocalSocket::state()
435 \value UnconnectedState The socket is not connected.
436 \value ConnectingState The socket has started establishing a connection.
437 \value ConnectedState A connection is established.
438 \value ClosingState The socket is about to close
439 (data may still be waiting to be written).
442 #ifndef QT_NO_DEBUG_STREAM
443 QDebug operator<<(QDebug debug, QLocalSocket::LocalSocketError error)
446 case QLocalSocket::ConnectionRefusedError:
447 debug << "QLocalSocket::ConnectionRefusedError";
449 case QLocalSocket::PeerClosedError:
450 debug << "QLocalSocket::PeerClosedError";
452 case QLocalSocket::ServerNotFoundError:
453 debug << "QLocalSocket::ServerNotFoundError";
455 case QLocalSocket::SocketAccessError:
456 debug << "QLocalSocket::SocketAccessError";
458 case QLocalSocket::SocketResourceError:
459 debug << "QLocalSocket::SocketResourceError";
461 case QLocalSocket::SocketTimeoutError:
462 debug << "QLocalSocket::SocketTimeoutError";
464 case QLocalSocket::DatagramTooLargeError:
465 debug << "QLocalSocket::DatagramTooLargeError";
467 case QLocalSocket::ConnectionError:
468 debug << "QLocalSocket::ConnectionError";
470 case QLocalSocket::UnsupportedSocketOperationError:
471 debug << "QLocalSocket::UnsupportedSocketOperationError";
473 case QLocalSocket::UnknownSocketError:
474 debug << "QLocalSocket::UnknownSocketError";
477 debug << "QLocalSocket::SocketError(" << int(error) << ')';
483 QDebug operator<<(QDebug debug, QLocalSocket::LocalSocketState state)
486 case QLocalSocket::UnconnectedState:
487 debug << "QLocalSocket::UnconnectedState";
489 case QLocalSocket::ConnectingState:
490 debug << "QLocalSocket::ConnectingState";
492 case QLocalSocket::ConnectedState:
493 debug << "QLocalSocket::ConnectedState";
495 case QLocalSocket::ClosingState:
496 debug << "QLocalSocket::ClosingState";
499 debug << "QLocalSocket::SocketState(" << int(state) << ')';
510 #include "moc_qlocalsocket.cpp"