Revize 04839796
Přidáno uživatelem Oto Šťáva před téměř 4 roky(ů)
deltarobot/curvedataserver.h | ||
---|---|---|
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 |
/** |
18 | 28 |
* Size of the client buffer. |
... | ... | |
21 | 31 |
*/ |
22 | 32 |
static const size_t BUF_SIZE = 4096; |
23 | 33 |
|
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. |
|
34 |
static const size_t MESSAGE_ID_SIZE = sizeof(uint16_t); ///< Size of the protocol message identifier |
|
35 |
static const size_t MESSAGE_LENGTH_SIZE = sizeof(uint32_t); ///< Size of the variable-length message's content length |
|
36 |
|
|
37 |
QTcpSocket* socket; ///< Communication socket |
|
38 |
std::map<uint64_t, uint64_t> ping; ///< Key: ping ID; Value: ping timestamp (msec); |
|
39 |
uint64_t latency; ///< Current latency |
|
40 |
uint64_t lastPong; ///< Timestamp of the last pong |
|
26 | 41 |
|
27 |
QTcpSocket* socket; |
|
28 |
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 |
|
42 |
char buf[BUF_SIZE]; ///< Message buffer |
|
43 |
size_t bufUsed = 0; ///< Number of bytes used up in the message buffer |
|
31 | 44 |
|
32 |
char buf[BUF_SIZE]; |
|
33 |
size_t bufUsed; |
|
45 |
bool magic = false; ///< Whether the client has sent the correct protocol magic |
|
46 |
bool version = false; ///< Whether the client has sent the correct protocol version |
|
47 |
bool protocolValid = false; ///< Whether the client has sent both the correct protocol magic and version. |
|
34 | 48 |
|
35 |
bool magic = false; |
|
36 |
bool version = false; |
|
37 |
bool initialized = false; |
|
49 |
bool initialized = false; ///< Whether the server has initialized this client with the current curve |
|
38 | 50 |
|
39 | 51 |
Client(QTcpSocket* socket); |
40 | 52 |
}; |
... | ... | |
42 | 54 |
explicit CurveDataServer(); |
43 | 55 |
~CurveDataServer(); |
44 | 56 |
|
57 |
/** Sends the current actuator position. */ |
|
45 | 58 |
void sendActuatorPosition(float x, float y, float z); |
59 |
|
|
60 |
/** Sends the actual direction the actuator is moving in. */ |
|
46 | 61 |
void sendActualDirection(float x, float y, float z); |
62 |
|
|
63 |
/** Sends the direction the actuator *should* be moving in, in order to draw the curve. */ |
|
47 | 64 |
void sendTargetDirection(float x, float y, float z); |
65 |
|
|
66 |
/** Stores and sends the specified curve. */ |
|
48 | 67 |
void sendNewCurve(QList<QVector4D> &points); |
68 |
|
|
69 |
/** Stores and sends the specified curve. */ |
|
49 | 70 |
void sendNewCurve(QList<QVector3D> &points); |
71 |
|
|
72 |
/** Sends the currently stored curve. This is used for newly connected clients. */ |
|
50 | 73 |
void sendCurve(Client &client); |
51 | 74 |
|
52 |
void processPings(); ///< send pings, check timeouts |
|
75 |
/** Sends ping messages to the clients, checks whether any clients have timed out, and disconnects any that have. */ |
|
76 |
void processPings(); |
|
53 | 77 |
|
54 | 78 |
public slots: |
55 | 79 |
void onNewConnection(); |
... | ... | |
57 | 81 |
void onReadyRead(); |
58 | 82 |
|
59 | 83 |
private: |
84 |
/** Gets the client that is connected via the specified socket. */ |
|
60 | 85 |
QList<Client>::iterator clientOf(QTcpSocket *socket); |
61 | 86 |
|
62 | 87 |
void handleMessage(Client& client, uint16_t messageId, void* content, size_t contentSize); |
... | ... | |
65 | 90 |
void handlePong(Client& client, uint64_t* content); |
66 | 91 |
void handleEOT(Client& client, char* content, size_t contentSize); |
67 | 92 |
|
68 |
void validPreamble(Client& client); |
|
93 |
/** |
|
94 |
* Checks whether the specified client has already sent both its protocol magic and version. If so, the appropriate |
|
95 |
* flag is set and a log is written out into qDebug. |
|
96 |
*/ |
|
97 |
void validateProtocol(Client& client); |
|
98 |
|
|
69 | 99 |
void removeSocket(QTcpSocket *socket); |
70 | 100 |
void terminateConnection(Client& client, std::string&& reason); |
71 | 101 |
void sendPing(Client& client); |
... | ... | |
77 | 107 |
|
78 | 108 |
void sendMessageToAllConnected(QByteArray &message); |
79 | 109 |
|
80 |
void threadFunction(); |
|
81 |
|
|
82 | 110 |
QTcpServer _server; |
83 |
std::thread _pingThread; |
|
84 |
|
|
85 |
volatile bool _threadIsRunning = true; |
|
86 | 111 |
|
87 | 112 |
std::recursive_mutex _socketsLock; |
88 | 113 |
QList<Client> _sockets; |
Také k dispozici: Unified diff
Re #8906 - Server and client refactor