split webrtc & WS destroy.

This commit is contained in:
Miroslav Šedivý 2021-03-25 14:08:26 +01:00
parent a8e44160dc
commit 78b6264494
5 changed files with 57 additions and 33 deletions

View File

@ -121,13 +121,20 @@ func (manager *SessionManagerCtx) Delete(id string) error {
delete(manager.sessions, id)
manager.sessionsMu.Unlock()
var err error
var errs []error
if session.State().IsConnected {
err = session.Disconnect("session deleted")
err := session.GetWebSocketPeer().Destroy()
errs = append(errs, err)
}
if session.State().IsWatching {
err := session.GetWebRTCPeer().Destroy()
errs = append(errs, err)
}
manager.emmiter.Emit("deleted", session)
return err
return utils.ErrorsJoin(errs)
}
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {

View File

@ -4,8 +4,6 @@ import (
"github.com/rs/zerolog"
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
)
type SessionCtx struct {
@ -32,14 +30,14 @@ func (session *SessionCtx) profileChanged() {
session.manager.ClearHost()
}
if !session.profile.CanWatch && session.state.IsWatching {
if (!session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch) && session.state.IsWatching {
if err := session.webrtcPeer.Destroy(); err != nil {
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
}
}
if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected {
if err := session.Disconnect("profile changed"); err != nil {
if err := session.websocketPeer.Destroy(); err != nil {
session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
}
}
@ -83,6 +81,10 @@ func (session *SessionCtx) SetWebSocketConnected(websocketPeer types.WebSocketPe
session.websocketPeer = nil
}
func (session *SessionCtx) GetWebSocketPeer() types.WebSocketPeer {
return session.websocketPeer
}
func (session *SessionCtx) Send(v interface{}) error {
if session.websocketPeer == nil {
return nil
@ -91,30 +93,6 @@ func (session *SessionCtx) Send(v interface{}) error {
return session.websocketPeer.Send(v)
}
func (session *SessionCtx) Disconnect(reason string) error {
if err := session.Send(
message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT,
Message: reason,
}); err != nil {
return err
}
if session.websocketPeer != nil {
if err := session.websocketPeer.Destroy(); err != nil {
return err
}
}
if session.webrtcPeer != nil {
if err := session.webrtcPeer.Destroy(); err != nil {
return err
}
}
return nil
}
// ---
// webrtc
// ---

View File

@ -16,8 +16,8 @@ type Session interface {
// websocket
SetWebSocketPeer(websocketPeer WebSocketPeer)
SetWebSocketConnected(websocketPeer WebSocketPeer, connected bool)
GetWebSocketPeer() WebSocketPeer
Send(v interface{}) error
Disconnect(reason string) error
// webrtc
SetWebRTCPeer(webrtcPeer WebRTCPeer)

22
internal/utils/errors.go Normal file
View File

@ -0,0 +1,22 @@
package utils
import "fmt"
func ErrorsJoin(errs []error) error {
var resErr error
for _, err := range errs {
if err == nil {
continue
}
if resErr == nil {
resErr = err
continue
}
resErr = fmt.Errorf("%w; %s", resErr, err.Error())
}
return resErr
}

View File

@ -6,7 +6,10 @@ import (
"github.com/gorilla/websocket"
"demodesk/neko/internal/utils"
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
)
type WebSocketPeerCtx struct {
@ -43,5 +46,19 @@ func (peer *WebSocketPeerCtx) Destroy() error {
return nil
}
return peer.connection.Close()
var errs []error
// send disconnect
err := peer.Send(
message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT,
Message: "connection destroyed",
})
errs = append(errs, err)
// close connection
err = peer.connection.Close()
errs = append(errs, err)
return utils.ErrorsJoin(errs)
}