package transport
import (
"io"
"net"
"github.com/go-faster/errors"
"go.uber.org/multierr"
)
type Listener struct {
codec func () Codec
listener net .Listener
}
func Listen (listener net .Listener ) Listener {
return ListenCodec (nil , listener )
}
func ListenCodec (codec func () Codec , listener net .Listener ) Listener {
return Listener {
codec : codec ,
listener : &onceCloseListener {Listener : listener },
}
}
type wrappedConn struct {
reader io .Reader
net .Conn
}
func (w wrappedConn ) Read (b []byte ) (int , error ) {
return w .reader .Read (b )
}
func (l Listener ) Accept () (_ Conn , rErr error ) {
conn , err := l .listener .Accept ()
if err != nil {
return nil , err
}
defer func () {
if rErr != nil {
multierr .AppendInto (&rErr , conn .Close ())
}
}()
if l .codec != nil {
codec := l .codec ()
if err := codec .ReadHeader (conn ); err != nil {
return nil , errors .Wrap (err , "read header" )
}
return &connection {
conn : conn ,
codec : codec ,
}, nil
}
transportCodec , reader , err := detectCodec (conn )
if err != nil {
return nil , errors .Wrap (err , "detect codec" )
}
return &connection {
conn : wrappedConn {
reader : reader ,
Conn : conn ,
},
codec : transportCodec ,
}, nil
}
func (l Listener ) Close () error {
return l .listener .Close ()
}
func (l Listener ) Addr () net .Addr {
return l .listener .Addr ()
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .