lkcp9_fec.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
  2. // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
  3. // Copyright © 2020 Gridfinity, LLC. <admin@gridfinity.com>.
  4. // Copyright © 2020 Jeffrey H. Johnson <jeff@gridfinity.com>.
  5. //
  6. // All rights reserved.
  7. //
  8. // All use of this code is governed by the MIT license.
  9. // The complete license is available in the LICENSE file.
  10. package lkcp9 // import "go.gridfinity.dev/lkcp9"
  11. import (
  12. "encoding/binary"
  13. "sync/atomic"
  14. "github.com/klauspost/reedsolomon"
  15. )
  16. const (
  17. fecHeaderSize = 6
  18. fecHeaderSizePlus2 = fecHeaderSize + 2
  19. KTypeData = 0xf1
  20. KTypeParity = 0xf2
  21. )
  22. type FecPacket []byte
  23. func (
  24. bts FecPacket,
  25. ) seqid() uint32 {
  26. return binary.LittleEndian.Uint32(
  27. bts,
  28. )
  29. }
  30. func (
  31. bts FecPacket,
  32. ) flag() uint16 {
  33. return binary.LittleEndian.Uint16(
  34. bts[4:],
  35. )
  36. }
  37. func (bts FecPacket) data() []byte {
  38. return bts[6:]
  39. }
  40. type FecDecoder struct {
  41. rxlimit int
  42. dataShards int
  43. parityShards int
  44. shardSize int
  45. rx []FecPacket
  46. DecodeCache [][]byte
  47. flagCache []bool
  48. zeros []byte
  49. codec reedsolomon.Encoder
  50. }
  51. func KcpNewDECDecoder(
  52. rxlimit,
  53. dataShards,
  54. parityShards int,
  55. ) *FecDecoder {
  56. if dataShards <= 0 || parityShards <= 0 {
  57. return nil
  58. }
  59. if rxlimit < dataShards+parityShards {
  60. return nil
  61. }
  62. dec := new(
  63. FecDecoder,
  64. )
  65. dec.rxlimit = rxlimit
  66. dec.dataShards = dataShards
  67. dec.parityShards = parityShards
  68. dec.shardSize = dataShards + parityShards
  69. codec, err := reedsolomon.New(
  70. dataShards,
  71. parityShards,
  72. )
  73. if err != nil {
  74. return nil
  75. }
  76. dec.codec = codec
  77. dec.DecodeCache = make(
  78. [][]byte,
  79. dec.shardSize,
  80. )
  81. dec.flagCache = make(
  82. []bool,
  83. dec.shardSize,
  84. )
  85. dec.zeros = make(
  86. []byte,
  87. KcpMtuLimit,
  88. )
  89. return dec
  90. }
  91. func (dec *FecDecoder) Decode(in FecPacket) (recovered [][]byte) {
  92. n := len(dec.rx) - 1
  93. insertIdx := 0
  94. for i := n; i >= 0; i-- {
  95. if in.seqid() == dec.rx[i].seqid() {
  96. return nil
  97. } else if _itimediff(in.seqid(), dec.rx[i].seqid()) > 0 {
  98. insertIdx = i + 1
  99. break
  100. }
  101. }
  102. // make a copy
  103. pkt := FecPacket(KxmitBuf.Get().([]byte)[:len(in)])
  104. copy(pkt, in)
  105. if insertIdx == n+1 {
  106. dec.rx = append(dec.rx, pkt)
  107. } else {
  108. dec.rx = append(dec.rx, FecPacket{})
  109. copy(dec.rx[insertIdx+1:], dec.rx[insertIdx:])
  110. dec.rx[insertIdx] = pkt
  111. }
  112. shardBegin := pkt.seqid() - pkt.seqid()%uint32(dec.shardSize)
  113. shardEnd := shardBegin + uint32(dec.shardSize) - 1
  114. searchBegin := insertIdx - int(pkt.seqid()%uint32(dec.shardSize))
  115. if searchBegin < 0 {
  116. searchBegin = 0
  117. }
  118. searchEnd := searchBegin + dec.shardSize - 1
  119. if searchEnd >= len(dec.rx) {
  120. searchEnd = len(dec.rx) - 1
  121. }
  122. if searchEnd-searchBegin+1 >= dec.dataShards {
  123. var numshard, numDataShard, first, maxlen int
  124. shards := dec.DecodeCache
  125. shardsflag := dec.flagCache
  126. for k := range dec.DecodeCache {
  127. shards[k] = nil
  128. shardsflag[k] = false
  129. }
  130. for i := searchBegin; i <= searchEnd; i++ {
  131. seqid := dec.rx[i].seqid()
  132. if _itimediff(seqid, shardEnd) > 0 {
  133. break
  134. } else if _itimediff(seqid, shardBegin) >= 0 {
  135. shards[seqid%uint32(dec.shardSize)] = dec.rx[i].data()
  136. shardsflag[seqid%uint32(dec.shardSize)] = true
  137. numshard++
  138. if dec.rx[i].flag() == KTypeData {
  139. numDataShard++
  140. }
  141. if numshard == 1 {
  142. first = i
  143. }
  144. if len(dec.rx[i].data()) > maxlen {
  145. maxlen = len(dec.rx[i].data())
  146. }
  147. }
  148. }
  149. if numDataShard == dec.dataShards {
  150. dec.rx = dec.freeRange(first, numshard, dec.rx)
  151. } else if numshard >= dec.dataShards {
  152. for k := range shards {
  153. if shards[k] != nil {
  154. dlen := len(shards[k])
  155. shards[k] = shards[k][:maxlen]
  156. copy(shards[k][dlen:], dec.zeros)
  157. } else {
  158. shards[k] = KxmitBuf.Get().([]byte)[:0]
  159. }
  160. }
  161. if err := dec.codec.ReconstructData(shards); err == nil {
  162. for k := range shards[:dec.dataShards] {
  163. if !shardsflag[k] {
  164. recovered = append(recovered, shards[k])
  165. }
  166. }
  167. }
  168. dec.rx = dec.freeRange(first, numshard, dec.rx)
  169. }
  170. }
  171. if len(dec.rx) > dec.rxlimit {
  172. if dec.rx[0].flag() == KTypeData {
  173. atomic.AddUint64(&DefaultSnsi.KcpFECRuntShards, 1)
  174. }
  175. dec.rx = dec.freeRange(0, 1, dec.rx)
  176. }
  177. return
  178. }
  179. func (dec *FecDecoder) freeRange(first, n int, q []FecPacket) []FecPacket {
  180. for i := first; i < first+n; i++ {
  181. KxmitBuf.Put([]byte(q[i]))
  182. }
  183. if first == 0 && n < cap(q)/2 {
  184. return q[n:]
  185. }
  186. copy(q[first:], q[first+n:])
  187. return q[:len(q)-n]
  188. }
  189. type (
  190. FecEncoder struct {
  191. dataShards int
  192. parityShards int
  193. shardSize int
  194. paws uint32 // Protect Against Wrapped Sequence numbers
  195. next uint32 // next seqid
  196. shardCount int // count the number of datashards collected
  197. maxSize int // track maximum data length in datashard
  198. headerOffset int // FEC header offset
  199. payloadOffset int // FEC payload offset
  200. shardCache [][]byte
  201. EncodeCache [][]byte
  202. zeros []byte
  203. codec reedsolomon.Encoder
  204. }
  205. )
  206. func KcpNewDECEncoder(dataShards, parityShards, offset int) *FecEncoder {
  207. if dataShards <= 0 || parityShards <= 0 {
  208. return nil
  209. }
  210. enc := new(
  211. FecEncoder,
  212. )
  213. enc.dataShards = dataShards
  214. enc.parityShards = parityShards
  215. enc.shardSize = dataShards + parityShards
  216. enc.paws = (0xFFFFFFFF/uint32(enc.shardSize) - 1) * uint32(enc.shardSize)
  217. enc.headerOffset = offset
  218. enc.payloadOffset = enc.headerOffset + fecHeaderSize
  219. codec, err := reedsolomon.New(
  220. dataShards,
  221. parityShards,
  222. )
  223. if err != nil {
  224. return nil
  225. }
  226. enc.codec = codec
  227. enc.EncodeCache = make(
  228. [][]byte,
  229. enc.shardSize,
  230. )
  231. enc.shardCache = make(
  232. [][]byte,
  233. enc.shardSize,
  234. )
  235. for k := range enc.shardCache {
  236. enc.shardCache[k] = make(
  237. []byte,
  238. KcpMtuLimit,
  239. )
  240. }
  241. enc.zeros = make(
  242. []byte,
  243. KcpMtuLimit,
  244. )
  245. return enc
  246. }
  247. func (
  248. enc *FecEncoder,
  249. ) Encode(
  250. b []byte,
  251. ) (
  252. ps [][]byte,
  253. ) {
  254. enc.markData(
  255. b[enc.headerOffset:],
  256. )
  257. binary.LittleEndian.PutUint16(b[enc.payloadOffset:], uint16(len(b[enc.payloadOffset:])))
  258. sz := len(
  259. b,
  260. )
  261. enc.shardCache[enc.shardCount] = enc.shardCache[enc.shardCount][:sz]
  262. copy(enc.shardCache[enc.shardCount][enc.payloadOffset:], b[enc.payloadOffset:])
  263. enc.shardCount++
  264. if sz > enc.maxSize {
  265. enc.maxSize = sz
  266. }
  267. if enc.shardCount == enc.dataShards {
  268. for i := 0; i < enc.dataShards; i++ {
  269. shard := enc.shardCache[i]
  270. slen := len(
  271. shard,
  272. )
  273. copy(
  274. shard[slen:enc.maxSize],
  275. enc.zeros,
  276. )
  277. }
  278. cache := enc.EncodeCache
  279. for k := range cache {
  280. cache[k] = enc.shardCache[k][enc.payloadOffset:enc.maxSize]
  281. }
  282. if err := enc.codec.Encode(
  283. cache,
  284. ); err == nil {
  285. ps = enc.shardCache[enc.dataShards:]
  286. for k := range ps {
  287. enc.markParity(
  288. ps[k][enc.headerOffset:],
  289. )
  290. ps[k] = ps[k][:enc.maxSize]
  291. }
  292. }
  293. enc.shardCount = 0
  294. enc.maxSize = 0
  295. }
  296. return
  297. }
  298. func (enc *FecEncoder) markData(data []byte) {
  299. binary.LittleEndian.PutUint32(data, enc.next)
  300. binary.LittleEndian.PutUint16(data[4:], KTypeData)
  301. enc.next++
  302. }
  303. func (enc *FecEncoder) markParity(data []byte) {
  304. binary.LittleEndian.PutUint32(data, enc.next)
  305. binary.LittleEndian.PutUint16(data[4:], KTypeParity)
  306. enc.next = (enc.next + 1) % enc.paws
  307. }