2020-10-22 16:54:50 +02:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-03-03 18:46:08 +01:00
|
|
|
id string
|
2021-03-13 22:17:49 +01:00
|
|
|
token string
|
2021-03-03 18:46:08 +01:00
|
|
|
logger zerolog.Logger
|
|
|
|
manager *SessionManagerCtx
|
|
|
|
profile types.MemberProfile
|
2021-03-13 23:42:16 +01:00
|
|
|
state types.SessionState
|
2021-03-03 18:46:08 +01:00
|
|
|
websocketPeer types.WebSocketPeer
|
|
|
|
webrtcPeer types.WebRTCPeer
|
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-08-29 19:17:10 +02:00
|
|
|
session.webrtcPeer.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-02 21:52:23 +02:00
|
|
|
session.websocketPeer.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-03-13 23:27:28 +01:00
|
|
|
return session.manager.host != nil && session.manager.host == session
|
2020-11-25 20:02:53 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 17:29:38 +01:00
|
|
|
// ---
|
|
|
|
// webscoket
|
|
|
|
// ---
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
|
|
|
|
if session.websocketPeer != nil {
|
2021-09-02 21:52:23 +02:00
|
|
|
session.websocketPeer.Destroy("connection replaced")
|
2021-02-12 21:48:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
session.websocketPeer = websocketPeer
|
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) {
|
|
|
|
if websocketPeer != session.websocketPeer {
|
|
|
|
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)
|
2020-12-03 16:45:41 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
session.manager.emmiter.Emit("disconnected", session)
|
2021-02-14 17:11:21 +01:00
|
|
|
session.websocketPeer = nil
|
2020-12-02 17:29:38 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:08:26 +01:00
|
|
|
func (session *SessionCtx) GetWebSocketPeer() types.WebSocketPeer {
|
|
|
|
return session.websocketPeer
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:16:36 +02:00
|
|
|
func (session *SessionCtx) Send(event string, payload interface{}) {
|
2021-02-14 17:11:21 +01:00
|
|
|
if session.websocketPeer == nil {
|
2021-09-01 21:16:36 +02:00
|
|
|
return
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
2020-10-31 21:46:29 +01:00
|
|
|
|
2021-09-01 21:16:36 +02:00
|
|
|
session.websocketPeer.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) {
|
|
|
|
if session.webrtcPeer != nil {
|
2021-08-29 19:17:10 +02:00
|
|
|
session.webrtcPeer.Destroy()
|
2021-02-12 21:48:47 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:11:21 +01:00
|
|
|
session.webrtcPeer = webrtcPeer
|
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) {
|
|
|
|
if webrtcPeer != session.webrtcPeer {
|
|
|
|
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)
|
|
|
|
|
|
|
|
if !connected {
|
2021-02-14 17:11:21 +01:00
|
|
|
session.webrtcPeer = nil
|
2020-12-02 17:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 18:14:45 +01:00
|
|
|
func (session *SessionCtx) GetWebRTCPeer() types.WebRTCPeer {
|
2021-02-14 17:11:21 +01:00
|
|
|
return session.webrtcPeer
|
2021-02-02 20:43:33 +01:00
|
|
|
}
|