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 |
6d9543aa
|
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 |
285c6fe5
|
Oto Šťáva
|
/**
|
18 |
|
|
* Size of the client buffer.
|
19 |
|
|
*
|
20 |
|
|
* @note Current implementation truncates any variable-length messages longer than what would fit in the buffer.
|
21 |
|
|
*/
|
22 |
6d9543aa
|
Jakub Hejman
|
static const size_t BUF_SIZE = 4096;
|
23 |
285c6fe5
|
Oto Šťáva
|
|
24 |
|
|
static const size_t MESSAGE_ID_SIZE = sizeof(uint16_t); ///< Size of the protocol message identifier.
|
25 |
|
|
static const size_t MESSAGE_LENGTH_SIZE = sizeof(uint32_t); ///< Size of the variable-length message's content length.
|
26 |
0cda8e8d
|
Oto Šťáva
|
|
27 |
|
|
QTcpSocket* socket;
|
28 |
6d9543aa
|
Jakub Hejman
|
std::map<uint64_t, uint64_t> ping; ///< key: ping ID; value: ping timestamp (msec);
|
29 |
|
|
uint64_t latency; ///< client's latency
|
30 |
|
|
uint64_t lastPong; ///< timestamp of last pong
|
31 |
0cda8e8d
|
Oto Šťáva
|
|
32 |
|
|
char buf[BUF_SIZE];
|
33 |
|
|
size_t bufUsed;
|
34 |
|
|
|
35 |
6d9543aa
|
Jakub Hejman
|
bool magic = false;
|
36 |
|
|
bool version = false;
|
37 |
|
|
bool initialized = false;
|
38 |
|
|
|
39 |
0cda8e8d
|
Oto Šťáva
|
Client(QTcpSocket* socket);
|
40 |
|
|
};
|
41 |
|
|
|
42 |
|
|
explicit CurveDataServer();
|
43 |
|
|
~CurveDataServer();
|
44 |
|
|
|
45 |
e6053a29
|
Oto Šťáva
|
void sendActuatorPosition(float x, float y, float z);
|
46 |
f06d5ae1
|
Jakub Hejman
|
void sendActualDirection(float x, float y, float z);
|
47 |
|
|
void sendTargetDirection(float x, float y, float z);
|
48 |
705930ed
|
Jakub Hejman
|
void sendNewCurve(QList<QVector4D> &points);
|
49 |
|
|
void sendNewCurve(QList<QVector3D> &points);
|
50 |
6d9543aa
|
Jakub Hejman
|
void sendCurve(Client &client);
|
51 |
|
|
|
52 |
|
|
void processPings(); ///< send pings, check timeouts
|
53 |
681d1a97
|
Jakub Hejman
|
|
54 |
|
|
public slots:
|
55 |
|
|
void onNewConnection();
|
56 |
|
|
void onSocketStateChanged(QAbstractSocket::SocketState socketState);
|
57 |
|
|
void onReadyRead();
|
58 |
|
|
|
59 |
|
|
private:
|
60 |
0cda8e8d
|
Oto Šťáva
|
QList<Client>::iterator clientOf(QTcpSocket *socket);
|
61 |
|
|
|
62 |
285c6fe5
|
Oto Šťáva
|
void handleMessage(Client& client, uint16_t messageId, void* content, size_t contentSize);
|
63 |
6d9543aa
|
Jakub Hejman
|
void handleProtocolMagic(Client& client, char* content);
|
64 |
|
|
void handleVersion(Client& client, uint8_t content);
|
65 |
|
|
void handlePong(Client& client, uint64_t* content);
|
66 |
285c6fe5
|
Oto Šťáva
|
void handleEOT(Client& client, char* content, size_t contentSize);
|
67 |
6d9543aa
|
Jakub Hejman
|
|
68 |
|
|
void validPreamble(Client& client);
|
69 |
|
|
void removeSocket(QTcpSocket *socket);
|
70 |
|
|
void terminateConnection(Client& client, std::string&& reason);
|
71 |
|
|
void sendPing(Client& client);
|
72 |
0cda8e8d
|
Oto Šťáva
|
|
73 |
e60ade62
|
Jakub Hejman
|
void sendPreamble(QTcpSocket *socket);
|
74 |
|
|
void sendProtocolMagic(QTcpSocket *socket);
|
75 |
|
|
void sendVersion(QTcpSocket *socket);
|
76 |
0cda8e8d
|
Oto Šťáva
|
void sendEot(QTcpSocket *socket, std::string&& message);
|
77 |
681d1a97
|
Jakub Hejman
|
|
78 |
6d9543aa
|
Jakub Hejman
|
void sendMessageToAllConnected(QByteArray &message);
|
79 |
|
|
|
80 |
|
|
void threadFunction();
|
81 |
|
|
|
82 |
681d1a97
|
Jakub Hejman
|
QTcpServer _server;
|
83 |
6d9543aa
|
Jakub Hejman
|
std::thread _pingThread;
|
84 |
|
|
|
85 |
|
|
volatile bool _threadIsRunning = true;
|
86 |
0cda8e8d
|
Oto Šťáva
|
|
87 |
|
|
std::recursive_mutex _socketsLock;
|
88 |
|
|
QList<Client> _sockets;
|
89 |
6d9543aa
|
Jakub Hejman
|
|
90 |
|
|
std::recursive_mutex _currentCurveLock;
|
91 |
|
|
QList<QVector3D> _currentCurve;
|
92 |
681d1a97
|
Jakub Hejman
|
};
|
93 |
|
|
|
94 |
|
|
#endif // CURVEDATASERVER_H
|