2019-04-19 22:57:52 +01:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Listener waits for incoming sessions
|
|
|
|
type Listener struct {
|
2019-04-19 23:04:09 +01:00
|
|
|
core *Core
|
2019-04-19 22:57:52 +01:00
|
|
|
conn chan *Conn
|
|
|
|
close chan interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Accept blocks until a new incoming session is received
|
|
|
|
func (l *Listener) Accept() (*Conn, error) {
|
|
|
|
select {
|
|
|
|
case c, ok := <-l.conn:
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("listener closed")
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
case <-l.close:
|
|
|
|
return nil, errors.New("listener closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close will stop the listener
|
|
|
|
func (l *Listener) Close() (err error) {
|
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
err = errors.New("already closed")
|
|
|
|
}()
|
2019-04-19 23:04:09 +01:00
|
|
|
if l.core.sessions.listener == l {
|
|
|
|
l.core.sessions.listener = nil
|
|
|
|
}
|
2019-04-19 22:57:52 +01:00
|
|
|
close(l.close)
|
|
|
|
close(l.conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr is not implemented for this type yet
|
|
|
|
func (l *Listener) Addr() net.Addr {
|
|
|
|
return nil
|
|
|
|
}
|