-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint.cpp
More file actions
75 lines (64 loc) · 1.32 KB
/
Copy pathendpoint.cpp
File metadata and controls
75 lines (64 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "endpoint.h"
#include <QQueue>
#include <QTimer>
Endpoint::Endpoint(QObject *parent) :
QObject(parent),
m_queue(new QQueue<qint8>),
m_timer(new QTimer(this))
{
connect(m_timer, &QTimer::timeout, this, &Endpoint::update);
m_timer->setSingleShot(true);
m_timer->start(10);
max_burst_length = 1;
max_packet_length = 1;
}
Endpoint::~Endpoint()
{
m_timer->stop();
close();
delete m_timer;
delete m_queue;
}
void Endpoint::putData(const QByteArray &data)
{
for (auto &x : data){
m_queue->enqueue(x);
}
}
bool Endpoint::open()
{
m_queue->clear();
return true;
}
void Endpoint::close()
{
m_queue->clear();
}
void Endpoint::update()
{
bool success = true;
int burst = 0;
while (success)
{
if (!waiting.length())
{
success = false;
while (waiting.length() < max_packet_length && !m_queue->isEmpty())
{
char byte = m_queue->dequeue();
waiting.append(byte);
}
}
if (waiting.length())
{
success = writeData(waiting);
if (success)
{
waiting.clear();
}
}
if (++burst >= max_burst_length)
break;
}
m_timer->start(success ? 1 : 10);
}