You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/kcp/segment.go

242 lines
4.7 KiB

9 years ago
package kcp
import (
"github.com/v2ray/v2ray-core/common"
9 years ago
"github.com/v2ray/v2ray-core/common/alloc"
_ "github.com/v2ray/v2ray-core/common/log"
9 years ago
"github.com/v2ray/v2ray-core/common/serial"
)
8 years ago
type Command byte
9 years ago
const (
8 years ago
CommandACK Command = 0
CommandData Command = 1
CommandTerminate Command = 2
CommandPing Command = 3
9 years ago
)
type SegmentOption byte
const (
SegmentOptionClose SegmentOption = 1
)
type Segment interface {
common.Releasable
9 years ago
ByteSize() int
Bytes([]byte) []byte
}
9 years ago
const (
DataSegmentOverhead = 18
)
9 years ago
type DataSegment struct {
Conv uint16
8 years ago
Option SegmentOption
Timestamp uint32
Number uint32
SendingNext uint32
Data *alloc.Buffer
9 years ago
timeout uint32
ackSkipped uint32
transmit uint32
}
func NewDataSegment() *DataSegment {
return new(DataSegment)
}
9 years ago
func (this *DataSegment) Bytes(b []byte) []byte {
b = serial.Uint16ToBytes(this.Conv, b)
8 years ago
b = append(b, byte(CommandData), byte(this.Option))
9 years ago
b = serial.Uint32ToBytes(this.Timestamp, b)
b = serial.Uint32ToBytes(this.Number, b)
b = serial.Uint32ToBytes(this.SendingNext, b)
9 years ago
b = serial.Uint16ToBytes(uint16(this.Data.Len()), b)
b = append(b, this.Data.Value...)
return b
}
func (this *DataSegment) ByteSize() int {
return 2 + 1 + 1 + 4 + 4 + 4 + 2 + this.Data.Len()
9 years ago
}
func (this *DataSegment) Release() {
this.Data.Release()
this.Data = nil
}
type AckSegment struct {
9 years ago
Conv uint16
8 years ago
Option SegmentOption
9 years ago
ReceivingWindow uint32
ReceivingNext uint32
Timestamp uint32
9 years ago
Count byte
NumberList []uint32
}
func NewAckSegment() *AckSegment {
return new(AckSegment)
}
func (this *AckSegment) PutTimestamp(timestamp uint32) {
if timestamp-this.Timestamp < 0x7FFFFFFF {
this.Timestamp = timestamp
}
}
func (this *AckSegment) PutNumber(number uint32) {
this.Count++
this.NumberList = append(this.NumberList, number)
}
func (this *AckSegment) IsFull() bool {
return this.Count == 128
}
func (this *AckSegment) ByteSize() int {
return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int(this.Count)*4
9 years ago
}
func (this *AckSegment) Bytes(b []byte) []byte {
9 years ago
b = serial.Uint16ToBytes(this.Conv, b)
8 years ago
b = append(b, byte(CommandACK), byte(this.Option))
9 years ago
b = serial.Uint32ToBytes(this.ReceivingWindow, b)
b = serial.Uint32ToBytes(this.ReceivingNext, b)
b = serial.Uint32ToBytes(this.Timestamp, b)
9 years ago
b = append(b, this.Count)
for i := byte(0); i < this.Count; i++ {
b = serial.Uint32ToBytes(this.NumberList[i], b)
}
return b
}
func (this *AckSegment) Release() {
this.NumberList = nil
}
type CmdOnlySegment struct {
Conv uint16
8 years ago
Command Command
Option SegmentOption
SendingNext uint32
ReceivinNext uint32
PeerRTO uint32
9 years ago
}
func NewCmdOnlySegment() *CmdOnlySegment {
return new(CmdOnlySegment)
}
func (this *CmdOnlySegment) ByteSize() int {
return 2 + 1 + 1 + 4 + 4 + 4
9 years ago
}
func (this *CmdOnlySegment) Bytes(b []byte) []byte {
9 years ago
b = serial.Uint16ToBytes(this.Conv, b)
8 years ago
b = append(b, byte(this.Command), byte(this.Option))
b = serial.Uint32ToBytes(this.SendingNext, b)
b = serial.Uint32ToBytes(this.ReceivinNext, b)
b = serial.Uint32ToBytes(this.PeerRTO, b)
9 years ago
return b
}
func (this *CmdOnlySegment) Release() {
}
func ReadSegment(buf []byte) (Segment, []byte) {
if len(buf) <= 4 {
9 years ago
return nil, nil
}
conv := serial.BytesToUint16(buf)
buf = buf[2:]
8 years ago
cmd := Command(buf[0])
9 years ago
opt := SegmentOption(buf[1])
buf = buf[2:]
8 years ago
if cmd == CommandData {
seg := NewDataSegment()
seg.Conv = conv
8 years ago
seg.Option = opt
if len(buf) < 16 {
return nil, nil
}
9 years ago
seg.Timestamp = serial.BytesToUint32(buf)
buf = buf[4:]
seg.Number = serial.BytesToUint32(buf)
buf = buf[4:]
seg.SendingNext = serial.BytesToUint32(buf)
9 years ago
buf = buf[4:]
dataLen := int(serial.BytesToUint16(buf))
9 years ago
buf = buf[2:]
if len(buf) < dataLen {
return nil, nil
}
seg.Data = AllocateBuffer().Clear().Append(buf[:dataLen])
buf = buf[dataLen:]
9 years ago
return seg, buf
}
8 years ago
if cmd == CommandACK {
seg := NewAckSegment()
seg.Conv = conv
8 years ago
seg.Option = opt
if len(buf) < 13 {
return nil, nil
}
9 years ago
seg.ReceivingWindow = serial.BytesToUint32(buf)
buf = buf[4:]
seg.ReceivingNext = serial.BytesToUint32(buf)
9 years ago
buf = buf[4:]
seg.Timestamp = serial.BytesToUint32(buf)
buf = buf[4:]
count := int(buf[0])
9 years ago
buf = buf[1:]
if len(buf) < count*4 {
return nil, nil
}
for i := 0; i < count; i++ {
seg.PutNumber(serial.BytesToUint32(buf))
buf = buf[4:]
9 years ago
}
return seg, buf
}
seg := NewCmdOnlySegment()
seg.Conv = conv
8 years ago
seg.Command = cmd
seg.Option = opt
9 years ago
if len(buf) < 12 {
return nil, nil
}
seg.SendingNext = serial.BytesToUint32(buf)
buf = buf[4:]
seg.ReceivinNext = serial.BytesToUint32(buf)
buf = buf[4:]
seg.PeerRTO = serial.BytesToUint32(buf)
buf = buf[4:]
return seg, buf
9 years ago
}