cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
serverengine.h
1 /*
2  * SPDX-FileCopyrightText: (C) 2016-2023 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #pragma once
6 
7 #include <Cutelyst/Engine>
8 
9 #include <QElapsedTimer>
10 #include <QObject>
11 #include <QTimer>
12 
13 class QTcpServer;
14 
15 namespace Cutelyst {
16 
17 class TcpServer;
18 class Protocol;
19 class ProtocolFastCGI;
20 class ProtocolHttp;
21 class ProtocolHttp2;
22 class Server;
23 class Socket;
24 class ServerEngine final : public Cutelyst::Engine
25 {
26  Q_OBJECT
27 public:
29  int workerCore,
30  const QVariantMap &opts,
31  Server *wsgi);
32  virtual ~ServerEngine() override;
33 
34  virtual int workerId() const override;
35 
36  void setServers(const std::vector<QObject *> &servers);
37 
38  void postFork(int workerId);
39 
40  int m_workerId = 0;
41 
42  virtual bool init() override;
43 
44  inline QByteArray lastDate()
45  {
46  if (m_lastDateTimer.hasExpired(1000)) {
47  m_lastDate = dateHeader();
48  m_lastDateTimer.restart();
49  }
50  return m_lastDate;
51  }
52 
53  void handleSocketShutdown(Socket *sock);
54 
55 Q_SIGNALS:
56  void started();
57  void shutdown();
58  void shutdownCompleted(Cutelyst::ServerEngine *engine);
59 
60 protected:
61  inline void startSocketTimeout()
62  {
63  if (m_socketTimeout && ++m_serversTimeout == 1) {
64  m_socketTimeout->start();
65  }
66  }
67 
68  inline void stopSocketTimeout()
69  {
70  if (m_socketTimeout && --m_serversTimeout == 0) {
71  m_socketTimeout->stop();
72  }
73  }
74 
75  inline void serverShutdown()
76  {
77  if (--m_runningServers == 0) {
78  Q_EMIT shutdownCompleted(this);
79  }
80  }
81 
82  static QByteArray dateHeader();
83 
84 private:
85  friend class ProtocolHttp;
86  friend class ProtocolFastCGI;
87  friend class LocalServer;
88  friend class TcpServer;
89  friend class TcpSslServer;
90  friend class Connection;
91  friend class Socket;
92 
93  Protocol *getProtoHttp();
94  ProtocolHttp2 *getProtoHttp2();
95  Protocol *getProtoFastCgi();
96 
97  QByteArray m_lastDate;
98  QElapsedTimer m_lastDateTimer;
99  QTimer *m_socketTimeout = nullptr;
100  Server *m_wsgi;
101  ProtocolHttp *m_protoHttp = nullptr;
102  ProtocolHttp2 *m_protoHttp2 = nullptr;
103  ProtocolFastCGI *m_protoFcgi = nullptr;
104  int m_runningServers = 0;
105  int m_serversTimeout = 0;
106 };
107 
108 } // namespace Cutelyst
The Cutelyst application.
Definition: application.h:66
The Cutelyst Engine.
Definition: engine.h:20
int workerCore() const
Definition: engine.cpp:67
QVariantMap opts() const
Definition: engine.cpp:257
virtual int workerId() const override
virtual bool init() override
Implements a web server.
Definition: server.h:60
The Cutelyst namespace holds all public Cutelyst API.
bool hasExpired(qint64 timeout) const const
qint64 restart()
Q_EMITQ_EMIT
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
void start()
void stop()