tufao  0.8.1
An asynchronous web framework for C++ built on top of Qt
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
websocket.h
1 /*
2  Copyright (c) 2012 Vinícius dos Santos Oliveira
3 
4  Permission is hereby granted, free of charge, to any person obtaining a copy
5  of this software and associated documentation files (the "Software"), to deal
6  in the Software without restriction, including without limitation the rights
7  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  copies of the Software, and to permit persons to whom the Software is
9  furnished to do so, subject to the following conditions:
10 
11  The above copyright notice and this permission notice shall be included in all
12  copies or substantial portions of the Software.
13 
14  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  SOFTWARE.
21  */
22 
23 #ifndef TUFAO_WEBSOCKET_H
24 #define TUFAO_WEBSOCKET_H
25 
26 #include "abstractmessagesocket.h"
27 #include "headers.h"
28 
29 #include <QtNetwork/QAbstractSocket>
30 
31 #if defined(NO_ERROR) && defined(_WIN32)
32 # define TUFAO_WINERROR_WORKAROUND
33 # undef NO_ERROR
34 #endif
35 
36 class QSslError;
37 
38 namespace Tufao {
39 
40 class HttpServerRequest;
41 
71 class TUFAO_EXPORT WebSocket : public AbstractMessageSocket
72 {
73  Q_OBJECT
74 public:
86  enum Error
87  {
91  NO_ERROR = 0,
215  UNKNOWN_ERROR
216  };
217 
222  {
230  BINARY_MESSAGE
231  };
232 
238  explicit WebSocket(QObject *parent = 0);
239 
243  ~WebSocket();
244 
266  bool connectToHost(const QHostAddress &address, quint16 port,
267  const QByteArray &resource,
268  const Headers &headers = Headers());
269 
275  bool connectToHost(const QHostAddress &address, const QByteArray &resource,
276  const Headers &headers = Headers());
277 
284  bool connectToHost(const QString &hostname, quint16 port,
285  const QByteArray &resource,
286  const Headers &headers = Headers());
287 
296  bool connectToHost(const QString &hostname, const QByteArray &resource,
297  const Headers &headers = Headers());
298 
310  bool connectToHostEncrypted(const QString &hostname, quint16 port,
311  const QByteArray &resource,
312  const Headers &headers,
313  const QList<QSslError> &ignoredSslErrors);
314 
318  bool connectToHostEncrypted(const QString &hostname, quint16 port,
319  const QByteArray &resource,
320  const Headers &headers = Headers());
321 
327  bool connectToHostEncrypted(const QString &hostname,
328  const QByteArray &resource,
329  const Headers &headers = Headers());
330 
340  bool connectToHostEncrypted(const QHostAddress &address, quint16 port,
341  const QByteArray &resource,
342  const Headers &headers = Headers());
343 
352  bool connectToHostEncrypted(const QHostAddress &address,
353  const QByteArray &resource,
354  const Headers &headers = Headers());
355 
392  bool startServerHandshake(const HttpServerRequest *request,
393  const QByteArray &head,
394  const Headers &headers = Headers());
395 
406  void setMessagesType(MessageType type);
407 
415  MessageType messagesType() const;
416 
420  Error error() const;
421 
425  QString errorString() const;
426 
435  QHostAddress peerAddress() const;
436 
445  quint16 peerPort() const;
446 
451  static QList<QByteArray> supportedProtocols(const Headers &headers);
452 
453 signals:
465  void pong(QByteArray data);
466 
467 public slots:
468  void close();
469  bool sendMessage(const QByteArray &msg);
470 
478  bool sendBinaryMessage(const QByteArray &msg);
479 
487  bool sendUtf8Message(const QByteArray &msg);
488 
498  bool ping(const QByteArray &data);
499 
500 private slots:
501  void onSocketError(QAbstractSocket::SocketError error);
502  void onSslErrors(const QList<QSslError> &errors);
503  void onConnected();
504  void onReadyRead();
505  void onDisconnected();
506 
507 private:
508  void connectToHost(QAbstractSocket *socket, const QByteArray &resource,
509  const Headers &headers = Headers());
510 
511  bool isResponseOkay();
512  void onClientHandshakeError();
513 
514  void close(quint16 code);
515 
516  void readData(const QByteArray &data);
517  void parseBuffer();
518  bool parseFrame();
519  bool parseSize16();
520  bool parseSize64();
521  bool parseMaskingKey();
522  bool parsePayloadData();
523 
524  void decodeFragment(QByteArray &fragment);
525  void evaluateControlFrame();
526 
527  struct Priv;
528  Priv *priv;
529 };
530 
531 } // namespace Tufao
532 
533 #if defined(TUFAO_WINERROR_WORKAROUND)
534 # define NO_ERROR 0L
535 # undef TUFAO_WINERROR_WORKAROUND
536 #endif
537 
538 #endif // TUFAO_WEBSOCKET_H
See QAbstractSocket::ProxyNotFoundError.
Definition: websocket.h:189
See QAbstractSocket::ProxyConnectionTimeoutError.
Definition: websocket.h:182
This class represents a WebSocket connection.
Definition: websocket.h:71
See QAbstractSocket::HostNotFoundError.
Definition: websocket.h:112
UTF8 messages.
Definition: websocket.h:226
See QAbstractSocket::SocketAccessError.
Definition: websocket.h:119
The Tufao::HttpServer represents a HTTP request received by Tufao::HttpServer.
Definition: httpserverrequest.h:47
See QAbstractSocket::SocketTimeoutError.
Definition: websocket.h:133
Error
This enum describes the possible erros tha can occur.
Definition: websocket.h:86
See QAbstractSocket::UnsupportedSocketOperationError.
Definition: websocket.h:147
See QAbstractSocket::ProxyAuthenticationRequiredError.
Definition: websocket.h:154
It occurs when the server doesn't support WebSocket for the resource asked for (or for any resource a...
Definition: websocket.h:204
MessageType
This enum represents the possible message's types that WebSocket supports.
Definition: websocket.h:221
See QAbstractSocket::ProxyProtocolError.
Definition: websocket.h:196
See QAbstractSocket::NetworkError.
Definition: websocket.h:140
See QAbstractSocket::SslHandshakeFailedError.
Definition: websocket.h:161
The Tufao::AbstractMessageSocket class represents a socket that sends and receives messages...
Definition: abstractmessagesocket.h:50
See QAbstractSocket::ProxyConnectionClosedError.
Definition: websocket.h:175
See QAbstractSocket::ProxyConnectionRefusedError.
Definition: websocket.h:168
This class provides a representation of HTTP headers.
Definition: headers.h:42
See QAbstractSocket::SocketResourceError.
Definition: websocket.h:126
See QAbstractSocket::ConnectionRefusedError.
Definition: websocket.h:98
It occurs when the remote peer (or an intermediary) violates the WebSocket protocol.
Definition: websocket.h:209
See QAbstractSocket::RemoteHostClosedError.
Definition: websocket.h:105