segment.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 Command byte
  9. const (
  10. CommandACK Command = 0
  11. CommandData Command = 1
  12. CommandTerminate Command = 2
  13. CommandPing Command = 3
  14. )
  15. type SegmentOption byte
  16. const (
  17. SegmentOptionClose SegmentOption = 1
  18. )
  19. type Segment interface {
  20. common.Releasable
  21. ByteSize() int
  22. Bytes([]byte) []byte
  23. }
  24. const (
  25. DataSegmentOverhead = 18
  26. )
  27. type DataSegment struct {
  28. Conv uint16
  29. Option SegmentOption
  30. Timestamp uint32
  31. Number uint32
  32. SendingNext uint32
  33. Data *alloc.Buffer
  34. timeout uint32
  35. ackSkipped uint32
  36. transmit uint32
  37. }
  38. func NewDataSegment() *DataSegment {
  39. return new(DataSegment)
  40. }
  41. func (this *DataSegment) Bytes(b []byte) []byte {
  42. b = serial.Uint16ToBytes(this.Conv, b)
  43. b = append(b, byte(CommandData), byte(this.Option))
  44. b = serial.Uint32ToBytes(this.Timestamp, b)
  45. b = serial.Uint32ToBytes(this.Number, b)
  46. b = serial.Uint32ToBytes(this.SendingNext, b)
  47. b = serial.Uint16ToBytes(uint16(this.Data.Len()), b)
  48. b = append(b, this.Data.Value...)
  49. return b
  50. }
  51. func (this *DataSegment) ByteSize() int {
  52. return 2 + 1 + 1 + 4 + 4 + 4 + 2 + this.Data.Len()
  53. }
  54. func (this *DataSegment) Release() {
  55. this.Data.Release()
  56. this.Data = nil
  57. }
  58. type AckSegment struct {
  59. Conv uint16
  60. Option SegmentOption
  61. ReceivingWindow uint32
  62. ReceivingNext uint32
  63. Count byte
  64. NumberList []uint32
  65. TimestampList []uint32
  66. }
  67. func NewAckSegment() *AckSegment {
  68. return new(AckSegment)
  69. }
  70. func (this *AckSegment) PutNumber(number uint32, timestamp uint32) {
  71. this.Count++
  72. this.NumberList = append(this.NumberList, number)
  73. this.TimestampList = append(this.TimestampList, timestamp)
  74. }
  75. func (this *AckSegment) IsFull() bool {
  76. return this.Count == 128
  77. }
  78. func (this *AckSegment) ByteSize() int {
  79. return 2 + 1 + 1 + 4 + 4 + 1 + int(this.Count)*4 + int(this.Count)*4
  80. }
  81. func (this *AckSegment) Bytes(b []byte) []byte {
  82. b = serial.Uint16ToBytes(this.Conv, b)
  83. b = append(b, byte(CommandACK), byte(this.Option))
  84. b = serial.Uint32ToBytes(this.ReceivingWindow, b)
  85. b = serial.Uint32ToBytes(this.ReceivingNext, b)
  86. b = append(b, this.Count)
  87. for i := byte(0); i < this.Count; i++ {
  88. b = serial.Uint32ToBytes(this.NumberList[i], b)
  89. b = serial.Uint32ToBytes(this.TimestampList[i], b)
  90. }
  91. return b
  92. }
  93. func (this *AckSegment) Release() {
  94. this.NumberList = nil
  95. this.TimestampList = nil
  96. }
  97. type CmdOnlySegment struct {
  98. Conv uint16
  99. Command Command
  100. Option SegmentOption
  101. SendingNext uint32
  102. ReceivinNext uint32
  103. }
  104. func NewCmdOnlySegment() *CmdOnlySegment {
  105. return new(CmdOnlySegment)
  106. }
  107. func (this *CmdOnlySegment) ByteSize() int {
  108. return 2 + 1 + 1 + 4 + 4
  109. }
  110. func (this *CmdOnlySegment) Bytes(b []byte) []byte {
  111. b = serial.Uint16ToBytes(this.Conv, b)
  112. b = append(b, byte(this.Command), byte(this.Option))
  113. b = serial.Uint32ToBytes(this.SendingNext, b)
  114. b = serial.Uint32ToBytes(this.ReceivinNext, b)
  115. return b
  116. }
  117. func (this *CmdOnlySegment) Release() {
  118. }
  119. func ReadSegment(buf []byte) (Segment, []byte) {
  120. if len(buf) <= 4 {
  121. return nil, nil
  122. }
  123. conv := serial.BytesToUint16(buf)
  124. buf = buf[2:]
  125. cmd := Command(buf[0])
  126. opt := SegmentOption(buf[1])
  127. buf = buf[2:]
  128. if cmd == CommandData {
  129. seg := NewDataSegment()
  130. seg.Conv = conv
  131. seg.Option = opt
  132. if len(buf) < 16 {
  133. return nil, nil
  134. }
  135. seg.Timestamp = serial.BytesToUint32(buf)
  136. buf = buf[4:]
  137. seg.Number = serial.BytesToUint32(buf)
  138. buf = buf[4:]
  139. seg.SendingNext = serial.BytesToUint32(buf)
  140. buf = buf[4:]
  141. dataLen := int(serial.BytesToUint16(buf))
  142. buf = buf[2:]
  143. if len(buf) < dataLen {
  144. return nil, nil
  145. }
  146. seg.Data = alloc.NewSmallBuffer().Clear().Append(buf[:dataLen])
  147. buf = buf[dataLen:]
  148. return seg, buf
  149. }
  150. if cmd == CommandACK {
  151. seg := NewAckSegment()
  152. seg.Conv = conv
  153. seg.Option = opt
  154. if len(buf) < 9 {
  155. return nil, nil
  156. }
  157. seg.ReceivingWindow = serial.BytesToUint32(buf)
  158. buf = buf[4:]
  159. seg.ReceivingNext = serial.BytesToUint32(buf)
  160. buf = buf[4:]
  161. count := int(buf[0])
  162. buf = buf[1:]
  163. if len(buf) < count*8 {
  164. return nil, nil
  165. }
  166. for i := 0; i < count; i++ {
  167. seg.PutNumber(serial.BytesToUint32(buf), serial.BytesToUint32(buf[4:]))
  168. buf = buf[8:]
  169. }
  170. return seg, buf
  171. }
  172. seg := NewCmdOnlySegment()
  173. seg.Conv = conv
  174. seg.Command = cmd
  175. seg.Option = opt
  176. if len(buf) < 8 {
  177. return nil, nil
  178. }
  179. seg.SendingNext = serial.BytesToUint32(buf)
  180. buf = buf[4:]
  181. seg.ReceivinNext = serial.BytesToUint32(buf)
  182. buf = buf[4:]
  183. return seg, buf
  184. }