Commit 1e79033a authored by Steven Allen's avatar Steven Allen

turn NetListener() method on listener interface into a helper method

This method made implementing the manet.Listener interface annoying. Also, this
lets us drop the "use with caution" warning as we're now *wrapping* the listener
instead of exposing internal state.
parent 97d80565
......@@ -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
......
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