2020-10-23 03:54:50 +13:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2021-09-29 12:01:03 +13:00
|
|
|
"sync"
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
"github.com/rs/zerolog"
|
2020-10-30 06:07:04 +13:00
|
|
|
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
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-10-24 07:25:18 +13:00
|
|
|
positionX int
|
|
|
|
positionY int
|
|
|
|
positionMu sync.Mutex
|
|
|
|
|
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-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) ID() string {
|
2020-10-23 03:54:50 +13:00
|
|
|
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-10-24 07:25:18 +13:00
|
|
|
// ---
|
|
|
|
// cursor position
|
|
|
|
// ---
|
|
|
|
|
|
|
|
func (session *SessionCtx) SetPosition(x, y int) {
|
|
|
|
session.positionMu.Lock()
|
|
|
|
defer session.positionMu.Unlock()
|
|
|
|
|
|
|
|
session.positionX = x
|
|
|
|
session.positionY = y
|
|
|
|
}
|
|
|
|
|
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-02-13 09:48:47 +13:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-27 03:04:50 +13:00
|
|
|
session.state.IsConnected = connected
|
2020-12-04 04:45:41 +13:00
|
|
|
|
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
|
2020-12-04 04:45:41 +13:00
|
|
|
}
|
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-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-13 09:48:47 +13:00
|
|
|
|
2021-09-29 12:01:03 +13:00
|
|
|
if webrtcPeer != nil && webrtcPeer != session.webrtcPeer {
|
|
|
|
webrtcPeer.Destroy()
|
|
|
|
}
|
2020-12-03 05:29:38 +13:00
|
|
|
}
|
|
|
|
|
2021-02-15 05:17:06 +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 {
|
2021-02-15 05:17:06 +13:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
if webrtcPeer == session.webrtcPeer {
|
|
|
|
session.webrtcPeer = nil
|
|
|
|
}
|
|
|
|
session.webrtcMu.Unlock()
|
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
|
|
|
}
|