-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.h
49 lines (43 loc) · 1.01 KB
/
Client.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
#pragma once
#include <QTcpSocket>
#include <QTcpServer>
#include <QHostAddress>
#include <QMap>
enum class MessageIdType:uint16_t
{
PowerOn,PowerOff,Restart,Update
};
struct Message
{
uint8_t address;
MessageIdType id;
uint32_t number;
uint32_t size;
//and so on...I use this to fixed size of message structure.
};
class Client : public QObject
{
Q_OBJECT
public:
Client(QObject* parent = nullptr);
bool isConnected();
private:
void doConnect();
void write(QByteArray data);
Message deserialize(QByteArray rawaData);
//get same signature for all function to use in map/function
bool powerOn(Message);
bool powerOff(Message);
bool restart(Message);
bool update(Message);
private slots:
void handleData();
void onNewConnection();
void onDisconnected();
void onByteWritten(qint64 bytes);
private:
QTcpServer* m_tcpServer = nullptr;
QTcpSocket* m_tcpSocket = nullptr;
bool m_isConnected = false;
QMap<MessageIdType,std::function<bool(Message)>> m_functions;
};