35 , m_linkService(linkService)
36 , m_firstUnackedFrag(m_unackedFrags.begin())
38 , m_isIdleAckTimerRunning(false)
40 BOOST_ASSERT(m_linkService !=
nullptr);
51 this->stopIdleAckTimer();
68 auto unackedFragsIt = m_unackedFrags.begin();
71 auto netPkt = make_shared<NetPkt>(std::move(pkt), isInterest);
72 netPkt->unackedFrags.reserve(frags.size());
79 unackedFragsIt = m_unackedFrags.emplace_hint(unackedFragsIt,
80 std::piecewise_construct,
81 std::forward_as_tuple(txSeq),
82 std::forward_as_tuple(frag));
83 unackedFragsIt->second.sendTime = sendTime;
84 unackedFragsIt->second.rtoTimer =
86 unackedFragsIt->second.netPkt = netPkt;
88 if (m_unackedFrags.size() == 1) {
89 m_firstUnackedFrag = m_unackedFrags.begin();
93 netPkt->unackedFrags.push_back(unackedFragsIt);
106 auto fragIt = m_unackedFrags.find(ackSeq);
107 if (fragIt == m_unackedFrags.end()) {
111 auto& frag = fragIt->second;
114 frag.rtoTimer.cancel();
116 if (frag.retxCount == 0) {
118 m_rto.
addMeasurement(time::duration_cast<RttEstimator::Duration>(now - frag.sendTime));
124 auto lostLpPackets = findLostLpPackets(fragIt);
128 onLpPacketAcknowledged(fragIt);
134 std::set<lp::Sequence> removedLpPackets;
138 if (removedLpPackets.find(txSeq) == removedLpPackets.end()) {
139 auto removedThisTxSeq = this->onLpPacketLost(txSeq);
140 for (
auto removedTxSeq : removedThisTxSeq) {
141 removedLpPackets.insert(removedTxSeq);
150 if (!m_isIdleAckTimerRunning) {
151 this->startIdleAckTimer();
167 remainingSpace -= pktSize;
169 while (!m_ackQueue.empty()) {
176 if (ackSize > remainingSpace) {
182 remainingSpace -= ackSize;
187 LpReliability::assignTxSequence(
lp::Packet& frag)
191 if (m_unackedFrags.size() > 0 && m_lastTxSeqNo == m_firstUnackedFrag->first) {
192 BOOST_THROW_EXCEPTION(std::length_error(
"TxSequence range exceeded"));
194 return m_lastTxSeqNo;
198 LpReliability::startIdleAckTimer()
200 BOOST_ASSERT(!m_isIdleAckTimerRunning);
201 m_isIdleAckTimerRunning =
true;
204 while (!m_ackQueue.empty()) {
205 m_linkService->requestIdlePacket();
208 m_isIdleAckTimerRunning =
false;
213 LpReliability::stopIdleAckTimer()
215 m_idleAckTimer.cancel();
216 m_isIdleAckTimerRunning =
false;
219 std::vector<lp::Sequence>
222 std::vector<lp::Sequence> lostLpPackets;
224 for (
auto it = m_firstUnackedFrag; ; ++it) {
225 if (it == m_unackedFrags.end()) {
226 it = m_unackedFrags.begin();
229 if (it->first == ackIt->first) {
233 auto& unackedFrag = it->second;
234 unackedFrag.nGreaterSeqAcks++;
236 if (unackedFrag.nGreaterSeqAcks >= m_options.seqNumLossThreshold) {
237 lostLpPackets.push_back(it->first);
241 return lostLpPackets;
244 std::vector<lp::Sequence>
247 BOOST_ASSERT(m_unackedFrags.count(txSeq) > 0);
248 auto txSeqIt = m_unackedFrags.find(txSeq);
250 auto& txFrag = txSeqIt->second;
251 txFrag.rtoTimer.cancel();
252 auto netPkt = txFrag.netPkt;
253 std::vector<lp::Sequence> removedThisTxSeq;
256 if (txFrag.retxCount >= m_options.maxRetx) {
258 for (
size_t i = 0; i < netPkt->unackedFrags.size(); i++) {
259 if (netPkt->unackedFrags[i] != txSeqIt) {
260 removedThisTxSeq.push_back(netPkt->unackedFrags[i]->first);
261 deleteUnackedFrag(netPkt->unackedFrags[i]);
265 ++m_linkService->nRetxExhausted;
268 if (netPkt->isInterest) {
270 ndn::Buffer::const_iterator fragBegin, fragEnd;
272 Block frag(&*fragBegin, std::distance(fragBegin, fragEnd));
276 removedThisTxSeq.push_back(txSeqIt->first);
277 deleteUnackedFrag(txSeqIt);
282 netPkt->didRetx =
true;
285 auto newTxFragIt = m_unackedFrags.emplace_hint(
286 m_firstUnackedFrag != m_unackedFrags.end() && m_firstUnackedFrag->first > newTxSeq
288 : m_unackedFrags.end(),
289 std::piecewise_construct,
290 std::forward_as_tuple(newTxSeq),
291 std::forward_as_tuple(txFrag.pkt));
292 auto& newTxFrag = newTxFragIt->second;
293 newTxFrag.retxCount = txFrag.retxCount + 1;
294 newTxFrag.netPkt = netPkt;
297 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), txSeqIt);
298 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
299 *fragInNetPkt = newTxFragIt;
301 removedThisTxSeq.push_back(txSeqIt->first);
302 deleteUnackedFrag(txSeqIt);
305 m_linkService->sendLpPacket(
lp::Packet(newTxFrag.pkt));
309 bind(&LpReliability::onLpPacketLost,
this, newTxSeq));
312 return removedThisTxSeq;
318 auto netPkt = fragIt->second.netPkt;
321 auto fragInNetPkt = std::find(netPkt->unackedFrags.begin(), netPkt->unackedFrags.end(), fragIt);
322 BOOST_ASSERT(fragInNetPkt != netPkt->unackedFrags.end());
323 *fragInNetPkt = netPkt->unackedFrags.back();
324 netPkt->unackedFrags.pop_back();
327 if (netPkt->unackedFrags.empty()) {
328 if (netPkt->didRetx) {
329 ++m_linkService->nRetransmitted;
332 ++m_linkService->nAcknowledged;
336 deleteUnackedFrag(fragIt);
342 lp::Sequence firstUnackedTxSeq = m_firstUnackedFrag->first;
344 auto nextFragIt = m_unackedFrags.erase(fragIt);
346 if (!m_unackedFrags.empty() && firstUnackedTxSeq == currentTxSeq) {
348 if (nextFragIt == m_unackedFrags.end()) {
349 m_firstUnackedFrag = m_unackedFrags.begin();
352 m_firstUnackedFrag = nextFragIt;
355 else if (m_unackedFrags.empty()) {
356 m_firstUnackedFrag = m_unackedFrags.end();
360 LpReliability::UnackedFrag::UnackedFrag(
lp::Packet pkt)
361 : pkt(
std::move(pkt))
362 , sendTime(
time::steady_clock::now())
368 LpReliability::NetPkt::NetPkt(
lp::Packet&& pkt,
bool isInterest)
369 : pkt(
std::move(pkt))
370 , isInterest(isInterest)
Duration computeRto() const
std::vector< typename FIELD::ValueType > list() const
void setOptions(const Options &options)
set options for reliability
GenericLinkService is a LinkService that implements the NDNLPv2 protocol.
void processIncomingPacket(const lp::Packet &pkt)
extract and parse all Acks and add Ack for contained Fragment (if any) to AckQueue ...
Packet & set(const typename FIELD::ValueType &value)
remove all occurrences of FIELD, and add a FIELD with value
const ssize_t MTU_UNLIMITED
indicates the transport has no limit on payload size
static time_point now() noexcept
void piggyback(lp::Packet &pkt, ssize_t mtu)
called by GenericLinkService to attach Acks onto an outgoing LpPacket
Packet & add(const typename FIELD::ValueType &value)
add a FIELD with value
void handleOutgoing(std::vector< lp::Packet > &frags, lp::Packet &&pkt, bool isInterest)
observe outgoing fragment(s) of a network packet and store for potential retransmission ...
bool isEnabled
enables link-layer reliability
uint64_t Sequence
represents a sequence number
Table::const_iterator iterator
void addMeasurement(Duration measure)
size_t size() const
Get size of encoded wire, including Type-Length-Value.
FIELD::ValueType get(size_t index=0) const
Copyright (c) 2011-2015 Regents of the University of California.
const GenericLinkService * getLinkService() const
LpReliability(const Options &options, GenericLinkService *linkService)
EventId schedule(time::nanoseconds after, const EventCallback &event)
Schedule an event.
Block wireEncode() const
encode packet into wire format
time::nanoseconds idleAckTimerPeriod
period between sending pending Acks in an IDLE packet
constexpr size_t sizeOfVarNumber(uint64_t number)
Get number of bytes necessary to hold value of VAR-NUMBER.
uint32_t type() const
Get TLV-TYPE.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size