mirror of https://github.com/bjdgyc/anylink.git
更改dtlssession的存储方式
This commit is contained in:
parent
f342b12372
commit
061f625448
|
@ -9,6 +9,7 @@ import (
|
|||
)
|
||||
|
||||
func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
|
||||
base.Debug("LinkDtls connect", cSess.IpAddr, conn.RemoteAddr())
|
||||
dSess := cSess.NewDtlsConn()
|
||||
if dSess == nil {
|
||||
// 创建失败,直接关闭链接
|
||||
|
@ -28,14 +29,7 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
|
|||
|
||||
go dtlsWrite(conn, dSess, cSess)
|
||||
|
||||
now := time.Now()
|
||||
|
||||
for {
|
||||
|
||||
if time.Now().Sub(now) > time.Second*30 {
|
||||
// return
|
||||
}
|
||||
|
||||
err := conn.SetReadDeadline(time.Now().Add(dead))
|
||||
if err != nil {
|
||||
base.Error("SetDeadline: ", err)
|
||||
|
@ -63,15 +57,7 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
|
|||
return
|
||||
case 0x03: // DPD-REQ
|
||||
// base.Debug("recv DPD-REQ", cSess.IpAddr)
|
||||
payload := &sessdata.Payload{
|
||||
LType: sessdata.LTypeIPData,
|
||||
PType: 0x04,
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
select {
|
||||
case cSess.PayloadOutDtls <- payload:
|
||||
case <-dSess.CloseChan:
|
||||
if payloadOutDtls(cSess, dSess, sessdata.LTypeIPData, 0x04, nil) {
|
||||
return
|
||||
}
|
||||
case 0x04:
|
||||
|
|
|
@ -39,7 +39,7 @@ func payloadOut(cSess *sessdata.ConnSession, lType sessdata.LType, pType byte, d
|
|||
if dSess == nil {
|
||||
return payloadOutCstp(cSess, lType, pType, data)
|
||||
} else {
|
||||
return payloadOutDtls(dSess, lType, pType, data)
|
||||
return payloadOutDtls(cSess, dSess, lType, pType, data)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@ func payloadOutCstp(cSess *sessdata.ConnSession, lType sessdata.LType, pType byt
|
|||
return closed
|
||||
}
|
||||
|
||||
func payloadOutDtls(dSess *sessdata.DtlsSession, lType sessdata.LType, pType byte, data []byte) bool {
|
||||
func payloadOutDtls(cSess *sessdata.ConnSession, dSess *sessdata.DtlsSession, lType sessdata.LType, pType byte, data []byte) bool {
|
||||
payload := &sessdata.Payload{
|
||||
LType: lType,
|
||||
PType: pType,
|
||||
|
@ -69,7 +69,7 @@ func payloadOutDtls(dSess *sessdata.DtlsSession, lType sessdata.LType, pType byt
|
|||
}
|
||||
|
||||
select {
|
||||
case dSess.CSess.PayloadOutDtls <- payload:
|
||||
case cSess.PayloadOutDtls <- payload:
|
||||
case <-dSess.CloseChan:
|
||||
}
|
||||
|
||||
|
|
|
@ -55,10 +55,11 @@ type ConnSession struct {
|
|||
}
|
||||
|
||||
type DtlsSession struct {
|
||||
isActive int32
|
||||
CSess *ConnSession
|
||||
isActive int32
|
||||
// CSess *ConnSession
|
||||
CloseChan chan struct{}
|
||||
closeOnce sync.Once
|
||||
IpAddr net.IP
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
|
@ -190,10 +191,7 @@ func (s *Session) NewConn() *ConnSession {
|
|||
}
|
||||
|
||||
dSess := &DtlsSession{
|
||||
isActive: -1,
|
||||
CSess: cSess,
|
||||
CloseChan: make(chan struct{}),
|
||||
closeOnce: sync.Once{},
|
||||
isActive: -1,
|
||||
}
|
||||
cSess.dSess.Store(dSess)
|
||||
|
||||
|
@ -239,10 +237,11 @@ func (cs *ConnSession) NewDtlsConn() *DtlsSession {
|
|||
}
|
||||
|
||||
dSess := &DtlsSession{
|
||||
isActive: 1,
|
||||
CSess: cs,
|
||||
isActive: 1,
|
||||
// CSess: cs,
|
||||
CloseChan: make(chan struct{}),
|
||||
closeOnce: sync.Once{},
|
||||
IpAddr: cs.IpAddr,
|
||||
}
|
||||
cs.dSess.Store(dSess)
|
||||
return dSess
|
||||
|
@ -251,7 +250,7 @@ func (cs *ConnSession) NewDtlsConn() *DtlsSession {
|
|||
// 关闭dtls链接
|
||||
func (ds *DtlsSession) Close() {
|
||||
ds.closeOnce.Do(func() {
|
||||
base.Info("closeOnce dtls:", ds.CSess.IpAddr)
|
||||
base.Info("closeOnce dtls:", ds.IpAddr)
|
||||
|
||||
atomic.StoreInt32(&ds.isActive, -1)
|
||||
close(ds.CloseChan)
|
||||
|
|
Loading…
Reference in New Issue