PacketBase.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__BIT_TORRENT__PACKETS__PACKET_BASE_H
00024 #define PROTOCOLS__BIT_TORRENT__PACKETS__PACKET_BASE_H
00025 
00026 #include "Imports.h"
00027 
00028 namespace Protocols {
00029 namespace BitTorrent {
00030 namespace Packets {
00031 
00033 // \todo add PortPacket see unofficial BitTorrent specs.
00034 enum PacketType
00035 {
00036     KeepalivePacket         = 0xFF,
00037     ChokePacket             = 0x00,
00038     UnchokePacket           = 0x01,
00039     InterestedPacket        = 0x02,
00040     NotInterestedPacket     = 0x03,
00041     HavePacket              = 0x04,
00042     BitFieldPacket          = 0x05,
00043     RequestPacket           = 0x06,
00044     PiecePacket             = 0x07,
00045     CancelPacket            = 0x08,
00046     PortPacket              = 0x09,
00047                             //0x0A,
00048                             //0x0B
00049                             //0x0C
00050     SuggestPiecePacket      = 0x0D,
00051     HaveAllPacket           = 0x0E,
00052     HaveNonePacket          = 0x0F,
00053     RejectRequestPacket     = 0x10,
00054     AllowedFastPacket       = 0x11
00055 };
00056 
00058 
00074 class PacketBase : public GenericsPacketBase
00075 {
00076 public:
00077     virtual QString     protocol() const;
00078     virtual QString     name() const = 0;
00079     virtual auto_ptr <DataBase>
00080                         copy() const = 0;
00081 
00082     inline PacketType   packetType() const;
00083 
00084 protected:
00085     class Data;         // Needed for ctor declaration.
00086                         PacketBase (Data *d, PacketType packetType);
00087 
00088     virtual bool        readHeader (const QByteArray &rawHeader);
00089     virtual bool        readPayload (const QByteArray &rawPayload);
00090 
00091     virtual QByteArray  writeHeader () const;
00092     virtual QByteArray  writePayload () const;
00093 
00094     virtual void        readPayload (BinaryReader &) = 0;
00095     virtual void        writePayload (BinaryWriter &) const = 0;
00096 
00098 
00104     class Data: public GenericsPacketBase::Data
00105     {
00106     public:
00107         PacketType          packetType;
00108     };
00109 
00110 private:
00111     Q_DECLARE_SHARED_DATA (Data);
00112 };
00113 
00115 
00118 inline PacketType PacketBase::packetType() const
00119 { Q_SD (const Data); return d->packetType; }
00120 
00121 } // namespace Packets
00122 } // namespace BitTorrent
00123 } // namespace Protocols
00124 
00125 #endif // PROTOCOLS__BIT_TORRENT__PACKETS__PACKET_BASE_H