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