You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

333 lines
5.9 KiB

  1. package udptransfer
  2. type qNode struct {
  3. *packet
  4. prev *qNode
  5. next *qNode
  6. sent int64 // last sent time
  7. sent_1 int64 // prev sent time
  8. miss int // sack miss count
  9. }
  10. type linkedMap struct {
  11. head *qNode
  12. tail *qNode
  13. qmap map[uint32]*qNode
  14. lastIns *qNode
  15. maxCtnSeq uint32
  16. mode int
  17. }
  18. const (
  19. _QModeIn = 1
  20. _QModeOut = 2
  21. )
  22. func newLinkedMap(qmode int) *linkedMap {
  23. return &linkedMap{
  24. qmap: make(map[uint32]*qNode),
  25. mode: qmode,
  26. }
  27. }
  28. func (l *linkedMap) get(seq uint32) (i *qNode) {
  29. i = l.qmap[seq]
  30. return
  31. }
  32. func (l *linkedMap) contains(seq uint32) bool {
  33. _, y := l.qmap[seq]
  34. return y
  35. }
  36. func (l *linkedMap) size() int32 {
  37. return int32(len(l.qmap))
  38. }
  39. func (l *linkedMap) reset() {
  40. l.head = nil
  41. l.tail = nil
  42. l.lastIns = nil
  43. l.maxCtnSeq = 0
  44. l.qmap = make(map[uint32]*qNode)
  45. }
  46. func (l *linkedMap) isEqualsHead(seq uint32) bool {
  47. return l.head != nil && seq == l.head.seq
  48. }
  49. func (l *linkedMap) distanceOfHead(seq uint32) int32 {
  50. if l.head != nil {
  51. return int32(seq - l.head.seq)
  52. } else {
  53. return -1
  54. }
  55. }
  56. func (l *linkedMap) appendTail(one *qNode) {
  57. if l.tail != nil {
  58. l.tail.next = one
  59. one.prev = l.tail
  60. l.tail = one
  61. } else {
  62. l.head = one
  63. l.tail = one
  64. }
  65. l.qmap[one.seq] = one
  66. }
  67. // xxx - n - yyy
  68. // xxx - yyy
  69. func (l *linkedMap) deleteAt(n *qNode) {
  70. x, y := n.prev, n.next
  71. if x != nil {
  72. x.next = y
  73. } else {
  74. l.head = y
  75. }
  76. if y != nil {
  77. y.prev = x
  78. } else {
  79. l.tail = x
  80. }
  81. n.prev, n.next = nil, nil
  82. delete(l.qmap, n.seq)
  83. }
  84. // delete with n <- ...n |
  85. func (l *linkedMap) deleteBefore(n *qNode) (left *qNode, deleted int32) {
  86. for i := n; i != nil; i = i.prev {
  87. delete(l.qmap, i.seq)
  88. if i.scnt != _SENT_OK {
  89. deleted++
  90. // only outQ could delete at here
  91. if l.mode == _QModeOut {
  92. bpool.Put(i.buffer)
  93. i.buffer = nil
  94. }
  95. }
  96. }
  97. left = l.head
  98. l.head = n.next
  99. n.next = nil
  100. if l.head != nil {
  101. l.head.prev = nil
  102. } else { // n.next is the tail and is nil
  103. l.tail = nil
  104. }
  105. return
  106. }
  107. // xxx - ref
  108. // xxx - one - ref
  109. func (l *linkedMap) insertBefore(ref, one *qNode) {
  110. x := ref.prev
  111. if x != nil {
  112. x.next = one
  113. one.prev = x
  114. } else {
  115. l.head = one
  116. }
  117. ref.prev = one
  118. one.next = ref
  119. l.qmap[one.seq] = one
  120. }
  121. // ref - zzz
  122. // ref - one - zzz
  123. func (l *linkedMap) insertAfter(ref, one *qNode) {
  124. z := ref.next
  125. if z == nil { // append
  126. ref.next = one
  127. l.tail = one
  128. } else { // insert mid
  129. z.prev = one
  130. ref.next = one
  131. }
  132. one.prev = ref
  133. one.next = z
  134. l.qmap[one.seq] = one
  135. }
  136. // baseHead: the left outside boundary
  137. // if inserted, return the distance between newNode with baseHead
  138. func (l *linkedMap) searchInsert(one *qNode, baseHead uint32) (dis int64) {
  139. for i := l.tail; i != nil; i = i.prev {
  140. dis = int64(one.seq) - int64(i.seq)
  141. if dis > 0 {
  142. l.insertAfter(i, one)
  143. return
  144. } else if dis == 0 {
  145. // duplicated
  146. return
  147. }
  148. }
  149. if one.seq <= baseHead {
  150. return 0
  151. }
  152. if l.head != nil {
  153. l.insertBefore(l.head, one)
  154. } else {
  155. l.head = one
  156. l.tail = one
  157. l.qmap[one.seq] = one
  158. }
  159. dis = int64(one.seq) - int64(baseHead)
  160. return
  161. }
  162. func (l *linkedMap) updateContinuous(i *qNode) bool {
  163. var lastCtnSeq = l.maxCtnSeq
  164. for ; i != nil; i = i.next {
  165. if i.seq-lastCtnSeq == 1 {
  166. lastCtnSeq = i.seq
  167. } else {
  168. break
  169. }
  170. }
  171. if lastCtnSeq != l.maxCtnSeq {
  172. l.maxCtnSeq = lastCtnSeq
  173. return true
  174. }
  175. return false
  176. }
  177. func (l *linkedMap) isWholeContinuous() bool {
  178. return l.tail != nil && l.maxCtnSeq == l.tail.seq
  179. }
  180. /*
  181. func (l *linkedMap) searchMaxContinued(baseHead uint32) (*qNode, bool) {
  182. var last *qNode
  183. for i := l.head; i != nil; i = i.next {
  184. if last != nil {
  185. if i.seq-last.seq > 1 {
  186. return last, true
  187. }
  188. } else {
  189. if i.seq != baseHead {
  190. return nil, false
  191. }
  192. }
  193. last = i
  194. }
  195. if last != nil {
  196. return last, true
  197. } else {
  198. return nil, false
  199. }
  200. }
  201. */
  202. func (q *qNode) forward(n int) *qNode {
  203. for ; n > 0 && q != nil; n-- {
  204. q = q.next
  205. }
  206. return q
  207. }
  208. // prev of bitmap start point
  209. func (l *linkedMap) makeHolesBitmap(prev uint32) ([]uint64, uint32) {
  210. var start *qNode
  211. var bits uint64
  212. var j uint32
  213. for i := l.head; i != nil; i = i.next {
  214. if i.seq >= prev+1 {
  215. start = i
  216. break
  217. }
  218. }
  219. var bitmap []uint64
  220. // search start which is the recent successor of [prev]
  221. Iterator:
  222. for i := start; i != nil; i = i.next {
  223. dis := i.seq - prev
  224. prev = i.seq
  225. j += dis // j is next bit index
  226. for j >= 65 {
  227. if len(bitmap) >= 20 { // bitmap too long
  228. break Iterator
  229. }
  230. bitmap = append(bitmap, bits)
  231. bits = 0
  232. j -= 64
  233. }
  234. bits |= 1 << (j - 1)
  235. }
  236. if j > 0 {
  237. // j -> (0, 64]
  238. bitmap = append(bitmap, bits)
  239. }
  240. return bitmap, j
  241. }
  242. // from= the bitmap start point
  243. func (l *linkedMap) deleteByBitmap(bmap []uint64, from uint32, tailBitsLen uint32) (deleted, missed int32, lastContinued bool) {
  244. var start = l.qmap[from]
  245. if start != nil {
  246. // delete predecessors
  247. if pred := start.prev; pred != nil {
  248. _, n := l.deleteBefore(pred)
  249. deleted += n
  250. }
  251. } else {
  252. // [from] is out of bounds
  253. return
  254. }
  255. var j, bitsLen uint32
  256. var bits uint64
  257. bits, bmap = bmap[0], bmap[1:]
  258. if len(bmap) > 0 {
  259. bitsLen = 64
  260. } else {
  261. // bmap.len==1, tail is here
  262. bitsLen = tailBitsLen
  263. }
  264. // maxContinued will save the max continued node (from [start]) which could be deleted safely.
  265. // keep the queue smallest
  266. var maxContinued *qNode
  267. lastContinued = true
  268. for i := start; i != nil; j++ {
  269. if j >= bitsLen {
  270. if len(bmap) > 0 {
  271. j = 0
  272. bits, bmap = bmap[0], bmap[1:]
  273. if len(bmap) > 0 {
  274. bitsLen = 64
  275. } else {
  276. bitsLen = tailBitsLen
  277. }
  278. } else {
  279. // no more pages
  280. goto finished
  281. }
  282. }
  283. if bits&1 == 1 {
  284. if lastContinued {
  285. maxContinued = i
  286. }
  287. if i.scnt != _SENT_OK {
  288. // no mark means first deleting
  289. deleted++
  290. }
  291. // don't delete, just mark it
  292. i.scnt = _SENT_OK
  293. } else {
  294. // known it may be lost
  295. if i.miss == 0 {
  296. missed++
  297. }
  298. i.miss++
  299. lastContinued = false
  300. }
  301. bits >>= 1
  302. i = i.next
  303. }
  304. finished:
  305. if maxContinued != nil {
  306. l.deleteBefore(maxContinued)
  307. }
  308. return
  309. }