TCPSegment.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2004 Andras Varga
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00016 //
00017 
00018 
00019 #include "TCPSegment.h"
00020 
00021 Register_Class(TCPSegment);
00022 
00023 
00024 TCPSegment& TCPSegment::operator=(const TCPSegment& other)
00025 {
00026     TCPSegment_Base::operator=(other);
00027 
00028     for (std::list<TCPPayloadMessage>::const_iterator i=other.payloadList.begin(); i!=other.payloadList.end(); ++i)
00029         addPayloadMessage(i->msg->dup(), i->endSequenceNo);
00030 
00031     return *this;
00032 }
00033 
00034 TCPSegment::~TCPSegment()
00035 {
00036     while (!payloadList.empty())
00037     {
00038         cPacket *msg = payloadList.front().msg;
00039         payloadList.pop_front();
00040         dropAndDelete(msg);
00041     }
00042 }
00043 
00044 void TCPSegment::truncateSegment(uint32 firstSeqNo, uint32 endSeqNo)
00045 {
00046     if(seqLess(sequenceNo_var, firstSeqNo))
00047     {
00048         unsigned int truncleft = firstSeqNo - sequenceNo_var;
00049 
00050         while (!payloadList.empty())
00051         {
00052             if (seqGE(payloadList.front().endSequenceNo, firstSeqNo))
00053                 break;
00054 
00055             cPacket *msg = payloadList.front().msg;
00056             payloadList.pop_front();
00057             dropAndDelete(msg);
00058         }
00059 
00060         payloadLength_var -= truncleft;
00061         sequenceNo_var = firstSeqNo;
00062         //TODO truncate data correctly if need
00063     }
00064 
00065     if(seqGreater(sequenceNo_var+payloadLength_var, endSeqNo))
00066     {
00067         unsigned int truncright = sequenceNo_var + payloadLength_var - endSeqNo;
00068 
00069         while (!payloadList.empty())
00070         {
00071             if (seqLE(payloadList.back().endSequenceNo, endSeqNo))
00072                 break;
00073 
00074             cPacket *msg = payloadList.back().msg;
00075             payloadList.pop_back();
00076             dropAndDelete(msg);
00077         }
00078         payloadLength_var -= truncright;
00079         //TODO truncate data correctly if need
00080     }
00081 }
00082 
00083 void TCPSegment::parsimPack(cCommBuffer *b)
00084 {
00085     TCPSegment_Base::parsimPack(b);
00086     doPacking(b, payloadList);
00087 }
00088 
00089 void TCPSegment::parsimUnpack(cCommBuffer *b)
00090 {
00091     TCPSegment_Base::parsimUnpack(b);
00092     doUnpacking(b, payloadList);
00093 }
00094 
00095 void TCPSegment::setPayloadArraySize(unsigned int size)
00096 {
00097     throw cRuntimeError(this, "setPayloadArraySize() not supported, use addPayloadMessage()");
00098 }
00099 
00100 unsigned int TCPSegment::getPayloadArraySize() const
00101 {
00102     return payloadList.size();
00103 }
00104 
00105 TCPPayloadMessage& TCPSegment::getPayload(unsigned int k)
00106 {
00107     std::list<TCPPayloadMessage>::iterator i = payloadList.begin();
00108     while (k>0 && i!=payloadList.end())
00109         (++i, --k);
00110     return *i;
00111 }
00112 
00113 void TCPSegment::setPayload(unsigned int k, const TCPPayloadMessage& payload_var)
00114 {
00115     throw cRuntimeError(this, "setPayload() not supported, use addPayloadMessage()");
00116 }
00117 
00118 void TCPSegment::addPayloadMessage(cPacket *msg, uint32 endSequenceNo)
00119 {
00120     take(msg);
00121 
00122     TCPPayloadMessage payload;
00123     payload.endSequenceNo = endSequenceNo;
00124     payload.msg = msg;
00125     payloadList.push_back(payload);
00126 }
00127 
00128 cPacket *TCPSegment::removeFirstPayloadMessage(uint32& endSequenceNo)
00129 {
00130     if (payloadList.empty())
00131         return NULL;
00132 
00133     cPacket *msg = payloadList.front().msg;
00134     endSequenceNo = payloadList.front().endSequenceNo;
00135     payloadList.pop_front();
00136     drop(msg);
00137     return msg;
00138 }
00139