modify TcpListener

This commit is contained in:
Arceliar 2019-09-19 19:45:17 -05:00
parent 93e81867fd
commit eeb34ce4e4
2 changed files with 11 additions and 6 deletions

View File

@ -152,7 +152,7 @@ func (m *Multicast) announce() {
for name, listener := range m.listeners { for name, listener := range m.listeners {
// Prepare our stop function! // Prepare our stop function!
stop := func() { stop := func() {
listener.Stop <- true listener.Stop()
delete(m.listeners, name) delete(m.listeners, name)
m.log.Debugln("No longer multicasting on", name) m.log.Debugln("No longer multicasting on", name)
} }

View File

@ -47,7 +47,12 @@ type tcp struct {
// multicast interfaces. // multicast interfaces.
type TcpListener struct { type TcpListener struct {
Listener net.Listener Listener net.Listener
Stop chan bool stop chan struct{}
}
func (l *TcpListener) Stop() {
defer func() { recover() }()
close(l.stop)
} }
// Wrapper function to set additional options for specific connection types. // Wrapper function to set additional options for specific connection types.
@ -100,7 +105,7 @@ func (t *tcp) init(l *link) error {
func (t *tcp) stop() error { func (t *tcp) stop() error {
t.mutex.Lock() t.mutex.Lock()
for _, listener := range t.listeners { for _, listener := range t.listeners {
close(listener.Stop) listener.Stop()
} }
t.mutex.Unlock() t.mutex.Unlock()
t.waitgroup.Wait() t.waitgroup.Wait()
@ -132,7 +137,7 @@ func (t *tcp) reconfigure() {
t.mutex.Lock() t.mutex.Lock()
if listener, ok := t.listeners[d[6:]]; ok { if listener, ok := t.listeners[d[6:]]; ok {
t.mutex.Unlock() t.mutex.Unlock()
listener.Stop <- true listener.Stop()
t.link.core.log.Infoln("Stopped TCP listener:", d[6:]) t.link.core.log.Infoln("Stopped TCP listener:", d[6:])
} else { } else {
t.mutex.Unlock() t.mutex.Unlock()
@ -152,7 +157,7 @@ func (t *tcp) listen(listenaddr string) (*TcpListener, error) {
if err == nil { if err == nil {
l := TcpListener{ l := TcpListener{
Listener: listener, Listener: listener,
Stop: make(chan bool), stop: make(chan struct{}),
} }
t.waitgroup.Add(1) t.waitgroup.Add(1)
go t.listener(&l, listenaddr) go t.listener(&l, listenaddr)
@ -207,7 +212,7 @@ func (t *tcp) listener(l *TcpListener, listenaddr string) {
} }
t.waitgroup.Add(1) t.waitgroup.Add(1)
go t.handler(sock, true, nil) go t.handler(sock, true, nil)
case <-l.Stop: case <-l.stop:
// FIXME this races with the goroutine that Accepts a TCP connection, may leak connections when a listener is removed // FIXME this races with the goroutine that Accepts a TCP connection, may leak connections when a listener is removed
return return
} }