neko/internal/session/session.go

140 lines
3.1 KiB
Go
Raw Normal View History

package session
import (
"github.com/rs/zerolog"
2020-10-30 06:07:04 +13:00
2020-10-29 07:15:48 +13:00
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
)
2020-11-26 07:44:50 +13:00
type MemberProfile struct {
2020-11-26 08:26:29 +13:00
//token string
2020-11-26 07:44:50 +13:00
name string
is_admin bool
2020-11-26 08:26:29 +13:00
//enabled bool
//can_control bool
//can_watch bool
//clipboard_access bool
2020-11-26 07:44:50 +13:00
}
2020-11-02 04:09:48 +13:00
type SessionCtx struct {
2020-11-26 08:02:53 +13:00
id string
logger zerolog.Logger
manager *SessionManagerCtx
profile MemberProfile
websocket_peer types.WebSocketPeer
websocket_connected bool
webrtc_peer types.WebRTCPeer
webrtc_connected bool
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) ID() string {
return session.id
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) Name() string {
2020-11-26 07:44:50 +13:00
return session.profile.name
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) Admin() bool {
2020-11-26 07:44:50 +13:00
return session.profile.is_admin
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) IsHost() bool {
return session.manager.host != nil && session.manager.host.ID() == session.ID()
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) Connected() bool {
2020-11-26 08:26:11 +13:00
return session.websocket_connected && session.webrtc_connected
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) SetName(name string) {
2020-11-26 07:44:50 +13:00
session.profile.name = name
}
2020-11-26 06:36:33 +13:00
func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) {
session.websocket_peer = websocket_peer
2020-11-02 08:23:09 +13:00
session.manager.emmiter.Emit("created", session)
}
2020-11-26 08:02:53 +13:00
func (session *SessionCtx) SetWebSocketConnected(connected bool) {
if connected {
session.websocket_connected = true
session.manager.emmiter.Emit("websocket_connected", session)
} else {
session.websocket_connected = false
2020-11-26 08:26:11 +13:00
// TODO: Refactor.
//session.manager.emmiter.Emit("websocket_disconnected", session)
session.manager.emmiter.Emit("disconnected", session)
session.websocket_peer = nil
// TODO: Refactor.
_ = session.manager.Destroy(session.id)
2020-11-26 08:02:53 +13:00
}
}
2020-11-26 06:41:40 +13:00
func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) {
session.webrtc_peer = webrtc_peer
}
2020-11-26 08:02:53 +13:00
func (session *SessionCtx) SetWebRTCConnected(connected bool) {
if connected {
session.webrtc_connected = true
2020-11-26 08:26:11 +13:00
// TODO: Refactor.
//session.manager.emmiter.Emit("webrtc_connected", session)
session.manager.emmiter.Emit("connected", session)
2020-11-26 08:02:53 +13:00
} else {
session.webrtc_connected = false
session.manager.emmiter.Emit("webrtc_disconnected", session)
2020-11-02 09:03:25 +13:00
2020-11-26 08:26:11 +13:00
session.webrtc_peer = nil
2020-11-02 09:03:25 +13:00
}
2020-11-02 08:53:25 +13:00
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) Disconnect(reason string) error {
2020-11-26 08:26:11 +13:00
// TODO: End WebSocket connection.
// TODO: End WebRTC connection.
2020-11-19 08:30:33 +13:00
return session.Send(
message.Disconnect{
Event: event.SYSTEM_DISCONNECT,
Message: reason,
})
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) Send(v interface{}) error {
2020-11-26 06:36:33 +13:00
if session.websocket_peer == nil {
return nil
}
2020-11-01 09:46:29 +13:00
2020-11-26 06:36:33 +13:00
return session.websocket_peer.Send(v)
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) SignalAnswer(sdp string) error {
2020-11-26 06:41:40 +13:00
if session.webrtc_peer == nil {
return nil
}
2020-11-01 09:46:29 +13:00
2020-11-26 06:41:40 +13:00
return session.webrtc_peer.SignalAnswer(sdp)
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) destroy() error {
2020-11-26 06:36:33 +13:00
if session.websocket_peer != nil {
if err := session.websocket_peer.Destroy(); err != nil {
return err
}
}
2020-11-26 06:41:40 +13:00
if session.webrtc_peer != nil {
if err := session.webrtc_peer.Destroy(); err != nil {
return err
}
}
return nil
}