Skip to content

Commit

Permalink
[coap] fix retransmission of confirmable CoAP requests (openthread#150)
Browse files Browse the repository at this point in the history
This commit has two fixes to the CoAP retransmission of confirmable
requests:

- Stop the retransmission timer when all requests are cleared.

- Start the retransmission timer when there are queueing requests that
  are waiting for responses.
  • Loading branch information
wgtdkp authored Nov 10, 2020
1 parent 0c8b0ff commit f6beb7b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
38 changes: 11 additions & 27 deletions src/library/coap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,8 @@ void Coap::Retransmit(Timer &)
FinalizeTransaction(requestHolder, nullptr, ERROR_TIMEOUT("request to {} timeout", uri));
}
}

mRequestsCache.UpdateTimer();
}

Error Coap::SendHeaderResponse(Code aCode, const Request &aRequest)
Expand Down Expand Up @@ -819,17 +821,7 @@ void Coap::RequestsCache::Put(const RequestPtr aRequest, ResponseHandler aHandle
void Coap::RequestsCache::Put(const RequestHolder &aRequestHolder)
{
mContainer.emplace(aRequestHolder);
if (mRetransmissionTimer.IsRunning())
{
if (Earliest() < mRetransmissionTimer.GetFireTime())
{
mRetransmissionTimer.Start(Earliest());
}
}
else
{
mRetransmissionTimer.Start(Earliest());
}
UpdateTimer();
}

Coap::RequestHolder Coap::RequestsCache::Eliminate()
Expand All @@ -838,15 +830,8 @@ Coap::RequestHolder Coap::RequestsCache::Eliminate()
auto ret = *mContainer.begin();

mContainer.erase(mContainer.begin());
UpdateTimer();

if (IsEmpty())
{
// No more requests pending, stop the timer.
mRetransmissionTimer.Stop();
}

// No need to worry that the earliest pending message was removed -
// the timer would just shoot earlier and then it'd be setup again.
return ret;
}

Expand All @@ -860,18 +845,17 @@ void Coap::RequestsCache::Eliminate(const RequestHolder &aRequestHolder)
break;
}
}
}

void Coap::RequestsCache::Clear()
{
mRetransmissionTimer.Stop();

mContainer.clear();
UpdateTimer();
}

void Coap::RequestsCache::TryRetartTimer()
void Coap::RequestsCache::UpdateTimer()
{
if (!mRetransmissionTimer.IsRunning() && !IsEmpty())
if (IsEmpty())
{
mRetransmissionTimer.Stop();
}
else if (!mRetransmissionTimer.IsRunning() || Earliest() < mRetransmissionTimer.GetFireTime())
{
mRetransmissionTimer.Start(Earliest());
}
Expand Down
10 changes: 4 additions & 6 deletions src/library/coap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,12 +689,10 @@ class Coap
return *mContainer.begin();
}

// Clear all requests and stop retransmission timer.
void Clear();

// Try restart the retransmit timer if it is not running and there is
// pending requests.
void TryRetartTimer();
// Try starting the retransmit timer if it is not running
// and there is pending requests. Try stopping the retransmit
// timer if there is no more pending requests.
void UpdateTimer();

private:
Timer mRetransmissionTimer;
Expand Down

0 comments on commit f6beb7b

Please sign in to comment.