neko/internal/session/session.go

126 lines
2.9 KiB
Go
Raw Normal View History

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-11-01 16:09:48 +01:00
type SessionCtx struct {
2021-03-03 18:46:08 +01:00
id string
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-11-01 16:09:48 +01:00
func (session *SessionCtx) ID() string {
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-02-14 17:11:21 +01:00
if err := session.webrtcPeer.Destroy(); err != nil {
2020-12-06 19:29:39 +01:00
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
}
}
2021-03-14 00:45:51 +01:00
if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected {
2021-03-25 14:08:26 +01:00
if err := session.websocketPeer.Destroy(); err != nil {
2020-12-06 19:29:39 +01:00
session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
}
}
}
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 {
if err := session.websocketPeer.Destroy(); err != nil {
session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
}
}
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-02 17:29:38 +01:00
if connected {
session.manager.emmiter.Emit("connected", session)
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
}
2020-11-01 16:09:48 +01:00
func (session *SessionCtx) Send(v interface{}) error {
2021-02-14 17:11:21 +01:00
if session.websocketPeer == nil {
return nil
}
2020-10-31 21:46:29 +01:00
2021-02-14 17:11:21 +01:00
return session.websocketPeer.Send(v)
}
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 {
if err := session.webrtcPeer.Destroy(); err != nil {
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
}
}
2021-02-14 17:11:21 +01:00
session.webrtcPeer = webrtcPeer
2020-12-02 17:29:38 +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
}