PeerInfoPacket.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__PEER_INFO_PACKET_H
00024 #define PROTOCOLS__KAD__PACKETS__PEER_INFO_PACKET_H
00025 
00026 #include "Imports.h"
00027 #include "Packet.h"
00028 
00029 namespace Protocols {
00030 namespace Kad {
00031 namespace Packets {
00032 
00034 
00040 template <PacketType type>
00041 class PeerInfoPacket: public Packet
00042 {
00043 public:
00044     virtual QString         name() const;
00045     virtual PeerInfoPacket* copy() const;
00046 
00047                             PeerInfoPacket();
00048 
00049 protected:
00050     virtual void            readPayload (BinaryReader &);
00051     virtual void            writePayload (BinaryWriter &) const;
00052 
00053     enum Constants
00054     {
00055         PayloadLength       = 25,
00056     };
00057 
00059 
00062     class Data: public Packet::Data
00063     {
00064         Q_SHARED_DATA_COPY (Data);
00065 
00066     public:
00067         UInt128         id; 
00068         quint32         ip; 
00069         quint16         udpPort; 
00070         quint16         tcpPort; 
00071         quint8          notUsed; 
00072     };
00073 
00074     Q_DECLARE_SHARED_DATA (Data);
00075 
00076 private:
00078     static const QString    name_;
00079 };
00080 
00082 
00086 template <PacketType type>
00087 QString PeerInfoPacket <type>::name() const
00088 {
00089     return name_;
00090 }
00091 
00093 template <PacketType type>
00094 PeerInfoPacket<type>* PeerInfoPacket <type>::copy() const
00095 {
00096     return new PeerInfoPacket (*this);
00097 }
00098 
00100 
00105 template <PacketType type>
00106 PeerInfoPacket <type>::PeerInfoPacket()
00107     : Packet (new Data, type)
00108 {
00109 }
00110 
00112 
00118 template <PacketType type>
00119 void PeerInfoPacket <type>::readPayload (BinaryReader &reader)
00120 {
00121     Q_SD (Data);
00122 
00123     // Should I read the words here or should BinaryReader has a readUInt128()?
00124     d->id.readFromBinaryReader (reader);
00125     d->ip = reader.readUInt32();
00126     d->udpPort = reader.readUInt16();
00127     d->tcpPort = reader.readUInt16();
00128     d->notUsed = static_cast <quint8> (reader.readByte());
00129 }
00130 
00132 template <PacketType type>
00133 void PeerInfoPacket <type>::writePayload (BinaryWriter &writer) const
00134 {
00135     Q_SD (const Data);
00136     writer.reserve (PayloadLength);
00137 
00138     d->id.writeToBinaryWriter (writer);
00139     writer.writeUInt32 (d->ip);
00140     writer.writeUInt16 (d->udpPort);
00141     writer.writeUInt16 (d->tcpPort);
00142     writer.writeByte (static_cast <uchar> (d->notUsed));
00143 }
00144 
00145 } // namespace Packets
00146 } // namespace Kad
00147 } // namespace Protocols
00148 
00149 #endif // PROTOCOLS__KAD__PACKETS__PEER_INFO_PACKET_H