mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-10 07:11:08 +00:00
lib/discover, lib/protocol: Buffer allocation
This commit is contained in:
parent
4e2a9bb139
commit
eafb40460d
@ -114,19 +114,18 @@ func (c *localClient) announcementPkt(instanceID int64, msg []byte) ([]byte, boo
|
|||||||
return msg, false
|
return msg, false
|
||||||
}
|
}
|
||||||
|
|
||||||
if cap(msg) >= 4 {
|
|
||||||
msg = msg[:4]
|
|
||||||
} else {
|
|
||||||
msg = make([]byte, 4)
|
|
||||||
}
|
|
||||||
binary.BigEndian.PutUint32(msg, Magic)
|
|
||||||
|
|
||||||
pkt := Announce{
|
pkt := Announce{
|
||||||
ID: c.myID,
|
ID: c.myID,
|
||||||
Addresses: addrs,
|
Addresses: addrs,
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
}
|
}
|
||||||
bs, _ := pkt.Marshal()
|
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...)
|
msg = append(msg, bs...)
|
||||||
|
|
||||||
return msg, true
|
return msg, true
|
||||||
|
@ -90,7 +90,7 @@ func writeHello(c io.Writer, h HelloIntf) error {
|
|||||||
panic("bug: attempting to serialize too large hello message")
|
panic("bug: attempting to serialize too large hello message")
|
||||||
}
|
}
|
||||||
|
|
||||||
header := make([]byte, 6)
|
header := make([]byte, 6, 6+len(msg))
|
||||||
binary.BigEndian.PutUint32(header[:4], h.Magic())
|
binary.BigEndian.PutUint32(header[:4], h.Magic())
|
||||||
binary.BigEndian.PutUint16(header[4:], uint16(len(msg)))
|
binary.BigEndian.PutUint16(header[4:], uint16(len(msg)))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user