tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/mux/listen_net.go (about) 1 package mux 2 3 import ( 4 "net" 5 ) 6 7 // netListener wraps a net.Listener to return connected mux sessions. 8 type netListener struct { 9 net.Listener 10 } 11 12 // Accept waits for and returns the next connected session to the listener. 13 func (l *netListener) Accept() (Session, error) { 14 conn, err := l.Listener.Accept() 15 if err != nil { 16 return nil, err 17 } 18 return New(conn), nil 19 } 20 21 // Close closes the listener. 22 // Any blocked Accept operations will be unblocked and return errors. 23 func (l *netListener) Close() error { 24 return l.Listener.Close() 25 } 26 27 func (l *netListener) Addr() net.Addr { 28 return l.Listener.Addr() 29 } 30 31 func ListenerFrom(l net.Listener) Listener { 32 return &netListener{Listener: l} 33 } 34 35 // ListenTCP creates a TCP listener at the given address. 36 func ListenTCP(addr string) (Listener, error) { 37 l, err := net.Listen("tcp", addr) 38 if err != nil { 39 return nil, err 40 } 41 return ListenerFrom(l), nil 42 } 43 44 // ListenTCP creates a Unix domain socket listener at the given path. 45 func ListenUnix(path string) (Listener, error) { 46 l, err := net.Listen("unix", path) 47 if err != nil { 48 return nil, err 49 } 50 return ListenerFrom(l), nil 51 }