-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriggerserver.cpp
44 lines (32 loc) · 1.08 KB
/
triggerserver.cpp
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
#include "triggerserver.h"
#include <QNetworkDatagram>
TriggerServer::TriggerServer(int interval, quint16 port, QObject *parent) : QObject(parent), socket(new QUdpSocket(this))
{
socket->bind(QHostAddress::Any, port );
connect(socket, &QUdpSocket::readyRead, this, &TriggerServer::handlePendingDatagrams);
using namespace std::chrono;
startTimer(seconds(interval),Qt::PreciseTimer );
m_mode = m_modeProto.arg(socket->localPort()).arg(peers.size());
}
QString TriggerServer::mode() const
{
return m_mode;
}
void TriggerServer::handlePendingDatagrams()
{
while (socket->hasPendingDatagrams()) {
QNetworkDatagram datagram = socket->receiveDatagram();
Peer p(datagram.senderAddress(), datagram.senderPort());
peers.append(p);
qDebug() << "added client" << p.addr << p.port;
}
m_mode = m_modeProto.arg(socket->localPort()).arg(peers.size());
emit modeChanged();
}
void TriggerServer::timerEvent(QTimerEvent *event)
{
foreach(Peer p , peers) {
socket->writeDatagram("n", p.addr, p.port);
}
emit trigger();
}