neko/internal/session/session.go

172 lines
3.8 KiB
Go
Raw Normal View History

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