1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #ifndef QABSTRACTSOCKET_H
43 #define QABSTRACTSOCKET_H
45 #include <QtCore/qiodevice.h>
46 #include <QtCore/qobject.h>
47 #ifndef QT_NO_DEBUG_STREAM
48 #include <QtCore/qdebug.h>
57 #ifndef QT_NO_NETWORKPROXY
60 class QAbstractSocketPrivate;
63 class Q_NETWORK_EXPORT QAbstractSocket : public QIODevice
66 Q_ENUMS(SocketType NetworkLayerProtocol SocketError SocketState SocketOption)
71 UnknownSocketType = -1
73 enum NetworkLayerProtocol {
77 UnknownNetworkLayerProtocol = -1
80 ConnectionRefusedError,
81 RemoteHostClosedError,
85 SocketTimeoutError, /* 5 */
86 DatagramTooLargeError,
89 SocketAddressNotAvailableError,
90 UnsupportedSocketOperationError, /* 10 */
91 UnfinishedSocketOperationError,
92 ProxyAuthenticationRequiredError,
93 SslHandshakeFailedError,
94 ProxyConnectionRefusedError,
95 ProxyConnectionClosedError, /* 15 */
96 ProxyConnectionTimeoutError,
100 SslInternalError, /* 20 */
101 SslInvalidUserDataError,
103 UnknownSocketError = -1
115 LowDelayOption, // TCP_NODELAY
116 KeepAliveOption, // SO_KEEPALIVE
117 MulticastTtlOption, // IP_MULTICAST_TTL
118 MulticastLoopbackOption, // IP_MULTICAST_LOOPBACK
119 TypeOfServiceOption //IP_TOS
122 DefaultForPlatform = 0x0,
124 DontShareAddress = 0x2,
125 ReuseAddressHint = 0x4
127 Q_DECLARE_FLAGS(BindMode, BindFlag)
130 PauseOnSslErrors = 0x1
132 Q_DECLARE_FLAGS(PauseModes, PauseMode)
134 QAbstractSocket(SocketType socketType, QObject *parent);
135 virtual ~QAbstractSocket();
137 virtual void resume(); // to continue after proxy authentication required, SSL errors etc.
138 PauseModes pauseMode() const;
139 void setPauseMode(PauseModes pauseMode);
141 bool bind(const QHostAddress &address, quint16 port = 0, BindMode mode = DefaultForPlatform);
142 bool bind(quint16 port = 0, BindMode mode = DefaultForPlatform);
144 virtual void connectToHost(const QString &hostName, quint16 port, OpenMode mode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol);
145 virtual void connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite);
146 virtual void disconnectFromHost();
148 bool isValid() const;
150 qint64 bytesAvailable() const;
151 qint64 bytesToWrite() const;
153 bool canReadLine() const;
155 quint16 localPort() const;
156 QHostAddress localAddress() const;
157 quint16 peerPort() const;
158 QHostAddress peerAddress() const;
159 QString peerName() const;
161 qint64 readBufferSize() const;
162 virtual void setReadBufferSize(qint64 size);
166 virtual qintptr socketDescriptor() const;
167 virtual bool setSocketDescriptor(qintptr socketDescriptor, SocketState state = ConnectedState,
168 OpenMode openMode = ReadWrite);
170 virtual void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
171 virtual QVariant socketOption(QAbstractSocket::SocketOption option);
173 SocketType socketType() const;
174 SocketState state() const;
175 SocketError error() const;
179 bool isSequential() const;
183 // for synchronous access
184 virtual bool waitForConnected(int msecs = 30000);
185 bool waitForReadyRead(int msecs = 30000);
186 bool waitForBytesWritten(int msecs = 30000);
187 virtual bool waitForDisconnected(int msecs = 30000);
189 #ifndef QT_NO_NETWORKPROXY
190 void setProxy(const QNetworkProxy &networkProxy);
191 QNetworkProxy proxy() const;
198 void stateChanged(QAbstractSocket::SocketState);
199 void error(QAbstractSocket::SocketError);
200 #ifndef QT_NO_NETWORKPROXY
201 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
205 qint64 readData(char *data, qint64 maxlen);
206 qint64 readLineData(char *data, qint64 maxlen);
207 qint64 writeData(const char *data, qint64 len);
209 void setSocketState(SocketState state);
210 void setSocketError(SocketError socketError);
211 void setLocalPort(quint16 port);
212 void setLocalAddress(const QHostAddress &address);
213 void setPeerPort(quint16 port);
214 void setPeerAddress(const QHostAddress &address);
215 void setPeerName(const QString &name);
217 QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent = 0);
220 Q_DECLARE_PRIVATE(QAbstractSocket)
221 Q_DISABLE_COPY(QAbstractSocket)
223 Q_PRIVATE_SLOT(d_func(), void _q_connectToNextAddress())
224 Q_PRIVATE_SLOT(d_func(), void _q_startConnecting(const QHostInfo &))
225 Q_PRIVATE_SLOT(d_func(), void _q_abortConnectionAttempt())
226 Q_PRIVATE_SLOT(d_func(), void _q_testConnection())
227 Q_PRIVATE_SLOT(d_func(), void _q_forceDisconnect())
231 Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::BindMode)
232 Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::PauseModes)
234 #ifndef QT_NO_DEBUG_STREAM
235 Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError);
236 Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketState);
241 Q_DECLARE_METATYPE(QAbstractSocket::SocketState)
242 Q_DECLARE_METATYPE(QAbstractSocket::SocketError)
246 #endif // QABSTRACTSOCKET_H