Destroy() does not return error.

This commit is contained in:
Miroslav Šedivý 2021-08-29 19:17:10 +02:00
parent a03507227d
commit a95a987fb6
8 changed files with 37 additions and 63 deletions

View File

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

View File

@ -31,15 +31,11 @@ func (session *SessionCtx) profileChanged() {
} }
if (!session.profile.CanConnect || !session.profile.CanLogin || !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.webrtcPeer.Destroy()
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
}
} }
if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected { if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected {
if err := session.websocketPeer.Destroy(); err != nil { session.websocketPeer.Destroy()
session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
}
} }
} }
@ -57,9 +53,7 @@ func (session *SessionCtx) IsHost() bool {
func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) { func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
if session.websocketPeer != nil { if session.websocketPeer != nil {
if err := session.websocketPeer.Destroy(); err != nil { session.websocketPeer.Destroy()
session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
}
} }
session.websocketPeer = websocketPeer session.websocketPeer = websocketPeer
@ -99,9 +93,7 @@ func (session *SessionCtx) Send(v interface{}) error {
func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) { func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) {
if session.webrtcPeer != nil { if session.webrtcPeer != nil {
if err := session.webrtcPeer.Destroy(); err != nil { session.webrtcPeer.Destroy()
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
}
} }
session.webrtcPeer = webrtcPeer session.webrtcPeer = webrtcPeer

View File

@ -26,7 +26,7 @@ type WebRTCPeer interface {
SendCursorPosition(x, y int) error SendCursorPosition(x, y int) error
SendCursorImage(cur *CursorImage, img []byte) error SendCursorImage(cur *CursorImage, img []byte) error
Destroy() error Destroy()
} }
type WebRTCManager interface { type WebRTCManager interface {

View File

@ -8,7 +8,7 @@ type CheckOrigin func(r *http.Request) bool
type WebSocketPeer interface { type WebSocketPeer interface {
Send(v interface{}) error Send(v interface{}) error
Destroy() error Destroy()
} }
type WebSocketManager interface { type WebSocketManager interface {

View File

@ -1,22 +0,0 @@
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

@ -65,13 +65,21 @@ func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
return peer.changeVideo(videoID) return peer.changeVideo(videoID)
} }
func (peer *WebRTCPeerCtx) Destroy() error { func (peer *WebRTCPeerCtx) Destroy() {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil || peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected { if peer.connection == nil || peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
return nil return
} }
return peer.connection.Close() // TODO: Send webrtc disconnect event via websocket.
if err := peer.connection.Close(); err != nil {
peer.logger.Warn().Err(err).Msg("peer connection destroyed with an error")
} else {
peer.logger.Info().Msg("peer connection destroyed")
}
peer.connection = nil
} }

View File

@ -236,10 +236,8 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
logger.Info().Msg("replacing peer connection") logger.Info().Msg("replacing peer connection")
// replace peer connection // destroy previous peer connection
if err := session.GetWebSocketPeer().Destroy(); err != nil { session.GetWebSocketPeer().Destroy()
logger.Warn().Err(err).Msg("previous connection closed with an error")
}
} }
peer := &WebSocketPeerCtx{ peer := &WebSocketPeerCtx{

View File

@ -10,7 +10,6 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/types/event" "demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message" "demodesk/neko/internal/types/message"
"demodesk/neko/internal/utils"
) )
type WebSocketPeerCtx struct { type WebSocketPeerCtx struct {
@ -41,24 +40,27 @@ func (peer *WebSocketPeerCtx) Send(v interface{}) error {
return peer.connection.WriteMessage(websocket.TextMessage, raw) return peer.connection.WriteMessage(websocket.TextMessage, raw)
} }
func (peer *WebSocketPeerCtx) Destroy() error { func (peer *WebSocketPeerCtx) Destroy() {
peer.mu.Lock()
defer peer.mu.Unlock()
if peer.connection == nil { if peer.connection == nil {
return nil return
} }
var errs []error if err := peer.Send(
// send disconnect
err := peer.Send(
message.SystemDisconnect{ message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT, Event: event.SYSTEM_DISCONNECT,
Message: "connection destroyed", Message: "connection destroyed",
}) }); err != nil {
errs = append(errs, err) peer.logger.Warn().Err(err).Msg("failed to send disconnect event")
}
// close connection
err = peer.connection.Close() if err := peer.connection.Close(); err != nil {
errs = append(errs, err) peer.logger.Warn().Err(err).Msg("peer connection destroyed with an error")
} else {
return utils.ErrorsJoin(errs) peer.logger.Info().Msg("peer connection destroyed")
}
peer.connection = nil
} }