IpPort.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Qt.h"
00024 #include "IpPort.h"
00025 #include "Gnutella/Packets/Extensions/GgepBlock.h"
00026 #include "Gnutella/Packets/Packet.h"
00027
00028 using Gnutella::Packets::Extensions::Ggep;
00029 using Gnutella::Packets::Extensions::GgepBlock;
00030 using Gnutella::Packets::Extensions::Ggeps::IpPort;
00031 using Protocols::Transports::NodeAddress;
00032
00033 const Ggep::GgepId IpPort::Id ("IPP", 3);
00034
00035 IpPort::IpPort (const GgepId &id, int flags, int dataSize)
00036 : Ggep (id, flags, dataSize)
00037 {
00038 }
00039
00040 IpPort::IpPort (const QList <NodeAddress> &addresses)
00041 : Ggep (Id)
00042 {
00043 p.addresses = addresses;
00044 }
00045
00046 const IpPort * IpPort::findIn (const GgepBlock &ggepBlock)
00047 {
00048 const IpPort *res = 0;
00049 foreach (const Ggep *extension, ggepBlock.extensions()) {
00050 res = dynamic_cast <const IpPort *> (extension);
00051 if (res != 0)
00052 return res;
00053 }
00054 return 0;
00055 }
00056
00057 IpPort * IpPort::copy() const
00058 {
00059 return new IpPort (*this);
00060 }
00061
00062 bool IpPort::prepareReadData (const QByteArray &rawBytes)
00063 {
00064 return rawBytes.length() % 6 == 0;
00065 }
00066
00067 void IpPort::readData (QDataStream &stream)
00068 {
00069 using namespace Gnutella::Packets;
00070
00071 int count = dataLength() / 6;
00072 for (int i = 0; i < count; i++) {
00073 QHostAddress ip;
00074 quint16 port;
00075 Gnutella::Packets::operator>> (stream, ip);
00076 stream >> port;
00077 p.addresses.append (NodeAddress (ip, port));
00078 }
00079 }
00080
00081 int IpPort::prepareWriteData() const
00082 {
00083 return p.addresses.size() * 6;
00084 }
00085
00086 void IpPort::writeData (QDataStream &stream) const
00087 {
00088 using namespace Gnutella::Packets;
00089
00090 foreach (NodeAddress address, p.addresses) {
00091 Gnutella::Packets::operator<< (stream, address.hostAddress());
00092 stream << address.hostPort();
00093 }
00094 }