Bye.cpp

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 #include "Qt.h"
00024 #include "Bye.h"
00025 
00026 namespace Gnutella {
00027 namespace Packets {
00028 
00029 enum Constants
00030 {
00031     MinimalByePayloadLength     = 3     
00032 };
00033 
00034 }
00035 }
00036 
00037 using namespace Gnutella::Packets;
00038 
00039 Bye::Bye (const QByteArray &rawHeader, const QByteArray &rawPayload)
00040     : Packet (rawHeader, rawPayload)
00041 {
00042     parse();
00043 }
00044 
00045 Bye::Bye (quint16 code, const QByteArray &message)
00046     : Packet (ByeDescriptor)
00047 {
00048     d.code      = code;
00049     d.message   = message;
00050 }
00051 
00052 Bye & Bye::operator= (const Bye &other)
00053 {
00054     d.code      = other.d.code;
00055     d.message   = other.d.message;
00056 
00057     Packet::operator= (other);
00058     return *this;
00059 }
00060 
00061 Bye::~Bye()
00062 {
00063 }
00064 
00071 void Bye::invalidatePayload()
00072 {
00073     Packet::invalidatePayload();
00074 }
00075 
00076 bool Bye::prepareReadPayload (const QByteArray &rawPayload)
00077 {
00078     if (rawPayload.length() < MinimalByePayloadLength)
00079         return false;
00080     d.message.resize (rawPayload.length() - 2);
00081     return true;
00082 }
00083 
00084 void Bye::readPayload (QDataStream &stream)
00085 {
00086     stream >> d.code;
00087     stream.readRawData (d.message.data(), d.message.size());
00088 }
00089 
00090 int Bye::prepareWritePayload() const
00091 {
00092     return 2 + d.message.length() + 1; // 2b code, string + \0
00093 }
00094 
00095 void Bye::writePayload (QDataStream &stream) const
00096 {
00097     stream << d.code;
00098     stream.writeRawData (d.message.data(), d.message.size());
00099     stream << static_cast <uchar> (0);
00100 }