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 | |
4 | #ifndef QLOCALSOCKET_H |
5 | #define QLOCALSOCKET_H |
6 | |
7 | #include <QtNetwork/qtnetworkglobal.h> |
8 | #include <QtCore/qiodevice.h> |
9 | #include <QtNetwork/qabstractsocket.h> |
10 | |
11 | #ifndef QT_NO_DEBUG_STREAM |
12 | #include <QtCore/qdebug.h> |
13 | #endif |
14 | |
15 | QT_REQUIRE_CONFIG(localserver); |
16 | |
17 | QT_BEGIN_NAMESPACE |
18 | |
19 | class QLocalSocketPrivate; |
20 | |
21 | class Q_NETWORK_EXPORT QLocalSocket : public QIODevice |
22 | { |
23 | Q_OBJECT |
24 | Q_DECLARE_PRIVATE(QLocalSocket) |
25 | Q_PROPERTY(SocketOptions socketOptions READ socketOptions WRITE setSocketOptions |
26 | BINDABLE bindableSocketOptions) |
27 | |
28 | public: |
29 | enum LocalSocketError |
30 | { |
31 | ConnectionRefusedError = QAbstractSocket::ConnectionRefusedError, |
32 | PeerClosedError = QAbstractSocket::RemoteHostClosedError, |
33 | ServerNotFoundError = QAbstractSocket::HostNotFoundError, |
34 | SocketAccessError = QAbstractSocket::SocketAccessError, |
35 | SocketResourceError = QAbstractSocket::SocketResourceError, |
36 | SocketTimeoutError = QAbstractSocket::SocketTimeoutError, |
37 | DatagramTooLargeError = QAbstractSocket::DatagramTooLargeError, |
38 | ConnectionError = QAbstractSocket::NetworkError, |
39 | UnsupportedSocketOperationError = QAbstractSocket::UnsupportedSocketOperationError, |
40 | UnknownSocketError = QAbstractSocket::UnknownSocketError, |
41 | OperationError = QAbstractSocket::OperationError |
42 | }; |
43 | |
44 | enum LocalSocketState |
45 | { |
46 | UnconnectedState = QAbstractSocket::UnconnectedState, |
47 | ConnectingState = QAbstractSocket::ConnectingState, |
48 | ConnectedState = QAbstractSocket::ConnectedState, |
49 | ClosingState = QAbstractSocket::ClosingState |
50 | }; |
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 | bool waitForBytesWritten(int msecs = 30000) override; |
94 | bool waitForConnected(int msecs = 30000); |
95 | bool waitForDisconnected(int msecs = 30000); |
96 | bool waitForReadyRead(int msecs = 30000) override; |
97 | |
98 | Q_SIGNALS: |
99 | void connected(); |
100 | void disconnected(); |
101 | void errorOccurred(QLocalSocket::LocalSocketError socketError); |
102 | void stateChanged(QLocalSocket::LocalSocketState socketState); |
103 | |
104 | protected: |
105 | virtual qint64 readData(char*, qint64) override; |
106 | qint64 readLineData(char *data, qint64 maxSize) override; |
107 | qint64 skipData(qint64 maxSize) override; |
108 | virtual qint64 writeData(const char*, qint64) override; |
109 | |
110 | private: |
111 | Q_DISABLE_COPY(QLocalSocket) |
112 | #if defined(QT_LOCALSOCKET_TCP) |
113 | Q_PRIVATE_SLOT(d_func(), void _q_stateChanged(QAbstractSocket::SocketState)) |
114 | Q_PRIVATE_SLOT(d_func(), void _q_errorOccurred(QAbstractSocket::SocketError)) |
115 | #elif defined(Q_OS_WIN) |
116 | Q_PRIVATE_SLOT(d_func(), void _q_pipeClosed()) |
117 | Q_PRIVATE_SLOT(d_func(), void _q_winError(ulong, const QString &)) |
118 | #else |
119 | Q_PRIVATE_SLOT(d_func(), void _q_stateChanged(QAbstractSocket::SocketState)) |
120 | Q_PRIVATE_SLOT(d_func(), void _q_errorOccurred(QAbstractSocket::SocketError)) |
121 | Q_PRIVATE_SLOT(d_func(), void _q_connectToSocket()) |
122 | Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt()) |
123 | #endif |
124 | }; |
125 | |
126 | #ifndef QT_NO_DEBUG_STREAM |
127 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, QLocalSocket::LocalSocketError); |
128 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, QLocalSocket::LocalSocketState); |
129 | #endif |
130 | |
131 | Q_DECLARE_OPERATORS_FOR_FLAGS(QLocalSocket::SocketOptions) |
132 | |
133 | QT_END_NAMESPACE |
134 | |
135 | #endif // QLOCALSOCKET_H |
136 | |