Skip to content

Commit

Permalink
use number of bytes returned from read call
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Mar 29, 2019
1 parent d410952 commit da18d26
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions internal/peerconn/peerwriter/peerwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,19 @@ func (p *PeerWriter) messageWriter() {
// Reserve space for length and message ID
buf.Write([]byte{0, 0, 0, 0, 0})

var m int64
if wt, ok := msg.(io.WriterTo); ok {
_, err = wt.WriteTo(buf)
m, err = wt.WriteTo(buf)
} else {
_, err = buf.ReadFrom(msg)
m, err = buf.ReadFrom(msg)
}
if err != nil {
p.log.Errorf("cannot serialize message [%v]: %s", msg.ID(), err.Error())
return
}

// Put length
binary.BigEndian.PutUint32(buf.Bytes()[:4], uint32(1+buf.Len()-5))
binary.BigEndian.PutUint32(buf.Bytes()[:4], uint32(1+m))
// Put message ID
buf.Bytes()[4] = uint8(msg.ID())

Expand Down

0 comments on commit da18d26

Please sign in to comment.