segment.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/serial"
  6. )
  7. type SegmentCommand byte
  8. const (
  9. SegmentCommandACK SegmentCommand = 0
  10. SegmentCommandData SegmentCommand = 1
  11. SegmentCommandTerminated SegmentCommand = 2
  12. )
  13. type SegmentOption byte
  14. const (
  15. SegmentOptionClose SegmentOption = 1
  16. )
  17. type ISegment interface {
  18. common.Releasable
  19. ByteSize() int
  20. Bytes([]byte) []byte
  21. }
  22. type DataSegment struct {
  23. Conv uint16
  24. Opt SegmentOption
  25. ReceivingWindow uint32
  26. Timestamp uint32
  27. Number uint32
  28. Unacknowledged uint32
  29. Data *alloc.Buffer
  30. timeout uint32
  31. ackSkipped uint32
  32. transmit uint32
  33. }
  34. func (this *DataSegment) Bytes(b []byte) []byte {
  35. b = serial.Uint16ToBytes(this.Conv, b)
  36. b = append(b, byte(SegmentCommandData), byte(this.Opt))
  37. b = serial.Uint32ToBytes(this.ReceivingWindow, b)
  38. b = serial.Uint32ToBytes(this.Timestamp, b)
  39. b = serial.Uint32ToBytes(this.Number, b)
  40. b = serial.Uint32ToBytes(this.Unacknowledged, 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 + 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. Unacknowledged 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 + len(this.NumberList)*4 + len(this.TimestampList)*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.Unacknowledged, 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 TerminationSegment struct {
  77. Conv uint16
  78. Opt SegmentOption
  79. }
  80. func (this *TerminationSegment) ByteSize() int {
  81. return 2 + 1 + 1
  82. }
  83. func (this *TerminationSegment) Bytes(b []byte) []byte {
  84. b = serial.Uint16ToBytes(this.Conv, b)
  85. b = append(b, byte(SegmentCommandTerminated), byte(this.Opt))
  86. return b
  87. }
  88. func (this *TerminationSegment) Release() {}
  89. func ReadSegment(buf []byte) (ISegment, []byte) {
  90. if len(buf) <= 12 {
  91. return nil, nil
  92. }
  93. conv := serial.BytesToUint16(buf)
  94. buf = buf[2:]
  95. cmd := SegmentCommand(buf[0])
  96. opt := SegmentOption(buf[1])
  97. buf = buf[2:]
  98. if cmd == SegmentCommandData {
  99. seg := &DataSegment{
  100. Conv: conv,
  101. Opt: opt,
  102. }
  103. seg.ReceivingWindow = serial.BytesToUint32(buf)
  104. buf = buf[4:]
  105. seg.Timestamp = serial.BytesToUint32(buf)
  106. buf = buf[4:]
  107. seg.Number = serial.BytesToUint32(buf)
  108. buf = buf[4:]
  109. seg.Unacknowledged = serial.BytesToUint32(buf)
  110. buf = buf[4:]
  111. len := serial.BytesToUint16(buf)
  112. buf = buf[2:]
  113. seg.Data = alloc.NewSmallBuffer().Clear().Append(buf[:len])
  114. buf = buf[len:]
  115. return seg, buf
  116. }
  117. if cmd == SegmentCommandACK {
  118. seg := &ACKSegment{
  119. Conv: conv,
  120. Opt: opt,
  121. }
  122. seg.ReceivingWindow = serial.BytesToUint32(buf)
  123. buf = buf[4:]
  124. seg.Unacknowledged = serial.BytesToUint32(buf)
  125. buf = buf[4:]
  126. seg.Count = buf[0]
  127. buf = buf[1:]
  128. seg.NumberList = make([]uint32, 0, seg.Count)
  129. seg.TimestampList = make([]uint32, 0, seg.Count)
  130. for i := 0; i < int(seg.Count); i++ {
  131. seg.NumberList = append(seg.NumberList, serial.BytesToUint32(buf))
  132. seg.TimestampList = append(seg.TimestampList, serial.BytesToUint32(buf[4:]))
  133. buf = buf[8:]
  134. }
  135. return seg, buf
  136. }
  137. if cmd == SegmentCommandTerminated {
  138. return &TerminationSegment{
  139. Conv: conv,
  140. Opt: opt,
  141. }, buf
  142. }
  143. return nil, nil
  144. }