TcpPortPacket.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__TCP__PORT__PACKET
00024 #define PROTOCOLS__KAD__PACKETS__TCP__PORT__PACKET
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 TcpPortPacket: public Packet
00042 {
00043 public:
00044     virtual QString         name() const;
00045     virtual TcpPortPacket*  copy() const;
00046 
00047                             TcpPortPacket();
00048 
00049 protected:
00050     virtual void            readPayload (BinaryReader &);
00051     virtual void            writePayload (BinaryWriter &) const;
00052 
00053     enum Constants
00054     {
00055         PayloadLength       = 2,
00056     };
00057 
00059 
00062     class Data: public Packet::Data
00063     {
00064         Q_SHARED_DATA_COPY (Data);
00065 
00066     public:
00067         quint16         tcpPort;
00068     };
00069 
00070     Q_DECLARE_SHARED_DATA (Data);
00071 
00072 private:
00074     static const QString    name_;
00075 };
00076 
00078 
00082 template <PacketType type>
00083 QString TcpPortPacket <type>::name() const
00084 {
00085     return name_;
00086 }
00087 
00089 template <PacketType type>
00090 TcpPortPacket<type>* TcpPortPacket <type>::copy() const
00091 {
00092     return new TcpPortPacket (*this);
00093 }
00094 
00096 
00101 template <PacketType type>
00102 TcpPortPacket <type>::TcpPortPacket()
00103     : Packet (new Data, type)
00104 {
00105 }
00106 
00108 
00111 template <PacketType type>
00112 void TcpPortPacket <type>::readPayload (BinaryReader &reader)
00113 {
00114     Q_SD (Data);
00115     d->tcpPort = reader.readUInt16();
00116 }
00117 
00119 
00122 template <PacketType type>
00123 void TcpPortPacket <type>::writePayload (BinaryWriter &writer) const
00124 {
00125     Q_SD (const Data);
00126     writer.reserve (PayloadLength);
00127     writer.writeUInt16(d->tcpPort);
00128 }
00129 
00130 } // namespace Packets
00131 } // namespace Kad
00132 } // namespace Protocols
00133 
00134 #endif