From 57112fd90aa61d1d46d86822fa0eb30ad260feca Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 28 Jun 2024 10:59:34 +0100 Subject: [PATCH] added a timeout to the login method module Util::Timeout added, prepends Base --- lib/voicemeeter/base.rb | 6 +++--- lib/voicemeeter/util.rb | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/voicemeeter/base.rb b/lib/voicemeeter/base.rb index 96043bb..069b951 100644 --- a/lib/voicemeeter/base.rb +++ b/lib/voicemeeter/base.rb @@ -4,18 +4,21 @@ module Voicemeeter include Logging include Worker include Events::Director + prepend Util::Timeout prepend Util::Cache attr_reader :kind, :midi, :event, :delay, :cache RATELIMIT = 0.033 DELAY = 0.001 + LOGIN_TIMEOUT = 2 def initialize(kind, **kwargs) @kind = kind @sync = kwargs[:sync] || false @ratelimit = kwargs[:ratelimit] || RATELIMIT @delay = kwargs[:delay] || DELAY + @login_timeout = kwargs[:login_timeout] || LOGIN_TIMEOUT @event = Events::Tracker.new( **(kwargs.select { |k, _| %i[pdirty mdirty ldirty midi].include? k }) @@ -30,8 +33,6 @@ module Voicemeeter def login CBindings.call(:bind_login, ok: [0, 1]) == 1 and run_voicemeeter(kind.name) - clear_dirty - logger.info "Successfully logged into #{self} version #{version}" end def logout @@ -72,7 +73,6 @@ module Voicemeeter logger.debug "Voicemeeter engine running but the GUI appears to be down... launching." end CBindings.call(:bind_run_voicemeeter, kinds[kind_id]) - sleep(1) end def type diff --git a/lib/voicemeeter/util.rb b/lib/voicemeeter/util.rb index af54ff0..05de2b6 100644 --- a/lib/voicemeeter/util.rb +++ b/lib/voicemeeter/util.rb @@ -18,6 +18,26 @@ module Voicemeeter end end + module Timeout + def login + super + + err = nil + start = Time.now + begin + sleep 0.1 + logger.info "Successfully logged into #{self} version #{version}" + logger.debug "login time: #{(Time.now - start).round(2)}" + err = nil + rescue Errors::VMCAPIError => e + err = e + retry if Time.now < start + @login_timeout + end + raise Errors::VMError.new "Timeout logging into the api" if err + clear_dirty + end + end + module Cache def get(name, is_string = false) return cache.delete(name) if cache.key? name