2020-10-23 03:54:50 +13:00
|
|
|
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-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
type SessionCtx struct {
|
2020-10-23 03:54:50 +13:00
|
|
|
logger zerolog.Logger
|
|
|
|
id string
|
|
|
|
name string
|
|
|
|
admin bool
|
|
|
|
connected bool
|
2020-11-02 04:09:48 +13:00
|
|
|
manager *SessionManagerCtx
|
2020-10-23 03:54:50 +13:00
|
|
|
socket types.WebSocket
|
|
|
|
peer types.Peer
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) ID() string {
|
2020-10-23 03:54:50 +13:00
|
|
|
return session.id
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) Name() string {
|
2020-10-23 03:54:50 +13:00
|
|
|
return session.name
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) Admin() bool {
|
2020-10-23 03:54:50 +13:00
|
|
|
return session.admin
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) IsHost() bool {
|
2020-11-01 11:53:19 +13:00
|
|
|
return session.manager.host != nil && session.manager.host.ID() == session.ID()
|
2020-11-01 09:20:42 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) Connected() bool {
|
2020-10-23 03:54:50 +13:00
|
|
|
return session.connected
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) SetName(name string) {
|
2020-10-23 03:54:50 +13:00
|
|
|
session.name = name
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) SetSocket(socket types.WebSocket) {
|
2020-10-23 03:54:50 +13:00
|
|
|
session.socket = socket
|
2020-11-02 08:23:09 +13:00
|
|
|
session.manager.emmiter.Emit("created", session)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) SetPeer(peer types.Peer) {
|
2020-10-23 03:54:50 +13:00
|
|
|
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-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) Send(v interface{}) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if session.socket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-01 09:46:29 +13:00
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
return session.socket.Send(v)
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) SignalAnswer(sdp string) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
if session.peer == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-01 09:46:29 +13:00
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
return session.peer.SignalAnswer(sdp)
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (session *SessionCtx) destroy() error {
|
2020-10-23 03:54:50 +13:00
|
|
|
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
|
|
|
|
}
|