Commit 10897208 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

added utp listen/dial

parent 0c0f1f31
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net" "net"
utp "github.com/h2so5/utp"
ma "github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-multiaddr"
) )
...@@ -68,7 +69,7 @@ func (c *maConn) RemoteMultiaddr() ma.Multiaddr { ...@@ -68,7 +69,7 @@ func (c *maConn) RemoteMultiaddr() ma.Multiaddr {
// and RemoteAddr options are Multiaddrs, instead of net.Addrs. // and RemoteAddr options are Multiaddrs, instead of net.Addrs.
type Dialer struct { type Dialer struct {
// Dialer is just an embed net.Dialer, with all its options. // Dialer is just an embedded net.Dialer, with all its options.
net.Dialer net.Dialer
// LocalAddr is the local address to use when dialing an // LocalAddr is the local address to use when dialing an
...@@ -103,9 +104,24 @@ func (d *Dialer) Dial(remote ma.Multiaddr) (Conn, error) { ...@@ -103,9 +104,24 @@ func (d *Dialer) Dial(remote ma.Multiaddr) (Conn, error) {
} }
// ok, Dial! // ok, Dial!
nconn, err := d.Dialer.Dial(rnet, rnaddr) var nconn net.Conn
if err != nil { switch rnet {
return nil, err case "tcp":
nconn, err = d.Dialer.Dial(rnet, rnaddr)
if err != nil {
return nil, err
}
case "utp":
// construct utp dialer, with options on our net.Dialer
utpd := utp.Dialer{
Timeout: d.Dialer.Timeout,
LocalAddr: d.Dialer.LocalAddr,
}
nconn, err = utpd.Dial(rnet, rnaddr)
if err != nil {
return nil, err
}
} }
// get local address (pre-specified or assigned within net.Conn) // get local address (pre-specified or assigned within net.Conn)
...@@ -206,9 +222,18 @@ func Listen(laddr ma.Multiaddr) (Listener, error) { ...@@ -206,9 +222,18 @@ func Listen(laddr ma.Multiaddr) (Listener, error) {
return nil, err return nil, err
} }
nl, err := net.Listen(lnet, lnaddr) var nl net.Listener
if err != nil { switch lnet {
return nil, err case "utp":
nl, err = utp.Listen(lnet, lnaddr)
if err != nil {
return nil, err
}
case "tcp":
nl, err = net.Listen(lnet, lnaddr)
if err != nil {
return nil, err
}
} }
return &maListener{ return &maListener{
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
func newMultiaddr(t *testing.T, m string) ma.Multiaddr { func newMultiaddr(t *testing.T, m string) ma.Multiaddr {
maddr, err := ma.NewMultiaddr(m) maddr, err := ma.NewMultiaddr(m)
if err != nil { if err != nil {
t.Fatalf("failed to construct multiaddr: %s", m) t.Fatal("failed to construct multiaddr:", m, err)
} }
return maddr return maddr
} }
...@@ -199,6 +199,67 @@ func TestListenAndDial(t *testing.T) { ...@@ -199,6 +199,67 @@ func TestListenAndDial(t *testing.T) {
wg.Wait() wg.Wait()
} }
func TestListenAndDialUTP(t *testing.T) {
maddr := newMultiaddr(t, "/ip4/127.0.0.1/udp/4323/utp")
listener, err := Listen(maddr)
if err != nil {
t.Fatal("failed to listen")
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
cB, err := listener.Accept()
if err != nil {
t.Fatal("failed to accept")
}
if !cB.LocalMultiaddr().Equal(maddr) {
t.Fatal("local multiaddr not equal:", maddr, cB.LocalMultiaddr())
}
// echo out
buf := make([]byte, 1024)
for {
_, err := cB.Read(buf)
if err != nil {
break
}
cB.Write(buf)
}
wg.Done()
}()
cA, err := Dial(newMultiaddr(t, "/ip4/127.0.0.1/udp/4323/utp"))
if err != nil {
t.Fatal("failed to dial", err)
}
buf := make([]byte, 1024)
if _, err := cA.Write([]byte("beep boop")); err != nil {
t.Fatal("failed to write:", err)
}
if _, err := cA.Read(buf); err != nil {
t.Fatal("failed to read:", buf, err)
}
if !bytes.Equal(buf[:9], []byte("beep boop")) {
t.Fatal("failed to echo:", buf)
}
maddr2 := cA.RemoteMultiaddr()
if !maddr2.Equal(maddr) {
t.Fatal("remote multiaddr not equal:", maddr, maddr2)
}
cA.Close()
wg.Wait()
}
func TestIPLoopback(t *testing.T) { func TestIPLoopback(t *testing.T) {
if IP4Loopback.String() != "/ip4/127.0.0.1" { if IP4Loopback.String() != "/ip4/127.0.0.1" {
t.Error("IP4Loopback incorrect:", IP4Loopback) t.Error("IP4Loopback incorrect:", IP4Loopback)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment