1.加了TunnelPair
2.只注销一次
This commit is contained in:
@@ -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
35
model/tunnelpair.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user