8 |
8 |
#include <mutex>
|
9 |
9 |
#include <thread>
|
10 |
10 |
|
|
11 |
|
|
12 |
/**
|
|
13 |
* Manages communication with clients who can connect to the server in order to receive real-time data about the current
|
|
14 |
* exercise. The served data may be used e.g. for external visualization. A reference implementation of such a client is
|
|
15 |
* the deltarobot-vr Unity application.
|
|
16 |
*/
|
11 |
17 |
class CurveDataServer : QObject
|
12 |
18 |
{
|
13 |
19 |
Q_OBJECT
|
14 |
20 |
|
15 |
21 |
public:
|
|
22 |
|
|
23 |
/**
|
|
24 |
* Holds information about a client, its Qt socket, data buffer etc.
|
|
25 |
*/
|
16 |
26 |
struct Client {
|
17 |
27 |
static const size_t BUF_SIZE = 4096;
|
18 |
28 |
static const size_t MESSAGE_ID_SIZE = 2;
|
19 |
29 |
|
20 |
|
QTcpSocket* socket;
|
21 |
|
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
|
|
30 |
QTcpSocket* socket; ///< Communication socket
|
|
31 |
std::map<uint64_t, uint64_t> ping; ///< Key: ping ID; Value: ping timestamp (msec);
|
|
32 |
uint64_t latency; ///< Current latency
|
|
33 |
uint64_t lastPong; ///< Timestamp of the last pong
|
24 |
34 |
|
25 |
|
char buf[BUF_SIZE];
|
26 |
|
size_t bufUsed;
|
|
35 |
char buf[BUF_SIZE]; ///< Message buffer
|
|
36 |
size_t bufUsed = 0; ///< Number of bytes used up in the message buffer
|
27 |
37 |
|
28 |
|
bool magic = false;
|
29 |
|
bool version = false;
|
30 |
|
bool initialized = false;
|
|
38 |
bool magic = false; ///< Whether the client has sent the protocol magic
|
|
39 |
bool version = false; ///< Whether the client has sent the protocol version
|
|
40 |
bool initialized = false; ///< Whether the client has successfully initialized its communication (by sending its magic and version)
|
31 |
41 |
|
32 |
42 |
Client(QTcpSocket* socket);
|
33 |
43 |
};
|
... | ... | |
35 |
45 |
explicit CurveDataServer();
|
36 |
46 |
~CurveDataServer();
|
37 |
47 |
|
|
48 |
/** Sends the current actuator position. */
|
38 |
49 |
void sendActuatorPosition(float x, float y, float z);
|
|
50 |
|
|
51 |
/** Sends the actual direction the actuator is moving in. */
|
39 |
52 |
void sendActualDirection(float x, float y, float z);
|
|
53 |
|
|
54 |
/** Sends the direction the actuator *should* be moving in, in order to draw the curve. */
|
40 |
55 |
void sendTargetDirection(float x, float y, float z);
|
|
56 |
|
|
57 |
/** Stores and sends the specified curve. */
|
41 |
58 |
void sendNewCurve(QList<QVector4D> &points);
|
|
59 |
|
|
60 |
/** Stores and sends the specified curve. */
|
42 |
61 |
void sendNewCurve(QList<QVector3D> &points);
|
|
62 |
|
|
63 |
/** Sends the currently stored curve. This is used for newly connected clients. */
|
43 |
64 |
void sendCurve(Client &client);
|
44 |
65 |
|
45 |
|
void processPings(); ///< send pings, check timeouts
|
|
66 |
/** Sends ping messages to the clients, checks whether any clients have timed out, and disconnects any that have. */
|
|
67 |
void processPings();
|
46 |
68 |
|
47 |
69 |
public slots:
|
48 |
70 |
void onNewConnection();
|
... | ... | |
50 |
72 |
void onReadyRead();
|
51 |
73 |
|
52 |
74 |
private:
|
|
75 |
/** Gets the client that is connected via the specified socket. */
|
53 |
76 |
QList<Client>::iterator clientOf(QTcpSocket *socket);
|
54 |
77 |
|
55 |
78 |
void handleMessage(Client& client, uint16_t messageId, void* content);
|
... | ... | |
58 |
81 |
void handlePong(Client& client, uint64_t* content);
|
59 |
82 |
void handleEOT(Client& client, char* content);
|
60 |
83 |
|
61 |
|
void validPreamble(Client& client);
|
62 |
84 |
void removeSocket(QTcpSocket *socket);
|
63 |
85 |
void terminateConnection(Client& client, std::string&& reason);
|
64 |
86 |
void sendPing(Client& client);
|
... | ... | |
75 |
97 |
QTcpServer _server;
|
76 |
98 |
std::thread _pingThread;
|
77 |
99 |
|
|
100 |
/**
|
|
101 |
* A flag that can be switched to `false` when the application should end, in order to gracefully stop the ping
|
|
102 |
* thread.
|
|
103 |
*/
|
78 |
104 |
volatile bool _threadIsRunning = true;
|
79 |
105 |
|
80 |
106 |
std::recursive_mutex _socketsLock;
|
Re #8906 - Server doc comments; cleanup