HalfOpen: make(map[string]struct{}),
pieceStateChanges: pubsub.NewPubSub(),
}
- t.wantPeers.L = &t.stateMu
return
}
// TODO: Tidy this up?
t = newTorrent(spec.InfoHash)
t.cl = cl
+ t.wantPeers.L = &cl.mu
if spec.ChunkSize != 0 {
t.chunkSize = pp.Integer(spec.ChunkSize)
}
func (cl *Client) waitWantPeers(t *torrent) bool {
cl.mu.Lock()
defer cl.mu.Unlock()
- t.stateMu.Lock()
- defer t.stateMu.Unlock()
for {
select {
case <-t.ceasingNetworking:
return true
}
wait:
- cl.mu.Unlock()
t.wantPeers.Wait()
- t.stateMu.Unlock()
- cl.mu.Lock()
- t.stateMu.Lock()
}
}
// same state. The sum of the state run lengths is the number of pieces
// in the torrent.
func (t Torrent) PieceStateRuns() []PieceStateRun {
- t.torrent.stateMu.Lock()
- defer t.torrent.stateMu.Unlock()
+ t.cl.mu.Lock()
+ defer t.cl.mu.Unlock()
return t.torrent.pieceStateRuns()
}
func (t Torrent) PieceState(piece int) PieceState {
- t.torrent.stateMu.Lock()
- defer t.torrent.stateMu.Unlock()
+ t.cl.mu.Lock()
+ defer t.cl.mu.Unlock()
return t.torrent.pieceState(piece)
}
Port int
}
-// Is not aware of Client. Maintains state of torrent for with-in a Client.
+// Maintains state of torrent within a Client.
type torrent struct {
cl *Client
- stateMu sync.Mutex
closing chan struct{}
// Closed when no more network activity is desired. This includes
}
func (t *torrent) ceaseNetworking() {
- t.stateMu.Lock()
- defer t.stateMu.Unlock()
select {
case <-t.ceasingNetworking:
return