github.com/la5nta/wl2k-go@v0.11.8/transport/ax25/agwpe/listener.go (about)

     1  package agwpe
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  	"sync"
     7  )
     8  
     9  var ErrListenerClosed = errors.New("listener closed")
    10  
    11  type Listener struct {
    12  	p *Port
    13  
    14  	closeOnce sync.Once
    15  	done      chan struct{}
    16  }
    17  
    18  func newListener(p *Port) *Listener { return &Listener{p: p, done: make(chan struct{})} }
    19  
    20  func (ln *Listener) Accept() (net.Conn, error) {
    21  	select {
    22  	case conn, ok := <-ln.p.inboundConns:
    23  		if !ok {
    24  			return nil, ErrPortClosed
    25  		}
    26  		return conn, nil
    27  	case <-ln.done:
    28  		return nil, ErrListenerClosed
    29  	}
    30  }
    31  
    32  func (ln *Listener) Addr() net.Addr { return addr{dest: ln.p.mycall} }
    33  
    34  func (ln *Listener) Close() error {
    35  	ln.closeOnce.Do(func() { close(ln.done) })
    36  	return nil
    37  }