lib/discover, lib/protocol: Buffer allocation

This commit is contained in:
greatroar
2021-09-06 15:30:56 +02:00
committed by Jakob Borg
parent 4e2a9bb139
commit eafb40460d
2 changed files with 7 additions and 8 deletions
+6 -7
View File
@@ -114,19 +114,18 @@ func (c *localClient) announcementPkt(instanceID int64, msg []byte) ([]byte, boo
return msg, false
}
if cap(msg) >= 4 {
msg = msg[:4]
} else {
msg = make([]byte, 4)
}
binary.BigEndian.PutUint32(msg, Magic)
pkt := Announce{
ID: c.myID,
Addresses: addrs,
InstanceID: instanceID,
}
bs, _ := pkt.Marshal()
if pktLen := 4 + len(bs); cap(msg) < pktLen {
msg = make([]byte, 0, pktLen)
}
msg = msg[:4]
binary.BigEndian.PutUint32(msg, Magic)
msg = append(msg, bs...)
return msg, true