segment.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package kcp
  2. import (
  3. "github.com/v2ray/v2ray-core/common"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. _ "github.com/v2ray/v2ray-core/common/log"
  6. "github.com/v2ray/v2ray-core/common/serial"
  7. )
  8. type SegmentCommand byte
  9. const (
  10. SegmentCommandACK SegmentCommand = 0
  11. SegmentCommandData SegmentCommand = 1
  12. SegmentCommandTerminated SegmentCommand = 2
  13. SegmentCommandPing SegmentCommand = 3
  14. )
  15. type SegmentOption byte
  16. const (
  17. SegmentOptionClose SegmentOption = 1
  18. )
  19. type ISegment interface {
  20. common.Releasable
  21. ByteSize() int
  22. Bytes([]byte) []byte
  23. }
  24. type DataSegment struct {
  25. Conv uint16
  26. Opt SegmentOption
  27. Timestamp uint32
  28. Number uint32
  29. SendingNext uint32
  30. Data *alloc.Buffer
  31. timeout uint32
  32. ackSkipped uint32
  33. transmit uint32
  34. }
  35. func (this *DataSegment) Bytes(b []byte) []byte {
  36. b = serial.Uint16ToBytes(this.Conv, b)
  37. b = append(b, byte(SegmentCommandData), byte(this.Opt))
  38. b = serial.Uint32ToBytes(this.Timestamp, b)
  39. b = serial.Uint32ToBytes(this.Number, b)
  40. b = serial.Uint32ToBytes(this.SendingNext, b)
  41. b = serial.Uint16ToBytes(uint16(this.Data.Len()), b)
  42. b = append(b, this.Data.Value...)
  43. return b
  44. }
  45. func (this *DataSegment) ByteSize() int {
  46. return 2 + 1 + 1 + 4 + 4 + 4 + 2 + this.Data.Len()
  47. }
  48. func (this *DataSegment) Release() {
  49. this.Data.Release()
  50. }
  51. type ACKSegment struct {
  52. Conv uint16
  53. Opt SegmentOption
  54. ReceivingWindow uint32
  55. ReceivingNext uint32
  56. Count byte
  57. NumberList []uint32
  58. TimestampList []uint32
  59. }
  60. func (this *ACKSegment) ByteSize() int {
  61. return 2 + 1 + 1 + 4 + 4 + 1 + int(this.Count)*4 + int(this.Count)*4
  62. }
  63. func (this *ACKSegment) Bytes(b []byte) []byte {
  64. b = serial.Uint16ToBytes(this.Conv, b)
  65. b = append(b, byte(SegmentCommandACK), byte(this.Opt))
  66. b = serial.Uint32ToBytes(this.ReceivingWindow, b)
  67. b = serial.Uint32ToBytes(this.ReceivingNext, b)
  68. b = append(b, this.Count)
  69. for i := byte(0); i < this.Count; i++ {
  70. b = serial.Uint32ToBytes(this.NumberList[i], b)
  71. b = serial.Uint32ToBytes(this.TimestampList[i], b)
  72. }
  73. return b
  74. }
  75. func (this *ACKSegment) Release() {}
  76. type CmdOnlySegment struct {
  77. Conv uint16
  78. Cmd SegmentCommand
  79. Opt SegmentOption
  80. SendingNext uint32
  81. ReceivinNext uint32
  82. }
  83. func (this *CmdOnlySegment) ByteSize() int {
  84. return 2 + 1 + 1 + 4 + 4
  85. }
  86. func (this *CmdOnlySegment) Bytes(b []byte) []byte {
  87. b = serial.Uint16ToBytes(this.Conv, b)
  88. b = append(b, byte(this.Cmd), byte(this.Opt))
  89. b = serial.Uint32ToBytes(this.SendingNext, b)
  90. b = serial.Uint32ToBytes(this.ReceivinNext, b)
  91. return b
  92. }
  93. func (this *CmdOnlySegment) Release() {}
  94. func ReadSegment(buf []byte) (ISegment, []byte) {
  95. if len(buf) <= 6 {
  96. return nil, nil
  97. }
  98. conv := serial.BytesToUint16(buf)
  99. buf = buf[2:]
  100. cmd := SegmentCommand(buf[0])
  101. opt := SegmentOption(buf[1])
  102. buf = buf[2:]
  103. if cmd == SegmentCommandData {
  104. seg := &DataSegment{
  105. Conv: conv,
  106. Opt: opt,
  107. }
  108. seg.Timestamp = serial.BytesToUint32(buf)
  109. buf = buf[4:]
  110. seg.Number = serial.BytesToUint32(buf)
  111. buf = buf[4:]
  112. seg.SendingNext = serial.BytesToUint32(buf)
  113. buf = buf[4:]
  114. len := serial.BytesToUint16(buf)
  115. buf = buf[2:]
  116. seg.Data = alloc.NewSmallBuffer().Clear().Append(buf[:len])
  117. buf = buf[len:]
  118. return seg, buf
  119. }
  120. if cmd == SegmentCommandACK {
  121. seg := &ACKSegment{
  122. Conv: conv,
  123. Opt: opt,
  124. }
  125. seg.ReceivingWindow = serial.BytesToUint32(buf)
  126. buf = buf[4:]
  127. seg.ReceivingNext = serial.BytesToUint32(buf)
  128. buf = buf[4:]
  129. seg.Count = buf[0]
  130. buf = buf[1:]
  131. seg.NumberList = make([]uint32, 0, seg.Count)
  132. seg.TimestampList = make([]uint32, 0, seg.Count)
  133. for i := 0; i < int(seg.Count); i++ {
  134. seg.NumberList = append(seg.NumberList, serial.BytesToUint32(buf))
  135. seg.TimestampList = append(seg.TimestampList, serial.BytesToUint32(buf[4:]))
  136. buf = buf[8:]
  137. }
  138. return seg, buf
  139. }
  140. seg := &CmdOnlySegment{
  141. Conv: conv,
  142. Cmd: cmd,
  143. Opt: opt,
  144. }
  145. seg.SendingNext = serial.BytesToUint32(buf)
  146. buf = buf[4:]
  147. seg.ReceivinNext = serial.BytesToUint32(buf)
  148. buf = buf[4:]
  149. return seg, buf
  150. }