session websocket & webrtc add mutex.

This commit is contained in:
Miroslav Šedivý 2021-09-29 01:01:03 +02:00
parent 77d2ef9aaf
commit 5bae3503d0

View File

@ -1,20 +1,26 @@
package session package session
import ( import (
"sync"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
type SessionCtx struct { type SessionCtx struct {
id string id string
token string token string
logger zerolog.Logger logger zerolog.Logger
manager *SessionManagerCtx manager *SessionManagerCtx
profile types.MemberProfile profile types.MemberProfile
state types.SessionState state types.SessionState
websocketPeer types.WebSocketPeer websocketPeer types.WebSocketPeer
webrtcPeer types.WebRTCPeer websocketMu sync.Mutex
webrtcPeer types.WebRTCPeer
webrtcMu sync.Mutex
} }
func (session *SessionCtx) ID() string { func (session *SessionCtx) ID() string {
@ -31,11 +37,11 @@ func (session *SessionCtx) profileChanged() {
} }
if (!session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch) && session.state.IsWatching { if (!session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch) && session.state.IsWatching {
session.webrtcPeer.Destroy() session.GetWebRTCPeer().Destroy()
} }
if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected { if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected {
session.websocketPeer.Destroy("profile changed") session.GetWebSocketPeer().Destroy("profile changed")
} }
} }
@ -44,7 +50,7 @@ func (session *SessionCtx) State() types.SessionState {
} }
func (session *SessionCtx) IsHost() bool { func (session *SessionCtx) IsHost() bool {
return session.manager.host != nil && session.manager.host == session return session.manager.GetHost() == session
} }
// --- // ---
@ -52,39 +58,50 @@ func (session *SessionCtx) IsHost() bool {
// --- // ---
func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) { func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
if session.websocketPeer != nil { session.websocketMu.Lock()
session.websocketPeer.Destroy("connection replaced") session.websocketPeer, websocketPeer = websocketPeer, session.websocketPeer
} session.websocketMu.Unlock()
session.websocketPeer = websocketPeer if websocketPeer != nil && websocketPeer != session.websocketPeer {
websocketPeer.Destroy("connection replaced")
}
} }
func (session *SessionCtx) SetWebSocketConnected(websocketPeer types.WebSocketPeer, connected bool) { func (session *SessionCtx) SetWebSocketConnected(websocketPeer types.WebSocketPeer, connected bool) {
session.websocketMu.Lock()
if websocketPeer != session.websocketPeer { if websocketPeer != session.websocketPeer {
session.websocketMu.Unlock()
return return
} }
session.websocketMu.Unlock()
session.state.IsConnected = connected session.state.IsConnected = connected
if connected { if connected {
session.manager.emmiter.Emit("connected", session) session.manager.emmiter.Emit("connected", session)
return } else {
} session.manager.emmiter.Emit("disconnected", session)
session.manager.emmiter.Emit("disconnected", session) session.websocketMu.Lock()
session.websocketPeer = nil if websocketPeer == session.websocketPeer {
session.websocketPeer = nil
}
session.websocketMu.Unlock()
}
} }
func (session *SessionCtx) GetWebSocketPeer() types.WebSocketPeer { func (session *SessionCtx) GetWebSocketPeer() types.WebSocketPeer {
session.websocketMu.Lock()
defer session.websocketMu.Unlock()
return session.websocketPeer return session.websocketPeer
} }
func (session *SessionCtx) Send(event string, payload interface{}) { func (session *SessionCtx) Send(event string, payload interface{}) {
if session.websocketPeer == nil { peer := session.GetWebSocketPeer()
return if peer != nil {
peer.Send(event, payload)
} }
session.websocketPeer.Send(event, payload)
} }
// --- // ---
@ -92,26 +109,38 @@ func (session *SessionCtx) Send(event string, payload interface{}) {
// --- // ---
func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) { func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) {
if session.webrtcPeer != nil { session.webrtcMu.Lock()
session.webrtcPeer.Destroy() session.webrtcPeer, webrtcPeer = webrtcPeer, session.webrtcPeer
} session.webrtcMu.Unlock()
session.webrtcPeer = webrtcPeer if webrtcPeer != nil && webrtcPeer != session.webrtcPeer {
webrtcPeer.Destroy()
}
} }
func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, connected bool) { func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, connected bool) {
session.webrtcMu.Lock()
if webrtcPeer != session.webrtcPeer { if webrtcPeer != session.webrtcPeer {
session.webrtcMu.Unlock()
return return
} }
session.webrtcMu.Unlock()
session.state.IsWatching = connected session.state.IsWatching = connected
session.manager.emmiter.Emit("state_changed", session) session.manager.emmiter.Emit("state_changed", session)
if !connected { if !connected {
session.webrtcPeer = nil session.webrtcMu.Lock()
if webrtcPeer == session.webrtcPeer {
session.webrtcPeer = nil
}
session.webrtcMu.Unlock()
} }
} }
func (session *SessionCtx) GetWebRTCPeer() types.WebRTCPeer { func (session *SessionCtx) GetWebRTCPeer() types.WebRTCPeer {
session.webrtcMu.Lock()
defer session.webrtcMu.Unlock()
return session.webrtcPeer return session.webrtcPeer
} }