Replaced QTcpSocket * with a QIODevice * to make network independent unit tests possible.
[contrib/qtwebsockets.git] / src / qwebsocketserver.h
1 /*
2 QWebSockets implements the WebSocket protocol as defined in RFC 6455.
3 Copyright (C) 2013 Kurt Pattyn (pattyn.kurt@gmail.com)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 #ifndef QWEBSOCKETSERVER_H
21 #define QWEBSOCKETSERVER_H
22
23 #include <QObject>
24 #include <QString>
25 #include <QHostAddress>
26 #include "qwebsocketsglobal.h"
27 #include "qwebsocketprotocol.h"
28
29 QT_BEGIN_NAMESPACE
30
31 class QWebSocketServerPrivate;
32 class QWebSocket;
33
34 class Q_WEBSOCKETS_EXPORT QWebSocketServer : public QObject
35 {
36     Q_OBJECT
37
38 public:
39     explicit QWebSocketServer(const QString &serverName, QObject *parent = 0);
40     virtual ~QWebSocketServer();
41
42     bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
43     void close();
44
45     bool isListening() const;
46
47     void setMaxPendingConnections(int numConnections);
48     int maxPendingConnections() const;
49
50     quint16 serverPort() const;
51     QHostAddress serverAddress() const;
52
53     bool setSocketDescriptor(int socketDescriptor);
54     int socketDescriptor() const;
55
56     bool waitForNewConnection(int msec = 0, bool *timedOut = 0);
57     bool hasPendingConnections() const;
58     virtual QWebSocket *nextPendingConnection();
59
60     QAbstractSocket::SocketError serverError() const;
61     QString errorString() const;
62
63     void pauseAccepting();
64     void resumeAccepting();
65
66     void setServerName(const QString &serverName);
67     QString serverName() const;
68
69 #ifndef QT_NO_NETWORKPROXY
70     void setProxy(const QNetworkProxy &networkProxy);
71     QNetworkProxy proxy() const;
72 #endif
73
74     QList<QWebSocketProtocol::Version> supportedVersions() const;
75     QList<QString> supportedProtocols() const;
76     QList<QString> supportedExtensions() const;
77
78 protected:
79     virtual bool isOriginAllowed(const QString &origin) const;
80
81 Q_SIGNALS:
82     void acceptError(QAbstractSocket::SocketError socketError);
83     void newConnection();
84
85 private:
86     Q_DISABLE_COPY(QWebSocketServer)
87     Q_DECLARE_PRIVATE(QWebSocketServer)
88     QWebSocketServerPrivate * const d_ptr;
89 };
90
91 QT_END_NAMESPACE
92
93 #endif // QWEBSOCKETSERVER_H