Unverified Commit 418f1157 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #37 from multiformats/feat/refactor

turn NetListener() method on listener interface into a helper method
parents 97d80565 2f3005bb
1.5.7: QmRK2LxanhK2gZq6k6R7vk5ZoYZk8ULSSTB7FzDsMUX6CB
1.6.0: QmcGXGdw9BWDysPJQHxJinjGHha3eEg4vzFETre4woNwcX
......@@ -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)
......@@ -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) {
......@@ -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
......
......@@ -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()
}
......@@ -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"
}
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