更改dtlssession的存储方式

This commit is contained in:
bjdgyc 2021-05-26 21:24:53 +08:00
parent f342b12372
commit 061f625448
3 changed files with 13 additions and 28 deletions

View File

@ -9,6 +9,7 @@ import (
) )
func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) { func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
base.Debug("LinkDtls connect", cSess.IpAddr, conn.RemoteAddr())
dSess := cSess.NewDtlsConn() dSess := cSess.NewDtlsConn()
if dSess == nil { if dSess == nil {
// 创建失败,直接关闭链接 // 创建失败,直接关闭链接
@ -28,14 +29,7 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
go dtlsWrite(conn, dSess, cSess) go dtlsWrite(conn, dSess, cSess)
now := time.Now()
for { for {
if time.Now().Sub(now) > time.Second*30 {
// return
}
err := conn.SetReadDeadline(time.Now().Add(dead)) err := conn.SetReadDeadline(time.Now().Add(dead))
if err != nil { if err != nil {
base.Error("SetDeadline: ", err) base.Error("SetDeadline: ", err)
@ -63,15 +57,7 @@ func LinkDtls(conn net.Conn, cSess *sessdata.ConnSession) {
return return
case 0x03: // DPD-REQ case 0x03: // DPD-REQ
// base.Debug("recv DPD-REQ", cSess.IpAddr) // base.Debug("recv DPD-REQ", cSess.IpAddr)
payload := &sessdata.Payload{ if payloadOutDtls(cSess, dSess, sessdata.LTypeIPData, 0x04, nil) {
LType: sessdata.LTypeIPData,
PType: 0x04,
Data: nil,
}
select {
case cSess.PayloadOutDtls <- payload:
case <-dSess.CloseChan:
return return
} }
case 0x04: case 0x04:

View File

@ -39,7 +39,7 @@ func payloadOut(cSess *sessdata.ConnSession, lType sessdata.LType, pType byte, d
if dSess == nil { if dSess == nil {
return payloadOutCstp(cSess, lType, pType, data) return payloadOutCstp(cSess, lType, pType, data)
} else { } 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 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{ payload := &sessdata.Payload{
LType: lType, LType: lType,
PType: pType, PType: pType,
@ -69,7 +69,7 @@ func payloadOutDtls(dSess *sessdata.DtlsSession, lType sessdata.LType, pType byt
} }
select { select {
case dSess.CSess.PayloadOutDtls <- payload: case cSess.PayloadOutDtls <- payload:
case <-dSess.CloseChan: case <-dSess.CloseChan:
} }

View File

@ -55,10 +55,11 @@ type ConnSession struct {
} }
type DtlsSession struct { type DtlsSession struct {
isActive int32 isActive int32
CSess *ConnSession // CSess *ConnSession
CloseChan chan struct{} CloseChan chan struct{}
closeOnce sync.Once closeOnce sync.Once
IpAddr net.IP
} }
type Session struct { type Session struct {
@ -190,10 +191,7 @@ func (s *Session) NewConn() *ConnSession {
} }
dSess := &DtlsSession{ dSess := &DtlsSession{
isActive: -1, isActive: -1,
CSess: cSess,
CloseChan: make(chan struct{}),
closeOnce: sync.Once{},
} }
cSess.dSess.Store(dSess) cSess.dSess.Store(dSess)
@ -239,10 +237,11 @@ func (cs *ConnSession) NewDtlsConn() *DtlsSession {
} }
dSess := &DtlsSession{ dSess := &DtlsSession{
isActive: 1, isActive: 1,
CSess: cs, // CSess: cs,
CloseChan: make(chan struct{}), CloseChan: make(chan struct{}),
closeOnce: sync.Once{}, closeOnce: sync.Once{},
IpAddr: cs.IpAddr,
} }
cs.dSess.Store(dSess) cs.dSess.Store(dSess)
return dSess return dSess
@ -251,7 +250,7 @@ func (cs *ConnSession) NewDtlsConn() *DtlsSession {
// 关闭dtls链接 // 关闭dtls链接
func (ds *DtlsSession) Close() { func (ds *DtlsSession) Close() {
ds.closeOnce.Do(func() { ds.closeOnce.Do(func() {
base.Info("closeOnce dtls:", ds.CSess.IpAddr) base.Info("closeOnce dtls:", ds.IpAddr)
atomic.StoreInt32(&ds.isActive, -1) atomic.StoreInt32(&ds.isActive, -1)
close(ds.CloseChan) close(ds.CloseChan)