]> Sergey Matveev's repositories - btrtrc.git/blob - tracker/client.go
Pull AnnounceOpt into tracker package
[btrtrc.git] / tracker / client.go
1 package tracker
2
3 import (
4         "context"
5         "net/url"
6
7         trHttp "github.com/anacrolix/torrent/tracker/http"
8         "github.com/anacrolix/torrent/tracker/udp"
9 )
10
11 type Client interface {
12         Announce(context.Context, AnnounceRequest, AnnounceOpt) (AnnounceResponse, error)
13         Close() error
14 }
15
16 type AnnounceOpt = trHttp.AnnounceOpt
17
18 type NewClientOpts struct {
19         Http trHttp.NewClientOpts
20         // Overrides the network in the scheme. Probably a legacy thing.
21         UdpNetwork string
22 }
23
24 func NewClient(urlStr string, opts NewClientOpts) (Client, error) {
25         _url, err := url.Parse(urlStr)
26         if err != nil {
27                 return nil, err
28         }
29         switch _url.Scheme {
30         case "http", "https":
31                 return trHttp.NewClient(_url, opts.Http), nil
32         case "udp", "udp4", "udp6":
33                 network := _url.Scheme
34                 if opts.UdpNetwork != "" {
35                         network = opts.UdpNetwork
36                 }
37                 cc, err := udp.NewConnClient(udp.NewConnClientOpts{
38                         Network: network,
39                         Host:    _url.Host,
40                 })
41                 if err != nil {
42                         return nil, err
43                 }
44                 return &udpClient{
45                         cl:         cc,
46                         requestUri: _url.RequestURI(),
47                 }, nil
48         default:
49                 return nil, ErrBadScheme
50         }
51 }