mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-29 23:33:37 +00:00
network: write messages atomically
Right now message can be written in several Write's so concurrent calls of writeMsg() can in theory interleave. This commit fixes it. Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
394af5f777
commit
e3098ed0f8
1 changed files with 8 additions and 2 deletions
|
@ -63,8 +63,14 @@ func (p *TCPPeer) writeMsg(msg *Message) error {
|
||||||
case err := <-p.done:
|
case err := <-p.done:
|
||||||
return err
|
return err
|
||||||
default:
|
default:
|
||||||
w := io.NewBinWriterFromIO(p.conn)
|
w := io.NewBufBinWriter()
|
||||||
return msg.Encode(w)
|
if err := msg.Encode(w.BinWriter); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := p.conn.Write(w.Bytes())
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue