This commit is contained in:
ChillerDragon 2024-06-17 12:24:08 +08:00
parent 4605d3907c
commit 309062c7d3
4 changed files with 24 additions and 29 deletions

View file

@ -1,12 +1,12 @@
package chunk
const (
chunkFlagVital = 1
chunkFlagVital = 1
chunkFlagResend = 2
)
type ChunkFlags struct {
Vital bool
Vital bool
Resend bool
}
@ -15,22 +15,21 @@ type ChunkHeader struct {
Size int
// sequence number
// will be acknowledged in the packet header ack
Seq int
Seq int
}
type Chunk struct {
Header ChunkHeader
Data []byte
Data []byte
}
func (header *ChunkHeader) Unpack(data []byte) {
flagBits := (data[0] >> 6) & 0x03
header.Flags.Vital = (flagBits & chunkFlagVital) != 0
header.Flags.Resend = (flagBits & chunkFlagResend) != 0
header.Size = (int(data[0] & 0x3F) << 6) | (int(data[1]) & 0x3F)
header.Size = (int(data[0]&0x3F) << 6) | (int(data[1]) & 0x3F)
if header.Flags.Vital {
header.Seq = int((data[1] & 0xC0) << 2) | int(data[2])
header.Seq = int((data[1]&0xC0)<<2) | int(data[2])
}
}

View file

@ -14,13 +14,13 @@ func TestBrokenNonVitalHeader(t *testing.T) {
// {0x40, 0x3a, 0x01}
header.Unpack([]byte{0x3a, 0x01})
want := ChunkHeader {
Flags: ChunkFlags {
Vital: false,
want := ChunkHeader{
Flags: ChunkFlags{
Vital: false,
Resend: false,
},
Size: 3713,
Seq: 0,
Seq: 0,
}
if !reflect.DeepEqual(header, want) {
@ -35,13 +35,13 @@ func TestVitalHeaderMapChange(t *testing.T) {
header := ChunkHeader{}
header.Unpack([]byte{0x40, 0x3a, 0x01})
want := ChunkHeader {
Flags: ChunkFlags {
Vital: true,
want := ChunkHeader{
Flags: ChunkFlags{
Vital: true,
Resend: false,
},
Size: 58,
Seq: 1,
Seq: 1,
}
if !reflect.DeepEqual(header, want) {
@ -52,17 +52,16 @@ func TestVitalHeaderMapChange(t *testing.T) {
func TestVitalHeader(t *testing.T) {
header := ChunkHeader{}
header.Unpack([]byte{0x40, 0x10, 0x0a})
want := ChunkHeader {
Flags: ChunkFlags {
Vital: true,
want := ChunkHeader{
Flags: ChunkFlags{
Vital: true,
Resend: false,
},
Size: 16,
Seq: 10,
Seq: 10,
}
if !reflect.DeepEqual(header, want) {
t.Errorf("got %v, wanted %v", header, want)
}
}

View file

@ -17,7 +17,7 @@ func UnpackChunks(data []byte) []Chunk {
i++
}
end := i + chunk.Header.Size
chunk.Data = make([]byte, end - i)
chunk.Data = make([]byte, end-i)
copy(chunk.Data[:], data[i:end])
i += chunk.Header.Size
chunks = append(chunks, chunk)
@ -25,4 +25,3 @@ func UnpackChunks(data []byte) []Chunk {
return chunks
}

View file

@ -33,7 +33,7 @@ func TestSplitterMapChange(t *testing.T) {
// 0x00, 0x01, 0x01, 0x01, 0x02, 0x03, 0x04,
// }
payload := []byte {
payload := []byte{
0x40, 0x3a, 0x01, 0x05, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x5f, 0x70, 0x69, 0x63, 0x6b, 0x75, 0x70,
0x73, 0x00, 0x91, 0xa7, 0xf2, 0xa0, 0x05, 0x8b, 0x11, 0x08, 0xa8, 0x15, 0xa9, 0x19, 0x38, 0xaf, 0xfd,
0xb6, 0xcb, 0xf1, 0xdb, 0x5a, 0xfc, 0x73, 0x34, 0xae, 0xa3, 0x68, 0xa7, 0xfa, 0x35, 0x54, 0x37, 0xf9,
@ -122,15 +122,14 @@ func TestSplitter(t *testing.T) {
}
}
{
want := ChunkHeader {
Flags: ChunkFlags {
Vital: true,
want := ChunkHeader{
Flags: ChunkFlags{
Vital: true,
Resend: false,
},
Size: 1,
Seq: 5,
Seq: 5,
}
if !reflect.DeepEqual(chunks[0].Header, want) {
@ -138,4 +137,3 @@ func TestSplitter(t *testing.T) {
}
}
}