1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-04 17:27:23 -05:00

actually close tcp and udp servers

This commit is contained in:
Darien Raymond 2016-02-02 15:03:56 +00:00
parent 50007a9790
commit f19da2058a
2 changed files with 7 additions and 1 deletions

View File

@ -12,6 +12,7 @@ type Server struct {
Port v2net.Port Port v2net.Port
MsgProcessor func(msg []byte) []byte MsgProcessor func(msg []byte) []byte
accepting bool accepting bool
listener *net.TCPListener
} }
func (server *Server) Start() (v2net.Destination, error) { func (server *Server) Start() (v2net.Destination, error) {
@ -23,6 +24,7 @@ func (server *Server) Start() (v2net.Destination, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
server.listener = listener
go server.acceptConnections(listener) go server.acceptConnections(listener)
localAddr := listener.Addr().(*net.TCPAddr) localAddr := listener.Addr().(*net.TCPAddr)
return v2net.TCPDestination(v2net.IPAddress(localAddr.IP), v2net.Port(localAddr.Port)), nil return v2net.TCPDestination(v2net.IPAddress(localAddr.IP), v2net.Port(localAddr.Port)), nil
@ -55,5 +57,6 @@ func (server *Server) handleConnection(conn net.Conn) {
} }
func (this *Server) Close() { func (this *Server) Close() {
this.accepting = true this.accepting = false
this.listener.Close()
} }

View File

@ -11,6 +11,7 @@ type Server struct {
Port v2net.Port Port v2net.Port
MsgProcessor func(msg []byte) []byte MsgProcessor func(msg []byte) []byte
accepting bool accepting bool
conn *net.UDPConn
} }
func (server *Server) Start() (v2net.Destination, error) { func (server *Server) Start() (v2net.Destination, error) {
@ -22,6 +23,7 @@ func (server *Server) Start() (v2net.Destination, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
server.conn = conn
go server.handleConnection(conn) go server.handleConnection(conn)
localAddr := conn.LocalAddr().(*net.UDPAddr) localAddr := conn.LocalAddr().(*net.UDPAddr)
return v2net.UDPDestination(v2net.IPAddress(localAddr.IP), v2net.Port(localAddr.Port)), nil return v2net.UDPDestination(v2net.IPAddress(localAddr.IP), v2net.Port(localAddr.Port)), nil
@ -45,4 +47,5 @@ func (server *Server) handleConnection(conn *net.UDPConn) {
func (server *Server) Close() { func (server *Server) Close() {
server.accepting = false server.accepting = false
server.conn.Close()
} }