-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsixnetmessagehandler.cpp
92 lines (77 loc) · 2.62 KB
/
sixnetmessagehandler.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "sixnetmessagehandler.h"
SixnetMessageDispatcher::SixnetMessageDispatcher(QHostAddress address, int port, int timeout, QObject *parent) :
QObject(parent),
m_sentMessage(NULL),
m_sequence(0),
m_address(address),
m_port(port),
m_connected(false)
{
m_socket = new QUdpSocket(this);
m_socket->bind(QHostAddress::LocalHost, port);
connect(m_socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
connect(m_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
m_socket->connectToHost(address, port);
m_timer.setInterval(timeout);
m_timer.setSingleShot(true);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
}
void SixnetMessageDispatcher::connectToHost()
{
if (m_socket->state() == QUdpSocket::UnconnectedState) {
m_socket->connectToHost(m_address, m_port);
}
}
void SixnetMessageDispatcher::disconnectFromHost()
{
if (m_socket->state() != QUdpSocket::UnconnectedState) {
m_socket->disconnectFromHost();
}
}
void SixnetMessageDispatcher::sendMessage(SixnetMessage *msg)
{
m_waitingMessages.append(msg);
sendNextMessageFromWaiting();
}
void SixnetMessageDispatcher::sendNextMessageFromWaiting()
{
if ((m_sentMessage != NULL) || (m_waitingMessages.size() == 0))
return;
m_sentMessage = m_waitingMessages.takeFirst();
m_sentMessage->setSequence(m_sequence);
connect(m_sentMessage, SIGNAL(debug(QString)), this, SIGNAL(debug(QString)));
emit debug(tr("OUT %1").arg(QString(m_sentMessage->serialize().toHex())));
m_socket->write(m_sentMessage->serialize());
m_timer.start();
m_sequence++;
}
void SixnetMessageDispatcher::socketStateChanged(QAbstractSocket::SocketState newState)
{
m_connected = (newState == QUdpSocket::ConnectedState);
if (newState == QUdpSocket::ConnectedState) {
sendNextMessageFromWaiting();
}
}
void SixnetMessageDispatcher::socketReadyRead()
{
if (m_socket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(m_socket->pendingDatagramSize());
m_timer.stop();
m_socket->readDatagram(datagram.data(), datagram.size());
if (m_sentMessage != NULL) {
if(!m_sentMessage->checkAnswer(datagram))
emit debug(tr("Error: %1").arg(m_sentMessage->error()));
m_sentMessage = NULL;
}
}
}
void SixnetMessageDispatcher::timeout()
{
if (m_sentMessage != NULL) {
emit debug(tr("timeout"));
m_sentMessage->deleteLater();
m_sentMessage = NULL;
}
sendNextMessageFromWaiting();
}