Added copyright disclaimer to all files
[contrib/qtwebsockets.git] / src / qwebsocket.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 #ifndef QWEBSOCKET_H
20 #define QWEBSOCKET_H
21
22 #include <QUrl>
23 #include <QAbstractSocket>
24 #include <QHostAddress>
25 #ifndef QT_NO_NETWORKPROXY
26 #include <QNetworkProxy>
27 #endif
28 #include <QTime>
29 #include "qwebsocketsglobal.h"
30 #include "qwebsocketprotocol.h"
31
32 QT_BEGIN_NAMESPACE
33
34 class QTcpSocket;
35 class QWebSocketPrivate;
36
37 class Q_WEBSOCKETS_EXPORT QWebSocket:public QObject
38 {
39         Q_OBJECT
40
41 public:
42         explicit QWebSocket(QString origin = QString(), QWebSocketProtocol::Version version = QWebSocketProtocol::V_LATEST, QObject *parent = 0);
43         virtual ~QWebSocket();
44
45         void abort();
46         QAbstractSocket::SocketError error() const;
47         QString errorString() const;
48         bool flush();
49         bool isValid();
50         QHostAddress localAddress() const;
51         quint16 localPort() const;
52         QAbstractSocket::PauseModes pauseMode() const;
53         QHostAddress peerAddress() const;
54         QString peerName() const;
55         quint16 peerPort() const;
56 #ifndef QT_NO_NETWORKPROXY
57         QNetworkProxy proxy() const;
58         void setProxy(const QNetworkProxy &networkProxy);
59 #endif
60         qint64 readBufferSize() const;
61         void setReadBufferSize(qint64 size);
62
63         void resume();
64         void setPauseMode(QAbstractSocket::PauseModes pauseMode);
65
66         void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value);
67         QVariant socketOption(QAbstractSocket::SocketOption option);
68         QAbstractSocket::SocketState state() const;
69
70         bool waitForConnected(int msecs = 30000);
71         bool waitForDisconnected(int msecs = 30000);
72
73         QWebSocketProtocol::Version version();
74         QString resourceName();
75         QUrl requestUrl();
76         QString origin();
77         QString protocol();
78         QString extension();
79
80         qint64 write(const char *message);              //send data as text
81         qint64 write(const char *message, qint64 maxSize);              //send data as text
82         qint64 write(const QString &message);   //send data as text
83         qint64 write(const QByteArray &data);   //send data as binary
84
85 public Q_SLOTS:
86         virtual void close(QWebSocketProtocol::CloseCode closeCode = QWebSocketProtocol::CC_NORMAL, QString reason = QString());
87         virtual void open(const QUrl &url, bool mask = true);
88         void ping();
89
90 Q_SIGNALS:
91         void aboutToClose();
92         void connected();
93         void disconnected();
94         void stateChanged(QAbstractSocket::SocketState state);
95 #ifndef QT_NO_NETWORKPROXY
96         void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *pAuthenticator);
97 #endif
98         void readChannelFinished();
99         void textFrameReceived(QString frame, bool isLastFrame);
100         void binaryFrameReceived(QByteArray frame, bool isLastFrame);
101         void textMessageReceived(QString message);
102         void binaryMessageReceived(QByteArray message);
103         void error(QAbstractSocket::SocketError error);
104         void pong(quint64 elapsedTime);
105
106 private:
107         Q_DISABLE_COPY(QWebSocket)
108         QWebSocket(QTcpSocket *pTcpSocket, QWebSocketProtocol::Version version, QObject *parent = 0);
109         QWebSocketPrivate * const d_ptr;
110
111         friend class QWebSocketPrivate;
112 };
113
114 QT_END_NAMESPACE
115
116 #endif // QWEBSOCKET_H