1.加了TunnelPair

2.只注销一次
This commit is contained in:
dmy@lab
2016-01-14 21:58:41 +08:00
parent 731d320fa2
commit 1e61d0d9eb
4 changed files with 74 additions and 24 deletions

View File

@@ -18,6 +18,7 @@ type Tunnel struct {
cipherDirection constant.Direction
regChan chan interface{}
unregChan chan interface{}
unregistered bool
}
func NewTunnel(id string, src, dest net.Conn, cipherDirection constant.Direction) *Tunnel {
@@ -26,6 +27,7 @@ func NewTunnel(id string, src, dest net.Conn, cipherDirection constant.Direction
src: src,
dest: dest,
cipherDirection: cipherDirection,
unregistered: false,
}
}
@@ -73,9 +75,11 @@ func (this *Tunnel) receive(revCarrier *tscipher.Carrier) {
cipherDirection := this.cipherDirection
id := this.id
defer func() {
// log.Debug("tunnel id %s ends", id)
//注销
// this.unregChan <- this
if !this.unregistered { // 应该不存在异步问题
this.unregChan <- this
this.unregistered = true
}
if r := recover(); r != nil {
if src != nil {
src.Close()
@@ -110,7 +114,10 @@ func (this *Tunnel) send(sendCarrier *tscipher.Carrier) {
id := this.id
defer func() {
//注销
this.unregChan <- this
if !this.unregistered {
this.unregChan <- this
this.unregistered = true
}
if r := recover(); r != nil {
if src != nil {
src.Close()

35
model/tunnelpair.go Normal file
View File

@@ -0,0 +1,35 @@
package model
type TunnelPair struct {
pair []*Tunnel
}
func NewTunnelPair(a *Tunnel, b *Tunnel) *TunnelPair {
r := new(TunnelPair)
r.pair = make([]*Tunnel, 2)
p := r.pair
p[0] = a
p[1] = b
return r
}
func (this *TunnelPair) Run() {
p := this.pair
for _, v := range p {
go v.Run()
}
}
func (this *TunnelPair) SetRegChan(reg chan interface{}) {
p := this.pair
for _, v := range p {
v.SetRegChan(reg)
}
}
func (this *TunnelPair) SetUnRegChan(unreg chan interface{}) {
p := this.pair
for _, v := range p {
v.SetUnRegChan(unreg)
}
}