neko/internal/session/session.go

124 lines
2.8 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"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
)
2020-11-01 16:09:48 +01:00
type SessionCtx struct {
2020-11-25 20:02:53 +01:00
id string
logger zerolog.Logger
manager *SessionManagerCtx
profile types.MemberProfile
2020-11-25 20:02:53 +01:00
websocket_peer types.WebSocketPeer
websocket_connected bool
webrtc_peer types.WebRTCPeer
webrtc_connected bool
}
2020-11-01 16:09:48 +01:00
func (session *SessionCtx) ID() string {
return session.id
}
2020-11-01 16:09:48 +01:00
func (session *SessionCtx) Name() string {
return session.profile.Name
}
2020-11-01 16:09:48 +01:00
func (session *SessionCtx) Admin() bool {
return session.profile.IsAdmin
}
2020-11-01 16:09:48 +01:00
func (session *SessionCtx) IsHost() bool {
return session.manager.host != nil && session.manager.host.ID() == session.ID()
}
2020-11-27 19:59:54 +01:00
func (session *SessionCtx) VerifySecret(secret string) bool {
return session.profile.Secret == secret
}
2020-11-01 16:09:48 +01:00
func (session *SessionCtx) Connected() bool {
// TODO: Refactor.
2020-11-30 22:28:13 +01:00
return session.websocket_connected// && session.webrtc_connected
}
2020-11-25 18:36:33 +01:00
func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) {
session.websocket_peer = websocket_peer
}
2020-11-25 20:02:53 +01:00
func (session *SessionCtx) SetWebSocketConnected(connected bool) {
if connected {
session.websocket_connected = true
// TODO: Refactor.
//session.manager.emmiter.Emit("websocket_connected", session)
session.manager.emmiter.Emit("connected", session)
2020-11-25 20:02:53 +01:00
} else {
session.websocket_connected = false
2020-11-25 20:26:11 +01:00
// TODO: Refactor.
//session.manager.emmiter.Emit("websocket_disconnected", session)
session.manager.emmiter.Emit("disconnected", session)
session.websocket_peer = nil
2020-11-25 20:02:53 +01:00
}
}
2020-11-25 18:41:40 +01:00
func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) {
session.webrtc_peer = webrtc_peer
}
2020-11-25 20:02:53 +01:00
func (session *SessionCtx) SetWebRTCConnected(connected bool) {
if connected {
session.webrtc_connected = true
session.manager.emmiter.Emit("webrtc_connected", session)
2020-11-25 20:02:53 +01:00
} else {
session.webrtc_connected = false
session.manager.emmiter.Emit("webrtc_disconnected", session)
2020-11-01 21:03:25 +01:00
2020-11-25 20:26:11 +01:00
session.webrtc_peer = nil
2020-11-01 21:03:25 +01:00
}
2020-11-01 20:53:25 +01:00
}
2020-11-01 16:09:48 +01:00
func (session *SessionCtx) Disconnect(reason string) error {
2020-11-25 22:06:13 +01:00
if err := session.Send(
2020-12-01 19:44:23 +01:00
message.SystemDisconnect{
2020-11-18 20:30:33 +01:00
Event: event.SYSTEM_DISCONNECT,
Message: reason,
2020-11-25 22:06:13 +01:00
}); err != nil {
return err
}
if session.websocket_peer != nil {
if err := session.websocket_peer.Destroy(); err != nil {
return err
}
}
if session.webrtc_peer != nil {
if err := session.webrtc_peer.Destroy(); err != nil {
return err
}
}
return nil
}
2020-11-01 16:09:48 +01:00
func (session *SessionCtx) Send(v interface{}) error {
2020-11-25 18:36:33 +01:00
if session.websocket_peer == nil {
return nil
}
2020-10-31 21:46:29 +01:00
2020-11-25 18:36:33 +01:00
return session.websocket_peer.Send(v)
}
2020-11-01 16:09:48 +01:00
func (session *SessionCtx) SignalAnswer(sdp string) error {
2020-11-25 18:41:40 +01:00
if session.webrtc_peer == nil {
return nil
}
2020-10-31 21:46:29 +01:00
2020-11-25 18:41:40 +01:00
return session.webrtc_peer.SignalAnswer(sdp)
}