把Tunnel中的错误处理单独用一个函数做

This commit is contained in:
dmy@lab 2016-01-14 22:03:54 +08:00
parent 1e61d0d9eb
commit 97edfb5fff
2 changed files with 37 additions and 34 deletions

View File

@ -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
}

View File

@ -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())
}