Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/server/internal/session/session.go

139 lines
2.6 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.WebScoket
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
}
func (session *Session) Address() *string {
if session.socket == nil {
return nil
}
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-26 03:29:52 +13:00
session.connected = true
session.manager.emmiter.Emit("connected", session.id, session)
2020-01-21 05:22:24 +13:00
return nil
}
2020-01-25 04:47:37 +13:00
func (session *Session) SetSocket(socket types.WebScoket) error {
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-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
2020-01-25 04:47:37 +13:00
func (session *Session) WriteVideoSample(sample types.Sample) error {
if session.peer == nil || !session.connected {
return nil
2020-01-13 21:05:38 +13:00
}
2020-01-25 04:47:37 +13:00
return session.peer.WriteVideoSample(sample)
}
2020-01-13 21:05:38 +13:00
2020-01-25 04:47:37 +13:00
func (session *Session) WriteAudioSample(sample types.Sample) error {
if session.peer == nil || !session.connected {
return nil
}
return session.peer.WriteAudioSample(sample)
2020-01-13 21:05:38 +13:00
}
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
}