Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qlocalsocket.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QLOCALSOCKET_H
6#define QLOCALSOCKET_H
7
8#include <QtNetwork/qtnetworkglobal.h>
9#include <QtCore/qiodevice.h>
10#include <QtNetwork/qabstractsocket.h>
11
13
14QT_BEGIN_NAMESPACE
15
16class QDebug;
18
19class Q_NETWORK_EXPORT QLocalSocket : public QIODevice
20{
21 Q_OBJECT
22 Q_DECLARE_PRIVATE(QLocalSocket)
23 Q_PROPERTY(SocketOptions socketOptions READ socketOptions WRITE setSocketOptions
24 BINDABLE bindableSocketOptions)
25
26public:
27 enum LocalSocketError
28 {
29 ConnectionRefusedError = QAbstractSocket::ConnectionRefusedError,
30 PeerClosedError = QAbstractSocket::RemoteHostClosedError,
31 ServerNotFoundError = QAbstractSocket::HostNotFoundError,
32 SocketAccessError = QAbstractSocket::SocketAccessError,
33 SocketResourceError = QAbstractSocket::SocketResourceError,
34 SocketTimeoutError = QAbstractSocket::SocketTimeoutError,
35 DatagramTooLargeError = QAbstractSocket::DatagramTooLargeError,
36 ConnectionError = QAbstractSocket::NetworkError,
37 UnsupportedSocketOperationError = QAbstractSocket::UnsupportedSocketOperationError,
38 UnknownSocketError = QAbstractSocket::UnknownSocketError,
39 OperationError = QAbstractSocket::OperationError
40 };
41 Q_ENUM(LocalSocketError)
42
43 enum LocalSocketState
44 {
45 UnconnectedState = QAbstractSocket::UnconnectedState,
46 ConnectingState = QAbstractSocket::ConnectingState,
47 ConnectedState = QAbstractSocket::ConnectedState,
48 ClosingState = QAbstractSocket::ClosingState
49 };
50 Q_ENUM(LocalSocketState)
51
52 enum SocketOption {
53 NoOptions = 0x00,
54 AbstractNamespaceOption = 0x01
55 };
56 Q_DECLARE_FLAGS(SocketOptions, SocketOption)
57 Q_FLAG(SocketOptions)
58
59 QLocalSocket(QObject *parent = nullptr);
60 ~QLocalSocket();
61
62 void connectToServer(OpenMode openMode = ReadWrite);
63 void connectToServer(const QString &name, OpenMode openMode = ReadWrite);
64 void disconnectFromServer();
65
66 void setServerName(const QString &name);
67 QString serverName() const;
68 QString fullServerName() const;
69
70 void abort();
71 virtual bool isSequential() const override;
72 virtual qint64 bytesAvailable() const override;
73 virtual qint64 bytesToWrite() const override;
74 virtual bool canReadLine() const override;
75 virtual bool open(OpenMode openMode = ReadWrite) override;
76 virtual void close() override;
77 LocalSocketError error() const;
78 bool flush();
79 bool isValid() const;
80 qint64 readBufferSize() const;
81 void setReadBufferSize(qint64 size);
82
83 bool setSocketDescriptor(qintptr socketDescriptor,
84 LocalSocketState socketState = ConnectedState,
85 OpenMode openMode = ReadWrite);
86 qintptr socketDescriptor() const;
87
88 void setSocketOptions(SocketOptions option);
89 SocketOptions socketOptions() const;
90 QBindable<SocketOptions> bindableSocketOptions();
91
92 LocalSocketState state() const;
93
94 using QIODevice::waitForBytesWritten;
95 using QIODevice::waitForReadyRead;
96 bool waitForBytesWritten(QDeadlineTimer deadline = std::chrono::seconds(30)) QT7_ONLY(override);
97 bool waitForConnected(QDeadlineTimer deadline = std::chrono::seconds(30));
98 bool waitForDisconnected(QDeadlineTimer deadline = std::chrono::seconds(30));
99 bool waitForReadyRead(QDeadlineTimer deadline = std::chrono::seconds(30)) QT7_ONLY(override);
100
101#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
102 QT_NETWORK_INLINE_SINCE(6, 13)
103 bool waitForBytesWritten(int msecs) override;
104#endif
105 QT_NETWORK_INLINE_SINCE(6, 13)
106 bool waitForConnected(int msecs);
107 QT_NETWORK_INLINE_SINCE(6, 13)
108 bool waitForDisconnected(int msecs);
109#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
110 QT_NETWORK_INLINE_SINCE(6, 13)
111 bool waitForReadyRead(int msecs) override;
112#endif
113
114Q_SIGNALS:
115 void connected();
116 void disconnected();
117 void errorOccurred(QLocalSocket::LocalSocketError socketError);
118 void stateChanged(QLocalSocket::LocalSocketState socketState);
119
120protected:
121 virtual qint64 readData(char*, qint64) override;
122 qint64 readLineData(char *data, qint64 maxSize) override;
123 qint64 skipData(qint64 maxSize) override;
124 virtual qint64 writeData(const char*, qint64) override;
125
126private:
127 Q_DISABLE_COPY(QLocalSocket)
128#if defined(QT_LOCALSOCKET_TCP)
129 Q_PRIVATE_SLOT(d_func(), void _q_stateChanged(QAbstractSocket::SocketState))
130 Q_PRIVATE_SLOT(d_func(), void _q_errorOccurred(QAbstractSocket::SocketError))
131#elif defined(Q_OS_WIN)
132 Q_PRIVATE_SLOT(d_func(), void _q_pipeClosed())
133 Q_PRIVATE_SLOT(d_func(), void _q_winError(ulong, const QString &))
134#else
135 Q_PRIVATE_SLOT(d_func(), void _q_stateChanged(QAbstractSocket::SocketState))
136 Q_PRIVATE_SLOT(d_func(), void _q_errorOccurred(QAbstractSocket::SocketError))
137 Q_PRIVATE_SLOT(d_func(), void _q_connectToSocket())
138 Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt())
139#endif
140};
141
142#if QT_NETWORK_REMOVED_SINCE(6, 12)
143#ifndef QT_NO_DEBUG_STREAM
144Q_NETWORK_EXPORT QDebug operator<<(QDebug, QLocalSocket::LocalSocketError);
145Q_NETWORK_EXPORT QDebug operator<<(QDebug, QLocalSocket::LocalSocketState);
146#endif
147#endif // QT_NETWORK_REMOVED_SINCE(6, 12)
148
149Q_DECLARE_OPERATORS_FOR_FLAGS(QLocalSocket::SocketOptions)
150
151#if QT_NETWORK_INLINE_IMPL_SINCE(6, 13)
152bool QLocalSocket::waitForConnected(int msecs)
153{ return waitForConnected(QDeadlineTimer(msecs)); }
154bool QLocalSocket::waitForDisconnected(int msecs)
155{ return waitForDisconnected(QDeadlineTimer(msecs)); }
156#endif
157#if QT_NETWORK_INLINE_IMPL_SINCE(6, 13) && QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
158bool QLocalSocket::waitForBytesWritten(int msecs)
159{ return waitForBytesWritten(QDeadlineTimer(msecs)); }
160bool QLocalSocket::waitForReadyRead(int msecs)
161{ return waitForReadyRead(QDeadlineTimer(msecs)); }
162#endif
163
164QT_END_NAMESPACE
165
166#endif // QLOCALSOCKET_H
The QLocalServer class provides a local socket based server.
The QLocalSocket class provides a local socket.
QT_REQUIRE_CONFIG(animation)
QT_REQUIRE_CONFIG(localserver)