1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-06 03:54:22 -04:00
v2fly/transport/internet/ws/stopablehttplistener.go

60 lines
1.1 KiB
Go
Raw Normal View History

2016-08-15 06:16:54 -04:00
package ws
import (
"errors"
"net"
)
type StoppableListener struct {
2016-08-15 09:19:15 -04:00
net.Listener //Wrapped listener
stop chan int //Channel used only to indicate listener should shutdown
2016-08-15 06:16:54 -04:00
}
func NewStoppableListener(l net.Listener) (*StoppableListener, error) {
2016-08-15 09:19:15 -04:00
/*
tcpL, ok := l.(*net.TCPListener)
2016-08-15 06:16:54 -04:00
2016-08-15 09:19:15 -04:00
if !ok {
return nil, errors.New("Cannot wrap listener")
}
*/
2016-08-15 06:16:54 -04:00
retval := &StoppableListener{}
2016-08-15 09:19:15 -04:00
retval.Listener = l
2016-08-15 06:16:54 -04:00
retval.stop = make(chan int)
return retval, nil
}
var StoppedError = errors.New("Listener stopped")
func (sl *StoppableListener) Accept() (net.Conn, error) {
for {
2016-08-15 09:19:15 -04:00
newConn, err := sl.Listener.Accept()
2016-08-15 06:16:54 -04:00
//Check for the channel being closed
select {
case <-sl.stop:
return nil, StoppedError
default:
//If the channel is still open, continue as normal
}
if err != nil {
netErr, ok := err.(net.Error)
//If this is a timeout, then continue to wait for
//new connections
if ok && netErr.Timeout() && netErr.Temporary() {
continue
}
}
return newConn, err
}
}
func (sl *StoppableListener) Stop() {
close(sl.stop)
}