79d6e6344956b868e1c3ecb488a642068fa40780
[profile/ivi/qtbase.git] / src / network / socket / qabstractsocket.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QABSTRACTSOCKET_H
43 #define QABSTRACTSOCKET_H
44
45 #include <QtCore/qiodevice.h>
46 #include <QtCore/qobject.h>
47 #ifndef QT_NO_DEBUG_STREAM
48 #include <QtCore/qdebug.h>
49 #endif
50
51 QT_BEGIN_HEADER
52
53 QT_BEGIN_NAMESPACE
54
55
56 class QHostAddress;
57 #ifndef QT_NO_NETWORKPROXY
58 class QNetworkProxy;
59 #endif
60 class QAbstractSocketPrivate;
61 class QAuthenticator;
62
63 class Q_NETWORK_EXPORT QAbstractSocket : public QIODevice
64 {
65     Q_OBJECT
66     Q_ENUMS(SocketType NetworkLayerProtocol SocketError SocketState SocketOption)
67 public:
68     enum SocketType {
69         TcpSocket,
70         UdpSocket,
71         UnknownSocketType = -1
72     };
73     enum NetworkLayerProtocol {
74         IPv4Protocol,
75         IPv6Protocol,
76         AnyIPProtocol,
77         UnknownNetworkLayerProtocol = -1
78     };
79     enum SocketError {
80         ConnectionRefusedError,
81         RemoteHostClosedError,
82         HostNotFoundError,
83         SocketAccessError,
84         SocketResourceError,
85         SocketTimeoutError,                     /* 5 */
86         DatagramTooLargeError,
87         NetworkError,
88         AddressInUseError,
89         SocketAddressNotAvailableError,
90         UnsupportedSocketOperationError,        /* 10 */
91         UnfinishedSocketOperationError,
92         ProxyAuthenticationRequiredError,
93         SslHandshakeFailedError,
94         ProxyConnectionRefusedError,
95         ProxyConnectionClosedError,             /* 15 */
96         ProxyConnectionTimeoutError,
97         ProxyNotFoundError,
98         ProxyProtocolError,
99         OperationError,
100         SslInternalError,                       /* 20 */
101         SslInvalidUserDataError,
102
103         UnknownSocketError = -1
104     };
105     enum SocketState {
106         UnconnectedState,
107         HostLookupState,
108         ConnectingState,
109         ConnectedState,
110         BoundState,
111         ListeningState,
112         ClosingState
113     };
114     enum SocketOption {
115         LowDelayOption, // TCP_NODELAY
116         KeepAliveOption, // SO_KEEPALIVE
117         MulticastTtlOption, // IP_MULTICAST_TTL
118         MulticastLoopbackOption, // IP_MULTICAST_LOOPBACK
119         TypeOfServiceOption //IP_TOS
120     };
121     enum BindFlag {
122         DefaultForPlatform = 0x0,
123         ShareAddress = 0x1,
124         DontShareAddress = 0x2,
125         ReuseAddressHint = 0x4
126     };
127     Q_DECLARE_FLAGS(BindMode, BindFlag)
128     enum PauseMode {
129         PauseNever = 0x0,
130         PauseOnSslErrors = 0x1
131     };
132     Q_DECLARE_FLAGS(PauseModes, PauseMode)
133
134     QAbstractSocket(SocketType socketType, QObject *parent);
135     virtual ~QAbstractSocket();
136
137     virtual void resume(); // to continue after proxy authentication required, SSL errors etc.
138     PauseModes pauseMode() const;
139     void setPauseMode(PauseModes pauseMode);
140
141     bool bind(const QHostAddress &address, quint16 port = 0, BindMode mode = DefaultForPlatform);
142     bool bind(quint16 port = 0, BindMode mode = DefaultForPlatform);
143
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();
147
148     bool isValid() const;
149
150     qint64 bytesAvailable() const;
151     qint64 bytesToWrite() const;
152
153     bool canReadLine() const;
154
155     quint16 localPort() const;
156     QHostAddress localAddress() const;
157     quint16 peerPort() const;
158     QHostAddress peerAddress() const;
159     QString peerName() const;
160
161     qint64 readBufferSize() const;
162     virtual void setReadBufferSize(qint64 size);
163
164     void abort();
165
166     virtual qintptr socketDescriptor() const;
167     virtual bool setSocketDescriptor(qintptr socketDescriptor, SocketState state = ConnectedState,
168                              OpenMode openMode = ReadWrite);
169
170     virtual void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
171     virtual QVariant socketOption(QAbstractSocket::SocketOption option);
172
173     SocketType socketType() const;
174     SocketState state() const;
175     SocketError error() const;
176
177     // from QIODevice
178     void close();
179     bool isSequential() const;
180     bool atEnd() const;
181     bool flush();
182
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);
188
189 #ifndef QT_NO_NETWORKPROXY
190     void setProxy(const QNetworkProxy &networkProxy);
191     QNetworkProxy proxy() const;
192 #endif
193
194 Q_SIGNALS:
195     void hostFound();
196     void connected();
197     void disconnected();
198     void stateChanged(QAbstractSocket::SocketState);
199     void error(QAbstractSocket::SocketError);
200 #ifndef QT_NO_NETWORKPROXY
201     void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
202 #endif
203
204 protected:
205     qint64 readData(char *data, qint64 maxlen);
206     qint64 readLineData(char *data, qint64 maxlen);
207     qint64 writeData(const char *data, qint64 len);
208
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);
216
217     QAbstractSocket(SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent = 0);
218
219 private:
220     Q_DECLARE_PRIVATE(QAbstractSocket)
221     Q_DISABLE_COPY(QAbstractSocket)
222
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())
228 };
229
230
231 Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::BindMode)
232 Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractSocket::PauseModes)
233
234 #ifndef QT_NO_DEBUG_STREAM
235 Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketError);
236 Q_NETWORK_EXPORT QDebug operator<<(QDebug, QAbstractSocket::SocketState);
237 #endif
238
239 QT_END_NAMESPACE
240
241 Q_DECLARE_METATYPE(QAbstractSocket::SocketState)
242 Q_DECLARE_METATYPE(QAbstractSocket::SocketError)
243
244 QT_END_HEADER
245
246 #endif // QABSTRACTSOCKET_H