cutelyst  4.4.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
socket.h
1 /*
2  * SPDX-FileCopyrightText: (C) 2016-2018 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #ifndef SOCKET_H
6 #define SOCKET_H
7 
9 #include "protocol.h"
10 #include "serverengine.h"
11 
12 #include <Cutelyst/Headers>
13 
14 #include <QHostAddress>
15 #include <QLocalSocket>
16 #include <QSslSocket>
17 #include <QTcpSocket>
18 
19 class QIODevice;
20 
21 namespace Cutelyst {
22 
23 class Engine;
24 class Socket
25 {
26  Q_GADGET
27 public:
28  Socket(bool secure, Engine *_engine);
29  virtual ~Socket();
30 
31  virtual void connectionClose() = 0;
32 
33  // Returns false if disconnected
34  virtual bool requestFinished() = 0;
35  virtual bool flush() = 0;
36 
37  inline void resetSocket()
38  {
39  if (protoData->upgradedFrom) {
40  ProtocolData *data = protoData->upgradedFrom;
41  delete protoData;
42  protoData = data;
43  }
44  processing = 0;
45 
46  protoData->resetData();
47  }
48 
49  QByteArray serverAddress;
50  QHostAddress remoteAddress;
51  quint16 remotePort = 0;
52  Engine *engine;
53  Protocol *proto = nullptr;
54  ProtocolData *protoData = nullptr;
55  qint8 processing = 0;
56  bool isSecure;
57  bool timeout = false;
58 };
59 
60 class TcpSocket final
61  : public QTcpSocket
62  , public Socket
63 {
64  Q_OBJECT
65 public:
66  explicit TcpSocket(Cutelyst::Engine *engine, QObject *parent = nullptr);
67 
68  void connectionClose() override final;
69  bool requestFinished() override final;
70  bool flush() override final;
71  void socketDisconnected();
72 
73 Q_SIGNALS:
74  // Always connect this in queued mode as the client might have
75  // triggered the disconnect event like websocket close, and deleting
76  // it's context from this event will crash
77  void finished();
78 };
79 
80 #ifndef QT_NO_SSL
81 
82 class SslSocket final
83  : public QSslSocket
84  , public Socket
85 {
86  Q_OBJECT
87 public:
88  explicit SslSocket(Cutelyst::Engine *engine, QObject *parent = nullptr);
89 
90  void connectionClose() override final;
91  bool requestFinished() override final;
92  bool flush() override final;
93  void socketDisconnected();
94 
95 Q_SIGNALS:
96  // See TcpSocket note
97  void finished();
98 };
99 
100 #endif // QT_NO_SSL
101 
102 class LocalSocket final
103  : public QLocalSocket
104  , public Socket
105 {
106  Q_OBJECT
107 public:
108  explicit LocalSocket(Cutelyst::Engine *engine, QObject *parent = nullptr);
109 
110  void connectionClose() override final;
111  bool requestFinished() override final;
112  bool flush() override final;
113  void socketDisconnected();
114 
115 Q_SIGNALS:
116  // See TcpSocket note
117  void finished();
118 };
119 
120 } // namespace Cutelyst
121 
122 #endif // SOCKET_H
The Cutelyst Engine.
Definition: engine.h:20
The Cutelyst namespace holds all public Cutelyst API.
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
QObject * parent() const const