Rework torrent adding

This commit is contained in:
Matt Joiner 2014-12-01 16:40:03 -06:00
parent 362dfecbf2
commit e4f1f5d55e
1 changed files with 31 additions and 32 deletions

View File

@ -1524,29 +1524,19 @@ func (me Torrent) ReadAt(p []byte, off int64) (n int, err error) {
return me.cl.TorrentReadAt(me.InfoHash, off, p) return me.cl.TorrentReadAt(me.InfoHash, off, p)
} }
func (cl *Client) AddMagnet(uri string) (t Torrent, err error) { func (cl *Client) AddMagnet(uri string) (T Torrent, err error) {
t.cl = cl
m, err := ParseMagnetURI(uri) m, err := ParseMagnetURI(uri)
if err != nil { if err != nil {
return return
} }
cl.AddTorrentFromFile(cl.torrentFileCachePath(m.InfoHash))
cl.mu.Lock() cl.mu.Lock()
defer cl.mu.Unlock() defer cl.mu.Unlock()
t.torrent = cl.torrent(m.InfoHash) T, err = cl.addOrMergeTorrent(m.InfoHash, [][]string{m.Trackers})
if t.torrent != nil {
t.addTrackers([][]string{m.Trackers})
return
}
t.torrent, err = newTorrent(m.InfoHash, [][]string{m.Trackers}, cl.halfOpenLimit)
if err != nil { if err != nil {
return return
} }
t.DisplayName = m.DisplayName T.DisplayName = m.DisplayName
err = cl.addTorrent(t.torrent)
if err != nil {
t.Close()
}
go cl.connectionPruner(t.torrent)
return return
} }
@ -1593,39 +1583,48 @@ func (me *Client) DropTorrent(infoHash InfoHash) (err error) {
return return
} }
func (me *Client) addTorrent(t *torrent) (err error) { func (me *Client) addOrMergeTorrent(ih InfoHash, announceList [][]string) (T Torrent, err error) {
if _, ok := me.torrents[t.InfoHash]; ok { if _, ok := me.bannedTorrents[ih]; ok {
err = fmt.Errorf("torrent infohash collision") err = errors.New("banned torrent")
return return
} }
me.torrents[t.InfoHash] = t T.cl = me
if !me.disableTrackers { var ok bool
go me.announceTorrentTrackers(t) T.torrent, ok = me.torrents[ih]
} if ok {
if me.dHT != nil { T.torrent.addTrackers(announceList)
go me.announceTorrentDHT(t, true) } else {
T.torrent, err = newTorrent(ih, announceList, me.halfOpenLimit)
if err != nil {
return
}
me.torrents[ih] = T.torrent
if !me.disableTrackers {
go me.announceTorrentTrackers(T.torrent)
}
if me.dHT != nil {
go me.announceTorrentDHT(T.torrent, true)
}
go me.connectionPruner(T.torrent)
} }
return return
} }
// Adds the torrent to the client. // Adds the torrent to the client.
func (me *Client) AddTorrent(metaInfo *metainfo.MetaInfo) (t Torrent, err error) { func (me *Client) AddTorrent(metaInfo *metainfo.MetaInfo) (t Torrent, err error) {
t.cl = me
var ih InfoHash var ih InfoHash
CopyExact(&ih, metaInfo.Info.Hash) CopyExact(&ih, metaInfo.Info.Hash)
t.torrent, err = newTorrent(ih, metaInfo.AnnounceList, me.halfOpenLimit)
if err != nil {
return
}
me.mu.Lock() me.mu.Lock()
defer me.mu.Unlock() defer me.mu.Unlock()
err = me.addTorrent(t.torrent) t, err = me.addOrMergeTorrent(ih, metaInfo.AnnounceList)
if err != nil { if err != nil {
return return
} }
err = me.setMetaData(t.torrent, metaInfo.Info.Info, metaInfo.Info.Bytes) if !t.torrent.haveInfo() {
if err != nil { err = me.setMetaData(t.torrent, metaInfo.Info.Info, metaInfo.Info.Bytes)
return if err != nil {
return
}
} }
return return
} }