Calculate message lengths ourselves.

This commit is contained in:
Andrius Štikonas 2015-02-15 15:11:39 +00:00
parent 277a5f46c5
commit c414c091e8
1 changed files with 17 additions and 9 deletions

View File

@ -41,10 +41,10 @@ Socket::Socket ( QHostAddress IPaddress, QByteArray reply )
commandID[WriteSocketData] = QByteArray::fromHex ( "74 6d" );
// 2 hex bytes are the total length of the message
datagram[Subscribe] = magicKey + QByteArray::fromHex ( "00 1e" ) + commandID[Subscribe] + mac + twenties + rmac + twenties;
datagram[PowerOn] = magicKey + QByteArray::fromHex ( "00 17 64 63" ) + mac + twenties + zeros + one;
datagram[PowerOff] = magicKey + QByteArray::fromHex ( "00 17 64 63" ) + mac + twenties + zeros + zero;
datagram[TableData] = magicKey + QByteArray::fromHex ( "00 1d" ) + commandID[TableData] + mac + twenties + zeros + QByteArray::fromHex ( "01 00 00" ) + zeros;
datagram[Subscribe] = commandID[Subscribe] + mac + twenties + rmac + twenties;
datagram[PowerOn] = QByteArray::fromHex ( "64 63" ) + mac + twenties + zeros + one;
datagram[PowerOff] = QByteArray::fromHex ( "64 63" ) + mac + twenties + zeros + zero;
datagram[TableData] = commandID[TableData] + mac + twenties + zeros + QByteArray::fromHex ( "01 00 00" ) + zeros;
udpSocket = new QUdpSocket();
udpSocket->connectToHost ( ip, 10000 );
@ -76,7 +76,15 @@ void Socket::run()
{
while ( commands.size() > 0 )
{
udpSocket->write ( datagram[commands.head()] );
QByteArray currentDatagram = datagram[commands.head()];
QByteArray recordLength;
QDataStream stream(&recordLength, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::BigEndian);
uint16_t length = currentDatagram.length() + 4; // +4 for magicKey and total message length
stream << length;
currentDatagram = magicKey + recordLength + currentDatagram;
udpSocket->write ( currentDatagram );
QThread::msleep(100);
}
}
@ -122,15 +130,15 @@ void Socket::writeSocketData(QByteArray name, QByteArray password, QByteArray ti
uint16_t length = record.length();
stream << length;
datagram[WriteSocketData] = magicKey + QByteArray::fromHex ( "00 a5" ) + commandID[WriteSocketData] + mac + twenties + zeros + QByteArray::fromHex ( "04:00:01" ) /*table number and unknown*/ + recordLength + record;
datagram[WriteSocketData] = commandID[WriteSocketData] + mac + twenties + zeros + QByteArray::fromHex ( "04:00:01" ) /*table number and unknown*/ + recordLength + record;
sendDatagram ( WriteSocketData );
}
void Socket::tableData()
{
sendDatagram ( TableData );
datagram[SocketData] = magicKey + QByteArray::fromHex ( "00 1d" ) + commandID[SocketData] + mac + twenties + zeros + QByteArray::fromHex ( "04 00 00" ) + zeros;
datagram[TimingData] = magicKey + QByteArray::fromHex ( "00 1d" ) + commandID[TimingData] + mac + twenties + zeros + QByteArray::fromHex ( "03 00 00" ) + zeros;
datagram[SocketData] = commandID[SocketData] + mac + twenties + zeros + QByteArray::fromHex ( "04 00 00" ) + zeros;
datagram[TimingData] = commandID[TimingData] + mac + twenties + zeros + QByteArray::fromHex ( "03 00 00" ) + zeros;
// table number + 00 + version number
sendDatagram ( SocketData );
sendDatagram ( TimingData );