handle stream close depending on state

This commit is contained in:
orignal 2015-03-09 12:06:35 -04:00
parent dc599bbc63
commit cd0933522d
2 changed files with 36 additions and 18 deletions

View File

@ -34,6 +34,12 @@ namespace stream
}
Stream::~Stream ()
{
Terminate ();
LogPrint (eLogDebug, "Stream deleted");
}
void Stream::Terminate ()
{
m_AckSendTimer.cancel ();
while (!m_ReceiveQueue.empty ())
@ -52,7 +58,6 @@ namespace stream
for (auto it: m_SavedPackets)
delete it;
m_SavedPackets.clear ();
LogPrint (eLogDebug, "Stream deleted");
}
void Stream::HandleNextPacket (Packet * packet)
@ -438,23 +443,34 @@ namespace stream
void Stream::Close ()
{
if (m_Status == eStreamStatusOpen)
switch (m_Status)
{
case eStreamStatusOpen:
m_Status = eStreamStatusClosing;
if (m_SendBuffer.eof ()) // nothing to send
SendClose ();
}
if (m_Status == eStreamStatusReset || m_SentPackets.empty ())
Close (); // recursion
if (m_Status == eStreamStatusClosing) //still closing
LogPrint (eLogInfo, "Trying to send stream data before closing");
break;
case eStreamStatusReset:
Terminate ();
m_LocalDestination.DeleteStream (shared_from_this ());
break;
case eStreamStatusClosing:
if (m_SentPackets.empty () && m_SendBuffer.eof ()) // nothing to send
{
// closed by peer or everything has been acknowledged
if (m_Status == eStreamStatusClosing)
SendClose ();
m_ReceiveTimer.cancel ();
Terminate ();
m_LocalDestination.DeleteStream (shared_from_this ());
}
else
LogPrint (eLogInfo, "Trying to send stream data before closing");
break;
case eStreamStatusClosed:
// already closed
Terminate ();
m_LocalDestination.DeleteStream (shared_from_this ());
break;
default:
LogPrint (eLogWarning, "Unexpected stream status ", (int)m_Status);
};
}
void Stream::SendClose ()

View File

@ -131,6 +131,8 @@ namespace stream
private:
void Terminate ();
void SendBuffer ();
void SendQuickAck ();
void SendClose ();