QueryRoutingPatch.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-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 #include "Qt.h"
00024 #include "QueryRoutingPatch.h"
00025 
00026 using namespace Gnutella::Packets;
00027 
00028 enum Constants
00029 {
00030     MinimalPayloadLength = 5
00031 };
00032 
00034 
00036 QDataStream & operator>> (QDataStream &in, QueryRoutingPatch::Compressor &compressor)
00037 {
00038     uchar tmp;
00039     in >> tmp;
00040     compressor = static_cast <QueryRoutingPatch::Compressor> (tmp);
00041     return in;
00042 }
00043 
00045 
00047 QDataStream & operator<< (QDataStream &out, const QueryRoutingPatch::Compressor &compressor)
00048 {
00049     return out << static_cast <uchar> (compressor);
00050 }
00051 
00055 QueryRoutingPatch::QueryRoutingPatch (const QByteArray &rawHeader, const QByteArray &rawPayload)
00056     : QueryRouting (rawHeader, rawPayload)
00057 {
00058     parse();
00059 }
00060 
00061 QueryRoutingPatch::QueryRoutingPatch (quint8 seqNo, quint8 seqSize, Compressor compressor, quint8 entryBits, const QByteArray &data)
00062     : QueryRouting (QueryRouting::PatchVariant)
00063 {
00064     p.seqNo = seqNo;
00065     p.seqSize = seqSize;
00066     p.compressor = compressor;
00067     p.entryBits = entryBits;
00068     p.data = data;
00069 }
00070 
00071 QueryRoutingPatch::~QueryRoutingPatch()
00072 {
00073 }
00074 
00075 bool QueryRoutingPatch::prepareReadPayload (const QByteArray &rawPayload)
00076 {
00077     return rawPayload.length() > MinimalPayloadLength;
00078 }
00079 
00080 void QueryRoutingPatch::readPayload (QDataStream &stream)
00081 {
00082     QueryRouting::readPayload (stream); // Read the variant field.
00083     stream >> p.seqNo;
00084     stream >> p.seqSize;
00085     stream >> p.compressor;
00086     stream >> p.entryBits;
00087     p.data.resize (payloadLength() - MinimalPayloadLength);
00088     stream.readRawData (p.data.data(), p.data.length());
00089 }
00090 
00091 int QueryRoutingPatch::prepareWritePayload() const
00092 {
00093     return MinimalPayloadLength + p.data.length();
00094 }
00095 
00096 void QueryRoutingPatch::writePayload (QDataStream &stream) const
00097 {
00098     QueryRouting::writePayload (stream); // Write the variant field.
00099     stream << p.seqNo;
00100     stream << p.seqSize;
00101     stream << p.compressor;
00102     stream << p.entryBits;
00103     stream.writeRawData (p.data.data(), p.data.length());
00104 }