neko/internal/session/session.go

110 lines
2.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-02 04:09:48 +13:00
type SessionCtx struct {
logger zerolog.Logger
id string
name string
admin bool
connected bool
2020-11-02 04:09:48 +13:00
manager *SessionManagerCtx
socket types.WebSocket
peer types.Peer
}
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 {
return session.name
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) Admin() bool {
return session.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 {
return session.connected
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) SetName(name string) {
session.name = name
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) SetSocket(socket types.WebSocket) {
session.socket = socket
2020-11-02 08:23:09 +13:00
session.manager.emmiter.Emit("created", session)
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) SetPeer(peer types.Peer) {
session.peer = peer
}
2020-11-02 09:03:25 +13:00
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.socket = nil
// TODO: Refactor.
2020-11-04 12:41:04 +13:00
_ = session.manager.Destroy(session.id)
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-02 09:03:25 +13:00
session.SetConnected(false)
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 {
if session.socket == nil {
return nil
}
2020-11-01 09:46:29 +13:00
return session.socket.Send(v)
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) SignalAnswer(sdp string) error {
if session.peer == nil {
return nil
}
2020-11-01 09:46:29 +13:00
return session.peer.SignalAnswer(sdp)
}
2020-11-02 04:09:48 +13:00
func (session *SessionCtx) destroy() error {
if session.socket != nil {
if err := session.socket.Destroy(); err != nil {
return err
}
}
if session.peer != nil {
if err := session.peer.Destroy(); err != nil {
return err
}
}
return nil
}