Skip to content

Commit

Permalink
Merge pull request #37 from multiformats/feat/refactor
Browse files Browse the repository at this point in the history
turn NetListener() method on listener interface into a helper method
  • Loading branch information
Stebalien authored Jun 6, 2018
2 parents 97d8056 + 2f3005b commit 418f115
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.7: QmRK2LxanhK2gZq6k6R7vk5ZoYZk8ULSSTB7FzDsMUX6CB
1.6.0: QmcGXGdw9BWDysPJQHxJinjGHha3eEg4vzFETre4woNwcX
30 changes: 21 additions & 9 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,6 @@ func Dial(remote ma.Multiaddr) (Conn, error) {
// it uses an embedded net.Listener, overriding net.Listener.Accept to
// return a Conn and providing Multiaddr.
type Listener interface {

// NetListener returns the embedded net.Listener. Use with caution.
NetListener() net.Listener

// Accept waits for and returns the next connection to the listener.
// Returns a Multiaddr friendly Conn
Accept() (Conn, error)
Expand All @@ -172,17 +168,29 @@ type Listener interface {
Addr() net.Addr
}

type netListenerAdapter struct {
Listener
}

func (nla *netListenerAdapter) Accept() (net.Conn, error) {
return nla.Listener.Accept()
}

// NetListener turns this Listener into a net.Listener.
//
// * Connections returned from Accept implement multiaddr-net Conn.
// * Calling WrapNetListener on the net.Listener returned by this function will
// return the original (underlying) multiaddr-net Listener.
func NetListener(l Listener) net.Listener {
return &netListenerAdapter{l}
}

// maListener implements Listener
type maListener struct {
net.Listener
laddr ma.Multiaddr
}

// NetListener returns the embedded net.Listener. Use with caution.
func (l *maListener) NetListener() net.Listener {
return l.Listener
}

// Accept waits for and returns the next connection to the listener.
// Returns a Multiaddr friendly Conn
func (l *maListener) Accept() (Conn, error) {
Expand Down Expand Up @@ -237,6 +245,10 @@ func Listen(laddr ma.Multiaddr) (Listener, error) {

// WrapNetListener wraps a net.Listener with a manet.Listener.
func WrapNetListener(nl net.Listener) (Listener, error) {
if nla, ok := nl.(*netListenerAdapter); ok {
return nla.Listener, nil
}

laddr, err := FromNetAddr(nl.Addr())
if err != nil {
return nil, err
Expand Down
54 changes: 54 additions & 0 deletions net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,57 @@ func TestInterfaceAddressesWorks(t *testing.T) {
t.Fatal(err)
}
}

func TestNetListener(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:1234")
if err != nil {
t.Fatal(err)
}
defer listener.Close()
malist, err := WrapNetListener(listener)
if err != nil {
t.Fatal(err)
}
if !malist.Multiaddr().Equal(newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234")) {
t.Fatal("unexpected multiaddr")
}

go func() {
c, err := Dial(malist.Multiaddr())
if err != nil {
t.Fatal("failed to dial")
}
if !c.RemoteMultiaddr().Equal(malist.Multiaddr()) {
t.Fatal("dialed wrong target")
}
c.Close()

c, err = Dial(malist.Multiaddr())
if err != nil {
t.Fatal("failed to dial")
}
c.Close()
}()

c, err := malist.Accept()
if err != nil {
t.Fatal(err)
}
c.Close()
netList := NetListener(malist)
malist2, err := WrapNetListener(netList)
if err != nil {
t.Fatal(err)
}
if malist2 != malist {
t.Fatal("expected WrapNetListener(NetListener(malist)) == malist")
}
nc, err := netList.Accept()
if err != nil {
t.Fatal(err)
}
if !nc.(Conn).LocalMultiaddr().Equal(malist.Multiaddr()) {
t.Fatal("wrong multiaddr on conn")
}
nc.Close()
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"license": "",
"name": "go-multiaddr-net",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "1.5.7"
"version": "1.6.0"
}

0 comments on commit 418f115

Please sign in to comment.