Rename connection.closed->closing

This commit is contained in:
Matt Joiner 2014-08-28 09:31:05 +10:00
parent 5668582c8e
commit 035edbaf85
2 changed files with 13 additions and 17 deletions

View File

@ -694,8 +694,11 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error {
var msg pp.Message
err := decoder.Decode(&msg)
me.mu.Lock()
if c.getClosed() {
c.lastMessageReceived = time.Now()
select {
case <-c.closing:
return nil
default:
}
if err != nil {
if me.stopped() || err == io.EOF {

View File

@ -28,7 +28,7 @@ const (
type connection struct {
Socket net.Conn
Discovery peerSource
closed chan struct{}
closing chan struct{}
mu sync.Mutex // Only for closing.
post chan pp.Message
writeCh chan []byte
@ -61,7 +61,7 @@ func newConnection(sock net.Conn, peb peerExtensionBytes, peerID [20]byte) (c *c
PeerExtensionBytes: peb,
PeerID: peerID,
closed: make(chan struct{}),
closing: make(chan struct{}),
writeCh: make(chan []byte),
post: make(chan pp.Message),
}
@ -146,20 +146,13 @@ func (cn *connection) WriteStatus(w io.Writer) {
func (c *connection) Close() {
c.mu.Lock()
defer c.mu.Unlock()
if c.getClosed() {
return
}
close(c.closed)
c.Socket.Close()
}
func (c *connection) getClosed() bool {
select {
case <-c.closed:
return true
case <-c.closing:
return
default:
return false
}
close(c.closing)
c.Socket.Close()
}
func (c *connection) PeerHasPiece(index pp.Integer) bool {
@ -175,7 +168,7 @@ func (c *connection) PeerHasPiece(index pp.Integer) bool {
func (c *connection) Post(msg pp.Message) {
select {
case c.post <- msg:
case <-c.closed:
case <-c.closing:
}
}
@ -296,7 +289,7 @@ func (conn *connection) writer() {
conn.Close()
return
}
case <-conn.closed:
case <-conn.closing:
return
}
}
@ -354,7 +347,7 @@ func (conn *connection) writeOptimizer(keepAliveDelay time.Duration) {
if pending.Len() == 0 {
timer.Reset(keepAliveDelay)
}
case <-conn.closed:
case <-conn.closing:
return
}
}