mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
remove peer.writeMsg
- remove it, its redundant - remove duplicate code in peer.writeProtoMsg, now falling back to proto.WriteMsg - remove peer.writeMu, no need for mutex - no timer set in p.WriteMsg, - write will be async for the protocol anyway, so no write timeout argument anywhere, only constant set for conn writeDeadline
This commit is contained in:
parent
7a33109e85
commit
b29b7fedd0
2 changed files with 4 additions and 31 deletions
|
|
@ -143,27 +143,7 @@ func (p *Peer) writeProtoMsg(protoName string, msg Msg) error {
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("protocol %s not handled by peer", protoName)
|
return fmt.Errorf("protocol %s not handled by peer", protoName)
|
||||||
}
|
}
|
||||||
if msg.Code >= proto.maxcode {
|
return proto.WriteMsg(msg)
|
||||||
return newPeerError(errInvalidMsgCode, "code %x is out of range for protocol %q", msg.Code, protoName)
|
|
||||||
}
|
|
||||||
msg.Code += proto.offset
|
|
||||||
return p.writeMsg(msg, msgWriteTimeout)
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
this function is not needed write will be done directly by the msgReadWriter
|
|
||||||
with the connection attached
|
|
||||||
if the interface is channel, then no write locking is needed for synchronous write
|
|
||||||
*/
|
|
||||||
// writeMsg writes a message to the connection.
|
|
||||||
func (p *Peer) writeMsg(msg Msg, timeout time.Duration) error {
|
|
||||||
p.writeMu.Lock()
|
|
||||||
defer p.writeMu.Unlock()
|
|
||||||
p.conn.SetWriteDeadline(time.Now().Add(timeout))
|
|
||||||
if err := writeMsg(p.bufconn, msg); err != nil {
|
|
||||||
return newPeerError(errWrite, "%v", err)
|
|
||||||
}
|
|
||||||
return p.bufconn.Flush()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// proto will embed the same writer channel as given to the readwriter
|
// proto will embed the same writer channel as given to the readwriter
|
||||||
|
|
@ -180,12 +160,8 @@ func (rw *proto) WriteMsg(msg Msg) error {
|
||||||
return newPeerError(errInvalidMsgCode, "not handled")
|
return newPeerError(errInvalidMsgCode, "not handled")
|
||||||
}
|
}
|
||||||
msg.Code += rw.offset
|
msg.Code += rw.offset
|
||||||
select {
|
rw.out <- msg
|
||||||
case rw.out <- msg:
|
return nil
|
||||||
return nil
|
|
||||||
case <-time.After(msgWriteTimeout):
|
|
||||||
return newPeerError(errWrite, "messenger timeout")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rw *proto) EncodeMsg(code uint64, data ...interface{}) error {
|
func (rw *proto) EncodeMsg(code uint64, data ...interface{}) error {
|
||||||
|
|
|
||||||
|
|
@ -62,9 +62,6 @@ type Peer struct {
|
||||||
listenAddr *peerAddr // what remote peer is listening on
|
listenAddr *peerAddr // what remote peer is listening on
|
||||||
dialAddr *peerAddr // non-nil if dialing
|
dialAddr *peerAddr // non-nil if dialing
|
||||||
|
|
||||||
// The mutex protects the connection
|
|
||||||
// so only one protocol can write at a time.
|
|
||||||
writeMu sync.Mutex
|
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
bufconn *bufio.ReadWriter
|
bufconn *bufio.ReadWriter
|
||||||
outgoingMsgC chan Msg
|
outgoingMsgC chan Msg
|
||||||
|
|
@ -284,7 +281,7 @@ loop:
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
p.conn.SetDeadline(time.Now().Add(disconnectGracePeriod))
|
p.conn.SetDeadline(time.Now().Add(disconnectGracePeriod))
|
||||||
p.writeMsg(NewMsg(discMsg, reason), disconnectGracePeriod)
|
p.writeProtoMsg("", NewMsg(discMsg, reason))
|
||||||
io.Copy(ioutil.Discard, p.conn)
|
io.Copy(ioutil.Discard, p.conn)
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue