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

View File

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

View File

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

View File

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

View File

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

View File

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