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

306 lines
5.9 KiB

// +build !confonly
9 years ago
package kcp
import (
"encoding/binary"
"v2ray.com/core/common/buf"
9 years ago
)
8 years ago
// Command is a KCP command that indicate the purpose of a Segment.
8 years ago
type Command byte
9 years ago
const (
7 years ago
// CommandACK indicates an AckSegment.
8 years ago
CommandACK Command = 0
// CommandData indicates a DataSegment.
CommandData Command = 1
// CommandTerminate indicates that peer terminates the connection.
8 years ago
CommandTerminate Command = 2
8 years ago
// CommandPing indicates a ping.
CommandPing Command = 3
9 years ago
)
type SegmentOption byte
const (
SegmentOptionClose SegmentOption = 1
)
type Segment interface {
Release()
Conversation() uint16
Command() Command
ByteSize() int32
Serialize([]byte)
parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte)
9 years ago
}
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
9 years ago
payload *buf.Buffer
timeout uint32
transmit uint32
9 years ago
}
func NewDataSegment() *DataSegment {
return new(DataSegment)
}
func (s *DataSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
s.Conv = conv
s.Option = opt
if len(buf) < 15 {
return false, nil
}
s.Timestamp = binary.BigEndian.Uint32(buf)
buf = buf[4:]
s.Number = binary.BigEndian.Uint32(buf)
buf = buf[4:]
s.SendingNext = binary.BigEndian.Uint32(buf)
buf = buf[4:]
dataLen := int(binary.BigEndian.Uint16(buf))
buf = buf[2:]
if len(buf) < dataLen {
return false, nil
}
s.Data().Clear()
s.Data().Write(buf[:dataLen])
buf = buf[dataLen:]
return true, buf
}
7 years ago
func (s *DataSegment) Conversation() uint16 {
return s.Conv
}
7 years ago
func (*DataSegment) Command() Command {
return CommandData
}
7 years ago
func (s *DataSegment) Detach() *buf.Buffer {
r := s.payload
s.payload = nil
return r
}
7 years ago
func (s *DataSegment) Data() *buf.Buffer {
if s.payload == nil {
s.payload = buf.New()
}
7 years ago
return s.payload
}
func (s *DataSegment) Serialize(b []byte) {
binary.BigEndian.PutUint16(b, s.Conv)
b[2] = byte(CommandData)
b[3] = byte(s.Option)
binary.BigEndian.PutUint32(b[4:], s.Timestamp)
binary.BigEndian.PutUint32(b[8:], s.Number)
binary.BigEndian.PutUint32(b[12:], s.SendingNext)
binary.BigEndian.PutUint16(b[16:], uint16(s.payload.Len()))
copy(b[18:], s.payload.Bytes())
9 years ago
}
func (s *DataSegment) ByteSize() int32 {
7 years ago
return 2 + 1 + 1 + 4 + 4 + 4 + 2 + s.payload.Len()
9 years ago
}
7 years ago
func (s *DataSegment) Release() {
s.payload.Release()
s.payload = 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
NumberList []uint32
}
const ackNumberLimit = 128
func NewAckSegment() *AckSegment {
return new(AckSegment)
}
func (s *AckSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
s.Conv = conv
s.Option = opt
if len(buf) < 13 {
return false, nil
}
s.ReceivingWindow = binary.BigEndian.Uint32(buf)
buf = buf[4:]
s.ReceivingNext = binary.BigEndian.Uint32(buf)
buf = buf[4:]
s.Timestamp = binary.BigEndian.Uint32(buf)
buf = buf[4:]
count := int(buf[0])
buf = buf[1:]
if len(buf) < count*4 {
return false, nil
}
for i := 0; i < count; i++ {
s.PutNumber(binary.BigEndian.Uint32(buf))
buf = buf[4:]
}
return true, buf
}
7 years ago
func (s *AckSegment) Conversation() uint16 {
return s.Conv
}
7 years ago
func (*AckSegment) Command() Command {
return CommandACK
}
7 years ago
func (s *AckSegment) PutTimestamp(timestamp uint32) {
if timestamp-s.Timestamp < 0x7FFFFFFF {
s.Timestamp = timestamp
}
}
7 years ago
func (s *AckSegment) PutNumber(number uint32) {
s.NumberList = append(s.NumberList, number)
}
7 years ago
func (s *AckSegment) IsFull() bool {
return len(s.NumberList) == ackNumberLimit
}
7 years ago
func (s *AckSegment) IsEmpty() bool {
return len(s.NumberList) == 0
}
func (s *AckSegment) ByteSize() int32 {
return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int32(len(s.NumberList)*4)
9 years ago
}
func (s *AckSegment) Serialize(b []byte) {
binary.BigEndian.PutUint16(b, s.Conv)
b[2] = byte(CommandACK)
b[3] = byte(s.Option)
binary.BigEndian.PutUint32(b[4:], s.ReceivingWindow)
binary.BigEndian.PutUint32(b[8:], s.ReceivingNext)
binary.BigEndian.PutUint32(b[12:], s.Timestamp)
b[16] = byte(len(s.NumberList))
n := 17
for _, number := range s.NumberList {
binary.BigEndian.PutUint32(b[n:], number)
n += 4
9 years ago
}
}
func (s *AckSegment) Release() {}
type CmdOnlySegment struct {
7 years ago
Conv uint16
Cmd Command
Option SegmentOption
SendingNext uint32
ReceivingNext uint32
PeerRTO uint32
9 years ago
}
func NewCmdOnlySegment() *CmdOnlySegment {
return new(CmdOnlySegment)
}
func (s *CmdOnlySegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
s.Conv = conv
s.Cmd = cmd
s.Option = opt
if len(buf) < 12 {
return false, nil
}
s.SendingNext = binary.BigEndian.Uint32(buf)
buf = buf[4:]
s.ReceivingNext = binary.BigEndian.Uint32(buf)
buf = buf[4:]
s.PeerRTO = binary.BigEndian.Uint32(buf)
buf = buf[4:]
return true, buf
}
7 years ago
func (s *CmdOnlySegment) Conversation() uint16 {
return s.Conv
}
7 years ago
func (s *CmdOnlySegment) Command() Command {
return s.Cmd
}
func (*CmdOnlySegment) ByteSize() int32 {
return 2 + 1 + 1 + 4 + 4 + 4
9 years ago
}
func (s *CmdOnlySegment) Serialize(b []byte) {
binary.BigEndian.PutUint16(b, s.Conv)
b[2] = byte(s.Cmd)
b[3] = byte(s.Option)
binary.BigEndian.PutUint32(b[4:], s.SendingNext)
binary.BigEndian.PutUint32(b[8:], s.ReceivingNext)
binary.BigEndian.PutUint32(b[12:], s.PeerRTO)
9 years ago
}
7 years ago
func (*CmdOnlySegment) Release() {}
func ReadSegment(buf []byte) (Segment, []byte) {
if len(buf) < 4 {
9 years ago
return nil, nil
}
conv := binary.BigEndian.Uint16(buf)
9 years ago
buf = buf[2:]
8 years ago
cmd := Command(buf[0])
9 years ago
opt := SegmentOption(buf[1])
buf = buf[2:]
var seg Segment
switch cmd {
case CommandData:
seg = NewDataSegment()
case CommandACK:
seg = NewAckSegment()
default:
seg = NewCmdOnlySegment()
9 years ago
}
valid, extra := seg.parse(conv, cmd, opt, buf)
if !valid {
return nil, nil
}
return seg, extra
9 years ago
}