Skip to content

Commit 08674a0

Browse files
committed
Fix the resend body length and checksum issue
1 parent f3b56b7 commit 08674a0

3 files changed

Lines changed: 12 additions & 10 deletions

File tree

message.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ func formatCheckSum(value int) string {
588588

589589
// Build constructs a []byte from a Message instance.
590590
func (m *Message) build() []byte {
591-
m.cook()
591+
m.cook(m.Body.length(), m.Body.total())
592592

593593
var b bytes.Buffer
594594
m.Header.write(&b)
@@ -603,7 +603,7 @@ func (m *Message) build() []byte {
603603
// This func lets us pull the Message from the Store, parse it, update the Header, and then build it back into bytes using the original Body.
604604
// Note: The only standard non-Body group is NoHops. If that is used in the Header, this workaround may fail.
605605
func (m *Message) buildWithBodyBytes(bodyBytes []byte) []byte {
606-
m.cook()
606+
m.cook(len(bodyBytes), bytesTotal(bodyBytes))
607607

608608
var b bytes.Buffer
609609
m.Header.write(&b)
@@ -612,9 +612,9 @@ func (m *Message) buildWithBodyBytes(bodyBytes []byte) []byte {
612612
return b.Bytes()
613613
}
614614

615-
func (m *Message) cook() {
616-
bodyLength := m.Header.length() + m.Body.length() + m.Trailer.length()
615+
func (m *Message) cook(bodyLen, bodyTotal int) {
616+
bodyLength := m.Header.length() + bodyLen + m.Trailer.length()
617617
m.Header.SetInt(tagBodyLength, bodyLength)
618-
checkSum := (m.Header.total() + m.Body.total() + m.Trailer.total()) % 256
618+
checkSum := (m.Header.total() + bodyTotal + m.Trailer.total()) % 256
619619
m.Trailer.SetString(tagCheckSum, formatCheckSum(checkSum))
620620
}

tag_value.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,15 @@ func (tv TagValue) String() string {
8686
return string(tv.bytes)
8787
}
8888

89-
func (tv TagValue) total() int {
90-
total := 0
91-
92-
for _, b := range []byte(tv.bytes) {
89+
func bytesTotal(bytes []byte) (total int) {
90+
for _, b := range bytes {
9391
total += int(b)
9492
}
93+
return
94+
}
9595

96-
return total
96+
func (tv TagValue) total() int {
97+
return bytesTotal(tv.bytes)
9798
}
9899

99100
func (tv TagValue) length() int {

tag_value_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,5 @@ func TestTagValue_total(t *testing.T) {
8686
var tv TagValue
8787
require.Nil(t, tv.parse([]byte(stringField)))
8888
assert.Equal(t, 643, tv.total(), "Total is the summation of the ascii byte values of the field string")
89+
assert.Equal(t, 643, bytesTotal([]byte(stringField)))
8990
}

0 commit comments

Comments
 (0)