client.go | 6 ++++-- cmd/torrentfs/main.go | 16 +++++++++++++--- diff --git a/client.go b/client.go index 1914977f3e564ce2ee54877311d4f4cf41cf60a9..4a7ee817dde8cbdeeb0b21ef17323345ad278891 100644 --- a/client.go +++ b/client.go @@ -220,7 +220,7 @@ InfoHash InfoHash Pieces []*piece Data MMapSpan MetaInfo *metainfo.MetaInfo - Conns []*connection + Conns []*Connection Peers []Peer Priorities *list.List } @@ -383,7 +383,8 @@ HalfOpenLimit int PeerId [20]byte DataReady chan DataSpec - mu sync.Mutex + sync.Mutex + mu *sync.Mutex event sync.Cond halfOpen int @@ -435,6 +436,7 @@ return t.Data.ReadAt(p, off) } 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 edfd1d17690e790cc6f8680477b200f136252461..be2295fb846988ec1b0fde58e9d6e3fd6a469822 100644 --- a/cmd/torrentfs/main.go +++ b/cmd/torrentfs/main.go @@ -323,16 +323,26 @@ } 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)