]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Switch to my own UTP library
authorMatt Joiner <anacrolix@gmail.com>
Thu, 8 Jan 2015 12:26:39 +0000 (23:26 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 8 Jan 2015 12:26:39 +0000 (23:26 +1100)
client.go
client_test.go

index 0ddf41f72030e4649c4fc1b0048cf1df46a43b33..19458318e6cce188058224380496586cb716bd0a 100644 (file)
--- a/client.go
+++ b/client.go
@@ -36,21 +36,17 @@ import (
        "syscall"
        "time"
 
-       "bitbucket.org/anacrolix/sync"
-
-       "bitbucket.org/anacrolix/go.torrent/internal/pieceordering"
-
-       "github.com/h2so5/utp"
-
-       "github.com/anacrolix/libtorgo/bencode"
-       "github.com/anacrolix/libtorgo/metainfo"
-
        "bitbucket.org/anacrolix/go.torrent/dht"
+       "bitbucket.org/anacrolix/go.torrent/internal/pieceordering"
        "bitbucket.org/anacrolix/go.torrent/iplist"
        pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
        "bitbucket.org/anacrolix/go.torrent/tracker"
        _ "bitbucket.org/anacrolix/go.torrent/tracker/udp"
        . "bitbucket.org/anacrolix/go.torrent/util"
+       "bitbucket.org/anacrolix/sync"
+       "bitbucket.org/anacrolix/utp"
+       "github.com/anacrolix/libtorgo/bencode"
+       "github.com/anacrolix/libtorgo/metainfo"
 )
 
 var (
@@ -139,6 +135,7 @@ type Client struct {
        halfOpenLimit    int
        peerID           [20]byte
        listeners        []net.Listener
+       utpSock          *utp.Socket
        disableTrackers  bool
        downloadStrategy DownloadStrategy
        dHT              *dht.Server
@@ -470,20 +467,14 @@ func NewClient(cfg *Config) (cl *Client, err error) {
                cl.listeners = append(cl.listeners, l)
                go cl.acceptConnections(l, false)
        }
-       var utpL *utp.Listener
+       var utpSock *utp.Socket
        if !cl.disableUTP {
-               var utpAddr *utp.Addr
-               utpAddr, err = utp.ResolveAddr("utp", listenAddr())
-               if err != nil {
-                       err = fmt.Errorf("error resolving utp listen addr: %s", err)
-                       return
-               }
-               utpL, err = utp.Listen("utp", utpAddr)
+               utpSock, err = utp.NewSocket(listenAddr())
                if err != nil {
                        return
                }
-               cl.listeners = append(cl.listeners, utpL)
-               go cl.acceptConnections(utpL, true)
+               cl.listeners = append(cl.listeners, utpSock)
+               go cl.acceptConnections(utpSock, true)
        }
        if !cfg.NoDHT {
                dhtCfg := cfg.DHTConfig
@@ -493,8 +484,8 @@ func NewClient(cfg *Config) (cl *Client, err error) {
                if dhtCfg.Addr == "" {
                        dhtCfg.Addr = listenAddr()
                }
-               if dhtCfg.Conn == nil && utpL != nil {
-                       dhtCfg.Conn = utpL.RawConn
+               if dhtCfg.Conn == nil && utpSock != nil {
+                       dhtCfg.Conn = utpSock
                }
                cl.dHT, err = dht.NewServer(dhtCfg)
                if cl.ipBlockList != nil {
@@ -669,7 +660,7 @@ func (me *Client) initiateConn(peer Peer, t *torrent) {
                resCh := make(chan dialResult, left)
                if !me.disableUTP {
                        go doDial(func() (net.Conn, error) {
-                               return (&utp.Dialer{Timeout: dialTimeout}).Dial("utp", addr)
+                               return me.utpSock.DialTimeout(addr, dialTimeout)
                        }, resCh, true)
                }
                if !me.disableTCP {
index ca1cf6662c2b493e28f0ac3bdc8e0bd3245df0a0..9d9717b54081b6e7a67c53f305340edb2dc71d0f 100644 (file)
@@ -8,10 +8,9 @@ import (
        "testing"
        "time"
 
-       "github.com/h2so5/utp"
-
        "bitbucket.org/anacrolix/go.torrent/testutil"
        "bitbucket.org/anacrolix/go.torrent/util"
+       "bitbucket.org/anacrolix/utp"
        "github.com/anacrolix/libtorgo/bencode"
 )
 
@@ -111,15 +110,11 @@ func TestReducedDialTimeout(t *testing.T) {
 }
 
 func TestUTPRawConn(t *testing.T) {
-       l, err := utp.Listen("utp", &utp.Addr{&net.UDPAddr{Port: 0}})
+       l, err := utp.NewSocket("")
        if err != nil {
                t.Fatal(err)
        }
        defer l.Close()
-       utpAddr, err := utp.ResolveAddr("utp", fmt.Sprintf("localhost:%d", util.AddrPort(l.Addr())))
-       if err != nil {
-               t.Fatal(err)
-       }
        go func() {
                for {
                        _, err := l.Accept()
@@ -129,7 +124,10 @@ func TestUTPRawConn(t *testing.T) {
                }
        }()
        // Connect a UTP peer to see if the RawConn will still work.
-       utpPeer, err := utp.DialUTP("utp", nil, utpAddr)
+       utpPeer, err := func() *utp.Socket {
+               s, _ := utp.NewSocket("")
+               return s
+       }().Dial(fmt.Sprintf("localhost:%d", util.AddrPort(l.Addr())))
        if err != nil {
                t.Fatalf("error dialing utp listener: %s", err)
        }
@@ -148,7 +146,7 @@ func TestUTPRawConn(t *testing.T) {
                defer close(readerStopped)
                b := make([]byte, 500)
                for i := 0; i < N; i++ {
-                       n, _, err := l.RawConn.ReadFrom(b)
+                       n, _, err := l.ReadFrom(b)
                        if err != nil {
                                t.Fatalf("error reading from raw conn: %s", err)
                        }