neko/server/internal/session/session.go

148 lines
2.7 KiB
Go
Raw Normal View History

2020-01-19 12:30:09 +13:00
package session
2020-01-13 21:05:38 +13:00
import (
"sync"
2020-01-25 04:47:37 +13:00
"github.com/rs/zerolog"
"n.eko.moe/neko/internal/types"
"n.eko.moe/neko/internal/types/event"
"n.eko.moe/neko/internal/types/message"
2020-01-13 21:05:38 +13:00
)
2020-01-19 12:30:09 +13:00
type Session struct {
2020-01-25 04:47:37 +13:00
logger zerolog.Logger
id string
name string
admin bool
muted bool
2020-01-21 03:38:07 +13:00
connected bool
2020-01-25 04:47:37 +13:00
manager *SessionManager
socket types.WebSocket
2020-01-25 04:47:37 +13:00
peer types.Peer
2020-01-21 03:38:07 +13:00
mu sync.Mutex
2020-01-13 21:05:38 +13:00
}
2020-01-25 04:47:37 +13:00
func (session *Session) ID() string {
return session.id
}
func (session *Session) Name() string {
return session.name
}
func (session *Session) Admin() bool {
return session.admin
}
func (session *Session) Muted() bool {
return session.muted
}
func (session *Session) Connected() bool {
return session.connected
}
2020-04-05 15:49:43 +12:00
func (session *Session) Address() string {
2020-01-25 04:47:37 +13:00
if session.socket == nil {
2020-04-05 15:49:43 +12:00
return ""
2020-01-25 04:47:37 +13:00
}
return session.socket.Address()
}
func (session *Session) Member() *types.Member {
return &types.Member{
ID: session.id,
Name: session.name,
Admin: session.admin,
Muted: session.muted,
2020-01-21 05:22:24 +13:00
}
2020-01-25 04:47:37 +13:00
}
func (session *Session) SetMuted(muted bool) {
session.muted = muted
}
func (session *Session) SetName(name string) error {
session.name = name
2020-01-21 05:22:24 +13:00
return nil
}
func (session *Session) SetSocket(socket types.WebSocket) error {
2020-01-25 04:47:37 +13:00
session.socket = socket
return nil
}
func (session *Session) SetPeer(peer types.Peer) error {
session.peer = peer
2020-01-19 12:30:09 +13:00
return nil
}
2020-02-13 12:13:33 +13:00
func (session *Session) SetConnected(connected bool) error {
session.connected = connected
if connected {
session.manager.emmiter.Emit("connected", session.id, session)
}
return nil
}
2020-01-25 04:47:37 +13:00
func (session *Session) Kick(reason string) error {
if session.socket == nil {
return nil
}
if err := session.socket.Send(&message.Disconnect{
Event: event.SYSTEM_DISCONNECT,
Message: reason,
}); err != nil {
2020-01-21 05:22:24 +13:00
return err
}
return session.destroy()
}
2020-01-19 12:30:09 +13:00
func (session *Session) Send(v interface{}) error {
2020-01-25 04:47:37 +13:00
if session.socket == nil {
return nil
}
return session.socket.Send(v)
}
func (session *Session) Write(v interface{}) error {
if session.socket == nil {
return nil
}
return session.socket.Send(v)
}
2020-01-13 21:05:38 +13:00
func (session *Session) SignalAnswer(sdp string) error {
2020-02-13 12:13:33 +13:00
if session.peer == nil {
2020-01-25 04:47:37 +13:00
return nil
}
return session.peer.SignalAnswer(sdp)
2020-01-13 21:05:38 +13:00
}
2021-03-30 00:03:25 +13:00
func (session *Session) SignalCandidate(data string) error {
if session.socket == nil {
return nil
}
return session.socket.Send(&message.SignalCandidate{
Event: event.SIGNAL_CANDIDATE,
Data: data,
});
}
2020-01-19 12:30:09 +13:00
func (session *Session) destroy() error {
2020-01-26 03:29:52 +13:00
if session.socket != nil {
if err := session.socket.Destroy(); err != nil {
return err
}
2020-01-13 21:05:38 +13:00
}
2020-01-26 03:29:52 +13:00
if session.peer != nil {
if err := session.peer.Destroy(); err != nil {
return err
}
2020-01-13 21:05:38 +13:00
}
2020-01-19 12:30:09 +13:00
2020-01-13 21:05:38 +13:00
return nil
}