Browse Source

use unique_ptr for incomplete message

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

15
SSUData.cpp

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

6
SSUData.h

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

Loading…
Cancel
Save