Skip to content

Commit 418f115

Browse files
authored
Merge pull request #37 from multiformats/feat/refactor
turn NetListener() method on listener interface into a helper method
2 parents 97d8056 + 2f3005b commit 418f115

File tree

4 files changed

+77
-11
lines changed

4 files changed

+77
-11
lines changed

.gx/lastpubver

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.5.7: QmRK2LxanhK2gZq6k6R7vk5ZoYZk8ULSSTB7FzDsMUX6CB
1+
1.6.0: QmcGXGdw9BWDysPJQHxJinjGHha3eEg4vzFETre4woNwcX

net.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,6 @@ func Dial(remote ma.Multiaddr) (Conn, error) {
153153
// it uses an embedded net.Listener, overriding net.Listener.Accept to
154154
// return a Conn and providing Multiaddr.
155155
type Listener interface {
156-
157-
// NetListener returns the embedded net.Listener. Use with caution.
158-
NetListener() net.Listener
159-
160156
// Accept waits for and returns the next connection to the listener.
161157
// Returns a Multiaddr friendly Conn
162158
Accept() (Conn, error)
@@ -172,17 +168,29 @@ type Listener interface {
172168
Addr() net.Addr
173169
}
174170

171+
type netListenerAdapter struct {
172+
Listener
173+
}
174+
175+
func (nla *netListenerAdapter) Accept() (net.Conn, error) {
176+
return nla.Listener.Accept()
177+
}
178+
179+
// NetListener turns this Listener into a net.Listener.
180+
//
181+
// * Connections returned from Accept implement multiaddr-net Conn.
182+
// * Calling WrapNetListener on the net.Listener returned by this function will
183+
// return the original (underlying) multiaddr-net Listener.
184+
func NetListener(l Listener) net.Listener {
185+
return &netListenerAdapter{l}
186+
}
187+
175188
// maListener implements Listener
176189
type maListener struct {
177190
net.Listener
178191
laddr ma.Multiaddr
179192
}
180193

181-
// NetListener returns the embedded net.Listener. Use with caution.
182-
func (l *maListener) NetListener() net.Listener {
183-
return l.Listener
184-
}
185-
186194
// Accept waits for and returns the next connection to the listener.
187195
// Returns a Multiaddr friendly Conn
188196
func (l *maListener) Accept() (Conn, error) {
@@ -237,6 +245,10 @@ func Listen(laddr ma.Multiaddr) (Listener, error) {
237245

238246
// WrapNetListener wraps a net.Listener with a manet.Listener.
239247
func WrapNetListener(nl net.Listener) (Listener, error) {
248+
if nla, ok := nl.(*netListenerAdapter); ok {
249+
return nla.Listener, nil
250+
}
251+
240252
laddr, err := FromNetAddr(nl.Addr())
241253
if err != nil {
242254
return nil, err

net_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,57 @@ func TestInterfaceAddressesWorks(t *testing.T) {
514514
t.Fatal(err)
515515
}
516516
}
517+
518+
func TestNetListener(t *testing.T) {
519+
listener, err := net.Listen("tcp", "127.0.0.1:1234")
520+
if err != nil {
521+
t.Fatal(err)
522+
}
523+
defer listener.Close()
524+
malist, err := WrapNetListener(listener)
525+
if err != nil {
526+
t.Fatal(err)
527+
}
528+
if !malist.Multiaddr().Equal(newMultiaddr(t, "/ip4/127.0.0.1/tcp/1234")) {
529+
t.Fatal("unexpected multiaddr")
530+
}
531+
532+
go func() {
533+
c, err := Dial(malist.Multiaddr())
534+
if err != nil {
535+
t.Fatal("failed to dial")
536+
}
537+
if !c.RemoteMultiaddr().Equal(malist.Multiaddr()) {
538+
t.Fatal("dialed wrong target")
539+
}
540+
c.Close()
541+
542+
c, err = Dial(malist.Multiaddr())
543+
if err != nil {
544+
t.Fatal("failed to dial")
545+
}
546+
c.Close()
547+
}()
548+
549+
c, err := malist.Accept()
550+
if err != nil {
551+
t.Fatal(err)
552+
}
553+
c.Close()
554+
netList := NetListener(malist)
555+
malist2, err := WrapNetListener(netList)
556+
if err != nil {
557+
t.Fatal(err)
558+
}
559+
if malist2 != malist {
560+
t.Fatal("expected WrapNetListener(NetListener(malist)) == malist")
561+
}
562+
nc, err := netList.Accept()
563+
if err != nil {
564+
t.Fatal(err)
565+
}
566+
if !nc.(Conn).LocalMultiaddr().Equal(malist.Multiaddr()) {
567+
t.Fatal("wrong multiaddr on conn")
568+
}
569+
nc.Close()
570+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
"license": "",
2020
"name": "go-multiaddr-net",
2121
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
22-
"version": "1.5.7"
22+
"version": "1.6.0"
2323
}
2424

0 commit comments

Comments
 (0)