diff --git a/transport/internet/kcp/segment.go b/transport/internet/kcp/segment.go index 772f4280..7e8930cb 100644 --- a/transport/internet/kcp/segment.go +++ b/transport/internet/kcp/segment.go @@ -213,7 +213,7 @@ func ReadSegment(buf []byte) (Segment, []byte) { seg := NewDataSegment() seg.Conv = conv seg.Option = opt - if len(buf) < 16 { + if len(buf) < 15 { return nil, nil } seg.Timestamp = serial.BytesToUint32(buf) diff --git a/transport/internet/kcp/segment_test.go b/transport/internet/kcp/segment_test.go index 5be25495..c573c159 100644 --- a/transport/internet/kcp/segment_test.go +++ b/transport/internet/kcp/segment_test.go @@ -44,6 +44,34 @@ func TestDataSegment(t *testing.T) { assert.Bytes(seg2.Data.Bytes()).Equals(seg.Data.Bytes()) } +func Test1ByteDataSegment(t *testing.T) { + assert := assert.On(t) + + b := buf.NewLocal(512) + b.AppendBytes('a') + seg := &DataSegment{ + Conv: 1, + Timestamp: 3, + Number: 4, + SendingNext: 5, + Data: b, + } + + nBytes := seg.ByteSize() + bytes := make([]byte, nBytes) + seg.Bytes()(bytes) + + assert.Int(len(bytes)).Equals(nBytes) + + iseg, _ := ReadSegment(bytes) + seg2 := iseg.(*DataSegment) + assert.Uint16(seg2.Conv).Equals(seg.Conv) + assert.Uint32(seg2.Timestamp).Equals(seg.Timestamp) + assert.Uint32(seg2.SendingNext).Equals(seg.SendingNext) + assert.Uint32(seg2.Number).Equals(seg.Number) + assert.Bytes(seg2.Data.Bytes()).Equals(seg.Data.Bytes()) +} + func TestACKSegment(t *testing.T) { assert := assert.On(t)