]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use go-libutp if cgo is enabled
authorMatt Joiner <anacrolix@gmail.com>
Fri, 16 Jun 2017 08:08:24 +0000 (18:08 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 16 Jun 2017 08:08:24 +0000 (18:08 +1000)
client.go
client_test.go
utp.go [new file with mode: 0644]
utp_go.go [new file with mode: 0644]
utp_libutp.go [new file with mode: 0644]

index d444fe7e63e16d0a2b78394d1542dc4fd3e58872..635f96de67366e2fc40692bbaa2407e9c08ab7bf 100644 (file)
--- a/client.go
+++ b/client.go
@@ -22,7 +22,6 @@ import (
        "github.com/anacrolix/missinggo/pubsub"
        "github.com/anacrolix/missinggo/slices"
        "github.com/anacrolix/sync"
-       "github.com/anacrolix/utp"
        "github.com/dustin/go-humanize"
        "golang.org/x/time/rate"
 
@@ -48,7 +47,7 @@ type Client struct {
        defaultStorage *storage.Client
        onClose        []func()
        tcpListener    net.Listener
-       utpSock        *utp.Socket
+       utpSock        utpSocket
        dHT            *dht.Server
        ipBlockList    iplist.Ranger
        // Our BitTorrent protocol extension bytes, sent in our BT handshakes.
@@ -152,15 +151,15 @@ func (cl *Client) WriteStatus(_w io.Writer) {
        }
 }
 
-func listenUTP(networkSuffix, addr string) (*utp.Socket, error) {
-       return utp.NewSocket("udp"+networkSuffix, addr)
+func listenUTP(networkSuffix, addr string) (utpSocket, error) {
+       return NewUtpSocket("udp"+networkSuffix, addr)
 }
 
 func listenTCP(networkSuffix, addr string) (net.Listener, error) {
        return net.Listen("tcp"+networkSuffix, addr)
 }
 
-func listenBothSameDynamicPort(networkSuffix, host string) (tcpL net.Listener, utpSock *utp.Socket, listenedAddr string, err error) {
+func listenBothSameDynamicPort(networkSuffix, host string) (tcpL net.Listener, utpSock utpSocket, listenedAddr string, err error) {
        for {
                tcpL, err = listenTCP(networkSuffix, net.JoinHostPort(host, "0"))
                if err != nil {
@@ -179,7 +178,7 @@ func listenBothSameDynamicPort(networkSuffix, host string) (tcpL net.Listener, u
 }
 
 // Listen to enabled protocols, ensuring ports match.
-func listen(tcp, utp bool, networkSuffix, addr string) (tcpL net.Listener, utpSock *utp.Socket, listenedAddr string, err error) {
+func listen(tcp, utp bool, networkSuffix, addr string) (tcpL net.Listener, utpSock utpSocket, listenedAddr string, err error) {
        if addr == "" {
                addr = ":50007"
        }
@@ -341,7 +340,7 @@ func (cl *Client) Close() {
                cl.dHT.Close()
        }
        if cl.utpSock != nil {
-               cl.utpSock.CloseNow()
+               cl.utpSock.Close()
        }
        if cl.tcpListener != nil {
                cl.tcpListener.Close()
index 68553e6cf8c63d0d77f0359cdbffdd4c1b4933c4..bd5643d2ab5cd57b36aa5804535103dcd0252c18 100644 (file)
@@ -21,7 +21,6 @@ import (
        "github.com/anacrolix/missinggo"
        "github.com/anacrolix/missinggo/filecache"
        "github.com/anacrolix/missinggo/pubsub"
-       "github.com/anacrolix/utp"
        "github.com/bradfitz/iter"
        "github.com/stretchr/testify/assert"
        "github.com/stretchr/testify/require"
@@ -171,7 +170,7 @@ func TestReducedDialTimeout(t *testing.T) {
 }
 
 func TestUTPRawConn(t *testing.T) {
-       l, err := utp.NewSocket("udp", "")
+       l, err := NewUtpSocket("udp", "")
        if err != nil {
                t.Fatal(err)
        }
@@ -185,7 +184,7 @@ func TestUTPRawConn(t *testing.T) {
                }
        }()
        // Connect a UTP peer to see if the RawConn will still work.
-       s, _ := utp.NewSocket("udp", "")
+       s, _ := NewUtpSocket("udp", "")
        defer s.Close()
        utpPeer, err := s.Dial(fmt.Sprintf("localhost:%d", missinggo.AddrPort(l.Addr())))
        if err != nil {
diff --git a/utp.go b/utp.go
new file mode 100644 (file)
index 0000000..ea9a6f7
--- /dev/null
+++ b/utp.go
@@ -0,0 +1,22 @@
+package torrent
+
+import (
+       "net"
+       "time"
+)
+
+// Abstracts the utp Socket, so the implementation can be selected from
+// different packages.
+type utpSocket interface {
+       Accept() (net.Conn, error)
+       Addr() net.Addr
+       Close() error
+       LocalAddr() net.Addr
+       ReadFrom([]byte) (int, net.Addr, error)
+       SetDeadline(time.Time) error
+       SetWriteDeadline(time.Time) error
+       SetReadDeadline(time.Time) error
+       WriteTo([]byte, net.Addr) (int, error)
+       DialTimeout(string, time.Duration) (net.Conn, error)
+       Dial(string) (net.Conn, error)
+}
diff --git a/utp_go.go b/utp_go.go
new file mode 100644 (file)
index 0000000..3123902
--- /dev/null
+++ b/utp_go.go
@@ -0,0 +1,11 @@
+// +build !cgo
+
+package torrent
+
+import (
+       "github.com/anacrolix/utp"
+)
+
+func NewUtpSocket(network, addr string) (utpSocket, error) {
+       return utp.NewSocket(network, addr)
+}
diff --git a/utp_libutp.go b/utp_libutp.go
new file mode 100644 (file)
index 0000000..30bd69a
--- /dev/null
@@ -0,0 +1,11 @@
+// +build cgo
+
+package torrent
+
+import (
+       "github.com/anacrolix/go-libutp"
+)
+
+func NewUtpSocket(network, addr string) (utpSocket, error) {
+       return utp.NewSocket(network, addr)
+}