1
1
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

copy tcp writing into byte array

This commit is contained in:
Zack Scholl 2018-10-09 19:36:59 -07:00
parent b0164920c7
commit 9d2f07f478

View file

@ -33,8 +33,12 @@ func (c Comm) Close() {
}
func (c Comm) Write(b []byte) (int, error) {
n, err := c.connection.Write(append([]byte(fmt.Sprintf("%0.5d", len(b))), b...))
if n != len(b)+5 {
tmpCopy := make([]byte, len(b)+5)
// Copy the buffer so it doesn't get changed while read by the recipient.
copy(tmpCopy[:5], []byte(fmt.Sprintf("%0.5d", len(b))))
copy(tmpCopy[5:], b)
n, err := c.connection.Write(tmpCopy)
if n != len(tmpCopy) {
err = fmt.Errorf("wanted to write %d but wrote %d", len(b), n)
}
// log.Printf("wanted to write %d but wrote %d", n, len(b))