close method for inbound connection handler

This commit is contained in:
v2ray committed 2016-01-03 23:30:37 +01:00
1 parent 8a132643ef
commit 201481a82c
7 files changed
+171 -56

No files matched your search

+29 -7
View File
@@ -21,9 +21,11 @@ import (
// Inbound connection handler that handles messages in VMess format.
type VMessInboundHandler struct {
sync.Mutex
space app.Space
clients user.UserSet
accepting bool
listener *net.TCPListener
}
func NewVMessInboundHandler(space app.Space, clients user.UserSet) *VMessInboundHandler {
@@ -33,6 +35,18 @@ func NewVMessInboundHandler(space app.Space, clients user.UserSet) *VMessInbound
}
}
func (this *VMessInboundHandler) Close() {
this.accepting = false
if this.listener != nil {
this.Lock()
if this.listener != nil {
this.listener.Close()
this.listener = nil
}
this.Unlock()
}
}
func (this *VMessInboundHandler) Listen(port v2net.Port) error {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
@@ -44,19 +58,27 @@ func (this *VMessInboundHandler) Listen(port v2net.Port) error {
return err
}
this.accepting = true
go this.AcceptConnections(listener)
this.listener = listener
go this.AcceptConnections()
return nil
}
func (this *VMessInboundHandler) AcceptConnections(listener *net.TCPListener) error {
func (this *VMessInboundHandler) AcceptConnections() error {
for this.accepting {
retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
connection, err := listener.AcceptTCP()
if err != nil {
log.Error("Failed to accpet connection: %s", err.Error())
return err
if !this.accepting {
return nil
}
this.Lock()
defer this.Unlock()
if this.listener != nil {
connection, err := this.listener.AcceptTCP()
if err != nil {
log.Error("Failed to accpet connection: %s", err.Error())
return err
}
go this.HandleConnection(connection)
}
go this.HandleConnection(connection)
return nil
})