KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
tcpserverclientobject.h
1/**
2 * @brief Abstract base class for objects that handle a single TCP server client connection.
3 */
4#ifndef TCPSERVERCLIENTOBJECT_H
5#define TCPSERVERCLIENTOBJECT_H
6#include <QAbstractSocket>
7#include <QSslError>
8
9#include <Kanoop/utility/abstractthreadclass.h>
10
11class TcpServer;
12class QTcpSocket;
13
14/**
15 * @brief Manages the lifetime of a single accepted TCP (or SSL/TLS) client socket.
16 *
17 * Each accepted connection is wrapped in a TcpServerClientObject running in its own
18 * thread. Subclasses must implement receivedData() to process incoming bytes, and
19 * may override clientStarted() and clientFinished() for lifecycle hooks.
20 */
21class KANOOP_EXPORT TcpServerClientObject : public AbstractThreadClass
22{
23 Q_OBJECT
24public:
25 /** @brief Destructor — disconnects and cleans up the socket. */
27
28 /**
29 * @brief Send data to the connected client.
30 * @param data Bytes to transmit
31 */
32 void writeData(const QByteArray& data);
33
34protected:
35 /**
36 * @brief Construct a client object for an accepted connection.
37 * @param server Owning TcpServer
38 * @param fd Native socket file descriptor for the accepted connection
39 * @param useSsl Whether to negotiate SSL/TLS on this socket
40 */
41 TcpServerClientObject(TcpServer* server, qintptr fd, bool useSsl = false);
42
43 /** @brief Called on the client thread immediately after the connection is established. */
44 virtual void clientStarted() {}
45
46 /** @brief Called on the client thread just before the connection is torn down. */
47 virtual void clientFinished() {}
48
49 /**
50 * @brief Called whenever data arrives from the client.
51 * @param data Received bytes
52 */
53 virtual void receivedData(const QByteArray& data) = 0;
54
55 /**
56 * @brief Return the underlying socket for this client.
57 * @return Pointer to the QTcpSocket (may also be a QSslSocket in SSL mode)
58 */
59 QTcpSocket* socket() const { return _socket; }
60
61 /**
62 * @brief Return the owning TcpServer.
63 * @return Pointer to the parent TcpServer
64 */
65 TcpServer* server() const { return _server; }
66
67private:
68 /** @brief Invoked by the thread framework when the thread starts. */
69 virtual void threadStarted() override final;
70 /** @brief Invoked by the thread framework when the thread finishes. */
71 virtual void threadFinished() override final;
72
73 /** @brief Set up the socket for plain-text operation. */
74 void startClearTextClient();
75 /** @brief Set up the socket for SSL/TLS operation. */
76 void startSslClient();
77
78 TcpServer* _server;
79 qintptr _fd;
80
81 QTcpSocket* _socket;
82 bool _useSsl;
83
84private slots:
85 /** @brief Called when data is available to read from the socket. */
86 void onReadyRead();
87 /** @brief Called when the remote end disconnects. */
88 void onDisconnected();
89 /** @brief Called when a socket error occurs. */
90 void onErrorOcurred(QAbstractSocket::SocketError errorCode);
91 /** @brief Called when the SSL handshake completes. */
92 void onEncrypted();
93 /** @brief Called when SSL errors are reported. */
94 void onSslErrors(const QList<QSslError> errors);
95};
96
97#endif // TCPSERVERCLIENTOBJECT_H
Abstract base class for QObject-based workers that run in a dedicated QThread.
virtual void threadStarted()=0
Entry point called on the worker thread immediately after it starts.
Manages the lifetime of a single accepted TCP (or SSL/TLS) client socket.
virtual void receivedData(const QByteArray &data)=0
Called whenever data arrives from the client.
QTcpSocket * socket() const
Return the underlying socket for this client.
virtual ~TcpServerClientObject()
Destructor — disconnects and cleans up the socket.
TcpServer * server() const
Return the owning TcpServer.
virtual void clientStarted()
Called on the client thread immediately after the connection is established.
virtual void clientFinished()
Called on the client thread just before the connection is torn down.
void writeData(const QByteArray &data)
Send data to the connected client.
TcpServerClientObject(TcpServer *server, qintptr fd, bool useSsl=false)
Construct a client object for an accepted connection.
Abstract TCP server supporting both plain-text and TLS/SSL connections.
Definition tcpserver.h:27