-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpmanager.cpp
55 lines (46 loc) · 1.6 KB
/
udpmanager.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
45
46
47
48
49
50
51
52
53
54
55
#include "udpmanager.h"
#include <QMetaType>
QLoggingCategory UdpManager::UdpManagerCategory("UdpManager");
UdpManager::UdpManager(QObject *parent) : QObject{parent} {
init();
connections();
}
void UdpManager::init() {
// receiver başlamadan istek göndermenin manası yok.
setObjectName("UdpManager");
m_address.setAddress(IP);
// QHostAddress(UDP_IP)
// QHostAddress::SpecialAddress::AnyIPv4
// QHostAddress("10.1.1.0")
// QHostAddress("10.1.1.1")
// QHostAddress(VALIDATOR_IP)
// QHostAddress("10.1.1.55")
// QHostAddress("10.1.1.255")
m_sender = new UdpSender(m_address, UDP_SEND_PORT, this);
m_receiver = new UdpReceiver(UDP_RECEIVE_PORT);
m_thread = new QThread(this);
m_thread->setObjectName("ReceiverThread");
m_receiver->moveToThread(m_thread);
}
void UdpManager::connections() {
QObject::connect(m_thread, &QThread::started, m_receiver, &UdpReceiver::run,
Qt::ConnectionType::QueuedConnection);
qRegisterMetaType<std::optional<Utilities::HatStructList>>(
"OptionalHatStructList");
QObject::connect(
m_receiver,
QOverload<std::optional<Utilities::HatStructList>>::of(
&UdpReceiver::dataReceived),
[this](const std::optional<Utilities::HatStructList> &hatStructList) {
emit receive(hatStructList);
});
startAll();
}
void UdpManager::startAll() {
// start things(thread, timer, ...) after (signal-slot)connect
m_thread->start();
}
// slots
void UdpManager::sent(const QByteArray &message) {
m_sender->sent(std::forward<decltype(message)>(message));
}