package session import ( "github.com/rs/zerolog" "demodesk/neko/internal/types" "demodesk/neko/internal/types/event" "demodesk/neko/internal/types/message" ) type SessionCtx struct { logger zerolog.Logger id string name string admin bool connected bool manager *SessionManagerCtx websocket_peer types.WebSocketPeer peer types.Peer } func (session *SessionCtx) ID() string { return session.id } func (session *SessionCtx) Name() string { return session.name } func (session *SessionCtx) Admin() bool { return session.admin } func (session *SessionCtx) IsHost() bool { return session.manager.host != nil && session.manager.host.ID() == session.ID() } func (session *SessionCtx) Connected() bool { return session.connected } func (session *SessionCtx) SetName(name string) { session.name = name } func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) { session.websocket_peer = websocket_peer session.manager.emmiter.Emit("created", session) } func (session *SessionCtx) SetPeer(peer types.Peer) { session.peer = peer } func (session *SessionCtx) SetConnected(connected bool) { session.connected = connected if connected { session.manager.emmiter.Emit("connected", session) } else { session.manager.emmiter.Emit("disconnected", session) session.websocket_peer = nil // TODO: Refactor. _ = session.manager.Destroy(session.id) } } func (session *SessionCtx) Disconnect(reason string) error { session.SetConnected(false) return session.Send( message.Disconnect{ Event: event.SYSTEM_DISCONNECT, Message: reason, }) } func (session *SessionCtx) Send(v interface{}) error { if session.websocket_peer == nil { return nil } return session.websocket_peer.Send(v) } func (session *SessionCtx) SignalAnswer(sdp string) error { if session.peer == nil { return nil } return session.peer.SignalAnswer(sdp) } func (session *SessionCtx) destroy() error { if session.websocket_peer != nil { if err := session.websocket_peer.Destroy(); err != nil { return err } } if session.peer != nil { if err := session.peer.Destroy(); err != nil { return err } } return nil }