gfcp.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. // Package gfcp - A Fast and Reliable ARQ Protocol
  2. //
  3. // Copyright © 2015 Daniel Fu <daniel820313@gmail.com>.
  4. // Copyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.
  5. // Copyright © 2021 Gridfinity, LLC. <admin@gridfinity.com>.
  6. // Copyright © 2021 Jeffrey H. Johnson <jeff@gridfinity.com>.
  7. //
  8. // All rights reserved.
  9. //
  10. // All use of this code is governed by the MIT license.
  11. // The complete license is available in the LICENSE file.
  12. package gfcp // import "go.gridfinity.dev/gfcp"
  13. import (
  14. "encoding/binary"
  15. "math"
  16. "runtime/debug"
  17. "sync/atomic"
  18. gfcpLegal "go4.org/legal"
  19. )
  20. // Gfcp protocol constants
  21. const (
  22. GfcpRtoNdl = 20 // GfcpRtoNdl: NoDelay min RTO
  23. GfcpRtoMin = 120 // GfcpRtoMin: Regular min RTO
  24. GfcpRtoDef = 340
  25. GfcpRtoMax = 60000
  26. GfcpCmdPush = 81 // GfcpCmdPush: Push data
  27. GfcpCmdAck = 82 // GfcpCmdAck: Ack
  28. GfcpCmdWask = 83 // GfcpCmdWask: Get Window Size
  29. GfcpCmdWins = 84 // GfcpCmdWins: Set window Size
  30. GfcpAskSend = 1 // GfcpAskSend: Need to send GfcpCmdWask
  31. GfcpAskTell = 2 // GfcpAskTell: Need to send GfcpCmdWins
  32. GfcpWndSnd = 32
  33. GfcpWndRcv = 32
  34. GfcpMtuDef = 1480
  35. GfcpAckFast = 3
  36. GfcpInterval = 100
  37. GfcpOverhead = 24
  38. GfcpDeadLink = 20
  39. GfcpThreshInit = 2
  40. GfcpThreshMin = 2
  41. GfcpProbeInit = 7000 // 7s initial probe window
  42. GfcpProbeLimit = 102000 // 120s hard probe timeout
  43. )
  44. type outputCallback func(
  45. buf []byte,
  46. size int,
  47. )
  48. func gfcpEncode8u(
  49. p []byte,
  50. c byte,
  51. ) []byte {
  52. p[0] = c
  53. return p[1:]
  54. }
  55. func gfcpDecode8u(
  56. p []byte,
  57. c *byte,
  58. ) []byte {
  59. *c = p[0]
  60. return p[1:]
  61. }
  62. func gfcpEncode16u(
  63. p []byte,
  64. w uint16,
  65. ) []byte {
  66. binary.LittleEndian.PutUint16(
  67. p,
  68. w,
  69. )
  70. return p[2:]
  71. }
  72. func gfcpDecode16u(
  73. p []byte,
  74. w *uint16,
  75. ) []byte {
  76. *w = binary.LittleEndian.Uint16(
  77. p,
  78. )
  79. return p[2:]
  80. }
  81. func gfcpEncode32u(
  82. p []byte,
  83. l uint32,
  84. ) []byte {
  85. binary.LittleEndian.PutUint32(
  86. p,
  87. l,
  88. )
  89. return p[4:]
  90. }
  91. func gfcpDecode32u(
  92. p []byte,
  93. l *uint32,
  94. ) []byte {
  95. *l = binary.LittleEndian.Uint32(
  96. p,
  97. )
  98. return p[4:]
  99. }
  100. func _imin(
  101. a,
  102. b uint32,
  103. ) uint32 {
  104. if a <= b {
  105. return a
  106. }
  107. return b
  108. }
  109. func _imax(
  110. a,
  111. b uint32,
  112. ) uint32 {
  113. if a >= b {
  114. return a
  115. }
  116. return b
  117. }
  118. func _ibound(
  119. lower,
  120. middle,
  121. upper uint32,
  122. ) uint32 {
  123. return _imin(
  124. _imax(
  125. lower,
  126. middle,
  127. ),
  128. upper,
  129. )
  130. }
  131. func _itimediff(
  132. later,
  133. earlier uint32,
  134. ) int32 {
  135. return (int32)(later - earlier)
  136. }
  137. // Segment structure
  138. type Segment struct {
  139. conv uint32
  140. cmd uint8
  141. frg uint8
  142. wnd uint16
  143. ts uint32
  144. sn uint32
  145. una uint32
  146. rto uint32
  147. Kxmit uint32
  148. GFcpResendTs uint32
  149. fastack uint32
  150. acked uint32
  151. data []byte
  152. }
  153. func (
  154. GFcpSeg *Segment,
  155. ) encode(
  156. ptr []byte,
  157. ) []byte {
  158. ptr = gfcpEncode32u(
  159. ptr,
  160. GFcpSeg.conv,
  161. )
  162. ptr = gfcpEncode8u(
  163. ptr,
  164. GFcpSeg.cmd,
  165. )
  166. ptr = gfcpEncode8u(
  167. ptr,
  168. GFcpSeg.frg,
  169. )
  170. ptr = gfcpEncode16u(
  171. ptr,
  172. GFcpSeg.wnd,
  173. )
  174. ptr = gfcpEncode32u(
  175. ptr,
  176. GFcpSeg.ts,
  177. )
  178. ptr = gfcpEncode32u(
  179. ptr,
  180. GFcpSeg.sn,
  181. )
  182. ptr = gfcpEncode32u(
  183. ptr,
  184. GFcpSeg.una,
  185. )
  186. ptr = gfcpEncode32u(
  187. ptr, uint32(len(
  188. GFcpSeg.data,
  189. )))
  190. atomic.AddUint64(
  191. &DefaultSnsi.GFcpOutputSegments,
  192. 1,
  193. )
  194. return ptr
  195. }
  196. // GFCP primary structure
  197. type GFCP struct {
  198. conv, mtu, mss, state uint32
  199. sndUna, sndNxt, rcvNxt uint32
  200. ssthresh uint32
  201. rxRttVar, rxSrtt int32
  202. rxRto, rxMinRto uint32
  203. sndWnd, rcvWnd, rmtWnd, cwnd, probe uint32
  204. interval, tsFlush uint32
  205. nodelay, updated uint32
  206. tsProbe, probeWait uint32
  207. deadLink, incr uint32
  208. fastresend int32
  209. nocwnd, stream int32
  210. sndQueue []Segment
  211. rcvQueue []Segment
  212. SndBuf []Segment
  213. rcvBuf []Segment
  214. acklist []ackItem
  215. buffer []byte
  216. reserved int
  217. output outputCallback
  218. }
  219. type ackItem struct {
  220. sn uint32
  221. ts uint32
  222. }
  223. // NewGFCP creates a new GFcp control object.
  224. func NewGFCP(
  225. conv uint32,
  226. output outputCallback,
  227. ) *GFCP {
  228. GFcp := new(
  229. GFCP,
  230. )
  231. GFcp.conv = conv
  232. GFcp.sndWnd = GfcpWndSnd
  233. GFcp.rcvWnd = GfcpWndRcv
  234. GFcp.rmtWnd = GfcpWndRcv
  235. GFcp.mtu = GfcpMtuDef
  236. GFcp.mss = GFcp.mtu - GfcpOverhead
  237. GFcp.buffer = make(
  238. []byte,
  239. GFcp.mtu,
  240. )
  241. GFcp.rxRto = GfcpRtoDef
  242. GFcp.rxMinRto = GfcpRtoMin
  243. GFcp.interval = GfcpInterval
  244. GFcp.tsFlush = GfcpInterval
  245. GFcp.ssthresh = GfcpThreshInit
  246. GFcp.deadLink = GfcpDeadLink
  247. GFcp.output = output
  248. return GFcp
  249. }
  250. func (
  251. GFcp *GFCP,
  252. ) newSegment(
  253. size int,
  254. ) (
  255. GFcpSeg Segment,
  256. ) {
  257. GFcpSeg.data = KxmitBuf.Get().([]byte)[:size]
  258. return
  259. }
  260. func (
  261. GFcp *GFCP,
  262. ) delSegment(
  263. GFcpSeg *Segment,
  264. ) {
  265. if GFcpSeg.data != nil {
  266. KxmitBuf.Put(
  267. // TODO(jhj): Switch to pointer to avoid allocation
  268. GFcpSeg.data,
  269. )
  270. GFcpSeg.data = nil
  271. }
  272. }
  273. // ReserveBytes keeps 'n' bytes from the beginning of buffering.
  274. // Output callbacks use this to return 'false' if 'n' >= 'mss'.
  275. func (
  276. GFcp *GFCP,
  277. ) ReserveBytes(
  278. n int,
  279. ) bool {
  280. if n >= int(
  281. GFcp.mtu-GfcpOverhead,
  282. ) || n < 0 {
  283. return false
  284. }
  285. GFcp.reserved = n
  286. GFcp.mss = GFcp.mtu - GfcpOverhead - uint32(
  287. n,
  288. )
  289. return true
  290. }
  291. // PeekSize checks the size of next message in the receive queue.
  292. func (
  293. GFcp *GFCP,
  294. ) PeekSize() (
  295. length int,
  296. ) {
  297. if len(
  298. GFcp.rcvQueue,
  299. ) == 0 {
  300. return -1
  301. }
  302. GFcpSeg := &GFcp.rcvQueue[0]
  303. if GFcpSeg.frg == 0 {
  304. return len(
  305. GFcpSeg.data,
  306. )
  307. }
  308. if len(
  309. GFcp.rcvQueue,
  310. ) < int(
  311. GFcpSeg.frg+1,
  312. ) {
  313. return -1
  314. }
  315. for k := range GFcp.rcvQueue {
  316. GFcpSeg := &GFcp.rcvQueue[k]
  317. length += len(
  318. GFcpSeg.data,
  319. )
  320. if GFcpSeg.frg == 0 {
  321. break
  322. }
  323. }
  324. return
  325. }
  326. // Recv is upper level recviver; returns size or EAGAIN on error.
  327. func (
  328. GFcp *GFCP,
  329. ) Recv(
  330. buffer []byte,
  331. ) (
  332. n int,
  333. ) {
  334. if len(
  335. GFcp.rcvQueue,
  336. ) == 0 {
  337. return -1
  338. }
  339. peeksize := GFcp.PeekSize()
  340. if peeksize < 0 {
  341. return -2
  342. }
  343. if peeksize > len(
  344. buffer,
  345. ) {
  346. return -3
  347. }
  348. var fastRecovery bool
  349. if len(
  350. GFcp.rcvQueue,
  351. ) >= int(
  352. GFcp.rcvWnd,
  353. ) {
  354. fastRecovery = true
  355. }
  356. count := 0
  357. for k := range GFcp.rcvQueue {
  358. GFcpSeg := &GFcp.rcvQueue[k]
  359. copy(
  360. buffer,
  361. GFcpSeg.data,
  362. )
  363. buffer = buffer[len(
  364. GFcpSeg.data,
  365. ):]
  366. n += len(
  367. GFcpSeg.data,
  368. )
  369. count++
  370. GFcp.delSegment(
  371. GFcpSeg,
  372. )
  373. if GFcpSeg.frg == 0 {
  374. break
  375. }
  376. }
  377. if count > 0 {
  378. GFcp.rcvQueue = GFcp.removeFront(
  379. GFcp.rcvQueue,
  380. count,
  381. )
  382. }
  383. count = 0
  384. for k := range GFcp.rcvBuf {
  385. GFcpSeg := &GFcp.rcvBuf[k]
  386. if GFcpSeg.sn == GFcp.rcvNxt && len(
  387. GFcp.rcvQueue,
  388. ) < int(
  389. GFcp.rcvWnd,
  390. ) {
  391. GFcp.rcvNxt++
  392. count++
  393. } else {
  394. break
  395. }
  396. }
  397. if count > 0 {
  398. GFcp.rcvQueue = append(
  399. GFcp.rcvQueue,
  400. GFcp.rcvBuf[:count]...,
  401. )
  402. GFcp.rcvBuf = GFcp.removeFront(
  403. GFcp.rcvBuf,
  404. count,
  405. )
  406. }
  407. if len(
  408. GFcp.rcvQueue,
  409. ) < int(
  410. GFcp.rcvWnd,
  411. ) && fastRecovery {
  412. GFcp.probe |= GfcpAskTell
  413. }
  414. return
  415. }
  416. // Send is upper level sender, returns <0 on error.
  417. func (
  418. GFcp *GFCP,
  419. ) Send(
  420. buffer []byte,
  421. ) int {
  422. var count int
  423. if len(
  424. buffer,
  425. ) == 0 {
  426. return -1
  427. }
  428. if GFcp.stream != 0 {
  429. n := len(
  430. GFcp.sndQueue,
  431. )
  432. if n > 0 {
  433. GFcpSeg := &GFcp.sndQueue[n-1]
  434. if len(
  435. GFcpSeg.data,
  436. ) < int(
  437. GFcp.mss,
  438. ) {
  439. capacity := int(
  440. GFcp.mss,
  441. ) - len(
  442. GFcpSeg.data,
  443. )
  444. extend := capacity
  445. if len(
  446. buffer,
  447. ) < capacity {
  448. extend = len(
  449. buffer,
  450. )
  451. }
  452. oldlen := len(
  453. GFcpSeg.data,
  454. )
  455. GFcpSeg.data = GFcpSeg.data[:oldlen+extend]
  456. copy(
  457. GFcpSeg.data[oldlen:],
  458. buffer,
  459. )
  460. buffer = buffer[extend:]
  461. }
  462. }
  463. if len(
  464. buffer,
  465. ) == 0 {
  466. return 0
  467. }
  468. }
  469. if len(
  470. buffer,
  471. ) <= int(
  472. GFcp.mss,
  473. ) {
  474. count = 1
  475. } else {
  476. count = (len(
  477. buffer,
  478. ) + int(
  479. GFcp.mss,
  480. ) - 1) / int(
  481. GFcp.mss,
  482. )
  483. }
  484. if count > 255 {
  485. return -2
  486. }
  487. if count == 0 {
  488. count = 1
  489. }
  490. for i := 0; i < count; i++ {
  491. var size int
  492. if len(
  493. buffer,
  494. ) > int(
  495. GFcp.mss,
  496. ) {
  497. size = int(
  498. GFcp.mss,
  499. )
  500. } else {
  501. size = len(
  502. buffer,
  503. )
  504. }
  505. GFcpSeg := GFcp.newSegment(
  506. size,
  507. )
  508. copy(
  509. GFcpSeg.data,
  510. buffer[:size],
  511. )
  512. if GFcp.stream == 0 {
  513. GFcpSeg.frg = uint8(
  514. count - i - 1,
  515. )
  516. } else {
  517. GFcpSeg.frg = 0
  518. }
  519. GFcp.sndQueue = append(
  520. GFcp.sndQueue,
  521. GFcpSeg,
  522. )
  523. buffer = buffer[size:]
  524. }
  525. return 0
  526. }
  527. func (
  528. GFcp *GFCP,
  529. ) updateAck(
  530. rtt int32,
  531. ) {
  532. var rto uint32
  533. if GFcp.rxSrtt == 0 {
  534. GFcp.rxSrtt = rtt
  535. GFcp.rxRttVar = rtt >> 1
  536. } else {
  537. delta := rtt - GFcp.rxSrtt
  538. GFcp.rxSrtt += delta >> 3
  539. if delta < 0 {
  540. delta = -delta
  541. }
  542. if rtt < GFcp.rxSrtt-GFcp.rxRttVar {
  543. GFcp.rxRttVar += (delta - GFcp.rxRttVar) >> 5
  544. } else {
  545. GFcp.rxRttVar += (delta - GFcp.rxRttVar) >> 2
  546. }
  547. }
  548. rto = uint32(
  549. GFcp.rxSrtt,
  550. ) + _imax(
  551. GFcp.interval,
  552. uint32(
  553. GFcp.rxRttVar,
  554. )<<2)
  555. GFcp.rxRto = _ibound(
  556. GFcp.rxMinRto,
  557. rto,
  558. GfcpRtoMax,
  559. )
  560. }
  561. func (
  562. GFcp *GFCP,
  563. ) shrinkBuf() {
  564. if len(
  565. GFcp.SndBuf,
  566. ) > 0 {
  567. GFcpSeg := &GFcp.SndBuf[0]
  568. GFcp.sndUna = GFcpSeg.sn
  569. } else {
  570. GFcp.sndUna = GFcp.sndNxt
  571. }
  572. }
  573. func (
  574. GFcp *GFCP,
  575. ) parseAck(
  576. sn uint32,
  577. ) {
  578. if _itimediff(
  579. sn,
  580. GFcp.sndUna,
  581. ) < 0 || _itimediff(
  582. sn,
  583. GFcp.sndNxt,
  584. ) >= 0 {
  585. return
  586. }
  587. for k := range GFcp.SndBuf {
  588. GFcpSeg := &GFcp.SndBuf[k]
  589. if sn == GFcpSeg.sn {
  590. GFcpSeg.acked = 1
  591. GFcp.delSegment(
  592. GFcpSeg,
  593. )
  594. break
  595. }
  596. if _itimediff(
  597. sn,
  598. GFcpSeg.sn,
  599. ) < 0 {
  600. break
  601. }
  602. }
  603. }
  604. func (
  605. GFcp *GFCP,
  606. ) parseFastack(
  607. sn, ts uint32,
  608. ) {
  609. if _itimediff(
  610. sn,
  611. GFcp.sndUna,
  612. ) < 0 || _itimediff(
  613. sn,
  614. GFcp.sndNxt,
  615. ) >= 0 {
  616. return
  617. }
  618. for k := range GFcp.SndBuf {
  619. GFcpSeg := &GFcp.SndBuf[k]
  620. if _itimediff(
  621. sn,
  622. GFcpSeg.sn,
  623. ) < 0 {
  624. break
  625. } else if sn != GFcpSeg.sn && _itimediff(
  626. GFcpSeg.ts,
  627. ts,
  628. ) <= 0 {
  629. GFcpSeg.fastack++
  630. }
  631. }
  632. }
  633. func (
  634. GFcp *GFCP,
  635. ) parseUna(
  636. una uint32,
  637. ) {
  638. count := 0
  639. for k := range GFcp.SndBuf {
  640. GFcpSeg := &GFcp.SndBuf[k]
  641. if _itimediff(
  642. una,
  643. GFcpSeg.sn,
  644. ) > 0 {
  645. GFcp.delSegment(
  646. GFcpSeg,
  647. )
  648. count++
  649. } else {
  650. break
  651. }
  652. }
  653. if count > 0 {
  654. GFcp.SndBuf = GFcp.removeFront(
  655. GFcp.SndBuf,
  656. count,
  657. )
  658. }
  659. }
  660. func (
  661. GFcp *GFCP,
  662. ) ackPush(
  663. sn,
  664. ts uint32,
  665. ) {
  666. GFcp.acklist = append(
  667. GFcp.acklist,
  668. ackItem{
  669. sn,
  670. ts,
  671. })
  672. }
  673. func (
  674. GFcp *GFCP,
  675. ) parseData(
  676. newGFcpSeg Segment,
  677. ) bool {
  678. sn := newGFcpSeg.sn
  679. if _itimediff(
  680. sn,
  681. GFcp.rcvNxt+GFcp.rcvWnd,
  682. ) >= 0 ||
  683. _itimediff(
  684. sn,
  685. GFcp.rcvNxt,
  686. ) < 0 {
  687. return true
  688. }
  689. n := len(
  690. GFcp.rcvBuf,
  691. ) - 1
  692. insertIdx := 0
  693. repeat := false
  694. for i := n; i >= 0; i-- {
  695. GFcpSeg := &GFcp.rcvBuf[i]
  696. if GFcpSeg.sn == sn {
  697. repeat = true
  698. break
  699. }
  700. if _itimediff(
  701. sn,
  702. GFcpSeg.sn,
  703. ) > 0 {
  704. insertIdx = i + 1
  705. break
  706. }
  707. }
  708. if !repeat {
  709. dataCopy := KxmitBuf.Get().([]byte)[:len(newGFcpSeg.data)]
  710. copy(
  711. dataCopy,
  712. newGFcpSeg.data,
  713. )
  714. newGFcpSeg.data = dataCopy
  715. if insertIdx == n+1 {
  716. GFcp.rcvBuf = append(
  717. GFcp.rcvBuf,
  718. newGFcpSeg,
  719. )
  720. } else {
  721. GFcp.rcvBuf = append(
  722. GFcp.rcvBuf,
  723. Segment{},
  724. )
  725. copy(
  726. GFcp.rcvBuf[insertIdx+1:],
  727. GFcp.rcvBuf[insertIdx:],
  728. )
  729. GFcp.rcvBuf[insertIdx] = newGFcpSeg
  730. }
  731. }
  732. count := 0
  733. for k := range GFcp.rcvBuf {
  734. GFcpSeg := &GFcp.rcvBuf[k]
  735. if GFcpSeg.sn == GFcp.rcvNxt && len(
  736. GFcp.rcvQueue,
  737. ) < int(
  738. GFcp.rcvWnd,
  739. ) {
  740. GFcp.rcvNxt++
  741. count++
  742. } else {
  743. break
  744. }
  745. }
  746. if count > 0 {
  747. GFcp.rcvQueue = append(
  748. GFcp.rcvQueue,
  749. GFcp.rcvBuf[:count]...,
  750. )
  751. GFcp.rcvBuf = GFcp.removeFront(
  752. GFcp.rcvBuf,
  753. count,
  754. )
  755. }
  756. return repeat
  757. }
  758. // Input receives a (low-level) UDP packet, and determinines if
  759. // a full packet has been processsed (not by the FEC algorithm)
  760. func (
  761. GFcp *GFCP,
  762. ) Input(
  763. data []byte,
  764. regular,
  765. ackNoDelay bool,
  766. ) int {
  767. sndUna := GFcp.sndUna
  768. if len(
  769. data,
  770. ) < GfcpOverhead {
  771. return -1
  772. }
  773. var latest uint32
  774. var flag int
  775. var inSegs uint64
  776. for {
  777. var ts,
  778. sn,
  779. length,
  780. una,
  781. conv uint32
  782. var wnd uint16
  783. var cmd,
  784. frg uint8
  785. if len(
  786. data,
  787. ) < int(
  788. GfcpOverhead,
  789. ) {
  790. break
  791. }
  792. data = gfcpDecode32u(
  793. data,
  794. &conv,
  795. )
  796. if conv != GFcp.conv {
  797. return -1
  798. }
  799. data = gfcpDecode8u(
  800. data,
  801. &cmd,
  802. )
  803. data = gfcpDecode8u(
  804. data,
  805. &frg,
  806. )
  807. data = gfcpDecode16u(
  808. data,
  809. &wnd,
  810. )
  811. data = gfcpDecode32u(
  812. data,
  813. &ts,
  814. )
  815. data = gfcpDecode32u(
  816. data,
  817. &sn,
  818. )
  819. data = gfcpDecode32u(
  820. data,
  821. &una,
  822. )
  823. data = gfcpDecode32u(
  824. data,
  825. &length,
  826. )
  827. if len(
  828. data,
  829. ) < int(
  830. length,
  831. ) {
  832. return -2
  833. }
  834. if cmd != GfcpCmdPush && cmd != GfcpCmdAck &&
  835. cmd != GfcpCmdWask && cmd != GfcpCmdWins {
  836. return -3
  837. }
  838. if regular {
  839. GFcp.rmtWnd = uint32(
  840. wnd,
  841. )
  842. }
  843. GFcp.parseUna(
  844. una,
  845. )
  846. GFcp.shrinkBuf()
  847. if cmd == GfcpCmdAck {
  848. GFcp.parseAck(
  849. sn,
  850. )
  851. GFcp.parseFastack(
  852. sn,
  853. ts,
  854. )
  855. flag |= 1
  856. latest = ts
  857. } else if cmd == GfcpCmdPush {
  858. repeat := true
  859. if _itimediff(
  860. sn,
  861. GFcp.rcvNxt+GFcp.rcvWnd,
  862. ) < 0 {
  863. GFcp.ackPush(
  864. sn,
  865. ts,
  866. )
  867. if _itimediff(
  868. sn,
  869. GFcp.rcvNxt,
  870. ) >= 0 {
  871. var GFcpSeg Segment
  872. GFcpSeg.conv = conv
  873. GFcpSeg.cmd = cmd
  874. GFcpSeg.frg = frg
  875. GFcpSeg.wnd = wnd
  876. GFcpSeg.ts = ts
  877. GFcpSeg.sn = sn
  878. GFcpSeg.una = una
  879. GFcpSeg.data = data[:length]
  880. repeat = GFcp.parseData(
  881. GFcpSeg,
  882. )
  883. }
  884. }
  885. if regular && repeat {
  886. atomic.AddUint64(
  887. &DefaultSnsi.GFcpDupSegments,
  888. 1,
  889. )
  890. }
  891. } else if cmd == GfcpCmdWask {
  892. GFcp.probe |= GfcpAskTell
  893. //} else if cmd == GfcpCmdWins {
  894. // XXX(jhj) ??? FUCK YOU CHINKS
  895. } else {
  896. return -3
  897. }
  898. inSegs++
  899. data = data[length:]
  900. }
  901. atomic.AddUint64(
  902. &DefaultSnsi.GFcpInputSegments,
  903. inSegs,
  904. )
  905. if flag != 0 && regular {
  906. current := CurrentMs()
  907. if _itimediff(
  908. current,
  909. latest,
  910. ) >= 0 {
  911. GFcp.updateAck(
  912. _itimediff(
  913. current,
  914. latest,
  915. ),
  916. )
  917. }
  918. }
  919. if GFcp.nocwnd == 0 {
  920. if _itimediff(
  921. GFcp.sndUna,
  922. sndUna,
  923. ) > 0 {
  924. if GFcp.cwnd < GFcp.rmtWnd {
  925. mss := GFcp.mss
  926. if GFcp.cwnd < GFcp.ssthresh {
  927. GFcp.cwnd++
  928. GFcp.incr += mss
  929. } else {
  930. if GFcp.incr < mss {
  931. GFcp.incr = mss
  932. }
  933. GFcp.incr += (mss*mss)/GFcp.incr + (mss / 16)
  934. if (GFcp.cwnd+1)*mss <= GFcp.incr {
  935. GFcp.cwnd++
  936. }
  937. }
  938. if GFcp.cwnd > GFcp.rmtWnd {
  939. GFcp.cwnd = GFcp.rmtWnd
  940. GFcp.incr = GFcp.rmtWnd * mss
  941. }
  942. }
  943. }
  944. }
  945. if ackNoDelay && len(
  946. GFcp.acklist,
  947. ) > 0 {
  948. GFcp.Flush(
  949. true,
  950. )
  951. }
  952. return 0
  953. }
  954. func (
  955. GFcp *GFCP,
  956. ) wndUnused() uint16 {
  957. if len(
  958. GFcp.rcvQueue,
  959. ) < int(GFcp.rcvWnd) {
  960. return uint16(
  961. int(
  962. GFcp.rcvWnd,
  963. ) - len(
  964. GFcp.rcvQueue,
  965. ),
  966. )
  967. }
  968. return 0
  969. }
  970. // Flush ...
  971. func (
  972. GFcp *GFCP,
  973. ) Flush(
  974. ackOnly bool,
  975. ) uint32 {
  976. var GFcpSeg Segment
  977. GFcpSeg.conv = GFcp.conv
  978. GFcpSeg.cmd = GfcpCmdAck
  979. GFcpSeg.wnd = GFcp.wndUnused()
  980. GFcpSeg.una = GFcp.rcvNxt
  981. buffer := GFcp.buffer
  982. ptr := buffer[GFcp.reserved:]
  983. makeSpace := func(
  984. space int,
  985. ) {
  986. size := len(
  987. buffer,
  988. ) - len(
  989. ptr,
  990. )
  991. if size+space > int(
  992. GFcp.mtu,
  993. ) {
  994. GFcp.output(
  995. buffer,
  996. size,
  997. )
  998. ptr = buffer[GFcp.reserved:]
  999. }
  1000. }
  1001. FlushBuffer := func() {
  1002. size := len(
  1003. buffer,
  1004. ) - len(
  1005. ptr,
  1006. )
  1007. if size > GFcp.reserved {
  1008. GFcp.output(
  1009. buffer,
  1010. size,
  1011. )
  1012. }
  1013. }
  1014. for i, ack := range GFcp.acklist {
  1015. makeSpace(
  1016. GfcpOverhead,
  1017. )
  1018. if ack.sn >= GFcp.rcvNxt || len(
  1019. GFcp.acklist,
  1020. )-1 == i {
  1021. GFcpSeg.sn,
  1022. GFcpSeg.ts = ack.sn,
  1023. ack.ts
  1024. ptr = GFcpSeg.encode(
  1025. ptr,
  1026. )
  1027. }
  1028. }
  1029. GFcp.acklist = GFcp.acklist[0:0]
  1030. if ackOnly {
  1031. FlushBuffer()
  1032. return GFcp.interval
  1033. }
  1034. if GFcp.rmtWnd == 0 {
  1035. current := CurrentMs()
  1036. if GFcp.probeWait == 0 {
  1037. GFcp.probeWait = GfcpProbeInit
  1038. GFcp.tsProbe = current + GFcp.probeWait
  1039. } else if _itimediff(
  1040. current,
  1041. GFcp.tsProbe,
  1042. ) >= 0 {
  1043. if GFcp.probeWait < GfcpProbeInit {
  1044. GFcp.probeWait = GfcpProbeInit
  1045. }
  1046. GFcp.probeWait += GFcp.probeWait / 2
  1047. if GFcp.probeWait > GfcpProbeLimit {
  1048. GFcp.probeWait = GfcpProbeLimit
  1049. }
  1050. GFcp.tsProbe = current + GFcp.probeWait
  1051. GFcp.probe |= GfcpAskSend
  1052. }
  1053. }
  1054. GFcp.tsProbe = 0
  1055. GFcp.probeWait = 0
  1056. if (GFcp.probe & GfcpAskSend) != 0 {
  1057. GFcpSeg.cmd = GfcpCmdWask
  1058. makeSpace(
  1059. GfcpOverhead,
  1060. )
  1061. ptr = GFcpSeg.encode(
  1062. ptr,
  1063. )
  1064. }
  1065. if (GFcp.probe & GfcpAskTell) != 0 {
  1066. GFcpSeg.cmd = GfcpCmdWins
  1067. makeSpace(
  1068. GfcpOverhead,
  1069. )
  1070. ptr = GFcpSeg.encode(
  1071. ptr,
  1072. )
  1073. }
  1074. GFcp.probe = 0
  1075. cwnd := _imin(
  1076. GFcp.sndWnd,
  1077. GFcp.rmtWnd,
  1078. )
  1079. if GFcp.nocwnd == 0 {
  1080. cwnd = _imin(
  1081. GFcp.cwnd,
  1082. cwnd,
  1083. )
  1084. }
  1085. newSegsCount := 0
  1086. for k := range GFcp.sndQueue {
  1087. if _itimediff(
  1088. GFcp.sndNxt,
  1089. GFcp.sndUna+cwnd,
  1090. ) >= 0 {
  1091. break
  1092. }
  1093. newGFcpSeg := GFcp.sndQueue[k]
  1094. newGFcpSeg.conv = GFcp.conv
  1095. newGFcpSeg.cmd = GfcpCmdPush
  1096. newGFcpSeg.sn = GFcp.sndNxt
  1097. GFcp.SndBuf = append(
  1098. GFcp.SndBuf,
  1099. newGFcpSeg,
  1100. )
  1101. GFcp.sndNxt++
  1102. newSegsCount++
  1103. }
  1104. if newSegsCount > 0 {
  1105. GFcp.sndQueue = GFcp.removeFront(
  1106. GFcp.sndQueue,
  1107. newSegsCount,
  1108. )
  1109. }
  1110. resent := uint32(
  1111. GFcp.fastresend,
  1112. )
  1113. if GFcp.fastresend <= 0 {
  1114. resent = 0xFFFFFFFF
  1115. }
  1116. current := CurrentMs()
  1117. var change,
  1118. lost,
  1119. lostSegs,
  1120. fastGFcpRestransmittedSegments,
  1121. earlyGFcpRestransmittedSegments uint64
  1122. minrto := int32(
  1123. GFcp.interval,
  1124. )
  1125. ref := GFcp.SndBuf[:len(
  1126. GFcp.SndBuf,
  1127. )]
  1128. for k := range ref {
  1129. Segment := &ref[k]
  1130. needsend := false
  1131. if Segment.acked == 1 {
  1132. continue
  1133. }
  1134. if Segment.Kxmit == 0 {
  1135. needsend = true
  1136. Segment.rto = GFcp.rxRto
  1137. Segment.GFcpResendTs = current + Segment.rto
  1138. } else if _itimediff(
  1139. current,
  1140. Segment.GFcpResendTs,
  1141. ) >= 0 {
  1142. needsend = true
  1143. if GFcp.nodelay == 0 {
  1144. Segment.rto += GFcp.rxRto
  1145. } else {
  1146. Segment.rto += GFcp.rxRto / 2
  1147. }
  1148. Segment.GFcpResendTs = current + Segment.rto
  1149. lost++
  1150. lostSegs++
  1151. } else if Segment.fastack >= resent {
  1152. needsend = true
  1153. Segment.fastack = 0
  1154. Segment.rto = GFcp.rxRto
  1155. Segment.GFcpResendTs = current + Segment.rto
  1156. change++
  1157. fastGFcpRestransmittedSegments++
  1158. } else if Segment.fastack > 0 && newSegsCount == 0 {
  1159. needsend = true
  1160. Segment.fastack = 0
  1161. Segment.rto = GFcp.rxRto
  1162. Segment.GFcpResendTs = current + Segment.rto
  1163. change++
  1164. earlyGFcpRestransmittedSegments++
  1165. }
  1166. if needsend {
  1167. current = CurrentMs()
  1168. Segment.Kxmit++
  1169. Segment.ts = current
  1170. Segment.wnd = GFcpSeg.wnd
  1171. Segment.una = GFcpSeg.una
  1172. need := GfcpOverhead + len(
  1173. Segment.data,
  1174. )
  1175. makeSpace(
  1176. need,
  1177. )
  1178. ptr = Segment.encode(
  1179. ptr,
  1180. )
  1181. copy(
  1182. ptr,
  1183. Segment.data,
  1184. )
  1185. ptr = ptr[len(
  1186. Segment.data,
  1187. ):]
  1188. if Segment.Kxmit >= GFcp.deadLink {
  1189. GFcp.state = 0xFFFFFFFF
  1190. }
  1191. }
  1192. if rto := _itimediff(
  1193. Segment.GFcpResendTs,
  1194. current,
  1195. ); rto > 0 && rto < minrto {
  1196. minrto = rto
  1197. }
  1198. }
  1199. FlushBuffer()
  1200. sum := lostSegs
  1201. if lostSegs > 0 {
  1202. atomic.AddUint64(
  1203. &DefaultSnsi.GFcpLostSegments,
  1204. lostSegs,
  1205. )
  1206. }
  1207. if fastGFcpRestransmittedSegments > 0 {
  1208. atomic.AddUint64(
  1209. &DefaultSnsi.FastGFcpRestransmittedSegments,
  1210. fastGFcpRestransmittedSegments,
  1211. )
  1212. sum += fastGFcpRestransmittedSegments
  1213. }
  1214. if earlyGFcpRestransmittedSegments > 0 {
  1215. atomic.AddUint64(
  1216. &DefaultSnsi.EarlyGFcpRestransmittedSegments,
  1217. earlyGFcpRestransmittedSegments,
  1218. )
  1219. sum += earlyGFcpRestransmittedSegments
  1220. }
  1221. if sum > 0 {
  1222. atomic.AddUint64(
  1223. &DefaultSnsi.GFcpRestransmittedSegments,
  1224. sum,
  1225. )
  1226. }
  1227. if GFcp.nocwnd == 0 {
  1228. if change > 0 {
  1229. inflight := GFcp.sndNxt - GFcp.sndUna
  1230. GFcp.ssthresh = inflight / 2
  1231. if GFcp.ssthresh < GfcpThreshMin {
  1232. GFcp.ssthresh = GfcpThreshMin
  1233. }
  1234. GFcp.cwnd = GFcp.ssthresh + resent
  1235. GFcp.incr = GFcp.cwnd * GFcp.mss
  1236. }
  1237. if lost > 0 {
  1238. GFcp.ssthresh = cwnd / 2
  1239. if GFcp.ssthresh < GfcpThreshMin {
  1240. GFcp.ssthresh = GfcpThreshMin
  1241. }
  1242. GFcp.cwnd = 1
  1243. GFcp.incr = GFcp.mss
  1244. }
  1245. if GFcp.cwnd < 1 {
  1246. GFcp.cwnd = 1
  1247. GFcp.incr = GFcp.mss
  1248. }
  1249. }
  1250. return uint32(
  1251. minrto,
  1252. )
  1253. }
  1254. // Update is called repeatedly, 10ms to 100ms, queried via gfcp_check
  1255. // without gfcp_input or _send executing, returning timestamp in ms.
  1256. func (
  1257. GFcp *GFCP,
  1258. ) Update() {
  1259. var slap int32
  1260. current := CurrentMs()
  1261. if GFcp.updated == 0 {
  1262. GFcp.updated = 1
  1263. GFcp.tsFlush = current
  1264. }
  1265. slap = _itimediff(
  1266. current,
  1267. GFcp.tsFlush,
  1268. )
  1269. if slap >= 10000 || slap < -10000 {
  1270. GFcp.tsFlush = current
  1271. slap = 0
  1272. }
  1273. if slap >= 0 {
  1274. GFcp.tsFlush += GFcp.interval
  1275. if _itimediff(
  1276. current,
  1277. GFcp.tsFlush,
  1278. ) >= 0 {
  1279. GFcp.tsFlush = current + GFcp.interval
  1280. }
  1281. GFcp.Flush(
  1282. false,
  1283. )
  1284. }
  1285. }
  1286. // Check function helps determine when to invoke an gfcp_update.
  1287. // It returns when you should invoke gfcp_update, in milliseconds,
  1288. // if there is no gfcp_input or _send calling. You may repeatdly
  1289. // call gfcp_update instead of update, to reduce most unnacessary
  1290. // gfcp_update invocations. This function may be used to schedule
  1291. // gfcp_updates, when implementing an epoll-like mechanism, or for
  1292. // optimizing an gfcp_update loop handling massive GFcp connections.
  1293. func (
  1294. GFcp *GFCP,
  1295. ) Check() uint32 {
  1296. current := CurrentMs()
  1297. tsFlush := GFcp.tsFlush
  1298. tmFlush := int32(
  1299. math.MaxInt32,
  1300. )
  1301. tmPacket := int32(
  1302. math.MaxInt32,
  1303. )
  1304. minimal := uint32(
  1305. 0,
  1306. )
  1307. if GFcp.updated == 0 {
  1308. return current
  1309. }
  1310. if _itimediff(
  1311. current,
  1312. tsFlush,
  1313. ) >= 10000 ||
  1314. _itimediff(
  1315. current,
  1316. tsFlush,
  1317. ) < -10000 {
  1318. tsFlush = current
  1319. }
  1320. if _itimediff(
  1321. current,
  1322. tsFlush,
  1323. ) >= 0 {
  1324. return current
  1325. }
  1326. tmFlush = _itimediff(
  1327. tsFlush,
  1328. current,
  1329. )
  1330. for k := range GFcp.SndBuf {
  1331. GFcpSeg := &GFcp.SndBuf[k]
  1332. diff := _itimediff(
  1333. GFcpSeg.GFcpResendTs,
  1334. current,
  1335. )
  1336. if diff <= 0 {
  1337. return current
  1338. }
  1339. if diff < tmPacket {
  1340. tmPacket = diff
  1341. }
  1342. }
  1343. minimal = uint32(
  1344. tmPacket,
  1345. )
  1346. if tmPacket >= tmFlush {
  1347. minimal = uint32(
  1348. tmFlush,
  1349. )
  1350. }
  1351. if minimal >= GFcp.interval {
  1352. minimal = GFcp.interval
  1353. }
  1354. return current + minimal
  1355. }
  1356. // SetMtu changes MTU size.
  1357. func (
  1358. GFcp *GFCP,
  1359. ) SetMtu(
  1360. mtu int,
  1361. ) int {
  1362. if mtu < 50 || mtu < GfcpOverhead {
  1363. return -1
  1364. }
  1365. if GFcp.reserved >= int(
  1366. GFcp.mtu-GfcpOverhead,
  1367. ) || GFcp.reserved < 0 {
  1368. return -1
  1369. }
  1370. buffer := make(
  1371. []byte,
  1372. mtu,
  1373. )
  1374. /*if buffer == nil {
  1375. return -2
  1376. }*/ // XXX(jhj): buffer can't be nil?
  1377. GFcp.mtu = uint32(
  1378. mtu,
  1379. )
  1380. GFcp.mss = GFcp.mtu - GfcpOverhead - uint32(
  1381. GFcp.reserved,
  1382. )
  1383. GFcp.buffer = buffer
  1384. return 0
  1385. }
  1386. // NoDelay options:
  1387. // * fastest: gfcp_nodelay(GFcp, 1, 20, 2, 1)
  1388. // * nodelay: 0: disable (default), 1: enable
  1389. // * interval: internal update timer interval in milliseconds, defaults to 100ms
  1390. // * resend: 0: disable fast resends (default), 1: enable fast resends
  1391. // * nc: 0: normal congestion control (default), 1: disable congestion control
  1392. func (
  1393. GFcp *GFCP,
  1394. ) NoDelay(
  1395. nodelay,
  1396. interval,
  1397. resend,
  1398. nc int,
  1399. ) int {
  1400. if nodelay >= 0 {
  1401. GFcp.nodelay = uint32(
  1402. nodelay,
  1403. )
  1404. if nodelay != 0 {
  1405. GFcp.rxMinRto = GfcpRtoNdl
  1406. } else {
  1407. GFcp.rxMinRto = GfcpRtoMin
  1408. }
  1409. }
  1410. if interval >= 0 {
  1411. if interval > 5000 {
  1412. interval = 5000
  1413. } else if interval < 10 {
  1414. interval = 10
  1415. }
  1416. GFcp.interval = uint32(
  1417. interval,
  1418. )
  1419. }
  1420. if resend >= 0 {
  1421. GFcp.fastresend = int32(
  1422. resend,
  1423. )
  1424. }
  1425. if nc >= 0 {
  1426. GFcp.nocwnd = int32(
  1427. nc,
  1428. )
  1429. }
  1430. return 0
  1431. }
  1432. // WndSize sets maximum window size (efaults: sndwnd=32 and rcvwnd=32)
  1433. func (
  1434. GFcp *GFCP,
  1435. ) WndSize(
  1436. sndwnd,
  1437. rcvwnd int,
  1438. ) int {
  1439. if sndwnd > 0 {
  1440. GFcp.sndWnd = uint32(
  1441. sndwnd,
  1442. )
  1443. }
  1444. if rcvwnd > 0 {
  1445. GFcp.rcvWnd = uint32(
  1446. rcvwnd,
  1447. )
  1448. }
  1449. return 0
  1450. }
  1451. // WaitSnd shows how many packets are queued to be sent
  1452. func (
  1453. GFcp *GFCP,
  1454. ) WaitSnd() int {
  1455. return len(
  1456. GFcp.SndBuf,
  1457. ) + len(
  1458. GFcp.sndQueue,
  1459. )
  1460. }
  1461. func (
  1462. GFcp *GFCP,
  1463. ) removeFront(
  1464. q []Segment,
  1465. n int,
  1466. ) []Segment {
  1467. if n > cap(
  1468. q,
  1469. )/2 {
  1470. newn := copy(
  1471. q,
  1472. q[n:],
  1473. )
  1474. return q[:newn]
  1475. }
  1476. return q[n:]
  1477. }
  1478. func init() {
  1479. debug.SetGCPercent(
  1480. 180,
  1481. )
  1482. gfcpLegal.RegisterLicense(
  1483. "\nThe MIT License (MIT)\n\nCopyright © 2015 Daniel Fu <daniel820313@gmail.com>.\nCopyright © 2019 Loki 'l0k18' Verloren <stalker.loki@protonmail.ch>.\nCopyright © 2020 Gridfinity, LLC. <admin@gridfinity.com>.\nCopyright © 2020 Jeffrey H. Johnson <jeff@gridfinity.com>.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including, without limitation, the rights\nto use, copy, modify, merge, publish, distribute, sub-license, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice, and this permission notice, shall be\nincluded in all copies, or substantial portions, of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF, OR IN CONNECTION WITH THE SOFTWARE, OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n",
  1484. )
  1485. }