From 3528766e7f97cfc4ac9b236a61b3a2367985c203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 1 Nov 2021 17:49:02 +0100 Subject: [PATCH] inactive cursors to config. --- internal/config/session.go | 7 ++++ internal/session/manager.go | 4 ++ internal/session/session.go | 4 +- internal/types/session.go | 1 + internal/websocket/manager.go | 74 +++++++++++++++++++---------------- 5 files changed, 56 insertions(+), 34 deletions(-) diff --git a/internal/config/session.go b/internal/config/session.go index 1cff4b32..182e3f62 100644 --- a/internal/config/session.go +++ b/internal/config/session.go @@ -9,6 +9,7 @@ import ( type Session struct { ImplicitHosting bool + InactiveCursors bool MercifulReconnect bool APIToken string @@ -24,6 +25,11 @@ func (Session) Init(cmd *cobra.Command) error { return err } + cmd.PersistentFlags().Bool("session.inactive_cursors", true, "show inactive cursors on the screen") + if err := viper.BindPFlag("session.inactive_cursors", cmd.PersistentFlags().Lookup("session.inactive_cursors")); err != nil { + return err + } + cmd.PersistentFlags().Bool("session.merciful_reconnect", true, "allow reconnecting to websocket even if previous connection was not closed") if err := viper.BindPFlag("session.merciful_reconnect", cmd.PersistentFlags().Lookup("session.merciful_reconnect")); err != nil { return err @@ -60,6 +66,7 @@ func (Session) Init(cmd *cobra.Command) error { func (s *Session) Set() { s.ImplicitHosting = viper.GetBool("session.implicit_hosting") + s.InactiveCursors = viper.GetBool("session.inactive_cursors") s.MercifulReconnect = viper.GetBool("session.merciful_reconnect") s.APIToken = viper.GetString("session.api_token") diff --git a/internal/session/manager.go b/internal/session/manager.go index 549e2a9b..d7697ab9 100644 --- a/internal/session/manager.go +++ b/internal/session/manager.go @@ -312,6 +312,10 @@ func (manager *SessionManagerCtx) ImplicitHosting() bool { return manager.config.ImplicitHosting } +func (manager *SessionManagerCtx) InactiveCursors() bool { + return manager.config.InactiveCursors +} + func (manager *SessionManagerCtx) CookieEnabled() bool { return manager.config.CookieEnabled } diff --git a/internal/session/session.go b/internal/session/session.go index d46458d5..79461927 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -54,7 +54,9 @@ func (session *SessionCtx) IsHost() bool { } func (session *SessionCtx) SetCursor(cursor types.Cursor) { - session.manager.SetCursor(cursor, session) + if session.manager.InactiveCursors() { + session.manager.SetCursor(cursor, session) + } } // --- diff --git a/internal/types/session.go b/internal/types/session.go index 82d28210..dd96f924 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -70,6 +70,7 @@ type SessionManager interface { OnHostChanged(listener func(session Session)) ImplicitHosting() bool + InactiveCursors() bool CookieEnabled() bool MercifulReconnect() bool diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index ccd403c1..b81e8cc1 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -20,6 +20,9 @@ import ( // send pings to peer with this period - must be less than pongWait const pingPeriod = 10 * time.Second +// period for sending inactive cursor messages +const inactiveCursorsPeriod = 500 * time.Millisecond + func New( sessions types.SessionManager, desktop types.DesktopManager, @@ -132,45 +135,50 @@ func (manager *WebSocketManagerCtx) Start() { manager.fileChooserDialogEvents() - manager.wg.Add(1) - go func() { - defer manager.wg.Done() + if manager.sessions.InactiveCursors() { + manager.logger.Info().Msg("starting inactive cursors handler") - ticker := time.NewTicker(500 * time.Millisecond) - defer ticker.Stop() + manager.wg.Add(1) + go func() { + defer manager.wg.Done() - lastEmpty := false + ticker := time.NewTicker(inactiveCursorsPeriod) + defer ticker.Stop() - for { - select { - case <-manager.shutdown: - return - case <-ticker.C: - cursorsMap := manager.sessions.PopCursors() + lastEmpty := false - length := len(cursorsMap) - if length == 0 && lastEmpty { - continue + for { + select { + case <-manager.shutdown: + manager.logger.Info().Msg("stopping inactive cursors handler") + return + case <-ticker.C: + cursorsMap := manager.sessions.PopCursors() + + length := len(cursorsMap) + if length == 0 && lastEmpty { + continue + } + lastEmpty = length == 0 + + cursors := []message.SessionCursor{} + for session, cursor := range cursorsMap { + cursors = append( + cursors, + message.SessionCursor{ + ID: session.ID(), + X: uint16(cursor.X), + Y: uint16(cursor.Y), + }, + ) + } + + // TODO: Send to subscribers only. + manager.sessions.AdminBroadcast(event.SESSION_CURSORS, cursors, nil) } - lastEmpty = length == 0 - - cursors := []message.SessionCursor{} - for session, cursor := range cursorsMap { - cursors = append( - cursors, - message.SessionCursor{ - ID: session.ID(), - X: uint16(cursor.X), - Y: uint16(cursor.Y), - }, - ) - } - - // TODO: Send to subscribers only. - manager.sessions.AdminBroadcast(event.SESSION_CURSORS, cursors, nil) } - } - }() + }() + } manager.logger.Info().Msg("websocket starting") }