diff options
author | Jeffrey Wilcke <obscuren@users.noreply.github.com> | 2014-12-15 17:22:35 +0800 |
---|---|---|
committer | Jeffrey Wilcke <obscuren@users.noreply.github.com> | 2014-12-15 17:22:35 +0800 |
commit | 15e46b97b46864f7f72b932c0fe586ea6d223406 (patch) | |
tree | 83b08df50a2978cf518aae1bf70738ecd82ed4be /p2p/peer.go | |
parent | ccb4fc8d0370afd85ea9ffca635dae7e792dd66d (diff) | |
parent | da900f94358a9b293a286066b0922a6f7b5d571c (diff) | |
download | go-tangerine-15e46b97b46864f7f72b932c0fe586ea6d223406.tar go-tangerine-15e46b97b46864f7f72b932c0fe586ea6d223406.tar.gz go-tangerine-15e46b97b46864f7f72b932c0fe586ea6d223406.tar.bz2 go-tangerine-15e46b97b46864f7f72b932c0fe586ea6d223406.tar.lz go-tangerine-15e46b97b46864f7f72b932c0fe586ea6d223406.tar.xz go-tangerine-15e46b97b46864f7f72b932c0fe586ea6d223406.tar.zst go-tangerine-15e46b97b46864f7f72b932c0fe586ea6d223406.zip |
Merge pull request #204 from fjl/fix-p2p-disconnect
p2p: fix decoding of disconnect reason
Diffstat (limited to 'p2p/peer.go')
-rw-r--r-- | p2p/peer.go | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/p2p/peer.go b/p2p/peer.go index 893ba86d7..86c4d7ab5 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -300,7 +300,7 @@ func (p *Peer) dispatch(msg Msg, protoDone chan struct{}) (wait bool, err error) proto.in <- msg } else { wait = true - pr := &eofSignal{msg.Payload, protoDone} + pr := &eofSignal{msg.Payload, int64(msg.Size), protoDone} msg.Payload = pr proto.in <- msg } @@ -438,18 +438,25 @@ func (rw *proto) ReadMsg() (Msg, error) { return msg, nil } -// eofSignal wraps a reader with eof signaling. -// the eof channel is closed when the wrapped reader -// reaches EOF. +// eofSignal wraps a reader with eof signaling. the eof channel is +// closed when the wrapped reader returns an error or when count bytes +// have been read. +// type eofSignal struct { wrapped io.Reader + count int64 eof chan<- struct{} } +// note: when using eofSignal to detect whether a message payload +// has been read, Read might not be called for zero sized messages. + func (r *eofSignal) Read(buf []byte) (int, error) { n, err := r.wrapped.Read(buf) - if err != nil { + r.count -= int64(n) + if (err != nil || r.count <= 0) && r.eof != nil { r.eof <- struct{}{} // tell Peer that msg has been consumed + r.eof = nil } return n, err } |