list.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2018 Haivision Systems Inc.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. */
  10. /*****************************************************************************
  11. Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois.
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions are
  15. met:
  16. * Redistributions of source code must retain the above
  17. copyright notice, this list of conditions and the
  18. following disclaimer.
  19. * Redistributions in binary form must reproduce the
  20. above copyright notice, this list of conditions
  21. and the following disclaimer in the documentation
  22. and/or other materials provided with the distribution.
  23. * Neither the name of the University of Illinois
  24. nor the names of its contributors may be used to
  25. endorse or promote products derived from this
  26. software without specific prior written permission.
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *****************************************************************************/
  39. /*****************************************************************************
  40. written by
  41. Yunhong Gu, last updated 01/22/2011
  42. modified by
  43. Haivision Systems Inc.
  44. *****************************************************************************/
  45. #ifndef INC_SRT_LIST_H
  46. #define INC_SRT_LIST_H
  47. #include <deque>
  48. #include "udt.h"
  49. #include "common.h"
  50. namespace srt {
  51. class CSndLossList
  52. {
  53. public:
  54. CSndLossList(int size = 1024);
  55. ~CSndLossList();
  56. /// Insert a seq. no. into the sender loss list.
  57. /// @param [in] seqno1 sequence number starts.
  58. /// @param [in] seqno2 sequence number ends.
  59. /// @return number of packets that are not in the list previously.
  60. int insert(int32_t seqno1, int32_t seqno2);
  61. /// Remove the given sequence number and all numbers that precede it.
  62. /// @param [in] seqno sequence number.
  63. void removeUpTo(int32_t seqno);
  64. /// Read the loss length.∏
  65. /// @return The length of the list.
  66. int getLossLength() const;
  67. /// Read the first (smallest) loss seq. no. in the list and remove it.
  68. /// @return The seq. no. or -1 if the list is empty.
  69. int32_t popLostSeq();
  70. void traceState() const;
  71. // Debug/unittest support.
  72. int head() const { return m_iHead; }
  73. int next(int loc) const { return m_caSeq[loc].inext; }
  74. int last() const { return m_iLastInsertPos; }
  75. private:
  76. struct Seq
  77. {
  78. int32_t seqstart; // sequence number starts
  79. int32_t seqend; // sequence number ends
  80. int inext; // index of the next node in the list
  81. } * m_caSeq;
  82. int m_iHead; // first node
  83. int m_iLength; // loss length
  84. const int m_iSize; // size of the static array
  85. int m_iLastInsertPos; // position of last insert node
  86. mutable srt::sync::Mutex m_ListLock; // used to synchronize list operation
  87. private:
  88. /// Inserts an element to the beginning and updates head pointer.
  89. /// No lock.
  90. void insertHead(int pos, int32_t seqno1, int32_t seqno2);
  91. /// Inserts an element after previous element.
  92. /// No lock.
  93. void insertAfter(int pos, int pos_after, int32_t seqno1, int32_t seqno2);
  94. /// Check if it is possible to coalesce element at loc with further elements.
  95. /// @param loc - last changed location
  96. void coalesce(int loc);
  97. /// Update existing element with the new range (increase only)
  98. /// @param pos position of the element being updated
  99. /// @param seqno1 first sequence number in range
  100. /// @param seqno2 last sequence number in range (SRT_SEQNO_NONE if no range)
  101. bool updateElement(int pos, int32_t seqno1, int32_t seqno2);
  102. static const int LOC_NONE = -1;
  103. private:
  104. CSndLossList(const CSndLossList&);
  105. CSndLossList& operator=(const CSndLossList&);
  106. };
  107. ////////////////////////////////////////////////////////////////////////////////
  108. class CRcvLossList
  109. {
  110. public:
  111. CRcvLossList(int size = 1024);
  112. ~CRcvLossList();
  113. /// Insert a series of loss seq. no. between "seqno1" and "seqno2" into the receiver's loss list.
  114. /// @param [in] seqno1 sequence number starts.
  115. /// @param [in] seqno2 seqeunce number ends.
  116. /// @return length of the loss record inserted (seqlen(seqno1, seqno2)), -1 on error.
  117. int insert(int32_t seqno1, int32_t seqno2);
  118. /// Remove a loss seq. no. from the receiver's loss list.
  119. /// @param [in] seqno sequence number.
  120. /// @return if the packet is removed (true) or no such lost packet is found (false).
  121. bool remove(int32_t seqno);
  122. /// Remove all packets between seqno1 and seqno2.
  123. /// @param [in] seqno1 start sequence number.
  124. /// @param [in] seqno2 end sequence number.
  125. /// @return if the packet is removed (true) or no such lost packet is found (false).
  126. bool remove(int32_t seqno1, int32_t seqno2);
  127. /// Remove all numbers that precede the given sequence number.
  128. /// @param [in] seqno sequence number.
  129. /// @return the first removed sequence number
  130. int32_t removeUpTo(int32_t seqno);
  131. /// Find if there is any lost packets whose sequence number falling seqno1 and seqno2.
  132. /// @param [in] seqno1 start sequence number.
  133. /// @param [in] seqno2 end sequence number.
  134. /// @return True if found; otherwise false.
  135. bool find(int32_t seqno1, int32_t seqno2) const;
  136. /// Read the loss length.
  137. /// @return the length of the list.
  138. int getLossLength() const;
  139. /// Read the first (smallest) seq. no. in the list.
  140. /// @return the sequence number or -1 if the list is empty.
  141. int32_t getFirstLostSeq() const;
  142. /// Get a encoded loss array for NAK report.
  143. /// @param [out] array the result list of seq. no. to be included in NAK.
  144. /// @param [out] len physical length of the result array.
  145. /// @param [in] limit maximum length of the array.
  146. void getLossArray(int32_t* array, int& len, int limit);
  147. private:
  148. struct Seq
  149. {
  150. int32_t seqstart; // sequence number starts
  151. int32_t seqend; // sequence number ends
  152. int inext; // index of the next node in the list
  153. int iprior; // index of the previous node in the list
  154. } * m_caSeq;
  155. int m_iHead; // first node in the list
  156. int m_iTail; // last node in the list;
  157. int m_iLength; // loss length
  158. int m_iSize; // size of the static array
  159. int m_iLargestSeq; // largest seq ever seen
  160. private:
  161. CRcvLossList(const CRcvLossList&);
  162. CRcvLossList& operator=(const CRcvLossList&);
  163. public:
  164. struct iterator
  165. {
  166. int32_t head;
  167. Seq* seq;
  168. iterator(Seq* str, int32_t v)
  169. : head(v)
  170. , seq(str)
  171. {
  172. }
  173. iterator next() const
  174. {
  175. if (head == -1)
  176. return *this; // should report error, but we can only throw exception, so simply ignore it.
  177. return iterator(seq, seq[head].inext);
  178. }
  179. iterator& operator++()
  180. {
  181. *this = next();
  182. return *this;
  183. }
  184. iterator operator++(int)
  185. {
  186. iterator old(seq, head);
  187. *this = next();
  188. return old;
  189. }
  190. bool operator==(const iterator& second) const
  191. {
  192. // Ignore seq - should be the same and this is only a sanity check.
  193. return head == second.head;
  194. }
  195. bool operator!=(const iterator& second) const { return !(*this == second); }
  196. std::pair<int32_t, int32_t> operator*() { return std::make_pair(seq[head].seqstart, seq[head].seqend); }
  197. };
  198. iterator begin() { return iterator(m_caSeq, m_iHead); }
  199. iterator end() { return iterator(m_caSeq, -1); }
  200. };
  201. struct CRcvFreshLoss
  202. {
  203. int32_t seq[2];
  204. int ttl;
  205. srt::sync::steady_clock::time_point timestamp;
  206. CRcvFreshLoss(int32_t seqlo, int32_t seqhi, int initial_ttl);
  207. // Don't WTF when looking at this. The Windows system headers define
  208. // a publicly visible preprocessor macro with that name. REALLY!
  209. #ifdef DELETE
  210. #undef DELETE
  211. #endif
  212. enum Emod
  213. {
  214. NONE, //< the given sequence was not found in this range
  215. STRIPPED, //< it was equal to first or last, already taken care of
  216. SPLIT, //< found in the middle, you have to split this range into two
  217. DELETE //< This was a range of one element exactly equal to sequence. Simply delete it.
  218. };
  219. Emod revoke(int32_t sequence);
  220. Emod revoke(int32_t lo, int32_t hi);
  221. static bool removeOne(std::deque<CRcvFreshLoss>& w_container, int32_t sequence, int* had_ttl = NULL);
  222. };
  223. } // namespace srt
  224. #endif