-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcphandle.h
67 lines (50 loc) · 1.28 KB
/
tcphandle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef TCPHANDLECREATOR_H
#define TCPHANDLECREATOR_H
#include <QObject>
#include <QRunnable>
#include <QTcpSocket>
#include <QString>
#include <QByteArray>
#include <QThread>
// The TCP socket wrapper that allows other threads to share
// a socket to send/receive TCP data packets.
class TcpHandle : public QTcpSocket {
Q_OBJECT
public:
explicit TcpHandle(QObject *parent = 0);
~TcpHandle() override;
void init();
signals:
// Signals when new message has been received and buffered.
void receive(QByteArray data);
// Signals a new message will be sent out soon.
void send(QString msg);
// Signals to shutdown the socket.
void shutdown();
public slots:
void transmit(QString msg);
void truncRead();
void terminate();
private:
QByteArray buffer_;
};
// The helper class to instantiate the socket handle in a dedicated thread.
class TcpHandleCreator : public QObject, public QRunnable {
Q_OBJECT
public:
explicit TcpHandleCreator(qintptr socketDescriptor);
~TcpHandleCreator() override;
void execute();
signals:
void socketReady(TcpHandle *socket);
void socketFailed();
void create();
public slots:
void doCreation();
protected:
void run() override;
private:
static QThread socketThread_;
qintptr socketDescriptor_;
};
#endif // TCPHANDLECREATOR_H