Broadcast should not return error.

This commit is contained in:
Miroslav Šedivý 2020-11-18 20:30:33 +01:00
parent 3f4914d260
commit da7e6f7728
11 changed files with 103 additions and 90 deletions

View File

@ -24,13 +24,11 @@ func (h *RoomHandler) ControlRequest(w http.ResponseWriter, r *http.Request) {
h.sessions.SetHost(session) h.sessions.SetHost(session)
if err := h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.Control{
Event: event.CONTROL_LOCKED, Event: event.CONTROL_LOCKED,
ID: session.ID(), ID: session.ID(),
}, nil); err != nil { }, nil)
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED)
}
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
@ -43,14 +41,12 @@ func (h *RoomHandler) ControlRelease(w http.ResponseWriter, r *http.Request) {
} }
h.sessions.ClearHost() h.sessions.ClearHost()
if err := h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.Control{
Event: event.CONTROL_RELEASE, Event: event.CONTROL_RELEASE,
ID: session.ID(), ID: session.ID(),
}, nil); err != nil { }, nil)
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
}
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }

View File

@ -42,16 +42,15 @@ func (h *RoomHandler) ScreenConfigurationChange(w http.ResponseWriter, r *http.R
} }
session := auth.GetSession(r) session := auth.GetSession(r)
if err := h.sessions.Broadcast(
h.sessions.Broadcast(
message.ScreenResolution{ message.ScreenResolution{
Event: event.SCREEN_RESOLUTION, Event: event.SCREEN_RESOLUTION,
ID: session.ID(), ID: session.ID(),
Width: data.Width, Width: data.Width,
Height: data.Height, Height: data.Height,
Rate: data.Rate, Rate: data.Rate,
}, nil); err != nil { }, nil)
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.SCREEN_RESOLUTION)
}
utils.HttpSuccess(w, data) utils.HttpSuccess(w, data)
} }

View File

@ -142,7 +142,7 @@ func (manager *SessionManagerCtx) Members() []types.Session {
return sessions return sessions
} }
func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) error { func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) {
manager.membersMu.Lock() manager.membersMu.Lock()
defer manager.membersMu.Unlock() defer manager.membersMu.Unlock()
@ -158,11 +158,9 @@ func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{})
} }
if err := session.Send(v); err != nil { if err := session.Send(v); err != nil {
return err manager.logger.Warn().Err(err).Msgf("broadcasting event has failed")
} }
} }
return nil
} }
// --- // ---

View File

@ -67,20 +67,13 @@ func (session *SessionCtx) SetConnected(connected bool) {
} }
func (session *SessionCtx) Disconnect(reason string) error { func (session *SessionCtx) Disconnect(reason string) error {
if session.socket == nil {
return nil
}
// TODO: Refcator
if err := session.Send(&message.Disconnect{
Event: event.SYSTEM_DISCONNECT,
Message: reason,
}); err != nil {
return err
}
session.SetConnected(false) session.SetConnected(false)
return nil
return session.Send(
message.Disconnect{
Event: event.SYSTEM_DISCONNECT,
Message: reason,
})
} }
func (session *SessionCtx) Send(v interface{}) error { func (session *SessionCtx) Send(v interface{}) error {

View File

@ -29,7 +29,7 @@ type SessionManager interface {
Admins() []Session Admins() []Session
Members() []Session Members() []Session
Broadcast(v interface{}, exclude interface{}) error Broadcast(v interface{}, exclude interface{})
OnHost(listener func(session Session)) OnHost(listener func(session Session))
OnHostCleared(listener func(session Session)) OnHostCleared(listener func(session Session))

View File

@ -19,11 +19,13 @@ func (h *MessageHandlerCtx) adminLock(session types.Session) error {
h.locked = true h.locked = true
return h.sessions.Broadcast( h.sessions.Broadcast(
message.Admin{ message.Admin{
Event: event.ADMIN_LOCK, Event: event.ADMIN_LOCK,
ID: session.ID(), ID: session.ID(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) adminUnlock(session types.Session) error { func (h *MessageHandlerCtx) adminUnlock(session types.Session) error {
@ -39,11 +41,13 @@ func (h *MessageHandlerCtx) adminUnlock(session types.Session) error {
h.locked = false h.locked = false
return h.sessions.Broadcast( h.sessions.Broadcast(
message.Admin{ message.Admin{
Event: event.ADMIN_UNLOCK, Event: event.ADMIN_UNLOCK,
ID: session.ID(), ID: session.ID(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) adminControl(session types.Session) error { func (h *MessageHandlerCtx) adminControl(session types.Session) error {
@ -56,19 +60,21 @@ func (h *MessageHandlerCtx) adminControl(session types.Session) error {
h.sessions.SetHost(session) h.sessions.SetHost(session)
if host != nil { if host != nil {
return h.sessions.Broadcast( h.sessions.Broadcast(
message.AdminTarget{ message.AdminTarget{
Event: event.ADMIN_CONTROL, Event: event.ADMIN_CONTROL,
ID: session.ID(), ID: session.ID(),
Target: host.ID(), Target: host.ID(),
}, nil) }, nil)
} else {
h.sessions.Broadcast(
message.Admin{
Event: event.ADMIN_CONTROL,
ID: session.ID(),
}, nil)
} }
return h.sessions.Broadcast( return nil
message.Admin{
Event: event.ADMIN_CONTROL,
ID: session.ID(),
}, nil)
} }
func (h *MessageHandlerCtx) adminRelease(session types.Session) error { func (h *MessageHandlerCtx) adminRelease(session types.Session) error {
@ -81,19 +87,21 @@ func (h *MessageHandlerCtx) adminRelease(session types.Session) error {
h.sessions.ClearHost() h.sessions.ClearHost()
if host != nil { if host != nil {
return h.sessions.Broadcast( h.sessions.Broadcast(
message.AdminTarget{ message.AdminTarget{
Event: event.ADMIN_RELEASE, Event: event.ADMIN_RELEASE,
ID: session.ID(), ID: session.ID(),
Target: host.ID(), Target: host.ID(),
}, nil) }, nil)
} else {
h.sessions.Broadcast(
message.Admin{
Event: event.ADMIN_RELEASE,
ID: session.ID(),
}, nil)
} }
return h.sessions.Broadcast( return nil
message.Admin{
Event: event.ADMIN_RELEASE,
ID: session.ID(),
}, nil)
} }
func (h *MessageHandlerCtx) adminGive(session types.Session, payload *message.Admin) error { func (h *MessageHandlerCtx) adminGive(session types.Session, payload *message.Admin) error {
@ -110,12 +118,14 @@ func (h *MessageHandlerCtx) adminGive(session types.Session, payload *message.Ad
h.sessions.SetHost(target) h.sessions.SetHost(target)
return h.sessions.Broadcast( h.sessions.Broadcast(
message.AdminTarget{ message.AdminTarget{
Event: event.CONTROL_GIVE, Event: event.CONTROL_GIVE,
ID: session.ID(), ID: session.ID(),
Target: target.ID(), Target: target.ID(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) adminKick(session types.Session, payload *message.Admin) error { func (h *MessageHandlerCtx) adminKick(session types.Session, payload *message.Admin) error {
@ -139,10 +149,12 @@ func (h *MessageHandlerCtx) adminKick(session types.Session, payload *message.Ad
return err return err
} }
return h.sessions.Broadcast( h.sessions.Broadcast(
message.AdminTarget{ message.AdminTarget{
Event: event.ADMIN_KICK, Event: event.ADMIN_KICK,
Target: target.ID(), Target: target.ID(),
ID: session.ID(), ID: session.ID(),
}, []string{payload.ID}) }, []string{payload.ID})
return nil
} }

View File

@ -15,11 +15,13 @@ func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
h.logger.Debug().Str("id", session.ID()).Msgf("host called %s", event.CONTROL_RELEASE) h.logger.Debug().Str("id", session.ID()).Msgf("host called %s", event.CONTROL_RELEASE)
h.sessions.ClearHost() h.sessions.ClearHost()
return h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.Control{
Event: event.CONTROL_RELEASE, Event: event.CONTROL_RELEASE,
ID: session.ID(), ID: session.ID(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) controlRequest(session types.Session) error { func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
@ -30,26 +32,30 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
h.sessions.SetHost(session) h.sessions.SetHost(session)
// let everyone know // let everyone know
return h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.Control{
Event: event.CONTROL_LOCKED, Event: event.CONTROL_LOCKED,
ID: session.ID(), ID: session.ID(),
}, nil) }, nil)
} else {
// tell session there is a host
if err := session.Send(
message.Control{
Event: event.CONTROL_REQUEST,
ID: host.ID(),
}); err != nil {
return err
}
// tell host session wants to be host
return host.Send(
message.Control{
Event: event.CONTROL_REQUESTING,
ID: session.ID(),
})
} }
// tell session there is a host return nil
if err := session.Send(message.Control{
Event: event.CONTROL_REQUEST,
ID: host.ID(),
}); err != nil {
return err
}
// tell host session wants to be host
return host.Send(message.Control{
Event: event.CONTROL_REQUESTING,
ID: session.ID(),
})
} }
func (h *MessageHandlerCtx) controlGive(session types.Session, payload *message.Control) error { func (h *MessageHandlerCtx) controlGive(session types.Session, payload *message.Control) error {
@ -66,12 +72,14 @@ func (h *MessageHandlerCtx) controlGive(session types.Session, payload *message.
h.sessions.SetHost(target) h.sessions.SetHost(target)
return h.sessions.Broadcast( h.sessions.Broadcast(
message.ControlTarget{ message.ControlTarget{
Event: event.CONTROL_GIVE, Event: event.CONTROL_GIVE,
ID: session.ID(), ID: session.ID(),
Target: target.ID(), Target: target.ID(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) controlClipboard(session types.Session, payload *message.Clipboard) error { func (h *MessageHandlerCtx) controlClipboard(session types.Session, payload *message.Clipboard) error {

View File

@ -17,7 +17,7 @@ func (h *MessageHandlerCtx) screenSizeChange(session types.Session, payload *mes
return nil return nil
} }
return h.sessions.Broadcast( h.sessions.Broadcast(
message.ScreenResolution{ message.ScreenResolution{
Event: event.SCREEN_RESOLUTION, Event: event.SCREEN_RESOLUTION,
ID: session.ID(), ID: session.ID(),
@ -25,6 +25,8 @@ func (h *MessageHandlerCtx) screenSizeChange(session types.Session, payload *mes
Height: payload.Height, Height: payload.Height,
Rate: payload.Rate, Rate: payload.Rate,
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) screenSize(session types.Session) error { func (h *MessageHandlerCtx) screenSize(session types.Session) error {

View File

@ -39,10 +39,11 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
} }
// send list of members to session // send list of members to session
if err := session.Send(message.MembersList{ if err := session.Send(
Event: event.MEMBER_LIST, message.MembersList{
Memebers: members, Event: event.MEMBER_LIST,
}); err != nil { Memebers: members,
}); err != nil {
return err return err
} }
@ -54,22 +55,25 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
// tell session there is a host // tell session there is a host
host := h.sessions.GetHost() host := h.sessions.GetHost()
if host != nil { if host != nil {
if err := session.Send(message.Control{ if err := session.Send(
Event: event.CONTROL_LOCKED, message.Control{
ID: host.ID(), Event: event.CONTROL_LOCKED,
}); err != nil { ID: host.ID(),
}); err != nil {
return err return err
} }
} }
// let everyone know there is a new session // let everyone know there is a new session
return h.sessions.Broadcast( h.sessions.Broadcast(
message.Member{ message.Member{
Event: event.MEMBER_CONNECTED, Event: event.MEMBER_CONNECTED,
ID: session.ID(), ID: session.ID(),
Name: session.Name(), Name: session.Name(),
Admin: session.Admin(), Admin: session.Admin(),
}, nil) }, nil)
return nil
} }
func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error { func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
@ -77,20 +81,19 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
if session.IsHost() { if session.IsHost() {
h.sessions.ClearHost() h.sessions.ClearHost()
// gracefully handle error h.sessions.Broadcast(
if err := h.sessions.Broadcast(
message.Control{ message.Control{
Event: event.CONTROL_RELEASE, Event: event.CONTROL_RELEASE,
ID: session.ID(), ID: session.ID(),
}, nil); err != nil { }, nil)
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
}
} }
// let everyone know session disconnected // let everyone know session disconnected
return h.sessions.Broadcast( h.sessions.Broadcast(
message.MemberDisconnected{ message.MemberDisconnected{
Event: event.MEMBER_DISCONNECTED, Event: event.MEMBER_DISCONNECTED,
ID: session.ID(), ID: session.ID(),
}, nil); }, nil);
return nil
} }

View File

@ -12,13 +12,14 @@ func (h *MessageHandlerCtx) signalProvide(session types.Session) error {
return err return err
} }
return session.Send(message.SignalProvide{ return session.Send(
Event: event.SIGNAL_PROVIDE, message.SignalProvide{
ID: session.ID(), Event: event.SIGNAL_PROVIDE,
SDP: sdp, ID: session.ID(),
Lite: lite, SDP: sdp,
ICE: ice, Lite: lite,
}) ICE: ice,
})
} }
func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message.SignalAnswer) error { func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message.SignalAnswer) error {

View File

@ -97,10 +97,11 @@ func (ws *WebSocketManagerCtx) Start() {
break break
} }
if err := session.Send(message.Clipboard{ if err := session.Send(
Event: event.CONTROL_CLIPBOARD, message.Clipboard{
Text: text, Event: event.CONTROL_CLIPBOARD,
}); err != nil { Text: text,
}); err != nil {
ws.logger.Warn().Err(err).Msg("could not sync clipboard") ws.logger.Warn().Err(err).Msg("could not sync clipboard")
} }