KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
tcpserver.h
1/**
2 * @brief An abstract TCP server that manages client connections in a dedicated thread.
3 */
4#ifndef TCPSERVER_H
5#define TCPSERVER_H
6
7#include <QSslCertificate>
8#include <QSslKey>
9#include <QTcpServer>
10#include <QThread>
11
12#include <Kanoop/mutexevent.h>
13
14#include <Kanoop/utility/loggingbaseclass.h>
15
17
18/**
19 * @brief Abstract TCP server supporting both plain-text and TLS/SSL connections.
20 *
21 * Subclass TcpServer and implement createClient() to construct the appropriate
22 * TcpServerClientObject for each incoming connection. The server runs its listen
23 * loop in a dedicated QThread; call start() and stop() to control it.
24 */
25class KANOOP_EXPORT TcpServer : public QTcpServer,
26 public LoggingBaseClass
27{
28 Q_OBJECT
29public:
30 /**
31 * @brief Construct a plain-text TCP server.
32 * @param serverAddress Address to listen on
33 * @param serverPort Port to listen on
34 */
35 TcpServer(const QHostAddress& serverAddress, int serverPort);
36
37 /**
38 * @brief Construct an SSL/TLS TCP server with a single CA certificate.
39 * @param serverAddress Address to listen on
40 * @param serverPort Port to listen on
41 * @param privateKey Server private key
42 * @param localCertificate Server certificate
43 * @param caCert CA certificate for client verification
44 * @param verifyPeer Whether to require client certificate verification
45 */
46 TcpServer(const QHostAddress& serverAddress, int serverPort, const QSslKey& privateKey, const QSslCertificate& localCertificate, const QSslCertificate& caCert, bool verifyPeer = false);
47
48 /**
49 * @brief Construct an SSL/TLS TCP server with a list of CA certificates.
50 * @param serverAddress Address to listen on
51 * @param serverPort Port to listen on
52 * @param privateKey Server private key
53 * @param localCertificate Server certificate
54 * @param caCerts List of CA certificates
55 * @param verifyPeer Whether to require client certificate verification
56 */
57 TcpServer(const QHostAddress& serverAddress, int serverPort, const QSslKey& privateKey, const QSslCertificate& localCertificate, const QList<QSslCertificate>& caCerts, bool verifyPeer = false);
58
59 /** @brief Destructor — stops the server and cleans up all client connections. */
60 virtual ~TcpServer();
61
62 /**
63 * @brief Start the server and begin accepting connections.
64 * @return true if the server started successfully
65 */
66 bool start();
67
68 /** @brief Stop the server and close all active client connections. */
69 void stop();
70
71 /** @brief Return the server's private key (SSL mode only).
72 * @return Server private key */
73 QSslKey privateKey() const { return _privateKey; }
74 /** @brief Return the server's local certificate (SSL mode only).
75 * @return Server certificate */
76 QSslCertificate localCertificate() const { return _localCertificate; }
77 /** @brief Return the list of CA certificates (SSL mode only).
78 * @return List of CA certificates */
79 QList<QSslCertificate> caCerts() const { return _caCerts; }
80 /** @brief Return whether peer certificate verification is enabled.
81 * @return true if peer verification is enabled */
82 bool verifyPeer() const { return _verifyPeer; }
83
84 /**
85 * @brief Return the time the server was started.
86 * @return QDateTime of the last successful start()
87 */
88 QDateTime startTime() const { return _startTime; }
89
90protected:
91 /**
92 * @brief Factory method called for each incoming connection.
93 * @param server Pointer to this server
94 * @param fd Native socket file descriptor for the new connection
95 * @return Newly allocated TcpServerClientObject to manage the connection
96 */
97 virtual TcpServerClientObject* createClient(TcpServer* server, qintptr fd) = 0;
98
99 /** @brief Handle a new incoming connection by delegating to createClient().
100 * @param handle Native socket descriptor for the incoming connection */
101 virtual void incomingConnection(qintptr handle) override;
102
103private:
104 /** @brief Shared initialisation for all constructors. */
105 void commonInit();
106
107 QHostAddress _serverAddress;
108 int _serverPort;
109
110 bool _startSuccess;
111
112 QList<TcpServerClientObject*> _clients;
113
114 QSslKey _privateKey;
115 QSslCertificate _localCertificate;
116 QList<QSslCertificate> _caCerts;
117 bool _verifyPeer;
118
119 QThread _thread;
120 QDateTime _startTime;
121 MutexEvent _startEvent;
122 MutexEvent _stopEvent;
123
124private slots:
125 /** @brief Invoked when the server thread starts. */
126 void onThreadStarted();
127 /** @brief Invoked when the server thread finishes. */
128 void onThreadFinished();
129 /** @brief Invoked when a client object signals it has finished. */
130 void onClientFinished();
131};
132
133#endif // TCPSERVER_H
Mixin base class providing categorized logging at four nesting levels.
MutexEvent.
Definition mutexevent.h:32
Manages the lifetime of a single accepted TCP (or SSL/TLS) client socket.
Abstract TCP server supporting both plain-text and TLS/SSL connections.
Definition tcpserver.h:27
bool verifyPeer() const
Return whether peer certificate verification is enabled.
Definition tcpserver.h:82
QList< QSslCertificate > caCerts() const
Return the list of CA certificates (SSL mode only).
Definition tcpserver.h:79
TcpServer(const QHostAddress &serverAddress, int serverPort, const QSslKey &privateKey, const QSslCertificate &localCertificate, const QList< QSslCertificate > &caCerts, bool verifyPeer=false)
Construct an SSL/TLS TCP server with a list of CA certificates.
QSslKey privateKey() const
Return the server's private key (SSL mode only).
Definition tcpserver.h:73
QSslCertificate localCertificate() const
Return the server's local certificate (SSL mode only).
Definition tcpserver.h:76
QDateTime startTime() const
Return the time the server was started.
Definition tcpserver.h:88
TcpServer(const QHostAddress &serverAddress, int serverPort, const QSslKey &privateKey, const QSslCertificate &localCertificate, const QSslCertificate &caCert, bool verifyPeer=false)
Construct an SSL/TLS TCP server with a single CA certificate.
TcpServer(const QHostAddress &serverAddress, int serverPort)
Construct a plain-text TCP server.
bool start()
Start the server and begin accepting connections.
virtual void incomingConnection(qintptr handle) override
Handle a new incoming connection by delegating to createClient().
virtual ~TcpServer()
Destructor — stops the server and cleans up all client connections.
void stop()
Stop the server and close all active client connections.
virtual TcpServerClientObject * createClient(TcpServer *server, qintptr fd)=0
Factory method called for each incoming connection.