1 |
681d1a97
|
Jakub Hejman
|
#ifndef CURVEDATASERVER_H
|
2 |
|
|
#define CURVEDATASERVER_H
|
3 |
|
|
|
4 |
|
|
#include <QObject>
|
5 |
|
|
#include <QTcpServer>
|
6 |
|
|
#include <QTcpSocket>
|
7 |
0cda8e8d
|
Oto Šťáva
|
#include <map>
|
8 |
|
|
#include <mutex>
|
9 |
9360266c
|
Jakub Hejman
|
#include <thread>
|
10 |
f06d5ae1
|
Jakub Hejman
|
|
11 |
681d1a97
|
Jakub Hejman
|
class CurveDataServer : QObject
|
12 |
|
|
{
|
13 |
|
|
Q_OBJECT
|
14 |
|
|
|
15 |
|
|
public:
|
16 |
0cda8e8d
|
Oto Šťáva
|
struct Client {
|
17 |
9360266c
|
Jakub Hejman
|
static const size_t BUF_SIZE = 4096;
|
18 |
0cda8e8d
|
Oto Šťáva
|
static const size_t MESSAGE_ID_SIZE = 2;
|
19 |
|
|
|
20 |
|
|
QTcpSocket* socket;
|
21 |
9360266c
|
Jakub Hejman
|
std::map<uint64_t, uint64_t> ping; ///< key: ping ID; value: ping timestamp (msec);
|
22 |
|
|
uint64_t latency; ///< client's latency
|
23 |
|
|
uint64_t lastPong; ///< timestamp of last pong
|
24 |
0cda8e8d
|
Oto Šťáva
|
|
25 |
|
|
char buf[BUF_SIZE];
|
26 |
|
|
size_t bufUsed;
|
27 |
|
|
|
28 |
9360266c
|
Jakub Hejman
|
bool magic = false;
|
29 |
|
|
bool version = false;
|
30 |
|
|
bool initialized = false;
|
31 |
|
|
|
32 |
0cda8e8d
|
Oto Šťáva
|
Client(QTcpSocket* socket);
|
33 |
|
|
};
|
34 |
|
|
|
35 |
|
|
explicit CurveDataServer();
|
36 |
|
|
~CurveDataServer();
|
37 |
|
|
|
38 |
e6053a29
|
Oto Šťáva
|
void sendActuatorPosition(float x, float y, float z);
|
39 |
f06d5ae1
|
Jakub Hejman
|
void sendActualDirection(float x, float y, float z);
|
40 |
|
|
void sendTargetDirection(float x, float y, float z);
|
41 |
705930ed
|
Jakub Hejman
|
void sendNewCurve(QList<QVector4D> &points);
|
42 |
|
|
void sendNewCurve(QList<QVector3D> &points);
|
43 |
9360266c
|
Jakub Hejman
|
void sendCurve(Client &client);
|
44 |
681d1a97
|
Jakub Hejman
|
|
45 |
|
|
public slots:
|
46 |
|
|
void onNewConnection();
|
47 |
|
|
void onSocketStateChanged(QAbstractSocket::SocketState socketState);
|
48 |
|
|
void onReadyRead();
|
49 |
|
|
|
50 |
|
|
private:
|
51 |
0cda8e8d
|
Oto Šťáva
|
QList<Client>::iterator clientOf(QTcpSocket *socket);
|
52 |
|
|
|
53 |
|
|
void handleMessage(Client& client, uint16_t messageId, void* content);
|
54 |
9360266c
|
Jakub Hejman
|
void handleProtocolMagic(Client& client, char* content);
|
55 |
|
|
void handleVersion(Client& client, uint8_t content);
|
56 |
|
|
void handlePong(Client& client, void* content);
|
57 |
|
|
void handleEOT(Client& client, void* content);
|
58 |
|
|
|
59 |
|
|
void validPreamble(Client& client);
|
60 |
|
|
void removeSocket(QTcpSocket *socket);
|
61 |
|
|
void terminateConnection(Client& client, std::string&& reason);
|
62 |
|
|
void sendPing(Client& client);
|
63 |
0cda8e8d
|
Oto Šťáva
|
|
64 |
e60ade62
|
Jakub Hejman
|
void sendPreamble(QTcpSocket *socket);
|
65 |
|
|
void sendProtocolMagic(QTcpSocket *socket);
|
66 |
|
|
void sendVersion(QTcpSocket *socket);
|
67 |
0cda8e8d
|
Oto Šťáva
|
void sendEot(QTcpSocket *socket, std::string&& message);
|
68 |
681d1a97
|
Jakub Hejman
|
|
69 |
9360266c
|
Jakub Hejman
|
void sendMessageToAllConnected(QByteArray &message);
|
70 |
|
|
|
71 |
|
|
void threadFunction();
|
72 |
|
|
|
73 |
681d1a97
|
Jakub Hejman
|
QTcpServer _server;
|
74 |
9360266c
|
Jakub Hejman
|
std::thread _pingThread;
|
75 |
|
|
|
76 |
|
|
volatile bool _threadIsRunning = true;
|
77 |
0cda8e8d
|
Oto Šťáva
|
|
78 |
|
|
std::recursive_mutex _socketsLock;
|
79 |
|
|
QList<Client> _sockets;
|
80 |
9360266c
|
Jakub Hejman
|
|
81 |
|
|
std::recursive_mutex _currentCurveLock;
|
82 |
|
|
QList<QVector3D> _currentCurve;
|
83 |
681d1a97
|
Jakub Hejman
|
};
|
84 |
|
|
|
85 |
|
|
#endif // CURVEDATASERVER_H
|