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 | #ifndef QLOCALSOCKET_H |
41 | #define QLOCALSOCKET_H |
42 | |
43 | #include <QtNetwork/qtnetworkglobal.h> |
44 | #include <QtCore/qiodevice.h> |
45 | #include <QtNetwork/qabstractsocket.h> |
46 | |
47 | #ifndef QT_NO_DEBUG_STREAM |
48 | #include <QtCore/qdebug.h> |
49 | #endif |
50 | |
51 | QT_REQUIRE_CONFIG(localserver); |
52 | |
53 | QT_BEGIN_NAMESPACE |
54 | |
55 | class QLocalSocketPrivate; |
56 | |
57 | class Q_NETWORK_EXPORT QLocalSocket : public QIODevice |
58 | { |
59 | Q_OBJECT |
60 | Q_DECLARE_PRIVATE(QLocalSocket) |
61 | |
62 | public: |
63 | enum LocalSocketError |
64 | { |
65 | ConnectionRefusedError = QAbstractSocket::ConnectionRefusedError, |
66 | PeerClosedError = QAbstractSocket::RemoteHostClosedError, |
67 | ServerNotFoundError = QAbstractSocket::HostNotFoundError, |
68 | SocketAccessError = QAbstractSocket::SocketAccessError, |
69 | SocketResourceError = QAbstractSocket::SocketResourceError, |
70 | SocketTimeoutError = QAbstractSocket::SocketTimeoutError, |
71 | DatagramTooLargeError = QAbstractSocket::DatagramTooLargeError, |
72 | ConnectionError = QAbstractSocket::NetworkError, |
73 | UnsupportedSocketOperationError = QAbstractSocket::UnsupportedSocketOperationError, |
74 | UnknownSocketError = QAbstractSocket::UnknownSocketError, |
75 | OperationError = QAbstractSocket::OperationError |
76 | }; |
77 | |
78 | enum LocalSocketState |
79 | { |
80 | UnconnectedState = QAbstractSocket::UnconnectedState, |
81 | ConnectingState = QAbstractSocket::ConnectingState, |
82 | ConnectedState = QAbstractSocket::ConnectedState, |
83 | ClosingState = QAbstractSocket::ClosingState |
84 | }; |
85 | |
86 | QLocalSocket(QObject *parent = nullptr); |
87 | ~QLocalSocket(); |
88 | |
89 | void connectToServer(OpenMode openMode = ReadWrite); |
90 | void connectToServer(const QString &name, OpenMode openMode = ReadWrite); |
91 | void disconnectFromServer(); |
92 | |
93 | void setServerName(const QString &name); |
94 | QString serverName() const; |
95 | QString fullServerName() const; |
96 | |
97 | void abort(); |
98 | virtual bool isSequential() const override; |
99 | virtual qint64 bytesAvailable() const override; |
100 | virtual qint64 bytesToWrite() const override; |
101 | virtual bool canReadLine() const override; |
102 | virtual bool open(OpenMode openMode = ReadWrite) override; |
103 | virtual void close() override; |
104 | LocalSocketError error() const; |
105 | bool flush(); |
106 | bool isValid() const; |
107 | qint64 readBufferSize() const; |
108 | void setReadBufferSize(qint64 size); |
109 | |
110 | bool setSocketDescriptor(qintptr socketDescriptor, |
111 | LocalSocketState socketState = ConnectedState, |
112 | OpenMode openMode = ReadWrite); |
113 | qintptr socketDescriptor() const; |
114 | |
115 | LocalSocketState state() const; |
116 | bool waitForBytesWritten(int msecs = 30000) override; |
117 | bool waitForConnected(int msecs = 30000); |
118 | bool waitForDisconnected(int msecs = 30000); |
119 | bool waitForReadyRead(int msecs = 30000) override; |
120 | |
121 | Q_SIGNALS: |
122 | void connected(); |
123 | void disconnected(); |
124 | #if QT_DEPRECATED_SINCE(5,15) |
125 | QT_DEPRECATED_NETWORK_API_5_15_X("Use QLocalSocket::errorOccurred(QLocalSocket::LocalSocketError) instead" ) |
126 | void error(QLocalSocket::LocalSocketError socketError); |
127 | #endif |
128 | void errorOccurred(QLocalSocket::LocalSocketError socketError); |
129 | void stateChanged(QLocalSocket::LocalSocketState socketState); |
130 | |
131 | protected: |
132 | virtual qint64 readData(char*, qint64) override; |
133 | virtual qint64 writeData(const char*, qint64) override; |
134 | |
135 | private: |
136 | Q_DISABLE_COPY(QLocalSocket) |
137 | #if defined(QT_LOCALSOCKET_TCP) |
138 | Q_PRIVATE_SLOT(d_func(), void _q_stateChanged(QAbstractSocket::SocketState)) |
139 | Q_PRIVATE_SLOT(d_func(), void _q_errorOccurred(QAbstractSocket::SocketError)) |
140 | #elif defined(Q_OS_WIN) |
141 | Q_PRIVATE_SLOT(d_func(), void _q_canWrite()) |
142 | Q_PRIVATE_SLOT(d_func(), void _q_pipeClosed()) |
143 | Q_PRIVATE_SLOT(d_func(), void _q_winError(ulong, const QString &)) |
144 | #else |
145 | Q_PRIVATE_SLOT(d_func(), void _q_stateChanged(QAbstractSocket::SocketState)) |
146 | Q_PRIVATE_SLOT(d_func(), void _q_errorOccurred(QAbstractSocket::SocketError)) |
147 | Q_PRIVATE_SLOT(d_func(), void _q_connectToSocket()) |
148 | Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt()) |
149 | #endif |
150 | }; |
151 | |
152 | #ifndef QT_NO_DEBUG_STREAM |
153 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, QLocalSocket::LocalSocketError); |
154 | Q_NETWORK_EXPORT QDebug operator<<(QDebug, QLocalSocket::LocalSocketState); |
155 | #endif |
156 | |
157 | QT_END_NAMESPACE |
158 | |
159 | #endif // QLOCALSOCKET_H |
160 | |