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)
manager.sessionsMu.Unlock()
var errs []error
if session.State().IsConnected {
err := session.GetWebSocketPeer().Destroy()
errs = append(errs, err)
session.GetWebSocketPeer().Destroy()
}
if session.State().IsWatching {
err := session.GetWebRTCPeer().Destroy()
errs = append(errs, err)
session.GetWebRTCPeer().Destroy()
}
manager.emmiter.Emit("deleted", session)
return utils.ErrorsJoin(errs)
return nil
}
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 err := session.webrtcPeer.Destroy(); err != nil {
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
}
session.webrtcPeer.Destroy()
}
if (!session.profile.CanConnect || !session.profile.CanLogin) && session.state.IsConnected {
if err := session.websocketPeer.Destroy(); err != nil {
session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
}
session.websocketPeer.Destroy()
}
}
@ -57,9 +53,7 @@ func (session *SessionCtx) IsHost() bool {
func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) {
if session.websocketPeer != nil {
if err := session.websocketPeer.Destroy(); err != nil {
session.logger.Warn().Err(err).Msgf("websocket destroy has failed")
}
session.websocketPeer.Destroy()
}
session.websocketPeer = websocketPeer
@ -99,9 +93,7 @@ func (session *SessionCtx) Send(v interface{}) error {
func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) {
if session.webrtcPeer != nil {
if err := session.webrtcPeer.Destroy(); err != nil {
session.logger.Warn().Err(err).Msgf("webrtc destroy has failed")
}
session.webrtcPeer.Destroy()
}
session.webrtcPeer = webrtcPeer

View File

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

View File

@ -8,7 +8,7 @@ type CheckOrigin func(r *http.Request) bool
type WebSocketPeer interface {
Send(v interface{}) error
Destroy() error
Destroy()
}
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)
}
func (peer *WebRTCPeerCtx) Destroy() error {
func (peer *WebRTCPeerCtx) Destroy() {
peer.mu.Lock()
defer peer.mu.Unlock()
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")
// replace peer connection
if err := session.GetWebSocketPeer().Destroy(); err != nil {
logger.Warn().Err(err).Msg("previous connection closed with an error")
}
// destroy previous peer connection
session.GetWebSocketPeer().Destroy()
}
peer := &WebSocketPeerCtx{

View File

@ -10,7 +10,6 @@ import (
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
"demodesk/neko/internal/utils"
)
type WebSocketPeerCtx struct {
@ -41,24 +40,27 @@ func (peer *WebSocketPeerCtx) Send(v interface{}) error {
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 {
return nil
return
}
var errs []error
// send disconnect
err := peer.Send(
if err := peer.Send(
message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT,
Message: "connection destroyed",
})
errs = append(errs, err)
}); err != nil {
peer.logger.Warn().Err(err).Msg("failed to send disconnect event")
}
// close connection
err = peer.connection.Close()
errs = append(errs, err)
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")
}
return utils.ErrorsJoin(errs)
peer.connection = nil
}