diff options
author | Ben Boeckel <MathStuf@gmail.com> | 2008-11-01 02:53:25 +0000 |
---|---|---|
committer | Ben Boeckel <MathStuf@gmail.com> | 2008-11-01 02:53:25 +0000 |
commit | 09adff1540fe2b7a5033b2e5101f183efd56b1e4 (patch) | |
tree | a61d7920f0570e6382fa2b17b07218390afa3732 /signet/protocol/Packet.cpp | |
parent | eda2022859e7cfde9197a34d9318a9ccec50d841 (diff) | |
download | sigen-09adff1540fe2b7a5033b2e5101f183efd56b1e4.tar.gz sigen-09adff1540fe2b7a5033b2e5101f183efd56b1e4.tar.xz sigen-09adff1540fe2b7a5033b2e5101f183efd56b1e4.zip |
[FIX] Packets getting fleshed out
git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@290 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'signet/protocol/Packet.cpp')
-rw-r--r-- | signet/protocol/Packet.cpp | 81 |
1 files changed, 78 insertions, 3 deletions
diff --git a/signet/protocol/Packet.cpp b/signet/protocol/Packet.cpp index 34e6d125..3112b612 100644 --- a/signet/protocol/Packet.cpp +++ b/signet/protocol/Packet.cpp @@ -19,6 +19,8 @@ #include "Packet.h" // Qt include +#include <QtCore/QtEndian> +#include <QtCore/QBuffer> #include <QtCore/QIODevice> Signet::Protocol::Packet::Packet(const Type type) : @@ -26,11 +28,59 @@ Signet::Protocol::Packet::Packet(const Type type) : { } -void Signet::Protocol::Packet::determineType() +bool Signet::Protocol::Packet::determineType() { + QByteArray temp = m_rawData; + QBuffer buffer(&temp); + int size = m_rawData.size(); + union + { + char packetType_raw[2]; + qint16 packetType; + }; + union + { + char dataSize_raw[8]; + qint64 dataSize; + }; + union + { + char checksum_raw[2]; + quint16 checksum; + }; + if ((m_type == Invalid) && (6 <= size)) + { + buffer.read(packetType_raw, 2); + buffer.read(dataSize_raw, 8); + buffer.read(checksum_raw, 2); + packetType = qFromBigEndian(packetType); + dataSize = qFromBigEndian(dataSize); + checksum = qFromBigEndian(checksum); + switch (packetType) + { + case RawData: + m_type = RawData; + break; + case Acceptance: + m_type = Acceptance; + break; + case Denial: + m_type = Denial; + break; + case Challenge: + m_type = Challenge; + break; + default: + m_type = Invalid; + break; + } + } + if (m_type == Invalid) + return false; + // TODO: verify the size } -Signet::Protocol::Packet::Type Signet::Protocol::Packet::type() const +qint16 Signet::Protocol::Packet::type() const { return m_type; } @@ -46,7 +96,32 @@ void Signet::Protocol::Packet::write(const QByteArray& data) m_rawData.append(data); } -void Signet::Protocol::Packet::dump(QIODevice* device) const +void Signet::Protocol::Packet::rawDump(QIODevice* device) const { device->write(m_rawData); } + +void Signet::Protocol::Packet::dump(QIODevice* device) const +{ + if (m_type == Invalid) + { + qCritical("Cannot send a packet of invalid type"); + return; + } + union + { + char header[10]; + struct + { + qint16 type; + qint64 size; + quint16 checksum; + }headerParts; + }; + QByteArray data = m_rawData; + headerParts.type = qToBigEndian(m_type); + headerParts.size = qToBigEndian(m_rawData.size()); + headerParts.checksum = qToBigEndian(qChecksum(m_rawData.data(), m_rawData.size())); + data.prepend(header); + device->write(data); +} |