00001 /* 00002 00003 Copyright (C) 2005-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 "PacketBase.h" 00025 #include "Imports.cpp" 00026 00028 QString PacketBase::protocol() const 00029 { 00030 return "Gnutella"; 00031 } 00032 00034 00043 PacketBase::PacketBase(Data *dd, PacketType packetType) 00044 : GenericsPacketBase (dd) 00045 { 00046 Q_SD (Data); 00047 d->descriptorId = QUuid::createUuid(); 00048 d->hops = DefaultHops; 00049 d->ttl = DefaultTtl; 00050 d->packetType = packetType; 00051 } 00052 00054 00065 bool PacketBase::readHeader (const QByteArray &rawHeader) 00066 { 00067 Q_SD (Data); 00068 BinaryReader reader (rawHeader); 00069 d->descriptorId = reader.readUuid(); 00070 PacketType packetType = static_cast <PacketType> (reader.readByte()); 00071 // The read packet type must always match the runtime object type: 00072 Q_ASSERT (packetType == d->packetType); 00073 d->ttl = reader.readByte(); 00074 d->hops = reader.readByte(); 00075 quint32 payloadLength = reader.readUInt32(); 00076 Q_ASSERT (payloadLength == this->payloadLength()); 00077 return reader.hasReadAll() && !reader.hasReadPastEnd(); 00078 } 00079 00081 QByteArray PacketBase::writeHeader() const 00082 { 00083 Q_SD (const Data); 00084 QByteArray rawHeader (HeaderLength, 0); 00085 BinaryWriter writer (&rawHeader); 00086 writer.writeUuid (d->descriptorId); 00087 writer.writeByte (static_cast <uchar> (d->packetType)); 00088 writer.writeByte (d->ttl); 00089 writer.writeByte (d->hops); 00090 writer.writeUInt32 (payloadLength()); 00091 Q_ASSERT (HeaderLength == writer.bytesWritten()); 00092 return rawHeader; 00093 } 00094 00096 00110 bool PacketBase::readPayload (const QByteArray &rawPayload) 00111 { 00112 BinaryReader reader (rawPayload); 00113 readPayload (reader); 00114 return reader.hasReadAll() && !reader.hasReadPastEnd(); 00115 } 00116 00128 00129 00137 QByteArray PacketBase::writePayload() const 00138 { 00139 BinaryWriter writer; 00140 writePayload (writer); 00141 Q_ASSERT (writer.hasWrittenAll() && !writer.hasWrittenPastEnd()); 00142 return writer.buffer(); 00143 } 00144