2020-10-22 16:54:50 +02:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2021-09-29 01:01:03 +02:00
|
|
|
"sync"
|
|
|
|
|
2020-10-22 16:54:50 +02:00
|
|
|
"github.com/rs/zerolog"
|
2020-10-29 18:07:04 +01:00
|
|
|
|
2020-10-28 19:15:48 +01:00
|
|
|
"demodesk/neko/internal/types"
|
2020-10-22 16:54:50 +02:00
|
|
|
)
|
|
|
|
|
2020-11-01 16:09:48 +01:00
|
|
|
type SessionCtx struct {
|
2021-09-29 01:01:03 +02:00
|
|
|
id string
|
|
|
|
token string
|
|
|
|
logger zerolog.Logger
|
|
|
|
manager *SessionManagerCtx
|
|
|
|
profile types.MemberProfile
|
|
|
|
state types.SessionState
|
|
|
|
|
2021-03-03 18:46:08 +01:00
|
|
|
websocketPeer types.WebSocketPeer
|
2021-09-29 01:01:03 +02:00
|
|
|
websocketMu sync.Mutex
|
|
|
|
|
|
|
|
webrtcPeer types.WebRTCPeer
|
|
|
|
webrtcMu sync.Mutex
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
2020-11-01 16:09:48 +01:00
|
|
|
func (session *SessionCtx) ID() string {
|
2020-10-22 16:54:50 +02:00
|
|
|
return session.id
|
|
|
|
}
|
|
|
|
|
2021-03-14 00:45:51 +01:00
|
|
|
func (session *SessionCtx) Profile() types.MemberProfile {
|
2021-03-14 00:32:52 +01:00
|
|
|
return session.profile
|
2020-12-08 15:37:48 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 19:29:39 +01:00
|
|
|
func (session *SessionCtx) profileChanged() {
|
2021-03-14 00:45:51 +01:00
|
|
|
if !session.profile.CanHost && session.IsHost() {
|
2020-12-06 19:29:39 +01:00
|
|
|
session.manager.ClearHost()
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:08:26 +01:00
|
|
|
if (!session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch) && session.state.IsWatching {
|
2021-09-29 01:01:03 +02:00
|
|
|
session.GetWebRTCPeer().Destroy()
|
2020-12-06 19:29:39 +01:00
|
|
|
}
|
|
|
|
|
2021-03-14 00:45:51 +01:00
|
|
|
if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected {
|
2021-09-29 01:01:03 +02:00
|
|
|
session.GetWebSocketPeer().Destroy("profile changed")
|
2020-12-06 19:29:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-14 00:50:08 +01:00
|
|
|
func (session *SessionCtx) State() types.SessionState {
|
|
|
|
return session.state
|
|
|
|
}
|
2020-11-25 20:26:11 +01:00
|
|
|
|
2020-12-02 17:29:38 +01:00
|
|
|
func (session *SessionCtx) IsHost() bool {
|
2021-09-29 01:01:03 +02:00
|
|
|
return session.manager.GetHost() == session
|
2020-11-25 20:02:53 +01:00
|
|
|
}
|
|
|
|
|
2021-11-01 17:31:00 +01:00
|
|
|
func (session *SessionCtx) SetCursor(cursor types.Cursor) {
|
2021-11-03 21:59:38 +01:00
|
|
|
if session.manager.InactiveCursors() && session.profile.SendsInactiveCursor {
|
2021-11-01 17:49:02 +01:00
|
|
|
session.manager.SetCursor(cursor, session)
|
|
|
|
}
|
2021-10-23 20:25:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 17:29:38 +01:00
|
|
|
// ---
|
2021-09-30 19:42:25 +02:00
|
|
|
// websocket
|
2020-12-02 17:29:38 +01:00
|
|
|
// ---
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
|
2021-09-29 01:01:03 +02:00
|
|
|
session.websocketMu.Lock()
|
|
|
|
session.websocketPeer, websocketPeer = websocketPeer, session.websocketPeer
|
|
|
|
session.websocketMu.Unlock()
|
2021-02-12 21:48:47 +01:00
|
|
|
|
2021-09-29 01:01:03 +02:00
|
|
|
if websocketPeer != nil && websocketPeer != session.websocketPeer {
|
|
|
|
websocketPeer.Destroy("connection replaced")
|
|
|
|
}
|
2020-12-02 17:29:38 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 15:38:37 +01:00
|
|
|
func (session *SessionCtx) SetWebSocketConnected(websocketPeer types.WebSocketPeer, connected bool) {
|
2021-09-29 01:01:03 +02:00
|
|
|
session.websocketMu.Lock()
|
2021-09-30 19:42:25 +02:00
|
|
|
isCurrentPeer := websocketPeer == session.websocketPeer
|
|
|
|
session.websocketMu.Unlock()
|
|
|
|
|
|
|
|
if !isCurrentPeer {
|
2021-03-23 15:38:37 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:04:50 +01:00
|
|
|
session.state.IsConnected = connected
|
2020-12-03 16:45:41 +01:00
|
|
|
|
2020-12-02 17:29:38 +01:00
|
|
|
if connected {
|
|
|
|
session.manager.emmiter.Emit("connected", session)
|
2021-09-30 19:42:25 +02:00
|
|
|
return
|
2020-12-03 16:45:41 +01:00
|
|
|
}
|
2021-09-30 19:42:25 +02:00
|
|
|
|
|
|
|
session.manager.emmiter.Emit("disconnected", session)
|
|
|
|
|
|
|
|
session.websocketMu.Lock()
|
|
|
|
if websocketPeer == session.websocketPeer {
|
|
|
|
session.websocketPeer = nil
|
|
|
|
}
|
|
|
|
session.websocketMu.Unlock()
|
2020-12-02 17:29:38 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:08:26 +01:00
|
|
|
func (session *SessionCtx) GetWebSocketPeer() types.WebSocketPeer {
|
2021-09-29 01:01:03 +02:00
|
|
|
session.websocketMu.Lock()
|
|
|
|
defer session.websocketMu.Unlock()
|
|
|
|
|
2021-03-25 14:08:26 +01:00
|
|
|
return session.websocketPeer
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:16:36 +02:00
|
|
|
func (session *SessionCtx) Send(event string, payload interface{}) {
|
2021-09-29 01:01:03 +02:00
|
|
|
peer := session.GetWebSocketPeer()
|
|
|
|
if peer != nil {
|
|
|
|
peer.Send(event, payload)
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 17:29:38 +01:00
|
|
|
// ---
|
|
|
|
// webrtc
|
|
|
|
// ---
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) {
|
2021-09-29 01:01:03 +02:00
|
|
|
session.webrtcMu.Lock()
|
|
|
|
session.webrtcPeer, webrtcPeer = webrtcPeer, session.webrtcPeer
|
|
|
|
session.webrtcMu.Unlock()
|
2021-02-12 21:48:47 +01:00
|
|
|
|
2021-09-29 01:01:03 +02:00
|
|
|
if webrtcPeer != nil && webrtcPeer != session.webrtcPeer {
|
|
|
|
webrtcPeer.Destroy()
|
|
|
|
}
|
2020-12-02 17:29:38 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:17:06 +01:00
|
|
|
func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, connected bool) {
|
2021-09-29 01:01:03 +02:00
|
|
|
session.webrtcMu.Lock()
|
2021-09-30 19:42:25 +02:00
|
|
|
isCurrentPeer := webrtcPeer == session.webrtcPeer
|
|
|
|
session.webrtcMu.Unlock()
|
|
|
|
|
|
|
|
if !isCurrentPeer {
|
2021-02-14 17:17:06 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:04:50 +01:00
|
|
|
session.state.IsWatching = connected
|
2020-12-03 16:10:52 +01:00
|
|
|
session.manager.emmiter.Emit("state_changed", session)
|
|
|
|
|
2021-09-30 19:42:25 +02:00
|
|
|
if connected {
|
|
|
|
return
|
2020-12-02 17:29:38 +01:00
|
|
|
}
|
2021-09-30 19:42:25 +02:00
|
|
|
|
|
|
|
session.webrtcMu.Lock()
|
|
|
|
if webrtcPeer == session.webrtcPeer {
|
|
|
|
session.webrtcPeer = nil
|
|
|
|
}
|
|
|
|
session.webrtcMu.Unlock()
|
2020-12-02 17:29:38 +01:00
|
|
|
}
|
|
|
|
|
2021-02-05 18:14:45 +01:00
|
|
|
func (session *SessionCtx) GetWebRTCPeer() types.WebRTCPeer {
|
2021-09-29 01:01:03 +02:00
|
|
|
session.webrtcMu.Lock()
|
|
|
|
defer session.webrtcMu.Unlock()
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
return session.webrtcPeer
|
2021-02-02 20:43:33 +01:00
|
|
|
}
|