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 | |
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')
-rw-r--r-- | signet/protocol/ChallengeMediator.cpp | 8 | ||||
-rw-r--r-- | signet/protocol/Packet.cpp | 81 | ||||
-rw-r--r-- | signet/protocol/Packet.h | 18 | ||||
-rw-r--r-- | signet/protocol/PacketMaker.cpp | 1 |
4 files changed, 93 insertions, 15 deletions
diff --git a/signet/protocol/ChallengeMediator.cpp b/signet/protocol/ChallengeMediator.cpp index c26b6ea4..0cbe95ff 100644 --- a/signet/protocol/ChallengeMediator.cpp +++ b/signet/protocol/ChallengeMediator.cpp @@ -51,7 +51,7 @@ bool Signet::Protocol::ChallengeMediator::getServerKey(QByteArray* serverKey) Packet packet = PacketMaker::unwrap(m_socket); if (!packet.isValid()) return false; - packet.dump(&buffer); + packet.rawDump(&buffer); return true; } @@ -69,12 +69,12 @@ bool Signet::Protocol::ChallengeMediator::getClientResponse(QByteArray* clientKe Packet packet = PacketMaker::unwrap(m_socket); if (!packet.isValid()) return false; - packet.dump(&buffer); + packet.rawDump(&buffer); buffer.setBuffer(clientResponse); packet = PacketMaker::unwrap(m_socket); if (!packet.isValid()) return false; - packet.dump(&buffer); + packet.rawDump(&buffer); return true; } @@ -94,7 +94,7 @@ bool Signet::Protocol::ChallengeMediator::getServerResponse(QByteArray* serverRe if (packet.type() == Packet::Denial) m_state = Rejected; else - packet.dump(&buffer); + packet.rawDump(&buffer); return true; } 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); +} diff --git a/signet/protocol/Packet.h b/signet/protocol/Packet.h index 764ae26a..2d15c115 100644 --- a/signet/protocol/Packet.h +++ b/signet/protocol/Packet.h @@ -36,24 +36,26 @@ class SIGNET_EXPORT Packet public: enum Type { - Invalid = -1, - RawData = 0, - Acceptance = 1, - Denial = 2, - Challenge = 3 + Invalid = -1, + RawData = 0x0000, + Acceptance = 0x0001, + Denial = 0x0002, + Challenge = 0x0003, }; Packet(const Type type = Invalid); + // TODO: Add other ctors for other data packages - void determineType(); + bool determineType(); - Type type() const; + qint16 type() const; bool isValid() const; void write(const QByteArray& data); + void rawDump(QIODevice* device) const; void dump(QIODevice* device) const; private: - Type m_type; + qint16 m_type; QByteArray m_rawData; }; } diff --git a/signet/protocol/PacketMaker.cpp b/signet/protocol/PacketMaker.cpp index f2a05d2b..3eb3e0b2 100644 --- a/signet/protocol/PacketMaker.cpp +++ b/signet/protocol/PacketMaker.cpp @@ -32,4 +32,5 @@ void Signet::Protocol::PacketMaker::challengePacket(QIODevice* device) Signet::Protocol::Packet Signet::Protocol::PacketMaker::unwrap(QIODevice* device) { + Packet packet; } |