1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-08-26 22:34:20 -04:00
v2fly/transport/internet/kcp/segment.go

210 lines
4.3 KiB
Go
Raw Normal View History

2016-06-27 16:22:01 -04:00
package kcp
import (
2016-06-27 16:54:59 -04:00
"github.com/v2ray/v2ray-core/common"
2016-06-27 16:22:01 -04:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-06-29 04:34:34 -04:00
_ "github.com/v2ray/v2ray-core/common/log"
2016-06-27 16:22:01 -04:00
"github.com/v2ray/v2ray-core/common/serial"
)
type SegmentCommand byte
const (
SegmentCommandACK SegmentCommand = 0
SegmentCommandData SegmentCommand = 1
SegmentCommandTerminated SegmentCommand = 2
2016-06-29 04:34:34 -04:00
SegmentCommandPing SegmentCommand = 3
2016-06-27 16:22:01 -04:00
)
type SegmentOption byte
const (
SegmentOptionClose SegmentOption = 1
)
type ISegment interface {
2016-06-27 16:54:59 -04:00
common.Releasable
2016-06-27 16:22:01 -04:00
ByteSize() int
Bytes([]byte) []byte
}
2016-06-29 06:52:23 -04:00
const (
DataSegmentOverhead = 18
)
2016-06-27 16:22:01 -04:00
type DataSegment struct {
2016-06-29 04:34:34 -04:00
Conv uint16
Opt SegmentOption
Timestamp uint32
Number uint32
SendingNext uint32
Data *alloc.Buffer
2016-06-27 16:22:01 -04:00
timeout uint32
ackSkipped uint32
transmit uint32
}
func (this *DataSegment) Bytes(b []byte) []byte {
b = serial.Uint16ToBytes(this.Conv, b)
b = append(b, byte(SegmentCommandData), byte(this.Opt))
b = serial.Uint32ToBytes(this.Timestamp, b)
b = serial.Uint32ToBytes(this.Number, b)
2016-06-29 04:34:34 -04:00
b = serial.Uint32ToBytes(this.SendingNext, b)
2016-06-27 16:22:01 -04:00
b = serial.Uint16ToBytes(uint16(this.Data.Len()), b)
b = append(b, this.Data.Value...)
return b
}
func (this *DataSegment) ByteSize() int {
2016-06-29 04:34:34 -04:00
return 2 + 1 + 1 + 4 + 4 + 4 + 2 + this.Data.Len()
2016-06-27 16:22:01 -04:00
}
2016-06-27 16:54:59 -04:00
func (this *DataSegment) Release() {
this.Data.Release()
}
2016-07-02 16:17:41 -04:00
type AckSegment struct {
2016-06-27 16:22:01 -04:00
Conv uint16
Opt SegmentOption
ReceivingWindow uint32
2016-06-29 04:34:34 -04:00
ReceivingNext uint32
2016-06-27 16:22:01 -04:00
Count byte
NumberList []uint32
TimestampList []uint32
}
2016-07-02 16:17:41 -04:00
func (this *AckSegment) ByteSize() int {
2016-06-29 04:34:34 -04:00
return 2 + 1 + 1 + 4 + 4 + 1 + int(this.Count)*4 + int(this.Count)*4
2016-06-27 16:22:01 -04:00
}
2016-07-02 16:17:41 -04:00
func (this *AckSegment) Bytes(b []byte) []byte {
2016-06-27 16:22:01 -04:00
b = serial.Uint16ToBytes(this.Conv, b)
b = append(b, byte(SegmentCommandACK), byte(this.Opt))
b = serial.Uint32ToBytes(this.ReceivingWindow, b)
2016-06-29 04:34:34 -04:00
b = serial.Uint32ToBytes(this.ReceivingNext, b)
2016-06-27 16:22:01 -04:00
b = append(b, this.Count)
for i := byte(0); i < this.Count; i++ {
b = serial.Uint32ToBytes(this.NumberList[i], b)
b = serial.Uint32ToBytes(this.TimestampList[i], b)
}
return b
}
2016-07-02 16:17:41 -04:00
func (this *AckSegment) Release() {}
2016-06-27 16:54:59 -04:00
2016-06-29 04:34:34 -04:00
type CmdOnlySegment struct {
Conv uint16
Cmd SegmentCommand
Opt SegmentOption
SendingNext uint32
ReceivinNext uint32
2016-06-27 16:22:01 -04:00
}
2016-06-29 04:34:34 -04:00
func (this *CmdOnlySegment) ByteSize() int {
return 2 + 1 + 1 + 4 + 4
2016-06-27 16:22:01 -04:00
}
2016-06-29 04:34:34 -04:00
func (this *CmdOnlySegment) Bytes(b []byte) []byte {
2016-06-27 16:22:01 -04:00
b = serial.Uint16ToBytes(this.Conv, b)
2016-06-29 04:34:34 -04:00
b = append(b, byte(this.Cmd), byte(this.Opt))
b = serial.Uint32ToBytes(this.SendingNext, b)
b = serial.Uint32ToBytes(this.ReceivinNext, b)
2016-06-27 16:22:01 -04:00
return b
}
2016-06-29 04:34:34 -04:00
func (this *CmdOnlySegment) Release() {}
2016-06-27 16:54:59 -04:00
2016-06-27 16:22:01 -04:00
func ReadSegment(buf []byte) (ISegment, []byte) {
2016-06-29 18:12:36 -04:00
if len(buf) <= 4 {
2016-06-27 16:22:01 -04:00
return nil, nil
}
conv := serial.BytesToUint16(buf)
buf = buf[2:]
cmd := SegmentCommand(buf[0])
opt := SegmentOption(buf[1])
buf = buf[2:]
if cmd == SegmentCommandData {
seg := &DataSegment{
Conv: conv,
Opt: opt,
}
2016-06-29 18:12:36 -04:00
if len(buf) < 16 {
return nil, nil
}
2016-06-27 16:22:01 -04:00
seg.Timestamp = serial.BytesToUint32(buf)
buf = buf[4:]
seg.Number = serial.BytesToUint32(buf)
buf = buf[4:]
2016-06-29 04:34:34 -04:00
seg.SendingNext = serial.BytesToUint32(buf)
2016-06-27 16:22:01 -04:00
buf = buf[4:]
2016-06-29 18:12:36 -04:00
dataLen := int(serial.BytesToUint16(buf))
2016-06-27 16:22:01 -04:00
buf = buf[2:]
2016-06-29 18:12:36 -04:00
if len(buf) < dataLen {
return nil, nil
}
seg.Data = alloc.NewSmallBuffer().Clear().Append(buf[:dataLen])
buf = buf[dataLen:]
2016-06-27 16:22:01 -04:00
return seg, buf
}
if cmd == SegmentCommandACK {
2016-07-02 16:17:41 -04:00
seg := &AckSegment{
2016-06-27 16:22:01 -04:00
Conv: conv,
Opt: opt,
}
2016-06-29 18:12:36 -04:00
if len(buf) < 9 {
return nil, nil
}
2016-06-27 16:22:01 -04:00
seg.ReceivingWindow = serial.BytesToUint32(buf)
buf = buf[4:]
2016-06-29 04:34:34 -04:00
seg.ReceivingNext = serial.BytesToUint32(buf)
2016-06-27 16:22:01 -04:00
buf = buf[4:]
seg.Count = buf[0]
buf = buf[1:]
seg.NumberList = make([]uint32, 0, seg.Count)
seg.TimestampList = make([]uint32, 0, seg.Count)
2016-06-29 18:12:36 -04:00
if len(buf) < int(seg.Count)*8 {
return nil, nil
}
2016-06-27 16:22:01 -04:00
for i := 0; i < int(seg.Count); i++ {
seg.NumberList = append(seg.NumberList, serial.BytesToUint32(buf))
seg.TimestampList = append(seg.TimestampList, serial.BytesToUint32(buf[4:]))
buf = buf[8:]
}
return seg, buf
}
2016-06-29 04:34:34 -04:00
seg := &CmdOnlySegment{
Conv: conv,
Cmd: cmd,
Opt: opt,
2016-06-27 16:22:01 -04:00
}
2016-06-29 18:12:36 -04:00
if len(buf) < 8 {
return nil, nil
}
2016-06-29 04:34:34 -04:00
seg.SendingNext = serial.BytesToUint32(buf)
buf = buf[4:]
seg.ReceivinNext = serial.BytesToUint32(buf)
buf = buf[4:]
return seg, buf
2016-06-27 16:22:01 -04:00
}