把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,12 +69,9 @@ func (this *Tunnel) Run() { //单向的从src发送到dest
} }
func (this *Tunnel) receive(revCarrier *tscipher.Carrier) { func (this *Tunnel) onError() {
src := this.src src := this.src
dest := this.dest dest := this.dest
cipherDirection := this.cipherDirection
id := this.id
defer func() {
//注销 //注销
if !this.unregistered { // 应该不存在异步问题 if !this.unregistered { // 应该不存在异步问题
this.unregChan <- this this.unregChan <- this
@ -88,7 +85,28 @@ func (this *Tunnel) receive(revCarrier *tscipher.Carrier) {
dest.Close() dest.Close()
} }
} }
}() }
func (this *Tunnel) receive(revCarrier *tscipher.Carrier) {
src := this.src
cipherDirection := this.cipherDirection
id := this.id
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 n int
var err error var err error
for { for {
@ -108,25 +126,10 @@ func (this *Tunnel) receive(revCarrier *tscipher.Carrier) {
} }
func (this *Tunnel) send(sendCarrier *tscipher.Carrier) { func (this *Tunnel) send(sendCarrier *tscipher.Carrier) {
src := this.src
dest := this.dest dest := this.dest
cipherDirection := this.cipherDirection cipherDirection := this.cipherDirection
id := this.id id := this.id
defer func() { defer this.onError()
//注销
if !this.unregistered {
this.unregChan <- this
this.unregistered = true
}
if r := recover(); r != nil {
if src != nil {
src.Close()
}
if dest != nil {
dest.Close()
}
}
}()
if cipherDirection != constant.SEND { if cipherDirection != constant.SEND {
sendCarrier.Cipher = nil sendCarrier.Cipher = nil
} }

View File

@ -42,7 +42,7 @@ func (this *TunnelStatusManager) unregister(t interface{}) {
this.mux.Lock() this.mux.Lock()
defer this.mux.Unlock() defer this.mux.Unlock()
l := this.tunnelList 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) nl := new(list.List)
for e := l.Front(); e != nil; e = e.Next() { for e := l.Front(); e != nil; e = e.Next() {
if e.Value != t { if e.Value != t {
@ -51,7 +51,7 @@ func (this *TunnelStatusManager) unregister(t interface{}) {
} }
this.tunnelList = nl this.tunnelList = nl
log.Debug("%d tunnels after remove.", this.tunnelList.Len()) // log.Debug("%d tunnels after remove.", this.tunnelList.Len())
} }