Projekt

Obecné

Profil

Stáhnout (2.83 KB) Statistiky
| Větev: | Tag: | Revize:
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
#include <thread>
10

    
11
class CurveDataServer : QObject
12
{
13
    Q_OBJECT
14

    
15
public:
16
    struct Client {
17
        /**
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
        static const size_t BUF_SIZE = 4096;
23

    
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

    
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
31

    
32
        char buf[BUF_SIZE];
33
        size_t bufUsed;
34

    
35
        bool magic = false;
36
        bool version = false;
37
        bool initialized = false;
38

    
39
        Client(QTcpSocket* socket);
40
    };
41

    
42
    explicit CurveDataServer();
43
    ~CurveDataServer();
44

    
45
    void sendActuatorPosition(float x, float y, float z);
46
    void sendActualDirection(float x, float y, float z);
47
    void sendTargetDirection(float x, float y, float z);
48
    void sendNewCurve(QList<QVector4D> &points);
49
    void sendNewCurve(QList<QVector3D> &points);
50
    void sendCurve(Client &client);
51

    
52
    void processPings();            ///< send pings, check timeouts
53

    
54
public slots:
55
    void onNewConnection();
56
    void onSocketStateChanged(QAbstractSocket::SocketState socketState);
57
    void onReadyRead();
58

    
59
private:
60
    QList<Client>::iterator clientOf(QTcpSocket *socket);
61

    
62
    void handleMessage(Client& client, uint16_t messageId, void* content, size_t contentSize);
63
    void handleProtocolMagic(Client& client, char* content);
64
    void handleVersion(Client& client, uint8_t content);
65
    void handlePong(Client& client, uint64_t* content);
66
    void handleEOT(Client& client, char* content, size_t contentSize);
67

    
68
    void validPreamble(Client& client);
69
    void removeSocket(QTcpSocket *socket);
70
    void terminateConnection(Client& client, std::string&& reason);
71
    void sendPing(Client& client);
72

    
73
    void sendPreamble(QTcpSocket *socket);
74
    void sendProtocolMagic(QTcpSocket *socket);
75
    void sendVersion(QTcpSocket *socket);
76
    void sendEot(QTcpSocket *socket, std::string&& message);
77

    
78
    void sendMessageToAllConnected(QByteArray &message);
79

    
80
    void threadFunction();
81

    
82
    QTcpServer _server;
83
    std::thread _pingThread;
84

    
85
    volatile bool _threadIsRunning = true;
86

    
87
    std::recursive_mutex _socketsLock;
88
    QList<Client> _sockets;
89

    
90
    std::recursive_mutex _currentCurveLock;
91
    QList<QVector3D> _currentCurve;
92
};
93

    
94
#endif // CURVEDATASERVER_H
(3-3/19)