segment.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package kcp
  2. import (
  3. "sync"
  4. "github.com/v2ray/v2ray-core/common"
  5. "github.com/v2ray/v2ray-core/common/alloc"
  6. _ "github.com/v2ray/v2ray-core/common/log"
  7. "github.com/v2ray/v2ray-core/common/serial"
  8. )
  9. var (
  10. dataSegmentPool = &sync.Pool{
  11. New: func() interface{} { return new(DataSegment) },
  12. }
  13. ackSegmentPool = &sync.Pool{
  14. New: func() interface{} { return new(AckSegment) },
  15. }
  16. cmdSegmentPool = &sync.Pool{
  17. New: func() interface{} { return new(CmdOnlySegment) },
  18. }
  19. )
  20. type SegmentCommand byte
  21. const (
  22. SegmentCommandACK SegmentCommand = 0
  23. SegmentCommandData SegmentCommand = 1
  24. SegmentCommandTerminated SegmentCommand = 2
  25. SegmentCommandPing SegmentCommand = 3
  26. )
  27. type SegmentOption byte
  28. const (
  29. SegmentOptionClose SegmentOption = 1
  30. )
  31. type Segment interface {
  32. common.Releasable
  33. ByteSize() int
  34. Bytes([]byte) []byte
  35. }
  36. const (
  37. DataSegmentOverhead = 18
  38. )
  39. type DataSegment struct {
  40. Conv uint16
  41. Opt SegmentOption
  42. Timestamp uint32
  43. Number uint32
  44. SendingNext uint32
  45. Data *alloc.Buffer
  46. timeout uint32
  47. ackSkipped uint32
  48. transmit uint32
  49. }
  50. func NewDataSegment() *DataSegment {
  51. return dataSegmentPool.Get().(*DataSegment)
  52. }
  53. func (this *DataSegment) Bytes(b []byte) []byte {
  54. b = serial.Uint16ToBytes(this.Conv, b)
  55. b = append(b, byte(SegmentCommandData), byte(this.Opt))
  56. b = serial.Uint32ToBytes(this.Timestamp, b)
  57. b = serial.Uint32ToBytes(this.Number, b)
  58. b = serial.Uint32ToBytes(this.SendingNext, b)
  59. b = serial.Uint16ToBytes(uint16(this.Data.Len()), b)
  60. b = append(b, this.Data.Value...)
  61. return b
  62. }
  63. func (this *DataSegment) ByteSize() int {
  64. return 2 + 1 + 1 + 4 + 4 + 4 + 2 + this.Data.Len()
  65. }
  66. func (this *DataSegment) Release() {
  67. this.Data.Release()
  68. this.Data = nil
  69. this.Opt = 0
  70. this.timeout = 0
  71. this.ackSkipped = 0
  72. this.transmit = 0
  73. dataSegmentPool.Put(this)
  74. }
  75. type AckSegment struct {
  76. Conv uint16
  77. Opt SegmentOption
  78. ReceivingWindow uint32
  79. ReceivingNext uint32
  80. Count byte
  81. NumberList []uint32
  82. TimestampList []uint32
  83. }
  84. func NewAckSegment() *AckSegment {
  85. seg := ackSegmentPool.Get().(*AckSegment)
  86. if seg.NumberList == nil {
  87. seg.NumberList = make([]uint32, 0, 128)
  88. }
  89. if seg.TimestampList == nil {
  90. seg.TimestampList = make([]uint32, 0, 128)
  91. }
  92. return seg
  93. }
  94. func (this *AckSegment) ByteSize() int {
  95. return 2 + 1 + 1 + 4 + 4 + 1 + int(this.Count)*4 + int(this.Count)*4
  96. }
  97. func (this *AckSegment) Bytes(b []byte) []byte {
  98. b = serial.Uint16ToBytes(this.Conv, b)
  99. b = append(b, byte(SegmentCommandACK), byte(this.Opt))
  100. b = serial.Uint32ToBytes(this.ReceivingWindow, b)
  101. b = serial.Uint32ToBytes(this.ReceivingNext, b)
  102. b = append(b, this.Count)
  103. for i := byte(0); i < this.Count; i++ {
  104. b = serial.Uint32ToBytes(this.NumberList[i], b)
  105. b = serial.Uint32ToBytes(this.TimestampList[i], b)
  106. }
  107. return b
  108. }
  109. func (this *AckSegment) Release() {
  110. this.Opt = 0
  111. this.Count = 0
  112. this.NumberList = this.NumberList[:0]
  113. this.TimestampList = this.TimestampList[:0]
  114. ackSegmentPool.Put(this)
  115. }
  116. type CmdOnlySegment struct {
  117. Conv uint16
  118. Cmd SegmentCommand
  119. Opt SegmentOption
  120. SendingNext uint32
  121. ReceivinNext uint32
  122. }
  123. func NewCmdOnlySegment() *CmdOnlySegment {
  124. return cmdSegmentPool.Get().(*CmdOnlySegment)
  125. }
  126. func (this *CmdOnlySegment) ByteSize() int {
  127. return 2 + 1 + 1 + 4 + 4
  128. }
  129. func (this *CmdOnlySegment) Bytes(b []byte) []byte {
  130. b = serial.Uint16ToBytes(this.Conv, b)
  131. b = append(b, byte(this.Cmd), byte(this.Opt))
  132. b = serial.Uint32ToBytes(this.SendingNext, b)
  133. b = serial.Uint32ToBytes(this.ReceivinNext, b)
  134. return b
  135. }
  136. func (this *CmdOnlySegment) Release() {
  137. this.Opt = 0
  138. cmdSegmentPool.Put(this)
  139. }
  140. func ReadSegment(buf []byte) (Segment, []byte) {
  141. if len(buf) <= 4 {
  142. return nil, nil
  143. }
  144. conv := serial.BytesToUint16(buf)
  145. buf = buf[2:]
  146. cmd := SegmentCommand(buf[0])
  147. opt := SegmentOption(buf[1])
  148. buf = buf[2:]
  149. if cmd == SegmentCommandData {
  150. seg := &DataSegment{
  151. Conv: conv,
  152. Opt: opt,
  153. }
  154. if len(buf) < 16 {
  155. return nil, nil
  156. }
  157. seg.Timestamp = serial.BytesToUint32(buf)
  158. buf = buf[4:]
  159. seg.Number = serial.BytesToUint32(buf)
  160. buf = buf[4:]
  161. seg.SendingNext = serial.BytesToUint32(buf)
  162. buf = buf[4:]
  163. dataLen := int(serial.BytesToUint16(buf))
  164. buf = buf[2:]
  165. if len(buf) < dataLen {
  166. return nil, nil
  167. }
  168. seg.Data = alloc.NewSmallBuffer().Clear().Append(buf[:dataLen])
  169. buf = buf[dataLen:]
  170. return seg, buf
  171. }
  172. if cmd == SegmentCommandACK {
  173. seg := &AckSegment{
  174. Conv: conv,
  175. Opt: opt,
  176. }
  177. if len(buf) < 9 {
  178. return nil, nil
  179. }
  180. seg.ReceivingWindow = serial.BytesToUint32(buf)
  181. buf = buf[4:]
  182. seg.ReceivingNext = serial.BytesToUint32(buf)
  183. buf = buf[4:]
  184. seg.Count = buf[0]
  185. buf = buf[1:]
  186. seg.NumberList = make([]uint32, 0, seg.Count)
  187. seg.TimestampList = make([]uint32, 0, seg.Count)
  188. if len(buf) < int(seg.Count)*8 {
  189. return nil, nil
  190. }
  191. for i := 0; i < int(seg.Count); i++ {
  192. seg.NumberList = append(seg.NumberList, serial.BytesToUint32(buf))
  193. seg.TimestampList = append(seg.TimestampList, serial.BytesToUint32(buf[4:]))
  194. buf = buf[8:]
  195. }
  196. return seg, buf
  197. }
  198. seg := &CmdOnlySegment{
  199. Conv: conv,
  200. Cmd: cmd,
  201. Opt: opt,
  202. }
  203. if len(buf) < 8 {
  204. return nil, nil
  205. }
  206. seg.SendingNext = serial.BytesToUint32(buf)
  207. buf = buf[4:]
  208. seg.ReceivinNext = serial.BytesToUint32(buf)
  209. buf = buf[4:]
  210. return seg, buf
  211. }