Packet.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "Packet.h"
00025 #include "Imports.cpp"
00026 
00028 QString Packet::protocol() const
00029 {
00030     return "Kad";
00031 }
00032 
00034 
00042 Packet::Packet (Data *dd, PacketType packetType)
00043     : PacketBase (dd)
00044 {
00045     Q_SD (Data);
00046 
00047     // Initially the packet is not compressed
00048     d->protocolId = KadProtocol;
00049     d->packetType = packetType;
00050 }
00051 
00053 bool Packet::readHeader (const QByteArray &rawHeader)
00054 {
00055     Q_SD (Data);
00056     BinaryReader reader (rawHeader, BinaryReader::BigEndian);
00057 
00058     // Read protocol id (must be KadMuleProtocol)
00059     // \todo If the session/transport would ensure only KadMuleProtocol packet
00060     // are parsed here, then Q_ASSERT (d->protocolId == KadMuleProtocol);
00061     d->protocolId = static_cast <Protocol> (reader.readByte());
00062     // Read packet type
00063     d->packetType = static_cast <PacketType> (reader.readByte());
00064 
00065     Q_ASSERT(KadProtocol == d->protocolId || KadPackedProtocol == d->protocolId);
00066 
00067     return reader.hasReadAll() && !reader.hasReadPastEnd();
00068 }
00069 
00071 
00074 bool Packet::readTrailer (const QByteArray &rawTrailer)
00075 {
00076     Q_ASSERT (rawTrailer.length() == 0);
00077     return true;
00078 }
00079 
00081 QByteArray Packet::writeHeader() const
00082 {
00083     Q_SD (const Data);
00084     QByteArray rawHeader (2, 0);
00085     BinaryWriter writer (&rawHeader, BinaryWriter::BigEndian);
00086 
00087     writer.writeByte(static_cast <uchar> (d->protocolId));
00088     writer.writeByte(static_cast <uchar> (d->packetType));
00089 
00090     Q_ASSERT (2 == writer.bytesWritten());
00091     return rawHeader;
00092 }
00093 
00095 
00099 QByteArray Packet::writeTrailer() const
00100 {
00101     return QByteArray();
00102 }
00103 
00105 
00110 bool Packet::readPayload (const QByteArray &rawPayload)
00111 {
00112     BinaryReader reader (rawPayload, BinaryReader::BigEndian);
00113     readPayload (reader);
00114     return reader.hasReadAll() && !reader.hasReadPastEnd();
00115 }
00116 
00123 
00124 
00129 QByteArray Packet::writePayload() const
00130 {
00131     BinaryWriter writer (BinaryWriter::BigEndian);
00132     writePayload (writer);
00133     Q_ASSERT (writer.hasWrittenAll() && !writer.hasWrittenPastEnd());
00134     return writer.buffer();
00135 }
00136