PacketBase.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 "PacketBase.h"
00025 #include "Imports.cpp"
00026 
00041 
00042 
00047 PacketBase::PacketBase (Data *dd)
00048     : d_ptr (dd)
00049 {
00050     Q_SD (Data);
00051     d->rewriteHeader    = true;
00052     d->rewritePayload   = true;
00053 }
00054 
00056 
00059 PacketBase::~PacketBase()
00060 {
00061 }
00062 
00064 
00069 void PacketBase::invalidateHeader()
00070 {
00071     Q_SD (Data);
00072     d->rewriteHeader = true;
00073 }
00074 
00076 
00083 void PacketBase::invalidatePayload()
00084 {
00085     Q_SD (Data);
00086     d->rewriteHeader    = true;
00087     d->rewritePayload   = true;
00088 }
00089 
00091 
00097 bool PacketBase::parse (const QByteArray &rawHeader,
00098                         const QByteArray &rawPayload)
00099 {
00100 
00101     auto_ptr <DataBase> tempCopy (copy());
00102     PacketBase &tempPacket = static_cast <PacketBase &> (*tempCopy);
00103     if (tempPacket.doParse (rawHeader, rawPayload)) {
00104         *this = tempPacket;
00105         return true;
00106     } else {
00107         return false;
00108     }
00109 }
00110 
00112 
00125 QByteArray PacketBase::rawHeader() const
00126 {
00127     Q_SD (const Data);
00128     if (d->rewriteHeader) {
00129         d->rawHeader        = writeHeader();
00130         d->rewriteHeader    = false;
00131     }
00132     return d->rawHeader;
00133 }
00134 
00136 
00149 QByteArray PacketBase::rawPayload() const
00150 {
00151     Q_SD (const Data);
00152     if (d->rewritePayload) {
00153         d->rawPayload       = writePayload();
00154         d->rewritePayload   = false;
00155     }
00156     return d->rawPayload;
00157 }
00158 
00160 
00180 bool PacketBase::doParse (const QByteArray &rawHeader,
00181                           const QByteArray &rawPayload)
00182 {
00183     Q_SD (Data);
00184     d->rawHeader        = rawHeader;
00185     d->rawPayload       = rawPayload;
00186     d->rewriteHeader    = false;
00187     d->rewritePayload   = false;
00188 
00189     return readHeader (d->rawHeader) &&
00190            readPayload (d->rawPayload);
00191 }