Browse Source

use unique_ptr for incomplete message

pull/157/head
orignal 10 years ago
parent
commit
408a67f34f
  1. 13
      SSUData.cpp
  2. 6
      SSUData.h

13
SSUData.cpp

@ -23,11 +23,7 @@ namespace transport @@ -23,11 +23,7 @@ namespace transport
SSUData::~SSUData ()
{
for (auto it: m_IncomleteMessages)
if (it.second)
{
DeleteI2NPMessage (it.second->msg);
delete it.second;
}
for (auto it: m_SentMessages)
delete it.second;
}
@ -182,7 +178,7 @@ namespace transport @@ -182,7 +178,7 @@ namespace transport
// try saved fragments
for (auto it1 = incompleteMessage->savedFragments.begin (); it1 != incompleteMessage->savedFragments.end ();)
{
auto savedFragment = *it1;
auto& savedFragment = *it1;
if (savedFragment->fragmentNum == incompleteMessage->nextFragmentNum)
{
memcpy (msg->buf + msg->len, savedFragment->buf, savedFragment->len);
@ -190,7 +186,6 @@ namespace transport @@ -190,7 +186,6 @@ namespace transport
isLast = savedFragment->isLast;
incompleteMessage->nextFragmentNum++;
incompleteMessage->savedFragments.erase (it1++);
delete savedFragment;
}
else
break;
@ -209,11 +204,8 @@ namespace transport @@ -209,11 +204,8 @@ namespace transport
// missing fragment
LogPrint (eLogWarning, "Missing fragments from ", (int)incompleteMessage->nextFragmentNum, " to ", fragmentNum - 1, " of message ", msgID);
auto savedFragment = new Fragment (fragmentNum, buf, fragmentSize, isLast);
if (!incompleteMessage->savedFragments.insert (savedFragment).second)
{
if (!incompleteMessage->savedFragments.insert (std::unique_ptr<Fragment>(savedFragment)).second)
LogPrint (eLogWarning, "Fragment ", (int)fragmentNum, " of message ", msgID, " already saved");
delete savedFragment;
}
}
isLast = false;
}
@ -221,6 +213,7 @@ namespace transport @@ -221,6 +213,7 @@ namespace transport
if (isLast)
{
// delete incomplete message
incompleteMessage->msg = nullptr;
delete incompleteMessage;
m_IncomleteMessages.erase (msgID);
// process message

6
SSUData.h

@ -48,7 +48,7 @@ namespace transport @@ -48,7 +48,7 @@ namespace transport
struct FragmentCmp
{
bool operator() (const Fragment * f1, const Fragment * f2) const
bool operator() (const std::unique_ptr<Fragment>& f1, const std::unique_ptr<Fragment>& f2) const
{
return f1->fragmentNum < f2->fragmentNum;
};
@ -58,10 +58,10 @@ namespace transport @@ -58,10 +58,10 @@ namespace transport
{
I2NPMessage * msg;
int nextFragmentNum;
std::set<Fragment *, FragmentCmp> savedFragments;
std::set<std::unique_ptr<Fragment>, FragmentCmp> savedFragments;
IncompleteMessage (I2NPMessage * m): msg (m), nextFragmentNum (0) {};
~IncompleteMessage () { for (auto it: savedFragments) { delete it; }; };
~IncompleteMessage () { if (msg) DeleteI2NPMessage (msg); };
};
struct SentMessage

Loading…
Cancel
Save