1
|
#ifndef CURVEDATASERVER_H
|
2
|
#define CURVEDATASERVER_H
|
3
|
|
4
|
#include <QObject>
|
5
|
#include <QTcpServer>
|
6
|
#include <QTcpSocket>
|
7
|
#include <map>
|
8
|
#include <mutex>
|
9
|
|
10
|
class CurveDataServer : QObject
|
11
|
{
|
12
|
Q_OBJECT
|
13
|
|
14
|
public:
|
15
|
struct Client {
|
16
|
static const size_t BUF_SIZE = 32;
|
17
|
static const size_t MESSAGE_ID_SIZE = 2;
|
18
|
|
19
|
QTcpSocket* socket;
|
20
|
std::map<long, long> ping; ///< key: ping ID; value: ping timestamp (msec);
|
21
|
long latency; ///< client's latency
|
22
|
long lastPong; ///< timestamp of last pong
|
23
|
|
24
|
char buf[BUF_SIZE];
|
25
|
size_t bufUsed;
|
26
|
|
27
|
Client(QTcpSocket* socket);
|
28
|
};
|
29
|
|
30
|
explicit CurveDataServer();
|
31
|
~CurveDataServer();
|
32
|
|
33
|
void sendActuatorPosition(float x, float y, float z);
|
34
|
|
35
|
public slots:
|
36
|
void onNewConnection();
|
37
|
void onSocketStateChanged(QAbstractSocket::SocketState socketState);
|
38
|
void onReadyRead();
|
39
|
|
40
|
private:
|
41
|
QList<Client>::iterator clientOf(QTcpSocket *socket);
|
42
|
|
43
|
void handleMessage(Client& client, uint16_t messageId, void* content);
|
44
|
|
45
|
void sendPreamble(QTcpSocket *socket);
|
46
|
void sendProtocolMagic(QTcpSocket *socket);
|
47
|
void sendVersion(QTcpSocket *socket);
|
48
|
void sendEot(QTcpSocket *socket, std::string&& message);
|
49
|
|
50
|
QTcpServer _server;
|
51
|
|
52
|
std::recursive_mutex _socketsLock;
|
53
|
QList<Client> _sockets;
|
54
|
};
|
55
|
|
56
|
#endif // CURVEDATASERVER_H
|