Packet.h

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 #ifndef PROTOCOLS__KAD__PACKETS__PACKET
00024 #define PROTOCOLS__KAD__PACKETS__PACKET
00025 
00026 #include "Imports.h"
00027 
00028 namespace Protocols {
00029 namespace Kad {
00030 namespace Packets {
00031 
00033 enum Protocol {
00034     KadProtocol         = 0xE4, 
00035     KadPackedProtocol   = 0xE5  
00036 };
00037 
00039 enum PacketType
00040 {
00041     BootstrapReqPacket      = 0x00,
00042     BootstrapResPacket      = 0x08,
00043     HelloReqPacket          = 0x10,
00044     HelloResPacket          = 0x18,
00045     ReqPacket               = 0x20,
00046     ResPacket               = 0x28,
00047     SearchReqPacket         = 0x30,
00048     SearchNotesReqPacket    = 0x32,
00049     SearchResPacket         = 0x38,
00050     SearchNotesResPacket    = 0x3A,
00051     PublishReqPacket        = 0x40,
00052     PublishNotesReqPacket   = 0x42,
00053     PublishResPacket        = 0x48,
00054     PublishNotesResPacket   = 0x4A,
00055     FirewalledReqPacket     = 0x50,
00056     FindBuddyReqPacket      = 0x51,
00057     CallbackReqPacket       = 0x52,
00058     FirewalledResPacket     = 0x58,
00059     FirewalledAckPacket     = 0x59,
00060     FindBuddyResPacket      = 0x5A
00061 };
00062 
00063 
00065 
00079 class Packet : public PacketBase
00080 {
00081 public:
00082     virtual QString     protocol() const;
00083     virtual QString     name() const = 0;
00084     virtual Packet*     copy() const = 0;
00085 
00086     inline Protocol     protocolId() const;
00087     inline PacketType   packetType() const;
00088 
00089 protected:
00090     class Data;         // Needed for ctor declaration.
00091                         Packet (Data *d, PacketType packetType);
00092 
00093     virtual bool        readHeader (const QByteArray &rawHeader);
00094     virtual bool        readPayload (const QByteArray &rawPayload);
00095     virtual bool        readTrailer (const QByteArray &rawTrailer);
00096 
00097     virtual QByteArray  writeHeader () const;
00098     virtual QByteArray  writePayload () const;
00099     virtual QByteArray  writeTrailer () const;
00100 
00101     virtual void        readPayload (BinaryReader &) = 0;
00102     virtual void        writePayload (BinaryWriter &) const = 0;
00103 
00104 
00106 
00112     class Data: public PacketBase::Data
00113     {
00114     public:
00115         mutable Protocol    protocolId;
00116         PacketType          packetType;
00117     };
00118 
00119 private:
00120     Q_DECLARE_SHARED_DATA (Data);
00121 };
00122 
00124 inline PacketType Packet::packetType() const
00125 { Q_SD (const Data); return d->packetType; }
00126 
00128 inline Protocol Packet::protocolId() const
00129 { Q_SD (const Data); return d->protocolId; }
00130 
00131 } // namespace Packets
00132 } // namespace Kad
00133 } // namespace Protocols
00134 
00135 #endif // PROTOCOLS__KAD__PACKETS__PACKET