2019-12-22 17:43:39 -05:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
2019-12-22 22:25:57 -05:00
|
|
|
// Peer encodes connection information for a peer
|
2019-12-22 17:43:39 -05:00
|
|
|
type Peer struct {
|
|
|
|
|
IP net.IP
|
|
|
|
|
Port uint16
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 22:25:57 -05:00
|
|
|
// Downloader holds data required to download a torrent from a list of peers
|
|
|
|
|
type Downloader struct {
|
2019-12-22 22:37:27 -05:00
|
|
|
Peers []Peer
|
|
|
|
|
InfoHash [20]byte
|
|
|
|
|
Length int
|
2019-12-22 22:25:57 -05:00
|
|
|
}
|
2019-12-22 17:43:39 -05:00
|
|
|
|
2019-12-22 22:25:57 -05:00
|
|
|
// Download downloads a torrent
|
|
|
|
|
func (d *Downloader) Download() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-12-22 17:43:39 -05:00
|
|
|
|
2019-12-22 22:25:57 -05:00
|
|
|
func connect(p *Peer, peerID [20]byte, infoHash [20]byte) (net.Conn, error) {
|
|
|
|
|
hostPort := net.JoinHostPort(p.IP.String(), strconv.Itoa(int(p.Port)))
|
|
|
|
|
conn, err := net.Dial("tcp", hostPort)
|
2019-12-22 17:43:39 -05:00
|
|
|
if err != nil {
|
2019-12-22 22:25:57 -05:00
|
|
|
return nil, err
|
2019-12-22 17:43:39 -05:00
|
|
|
}
|
2019-12-22 22:25:57 -05:00
|
|
|
return conn, nil
|
2019-12-22 17:43:39 -05:00
|
|
|
}
|