session update mutex logic.

This commit is contained in:
Miroslav Šedivý 2021-09-30 19:42:25 +02:00
parent 325af8fc5a
commit e9502b0d9b

View File

@ -54,7 +54,7 @@ func (session *SessionCtx) IsHost() bool {
} }
// --- // ---
// webscoket // websocket
// --- // ---
func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) { func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
@ -69,17 +69,20 @@ func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
func (session *SessionCtx) SetWebSocketConnected(websocketPeer types.WebSocketPeer, connected bool) { func (session *SessionCtx) SetWebSocketConnected(websocketPeer types.WebSocketPeer, connected bool) {
session.websocketMu.Lock() session.websocketMu.Lock()
if websocketPeer != session.websocketPeer { isCurrentPeer := websocketPeer == session.websocketPeer
session.websocketMu.Unlock() session.websocketMu.Unlock()
if !isCurrentPeer {
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)
} else { return
}
session.manager.emmiter.Emit("disconnected", session) session.manager.emmiter.Emit("disconnected", session)
session.websocketMu.Lock() session.websocketMu.Lock()
@ -88,7 +91,6 @@ func (session *SessionCtx) SetWebSocketConnected(websocketPeer types.WebSocketPe
} }
session.websocketMu.Unlock() session.websocketMu.Unlock()
} }
}
func (session *SessionCtx) GetWebSocketPeer() types.WebSocketPeer { func (session *SessionCtx) GetWebSocketPeer() types.WebSocketPeer {
session.websocketMu.Lock() session.websocketMu.Lock()
@ -120,23 +122,26 @@ func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) {
func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, connected bool) { func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, connected bool) {
session.webrtcMu.Lock() session.webrtcMu.Lock()
if webrtcPeer != session.webrtcPeer { isCurrentPeer := webrtcPeer == session.webrtcPeer
session.webrtcMu.Unlock() session.webrtcMu.Unlock()
if !isCurrentPeer {
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 {
return
}
session.webrtcMu.Lock() session.webrtcMu.Lock()
if webrtcPeer == session.webrtcPeer { if webrtcPeer == session.webrtcPeer {
session.webrtcPeer = nil session.webrtcPeer = nil
} }
session.webrtcMu.Unlock() session.webrtcMu.Unlock()
} }
}
func (session *SessionCtx) GetWebRTCPeer() types.WebRTCPeer { func (session *SessionCtx) GetWebRTCPeer() types.WebRTCPeer {
session.webrtcMu.Lock() session.webrtcMu.Lock()