1/****************************************************************************
2**
3** Copyright (C) 2014 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 QABSTRACTSOCKETENGINE_P_H
43#define QABSTRACTSOCKETENGINE_P_H
44
45//
46// W A R N I N G
47// -------------
48//
49// This file is not part of the Qt API. It exists purely as an
50// implementation detail. This header file may change from version to
51// version without notice, or even be removed.
52//
53// We mean it.
54//
55
56#include "QtNetwork/qhostaddress.h"
57#include "QtNetwork/qabstractsocket.h"
58#include "private/qobject_p.h"
59
60QT_BEGIN_NAMESPACE
61
62class QAuthenticator;
63class QAbstractSocketEnginePrivate;
64#ifndef QT_NO_NETWORKINTERFACE
65class QNetworkInterface;
66#endif
67class QNetworkProxy;
68
69class QAbstractSocketEngineReceiver {
70public:
71 virtual ~QAbstractSocketEngineReceiver(){}
72 virtual void readNotification()= 0;
73 virtual void writeNotification()= 0;
74 virtual void exceptionNotification()= 0;
75 virtual void connectionNotification()= 0;
76#ifndef QT_NO_NETWORKPROXY
77 virtual void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)= 0;
78#endif
79};
80
81class Q_AUTOTEST_EXPORT QAbstractSocketEngine : public QObject
82{
83 Q_OBJECT
84public:
85
86 static QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType, const QNetworkProxy &, QObject *parent);
87 static QAbstractSocketEngine *createSocketEngine(int socketDescripter, QObject *parent);
88
89 QAbstractSocketEngine(QObject *parent = 0);
90
91 enum SocketOption {
92 NonBlockingSocketOption,
93 BroadcastSocketOption,
94 ReceiveBufferSocketOption,
95 SendBufferSocketOption,
96 AddressReusable,
97 BindExclusively,
98 ReceiveOutOfBandData,
99 LowDelayOption,
100 KeepAliveOption,
101 MulticastTtlOption,
102 MulticastLoopbackOption
103 };
104
105 virtual bool initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::IPv4Protocol) = 0;
106
107 virtual bool initialize(int socketDescriptor, QAbstractSocket::SocketState socketState = QAbstractSocket::ConnectedState) = 0;
108
109 virtual int socketDescriptor() const = 0;
110
111 virtual bool isValid() const = 0;
112
113 virtual bool connectToHost(const QHostAddress &address, quint16 port) = 0;
114 virtual bool connectToHostByName(const QString &name, quint16 port) = 0;
115 virtual bool bind(const QHostAddress &address, quint16 port) = 0;
116 virtual bool listen() = 0;
117 virtual int accept() = 0;
118 virtual void close() = 0;
119
120 virtual qint64 bytesAvailable() const = 0;
121
122 virtual qint64 read(char *data, qint64 maxlen) = 0;
123 virtual qint64 write(const char *data, qint64 len) = 0;
124
125#ifndef QT_NO_UDPSOCKET
126#ifndef QT_NO_NETWORKINTERFACE
127 virtual bool joinMulticastGroup(const QHostAddress &groupAddress,
128 const QNetworkInterface &iface) = 0;
129 virtual bool leaveMulticastGroup(const QHostAddress &groupAddress,
130 const QNetworkInterface &iface) = 0;
131 virtual QNetworkInterface multicastInterface() const = 0;
132 virtual bool setMulticastInterface(const QNetworkInterface &iface) = 0;
133#endif // QT_NO_NETWORKINTERFACE
134
135 virtual qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *addr = 0,
136 quint16 *port = 0) = 0;
137 virtual qint64 writeDatagram(const char *data, qint64 len, const QHostAddress &addr,
138 quint16 port) = 0;
139 virtual bool hasPendingDatagrams() const = 0;
140 virtual qint64 pendingDatagramSize() const = 0;
141#endif // QT_NO_UDPSOCKET
142
143 virtual qint64 bytesToWrite() const = 0;
144
145 virtual int option(SocketOption option) const = 0;
146 virtual bool setOption(SocketOption option, int value) = 0;
147
148 virtual bool waitForRead(int msecs = 30000, bool *timedOut = 0) = 0;
149 virtual bool waitForWrite(int msecs = 30000, bool *timedOut = 0) = 0;
150 virtual bool waitForReadOrWrite(bool *readyToRead, bool *readyToWrite,
151 bool checkRead, bool checkWrite,
152 int msecs = 30000, bool *timedOut = 0) = 0;
153
154 QAbstractSocket::SocketError error() const;
155 QString errorString() const;
156 QAbstractSocket::SocketState state() const;
157 QAbstractSocket::SocketType socketType() const;
158 QAbstractSocket::NetworkLayerProtocol protocol() const;
159
160 QHostAddress localAddress() const;
161 quint16 localPort() const;
162 QHostAddress peerAddress() const;
163 quint16 peerPort() const;
164
165 virtual bool isReadNotificationEnabled() const = 0;
166 virtual void setReadNotificationEnabled(bool enable) = 0;
167 virtual bool isWriteNotificationEnabled() const = 0;
168 virtual void setWriteNotificationEnabled(bool enable) = 0;
169 virtual bool isExceptionNotificationEnabled() const = 0;
170 virtual void setExceptionNotificationEnabled(bool enable) = 0;
171
172public Q_SLOTS:
173 void readNotification();
174 void writeNotification();
175 void exceptionNotification();
176 void connectionNotification();
177#ifndef QT_NO_NETWORKPROXY
178 void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator);
179#endif
180
181public:
182 void setReceiver(QAbstractSocketEngineReceiver *receiver);
183protected:
184 QAbstractSocketEngine(QAbstractSocketEnginePrivate &dd, QObject* parent = 0);
185
186 void setError(QAbstractSocket::SocketError error, const QString &errorString) const;
187 void setState(QAbstractSocket::SocketState state);
188 void setSocketType(QAbstractSocket::SocketType socketType);
189 void setProtocol(QAbstractSocket::NetworkLayerProtocol protocol);
190 void setLocalAddress(const QHostAddress &address);
191 void setLocalPort(quint16 port);
192 void setPeerAddress(const QHostAddress &address);
193 void setPeerPort(quint16 port);
194
195private:
196 Q_DECLARE_PRIVATE(QAbstractSocketEngine)
197 Q_DISABLE_COPY(QAbstractSocketEngine)
198};
199
200class QAbstractSocketEnginePrivate : public QObjectPrivate
201{
202 Q_DECLARE_PUBLIC(QAbstractSocketEngine)
203public:
204 QAbstractSocketEnginePrivate();
205
206 mutable QAbstractSocket::SocketError socketError;
207 mutable bool hasSetSocketError;
208 mutable QString socketErrorString;
209 QAbstractSocket::SocketState socketState;
210 QAbstractSocket::SocketType socketType;
211 QAbstractSocket::NetworkLayerProtocol socketProtocol;
212 QHostAddress localAddress;
213 quint16 localPort;
214 QHostAddress peerAddress;
215 quint16 peerPort;
216 QAbstractSocketEngineReceiver *receiver;
217};
218
219
220class Q_AUTOTEST_EXPORT QSocketEngineHandler
221{
222protected:
223 QSocketEngineHandler();
224 virtual ~QSocketEngineHandler();
225 virtual QAbstractSocketEngine *createSocketEngine(QAbstractSocket::SocketType socketType,
226 const QNetworkProxy &, QObject *parent) = 0;
227 virtual QAbstractSocketEngine *createSocketEngine(int socketDescripter, QObject *parent) = 0;
228
229private:
230 friend class QAbstractSocketEngine;
231};
232
233QT_END_NAMESPACE
234
235#endif // QABSTRACTSOCKETENGINE_P_H
236