2020-01-19 12:30:09 +13:00
|
|
|
package session
|
2020-01-13 21:05:38 +13:00
|
|
|
|
|
|
|
import (
|
2021-10-06 09:38:24 +13:00
|
|
|
"m1k1o/neko/internal/types"
|
|
|
|
"m1k1o/neko/internal/types/event"
|
|
|
|
"m1k1o/neko/internal/types/message"
|
|
|
|
|
2020-01-25 04:47:37 +13:00
|
|
|
"github.com/rs/zerolog"
|
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
|
2020-02-27 01:46:10 +13:00
|
|
|
socket types.WebSocket
|
2020-01-25 04:47:37 +13:00
|
|
|
peer types.Peer
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-27 01:46:10 +13:00
|
|
|
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 {
|
2023-01-30 06:18:37 +13:00
|
|
|
session.manager.eventsChannel <- types.SessionEvent{
|
|
|
|
Type: types.SESSION_CONNECTED,
|
2023-01-29 10:08:36 +13:00
|
|
|
Id: session.id,
|
|
|
|
Session: session,
|
|
|
|
}
|
2020-02-13 12:13:33 +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
|
|
|
|
}
|
2021-08-16 02:05:26 +12:00
|
|
|
if err := session.socket.Send(&message.SystemMessage{
|
2020-01-25 04:47:37 +13:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-12-03 11:43:36 +13:00
|
|
|
func (session *Session) SignalLocalOffer(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
|
|
|
|
}
|
2021-12-03 11:43:36 +13:00
|
|
|
session.logger.Info().Msg("signal update - LocalOffer")
|
|
|
|
return session.socket.Send(&message.SignalOffer{
|
|
|
|
Event: event.SIGNAL_OFFER,
|
|
|
|
SDP: sdp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) SignalLocalAnswer(sdp string) error {
|
|
|
|
if session.peer == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
session.logger.Info().Msg("signal update - LocalAnswer")
|
|
|
|
return session.socket.Send(&message.SignalAnswer{
|
|
|
|
Event: event.SIGNAL_ANSWER,
|
|
|
|
SDP: sdp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-18 12:49:25 +13:00
|
|
|
func (session *Session) SignalLocalCandidate(data string) error {
|
|
|
|
if session.socket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
session.peer.SetCandidate(data)
|
|
|
|
return session.socket.Send(&message.SignalCandidate{
|
|
|
|
Event: event.SIGNAL_CANDIDATE,
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-03 11:43:36 +13:00
|
|
|
func (session *Session) SignalRemoteOffer(sdp string) error {
|
|
|
|
if session.peer == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := session.peer.SetOffer(sdp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sdp, err := session.peer.CreateAnswer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
session.logger.Info().Msg("signal update - RemoteOffer")
|
|
|
|
return session.SignalLocalAnswer(sdp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (session *Session) SignalRemoteAnswer(sdp string) error {
|
|
|
|
if session.peer == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
session.logger.Info().Msg("signal update - RemoteAnswer")
|
|
|
|
return session.peer.SetAnswer(sdp)
|
2020-01-13 21:05:38 +13:00
|
|
|
}
|
|
|
|
|
2023-03-18 12:49:25 +13:00
|
|
|
func (session *Session) SignalRemoteCandidate(data string) error {
|
2021-02-15 05:30:24 +13:00
|
|
|
if session.socket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-18 12:49:25 +13:00
|
|
|
session.logger.Info().Msg("signal update - RemoteCandidate")
|
|
|
|
return session.peer.SetCandidate(data)
|
2021-02-15 05:30:24 +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
|
|
|
|
}
|