From 97edfb5fff4342312edb4cbb95f99a150ed79def Mon Sep 17 00:00:00 2001 From: "dmy@lab" Date: Thu, 14 Jan 2016 22:03:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=8ATunnel=E4=B8=AD=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86=E5=8D=95=E7=8B=AC=E7=94=A8=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=87=BD=E6=95=B0=E5=81=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/tunnel.go | 67 +++++++++++++++++++++++--------------------- stat/tunnelstatus.go | 4 +-- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/model/tunnel.go b/model/tunnel.go index 4982e2c..49a11dc 100644 --- a/model/tunnel.go +++ b/model/tunnel.go @@ -69,26 +69,44 @@ func (this *Tunnel) Run() { //单向的,从src发送到dest } -func (this *Tunnel) receive(revCarrier *tscipher.Carrier) { +func (this *Tunnel) onError() { src := this.src dest := this.dest + //注销 + if !this.unregistered { // 应该不存在异步问题 + this.unregChan <- this + this.unregistered = true + } + if r := recover(); r != nil { + if src != nil { + src.Close() + } + if dest != nil { + dest.Close() + } + } +} + +func (this *Tunnel) receive(revCarrier *tscipher.Carrier) { + src := this.src cipherDirection := this.cipherDirection id := this.id - defer func() { - //注销 - if !this.unregistered { // 应该不存在异步问题 - this.unregChan <- this - this.unregistered = true - } - if r := recover(); r != nil { - if src != nil { - src.Close() - } - if dest != nil { - dest.Close() - } - } - }() + defer this.onError() + // defer func() { + // //注销 + // if !this.unregistered { // 应该不存在异步问题 + // this.unregChan <- this + // this.unregistered = true + // } + // if r := recover(); r != nil { + // if src != nil { + // src.Close() + // } + // if dest != nil { + // dest.Close() + // } + // } + // }() var n int var err error for { @@ -108,25 +126,10 @@ func (this *Tunnel) receive(revCarrier *tscipher.Carrier) { } func (this *Tunnel) send(sendCarrier *tscipher.Carrier) { - src := this.src dest := this.dest cipherDirection := this.cipherDirection id := this.id - defer func() { - //注销 - if !this.unregistered { - this.unregChan <- this - this.unregistered = true - } - if r := recover(); r != nil { - if src != nil { - src.Close() - } - if dest != nil { - dest.Close() - } - } - }() + defer this.onError() if cipherDirection != constant.SEND { sendCarrier.Cipher = nil } diff --git a/stat/tunnelstatus.go b/stat/tunnelstatus.go index d024017..2d46a35 100644 --- a/stat/tunnelstatus.go +++ b/stat/tunnelstatus.go @@ -42,7 +42,7 @@ func (this *TunnelStatusManager) unregister(t interface{}) { this.mux.Lock() defer this.mux.Unlock() l := this.tunnelList - log.Debug("%d tunnels before remove.", this.tunnelList.Len()) + // log.Debug("%d tunnels before remove.", this.tunnelList.Len()) nl := new(list.List) for e := l.Front(); e != nil; e = e.Next() { if e.Value != t { @@ -51,7 +51,7 @@ func (this *TunnelStatusManager) unregister(t interface{}) { } this.tunnelList = nl - log.Debug("%d tunnels after remove.", this.tunnelList.Len()) + // log.Debug("%d tunnels after remove.", this.tunnelList.Len()) }