Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions transport/internet/kcp/receiving.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ type AckList struct {

flushCandidates []uint32
dirty bool

mss uint32
}

func NewAckList(writer SegmentWriter) *AckList {
func NewAckList(writer SegmentWriter, mss uint32) *AckList {
return &AckList{
writer: writer,
timestamps: make([]uint32, 0, 128),
numbers: make([]uint32, 0, 128),
nextFlush: make([]uint32, 0, 128),
flushCandidates: make([]uint32, 0, 128),
mss: mss,
}
}

Expand Down Expand Up @@ -90,7 +93,7 @@ func (l *AckList) Clear(una uint32) {
func (l *AckList) Flush(current uint32, rto uint32) {
l.flushCandidates = l.flushCandidates[:0]

seg := NewAckSegment()
seg := NewAckSegment((int(l.mss) - 17) / 4)
for i := 0; i < len(l.numbers); i++ {
if l.nextFlush[i] > current {
if len(l.flushCandidates) < cap(l.flushCandidates) {
Expand All @@ -109,7 +112,7 @@ func (l *AckList) Flush(current uint32, rto uint32) {
if seg.IsFull() {
l.writer.Write(seg)
seg.Release()
seg = NewAckSegment()
seg = NewAckSegment((int(l.mss) - 17) / 4)
l.dirty = false
}
}
Expand Down Expand Up @@ -144,7 +147,7 @@ func NewReceivingWorker(kcp *Connection) *ReceivingWorker {
window: NewReceivingWindow(),
windowSize: kcp.Config.GetReceivingInFlightSize(),
}
worker.acklist = NewAckList(worker)
worker.acklist = NewAckList(worker, kcp.mss+DataSegmentOverhead)
return worker
}

Expand Down
18 changes: 14 additions & 4 deletions transport/internet/kcp/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,22 @@ type AckSegment struct {
ReceivingNext uint32
Timestamp uint32
NumberList []uint32

Limit int
}

const ackNumberLimit = 128

func NewAckSegment() *AckSegment {
return new(AckSegment)
func NewAckSegment(limit int) *AckSegment {
if limit <= 0 {
limit = 1
}
if limit > ackNumberLimit {
limit = ackNumberLimit
}
return &AckSegment{
Limit: limit,
}
}

func (s *AckSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
Expand Down Expand Up @@ -188,7 +198,7 @@ func (s *AckSegment) PutNumber(number uint32) {
}

func (s *AckSegment) IsFull() bool {
return len(s.NumberList) == ackNumberLimit
return len(s.NumberList) == s.Limit
}

func (s *AckSegment) IsEmpty() bool {
Expand Down Expand Up @@ -290,7 +300,7 @@ func ReadSegment(buf []byte) (Segment, []byte) {
case CommandData:
seg = NewDataSegment()
case CommandACK:
seg = NewAckSegment()
seg = NewAckSegment(128)
default:
seg = NewCmdOnlySegment()
}
Expand Down
1 change: 1 addition & 0 deletions transport/internet/kcp/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestACKSegment(t *testing.T) {
ReceivingNext: 3,
Timestamp: 10,
NumberList: []uint32{1, 3, 5, 7, 9},
Limit: 128,
}

nBytes := seg.ByteSize()
Expand Down