1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00

remove useless code

This commit is contained in:
Darien Raymond 2017-02-27 15:13:22 +01:00
parent d2897633a6
commit ec0986d6a9
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 30 additions and 59 deletions

View File

@ -80,7 +80,7 @@ func (o *ServerConnection) Id() internal.ConnectionID {
// Listener defines a server listening for connections // Listener defines a server listening for connections
type Listener struct { type Listener struct {
sync.Mutex sync.Mutex
closed chan bool ctx context.Context
sessions map[ConnectionID]*Connection sessions map[ConnectionID]*Connection
hub *udp.Hub hub *udp.Hub
tlsConfig *tls.Config tlsConfig *tls.Config
@ -112,7 +112,7 @@ func NewListener(ctx context.Context, address v2net.Address, port v2net.Port, co
Security: security, Security: security,
}, },
sessions: make(map[ConnectionID]*Connection), sessions: make(map[ConnectionID]*Connection),
closed: make(chan bool), ctx: ctx,
config: kcpSettings, config: kcpSettings,
conns: conns, conns: conns,
} }
@ -143,20 +143,19 @@ func (v *Listener) OnReceive(payload *buf.Buffer, src v2net.Destination, origina
return return
} }
v.Lock()
defer v.Unlock()
select { select {
case <-v.closed: case <-v.ctx.Done():
return return
default: default:
} }
v.Lock()
defer v.Unlock()
if v.hub == nil { if v.hub == nil {
return return
} }
if payload.Len() < 4 {
return
}
conv := segments[0].Conversation() conv := segments[0].Conversation()
cmd := segments[0].Command() cmd := segments[0].Command()
@ -213,7 +212,7 @@ func (v *Listener) OnReceive(payload *buf.Buffer, src v2net.Destination, origina
func (v *Listener) Remove(id ConnectionID) { func (v *Listener) Remove(id ConnectionID) {
select { select {
case <-v.closed: case <-v.ctx.Done():
return return
default: default:
v.Lock() v.Lock()
@ -224,20 +223,14 @@ func (v *Listener) Remove(id ConnectionID) {
// Close stops listening on the UDP address. Already Accepted connections are not closed. // Close stops listening on the UDP address. Already Accepted connections are not closed.
func (v *Listener) Close() error { func (v *Listener) Close() error {
v.hub.Close()
v.Lock() v.Lock()
defer v.Unlock() defer v.Unlock()
select {
case <-v.closed:
return ErrClosedListener
default:
}
close(v.closed)
for _, conn := range v.sessions { for _, conn := range v.sessions {
go conn.Terminate() go conn.Terminate()
} }
v.hub.Close()
return nil return nil
} }

View File

@ -22,7 +22,7 @@ var (
type TCPListener struct { type TCPListener struct {
sync.Mutex sync.Mutex
acccepting bool ctx context.Context
listener *net.TCPListener listener *net.TCPListener
tlsConfig *tls.Config tlsConfig *tls.Config
authConfig internet.ConnectionAuthenticator authConfig internet.ConnectionAuthenticator
@ -43,10 +43,10 @@ func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conn
tcpSettings := networkSettings.(*Config) tcpSettings := networkSettings.(*Config)
l := &TCPListener{ l := &TCPListener{
acccepting: true, ctx: ctx,
listener: listener, listener: listener,
config: tcpSettings, config: tcpSettings,
conns: conns, conns: conns,
} }
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil { if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
tlsConfig, ok := securitySettings.(*v2tls.Config) tlsConfig, ok := securitySettings.(*v2tls.Config)
@ -70,13 +70,14 @@ func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conn
} }
func (v *TCPListener) KeepAccepting() { func (v *TCPListener) KeepAccepting() {
for v.acccepting { for {
select {
case <-v.ctx.Done():
return
default:
}
conn, err := v.listener.Accept() conn, err := v.listener.Accept()
v.Lock() v.Lock()
if !v.acccepting {
v.Unlock()
break
}
if err != nil { if err != nil {
log.Warning("TCP|Listener: Failed to accepted raw connections: ", err) log.Warning("TCP|Listener: Failed to accepted raw connections: ", err)
v.Unlock() v.Unlock()
@ -100,12 +101,10 @@ func (v *TCPListener) KeepAccepting() {
} }
func (v *TCPListener) Put(id internal.ConnectionID, conn net.Conn) { func (v *TCPListener) Put(id internal.ConnectionID, conn net.Conn) {
v.Lock()
defer v.Unlock()
if !v.acccepting {
return
}
select { select {
case <-v.ctx.Done():
conn.Close()
return
case v.conns <- internal.NewConnection(internal.ConnectionID{}, conn, v, internal.ReuseConnection(v.config.IsConnectionReuse())): case v.conns <- internal.NewConnection(internal.ConnectionID{}, conn, v, internal.ReuseConnection(v.config.IsConnectionReuse())):
case <-time.After(time.Second * 5): case <-time.After(time.Second * 5):
conn.Close() conn.Close()
@ -117,9 +116,6 @@ func (v *TCPListener) Addr() net.Addr {
} }
func (v *TCPListener) Close() error { func (v *TCPListener) Close() error {
v.Lock()
defer v.Unlock()
v.acccepting = false
v.listener.Close() v.listener.Close()
return nil return nil
} }

View File

@ -27,12 +27,6 @@ type Listener interface {
Addr() net.Addr Addr() net.Addr
} }
type TCPHub struct {
listener Listener
connCallback ConnectionHandler
closed chan bool
}
func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conns chan<- Connection) (Listener, error) { func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conns chan<- Connection) (Listener, error) {
settings := StreamSettingsFromContext(ctx) settings := StreamSettingsFromContext(ctx)
protocol := settings.GetEffectiveProtocol() protocol := settings.GetEffectiveProtocol()

View File

@ -40,6 +40,8 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
} }
select { select {
case <-h.ln.ctx.Done():
conn.Close()
case h.ln.conns <- internal.NewConnection(internal.ConnectionID{}, conn, h.ln, internal.ReuseConnection(h.ln.config.IsConnectionReuse())): case h.ln.conns <- internal.NewConnection(internal.ConnectionID{}, conn, h.ln, internal.ReuseConnection(h.ln.config.IsConnectionReuse())):
case <-time.After(time.Second * 5): case <-time.After(time.Second * 5):
conn.Close() conn.Close()
@ -48,7 +50,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
type Listener struct { type Listener struct {
sync.Mutex sync.Mutex
closed chan bool ctx context.Context
listener net.Listener listener net.Listener
tlsConfig *tls.Config tlsConfig *tls.Config
config *Config config *Config
@ -60,7 +62,7 @@ func ListenWS(ctx context.Context, address v2net.Address, port v2net.Port, conns
wsSettings := networkSettings.(*Config) wsSettings := networkSettings.(*Config)
l := &Listener{ l := &Listener{
closed: make(chan bool), ctx: ctx,
config: wsSettings, config: wsSettings,
conns: conns, conns: conns,
} }
@ -119,14 +121,9 @@ func converttovws(w http.ResponseWriter, r *http.Request) (*connection, error) {
} }
func (ln *Listener) Put(id internal.ConnectionID, conn net.Conn) { func (ln *Listener) Put(id internal.ConnectionID, conn net.Conn) {
ln.Lock()
defer ln.Unlock()
select {
case <-ln.closed:
return
default:
}
select { select {
case <-ln.ctx.Done():
conn.Close()
case ln.conns <- internal.NewConnection(internal.ConnectionID{}, conn, ln, internal.ReuseConnection(ln.config.IsConnectionReuse())): case ln.conns <- internal.NewConnection(internal.ConnectionID{}, conn, ln, internal.ReuseConnection(ln.config.IsConnectionReuse())):
case <-time.After(time.Second * 5): case <-time.After(time.Second * 5):
conn.Close() conn.Close()
@ -138,16 +135,7 @@ func (ln *Listener) Addr() net.Addr {
} }
func (ln *Listener) Close() error { func (ln *Listener) Close() error {
ln.Lock() return ln.listener.Close()
defer ln.Unlock()
select {
case <-ln.closed:
return ErrClosedListener
default:
}
close(ln.closed)
ln.listener.Close()
return nil
} }
func init() { func init() {