Skip to content

Commit

Permalink
optimize: add HeaderLen field
Browse files Browse the repository at this point in the history
  • Loading branch information
jayantxie committed Aug 15, 2024
1 parent 4a81f2a commit 29b4982
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion protocol/ttheader/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type DecodeParam struct {
// You can refer to metakey.go for more details.
StrInfo map[string]string

// HeaderLen is used to set up header length of a request/response.
HeaderLen int

// PayloadLen is used to set up payload length of a request/response.
PayloadLen int
}
Expand Down Expand Up @@ -112,7 +115,8 @@ func Decode(ctx context.Context, in bufiox.Reader) (param DecodeParam, err error
return
}

param.PayloadLen = int(totalLen - uint32(headerInfoSize) + Size32 - TTHeaderMetaSize)
param.HeaderLen = int(uint32(headerInfoSize) + TTHeaderMetaSize)
param.PayloadLen = int(totalLen) + Size32 - param.HeaderLen
return
}

Expand Down
12 changes: 8 additions & 4 deletions protocol/ttheader/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestEncodeToBytes(t *testing.T) {
if err != nil {
t.Fatalf("encode to bytes failed, %s", err.Error())
}
checkParamEqual(t, encodeParam, decodeParam)
checkParamEqual(t, encodeParam, decodeParam, len(buf), 0)
}

func TestEncode(t *testing.T) {
Expand Down Expand Up @@ -110,12 +110,13 @@ func TestEncode(t *testing.T) {
t.Fatalf("encode failed, %s", err.Error())
}
binary.BigEndian.PutUint32(totalLenField, uint32(bw.WrittenLen()-4))
headerLen := bw.WrittenLen()
bw.Flush()
wg.Wait()
checkParamEqual(t, encodeParam, decodeParam)
checkParamEqual(t, encodeParam, decodeParam, headerLen, 0)
}

func checkParamEqual(t *testing.T, encodeParam EncodeParam, decodeParam DecodeParam) {
func checkParamEqual(t *testing.T, encodeParam EncodeParam, decodeParam DecodeParam, headerLen, payloadLen int) {
if decodeParam.Flags != encodeParam.Flags {
t.Fatalf("encode to bytes failed, flags not equal")
}
Expand All @@ -131,7 +132,10 @@ func checkParamEqual(t *testing.T, encodeParam EncodeParam, decodeParam DecodePa
if !reflect.DeepEqual(decodeParam.StrInfo, encodeParam.StrInfo) {
t.Fatalf("encode to bytes failed, str info not equal")
}
if decodeParam.PayloadLen != 0 {
if decodeParam.HeaderLen != headerLen {
t.Fatalf("encode to bytes failed, header len not equal")
}
if decodeParam.PayloadLen != payloadLen {
t.Fatalf("encode to bytes failed, payload len not equal")
}
}

0 comments on commit 29b4982

Please sign in to comment.