WebRTC refactor -> session dependency removal.

This commit is contained in:
Miroslav Šedivý 2020-10-31 21:20:42 +01:00
parent dadac4f145
commit b1b5a70c64
6 changed files with 25 additions and 28 deletions

View File

@ -36,6 +36,10 @@ func (session *Session) Muted() bool {
return session.muted
}
func (session *Session) IsHost() bool {
return session.manager.host == session.id
}
func (session *Session) Connected() bool {
return session.connected
}
@ -75,18 +79,18 @@ func (session *Session) SetPeer(peer types.Peer) error {
return nil
}
func (session *Session) SetConnected(connected bool) error {
func (session *Session) SetConnected(connected bool) {
session.connected = connected
if connected {
session.manager.emmiter.Emit("connected", session.id, session)
}
return nil
}
func (session *Session) Kick(reason string) error {
func (session *Session) Disconnect(reason string) error {
if session.socket == nil {
return nil
}
if err := session.socket.Send(&message.Disconnect{
Event: event.SYSTEM_DISCONNECT,
Message: reason,
@ -94,7 +98,7 @@ func (session *Session) Kick(reason string) error {
return err
}
return session.destroy()
return session.manager.Destroy(session.id)
}
func (session *Session) Send(v interface{}) error {

View File

@ -12,15 +12,16 @@ type Session interface {
Name() string
Admin() bool
Muted() bool
IsHost() bool
Connected() bool
Member() *Member
SetMuted(muted bool)
SetName(name string) error
SetConnected(connected bool) error
SetConnected(connected bool)
SetSocket(socket WebSocket) error
SetPeer(peer Peer) error
Address() string
Kick(message string) error
Disconnect(message string) error
Send(v interface{}) error
SignalAnswer(sdp string) error
}

View File

@ -6,6 +6,8 @@ import (
"strconv"
"github.com/pion/webrtc/v2"
"demodesk/neko/internal/types"
)
const OP_MOVE = 0x01
@ -36,8 +38,8 @@ type PayloadKey struct {
Key uint64
}
func (manager *WebRTCManager) handle(id string, msg webrtc.DataChannelMessage) error {
if !manager.sessions.IsHost(id) {
func (manager *WebRTCManager) handle(session types.Session, msg webrtc.DataChannelMessage) error {
if !session.IsHost() {
return nil
}

View File

@ -15,11 +15,10 @@ import (
"demodesk/neko/internal/types/config"
)
func New(sessions types.SessionManager, remote types.RemoteManager, config *config.WebRTC) *WebRTCManager {
func New(remote types.RemoteManager, config *config.WebRTC) *WebRTCManager {
return &WebRTCManager{
logger: log.With().Str("module", "webrtc").Logger(),
remote: remote,
sessions: sessions,
config: config,
}
}
@ -30,7 +29,6 @@ type WebRTCManager struct {
audioTrack *webrtc.Track
videoCodec *webrtc.RTPCodec
audioCodec *webrtc.RTPCodec
sessions types.SessionManager
remote types.RemoteManager
config *config.WebRTC
}
@ -136,7 +134,7 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
connection.OnDataChannel(func(d *webrtc.DataChannel) {
d.OnMessage(func(msg webrtc.DataChannelMessage) {
if err = manager.handle(id, msg); err != nil {
if err = manager.handle(session, msg); err != nil {
manager.logger.Warn().Err(err).Msg("data handle failed")
}
})
@ -151,17 +149,10 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
case webrtc.PeerConnectionStateDisconnected:
case webrtc.PeerConnectionStateFailed:
manager.logger.Info().Str("id", id).Msg("peer disconnected")
if err = manager.sessions.Destroy(id); err != nil {
manager.logger.Warn().Err(err).Msg("error while destroying session")
}
session.Disconnect("peer connection state failed")
case webrtc.PeerConnectionStateConnected:
manager.logger.Info().Str("id", id).Msg("peer connected")
if err = session.SetConnected(true); err != nil {
manager.logger.Warn().Err(err).Msg("unable to set connected on peer")
if err = manager.sessions.Destroy(id); err != nil {
manager.logger.Warn().Err(err).Msg("error while destroying session")
}
}
session.SetConnected(true)
}
})

View File

@ -236,7 +236,7 @@ func (h *MessageHandler) adminKick(id string, session types.Session, payload *me
return nil
}
if err := target.Kick("kicked"); err != nil {
if err := target.Disconnect("kicked"); err != nil {
return err
}
@ -286,7 +286,7 @@ func (h *MessageHandler) adminBan(id string, session types.Session, payload *mes
h.banned[address[0]] = true
if err := target.Kick("banned"); err != nil {
if err := target.Disconnect("banned"); err != nil {
return err
}

View File

@ -137,17 +137,16 @@ func (neko *Neko) Start() {
)
neko.remoteManager.Start()
neko.sessionManager = session.New(
neko.remoteManager,
)
neko.webRTCManager = webrtc.New(
neko.sessionManager,
neko.remoteManager,
neko.Configs.WebRTC,
)
neko.webRTCManager.Start()
neko.sessionManager = session.New(
neko.remoteManager,
)
neko.webSocketHandler = websocket.New(
neko.sessionManager,
neko.remoteManager,