diff --git a/client.go b/client.go index 1914977f..4a7ee817 100644 --- a/client.go +++ b/client.go @@ -220,7 +220,7 @@ type Torrent struct { Pieces []*piece Data MMapSpan MetaInfo *metainfo.MetaInfo - Conns []*connection + Conns []*Connection Peers []Peer Priorities *list.List } @@ -383,7 +383,8 @@ type Client struct { PeerId [20]byte DataReady chan DataSpec - mu sync.Mutex + sync.Mutex + mu *sync.Mutex event sync.Cond halfOpen int @@ -435,6 +436,7 @@ func (cl *Client) TorrentReadAt(ih InfoHash, off int64, p []byte) (n int, err er } func (c *Client) Start() { + c.mu = &c.Mutex c.torrents = make(map[InfoHash]*Torrent) if c.HalfOpenLimit == 0 { c.HalfOpenLimit = 10 diff --git a/cmd/torrentfs/main.go b/cmd/torrentfs/main.go index edfd1d17..be2295fb 100644 --- a/cmd/torrentfs/main.go +++ b/cmd/torrentfs/main.go @@ -323,16 +323,26 @@ func main() { go fs.publishData() go func() { for { + torrentLoop: for _, t := range client.Torrents() { + client.Lock() + for _, c := range t.Conns { + if c.Socket.RemoteAddr().String() == testAddr.String() { + client.Unlock() + continue torrentLoop + } + } + client.Unlock() if testAddr != nil { - client.AddPeers(t.InfoHash, []torrent.Peer{{ + if err := client.AddPeers(t.InfoHash, []torrent.Peer{{ IP: testAddr.IP, Port: testAddr.Port, - }}) + }}); err != nil { + log.Print(err) + } } } time.Sleep(10 * time.Second) - break } }() fusefs.Serve(conn, fs)