Skip to content

Commit 57aa0e2

Browse files
committed
Generalize async command interface.
1 parent 8291f06 commit 57aa0e2

29 files changed

+572
-546
lines changed

ClientSource/Connection/BotBase.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,6 @@
1414
namespace PokemonAutomation{
1515

1616

17-
BotBaseContext::BotBaseContext(BotBase& botbase)
18-
: m_botbase(botbase)
19-
{}
20-
BotBaseContext::BotBaseContext(CancellableScope& parent, BotBase& botbase)
21-
: m_botbase(botbase)
22-
{
23-
attach(parent);
24-
}
25-
BotBaseContext::BotBaseContext(CancellableScope& parent, BotBaseContext& context)
26-
: m_botbase(context.botbase())
27-
{
28-
attach(parent);
29-
}
30-
BotBaseContext::~BotBaseContext(){
31-
detach();
32-
}
3317

3418
void BotBaseContext::wait_for_all_requests() const{
3519
m_lifetime_sanitizer.check_usage();

ClientSource/Connection/BotBase.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ namespace PokemonAutomation{
1515
class Logger;
1616
struct BotBaseMessage;
1717
class BotBaseRequest;
18+
class BotBaseContext;
1819

1920

2021

2122
class BotBase{
2223
public:
24+
using ContextType = BotBaseContext;
25+
2326
enum class State{
2427
RUNNING,
2528
STOPPING,
@@ -70,30 +73,48 @@ class BotBase{
7073

7174

7275

76+
class ControllerContext : public CancellableScope{
77+
public:
78+
virtual void wait_for_all_requests() const = 0;
79+
virtual void cancel_now() = 0;
80+
virtual void cancel_lazy() = 0;
81+
};
82+
83+
7384
// A wrapper for BotBase that allows for asynchronous cancelling.
74-
class BotBaseContext final : public CancellableScope{
85+
class BotBaseContext final : public ControllerContext{
86+
public:
87+
using ControllerType = BotBase;
88+
7589
public:
76-
BotBaseContext(BotBase& botbase);
77-
BotBaseContext(CancellableScope& parent, BotBase& botbase);
78-
BotBaseContext(CancellableScope& parent, BotBaseContext& context);
79-
virtual ~BotBaseContext();
90+
BotBaseContext(BotBase& botbase)
91+
: m_botbase(botbase)
92+
{}
93+
BotBaseContext(CancellableScope& parent, BotBase& botbase)
94+
: m_botbase(botbase)
95+
{
96+
attach(parent);
97+
}
98+
virtual ~BotBaseContext(){
99+
detach();
100+
}
80101

81102

82-
void wait_for_all_requests() const;
103+
virtual void wait_for_all_requests() const override;
83104

84-
// Don't use this unless you really need to.
85-
BotBase& botbase() const{ return m_botbase; }
105+
operator BotBase&() const{ return m_botbase; }
106+
BotBase& controller() const{ return m_botbase; }
86107

87108
// Stop all commands in this context now.
88-
void cancel_now();
109+
virtual void cancel_now() override;
89110

90111
// Stop the commands in this context, but do it lazily.
91112
// Still will stop new commands from being issued to the device,
92113
// and will tell the device that the next command that is issued
93114
// should replace the command queue.
94115
// This cancel is used when you need continuity from an ongoing
95116
// sequence.
96-
void cancel_lazy();
117+
virtual void cancel_lazy() override;
97118

98119

99120
virtual bool cancel(std::exception_ptr exception) noexcept override;

SerialPrograms/CMakeLists.txt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,6 @@ file(GLOB MAIN_SOURCES
426426
Source/CommonFramework/Logging/QueuedLogger.cpp
427427
Source/CommonFramework/Logging/QueuedLogger.h
428428
Source/CommonFramework/Main.cpp
429-
Source/CommonFramework/NewVersionCheck.cpp
430-
Source/CommonFramework/NewVersionCheck.h
431429
Source/CommonFramework/Notifications/EventNotificationOption.cpp
432430
Source/CommonFramework/Notifications/EventNotificationOption.h
433431
Source/CommonFramework/Notifications/EventNotificationsTable.cpp
@@ -525,8 +523,10 @@ file(GLOB MAIN_SOURCES
525523
Source/CommonFramework/Recording/StreamRecorder.h
526524
Source/CommonFramework/Resources/SpriteDatabase.cpp
527525
Source/CommonFramework/Resources/SpriteDatabase.h
528-
Source/CommonFramework/SetupSettings.cpp
529-
Source/CommonFramework/SetupSettings.h
526+
Source/CommonFramework/Startup/NewVersionCheck.cpp
527+
Source/CommonFramework/Startup/NewVersionCheck.h
528+
Source/CommonFramework/Startup/SetupSettings.cpp
529+
Source/CommonFramework/Startup/SetupSettings.h
530530
Source/CommonFramework/Tools/BlackBorderCheck.cpp
531531
Source/CommonFramework/Tools/BlackBorderCheck.h
532532
Source/CommonFramework/Tools/BotBaseHandle.cpp
@@ -2160,7 +2160,14 @@ if (MSVC)
21602160
)
21612161

21622162
target_link_libraries(SerialPrograms PRIVATE tesseractPA.lib OpenCV_lib Sleepy.lib dpp_lib)
2163-
target_compile_definitions(SerialPrograms PRIVATE PA_TESSERACT PA_SLEEPY PA_DPP _CRT_SECURE_NO_WARNINGS)
2163+
target_compile_definitions(
2164+
SerialPrograms PRIVATE
2165+
_CRT_SECURE_NO_WARNINGS
2166+
_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR
2167+
PA_TESSERACT
2168+
PA_SLEEPY
2169+
PA_DPP
2170+
)
21642171

21652172
target_compile_options(SerialPrograms PRIVATE /FAs /FaAssembly/ /MP /W4 /WX /utf-8)
21662173
target_compile_options(SerialPrograms PRIVATE /wd5054) # Deprecated enum arithemtic

SerialPrograms/SerialPrograms.pro

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ SOURCES += \
232232
Source/CommonFramework/Logging/OutputRedirector.cpp \
233233
Source/CommonFramework/Logging/QueuedLogger.cpp \
234234
Source/CommonFramework/Main.cpp \
235-
Source/CommonFramework/NewVersionCheck.cpp \
236235
Source/CommonFramework/Notifications/EventNotificationOption.cpp \
237236
Source/CommonFramework/Notifications/EventNotificationsTable.cpp \
238237
Source/CommonFramework/Notifications/MessageAttachment.cpp \
@@ -276,7 +275,8 @@ SOURCES += \
276275
Source/CommonFramework/Recording/StreamHistorySession.cpp \
277276
Source/CommonFramework/Recording/StreamRecorder.cpp \
278277
Source/CommonFramework/Resources/SpriteDatabase.cpp \
279-
Source/CommonFramework/SetupSettings.cpp \
278+
Source/CommonFramework/Startup/NewVersionCheck.cpp \
279+
Source/CommonFramework/Startup/SetupSettings.cpp \
280280
Source/CommonFramework/Tools/BlackBorderCheck.cpp \
281281
Source/CommonFramework/Tools/BotBaseHandle.cpp \
282282
Source/CommonFramework/Tools/DebugDumper.cpp \
@@ -1325,7 +1325,6 @@ HEADERS += \
13251325
Source/CommonFramework/Logging/Logger.h \
13261326
Source/CommonFramework/Logging/OutputRedirector.h \
13271327
Source/CommonFramework/Logging/QueuedLogger.h \
1328-
Source/CommonFramework/NewVersionCheck.h \
13291328
Source/CommonFramework/Notifications/EventNotificationOption.h \
13301329
Source/CommonFramework/Notifications/EventNotificationsTable.h \
13311330
Source/CommonFramework/Notifications/MessageAttachment.h \
@@ -1375,7 +1374,8 @@ HEADERS += \
13751374
Source/CommonFramework/Recording/StreamHistoryTracker_SaveFrames.h \
13761375
Source/CommonFramework/Recording/StreamRecorder.h \
13771376
Source/CommonFramework/Resources/SpriteDatabase.h \
1378-
Source/CommonFramework/SetupSettings.h \
1377+
Source/CommonFramework/Startup/NewVersionCheck.h \
1378+
Source/CommonFramework/Startup/SetupSettings.h \
13791379
Source/CommonFramework/Tools/BlackBorderCheck.h \
13801380
Source/CommonFramework/Tools/BotBaseHandle.h \
13811381
Source/CommonFramework/Tools/DebugDumper.h \

SerialPrograms/Source/CommonFramework/InferenceInfra/InferenceRoutines.cpp

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "Common/Cpp/Exceptions.h"
88
#include "Common/Cpp/CancellableScope.h"
9+
#include "Common/Cpp/Concurrency/AsyncDispatcher.h"
10+
#include "CommonFramework/Tools/ProgramEnvironment.h"
911
#include "InferenceSession.h"
1012
#include "InferenceRoutines.h"
1113

@@ -59,80 +61,50 @@ int run_until(
5961
);
6062

6163
try{
62-
if (command){
63-
command(subscope);
64-
}
64+
command(subscope);
6565
}catch (OperationCancelledException&){}
6666

6767
subscope.throw_if_cancelled_with_exception();
6868
scope.throw_if_cancelled();
6969

7070
return session.triggered_index();
7171
}
72-
#if 0
73-
int run_until(
74-
VideoStream& stream, BotBaseContext& context,
75-
std::function<void(BotBaseContext& context)>&& command,
76-
const std::vector<PeriodicInferenceCallback>& callbacks,
77-
std::chrono::milliseconds default_video_period,
78-
std::chrono::milliseconds default_audio_period
79-
){
80-
BotBaseContext subcontext(context, context.botbase());
81-
InferenceSession session(
82-
subcontext, stream,
83-
callbacks,
84-
default_video_period, default_audio_period
85-
);
86-
87-
try{
88-
if (command){
89-
command(subcontext);
90-
}
91-
subcontext.wait_for_all_requests();
92-
}catch (OperationCancelledException&){}
93-
94-
subcontext.throw_if_cancelled_with_exception();
95-
context.throw_if_cancelled();
96-
97-
return session.triggered_index();
98-
}
99-
#endif
10072

10173

10274

103-
#if 0
75+
#if 1
10476
int run_until_with_time_limit(
105-
ProgramEnvironment& env, VideoStream& stream, BotBaseContext& context,
77+
ProgramEnvironment& env, VideoStream& stream, CancellableScope& scope,
10678
WallClock deadline,
107-
std::function<void(BotBaseContext& context)>&& command,
79+
std::function<void(CancellableScope& scope)>&& command,
10880
const std::vector<PeriodicInferenceCallback>& callbacks,
10981
std::chrono::milliseconds default_video_period,
11082
std::chrono::milliseconds default_audio_period
11183
){
112-
BotBaseContext subcontext(context, context.botbase());
84+
CancellableHolder<CancellableScope> subscope(scope);
11385
InferenceSession session(
114-
subcontext, stream,
86+
subscope, stream,
11587
callbacks,
11688
default_video_period, default_audio_period
11789
);
11890

11991
bool timed_out = false;
12092
std::unique_ptr<AsyncTask> timer = env.realtime_dispatcher().dispatch([&]{
121-
subcontext.wait_until(deadline);
93+
subscope.wait_until(deadline);
12294
timed_out = true;
123-
subcontext.cancel_now();
95+
subscope.cancel(nullptr);
12496
});
12597

12698
try{
12799
if (command){
128-
command(subcontext);
100+
command(subscope);
129101
}
130-
subcontext.wait_for_all_requests();
102+
// subscope.wait_for_all_requests();
131103
}catch (OperationCancelledException&){}
132104

133105
timer->wait_and_rethrow_exceptions();
134-
subcontext.throw_if_cancelled_with_exception();
135-
context.throw_if_cancelled();
106+
subscope.throw_if_cancelled_with_exception();
107+
scope.throw_if_cancelled();
136108

137109
return timed_out ? -2 : session.triggered_index();
138110
}

SerialPrograms/Source/CommonFramework/InferenceInfra/InferenceRoutines.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,19 @@ int run_until(
6666
std::chrono::milliseconds default_video_period = std::chrono::milliseconds(50),
6767
std::chrono::milliseconds default_audio_period = std::chrono::milliseconds(20)
6868
);
69-
template <typename CommandContext>
69+
template <typename ControllerContext>
7070
int run_until(
71-
VideoStream& stream, CommandContext& context,
72-
std::function<void(CommandContext& context)>&& command,
71+
VideoStream& stream, ControllerContext& context,
72+
std::function<void(ControllerContext& context)>&& command,
7373
const std::vector<PeriodicInferenceCallback>& callbacks,
7474
std::chrono::milliseconds default_video_period = std::chrono::milliseconds(50),
7575
std::chrono::milliseconds default_audio_period = std::chrono::milliseconds(20)
7676
){
7777
return run_until(
7878
stream, context,
7979
[&](CancellableScope& scope){
80-
CommandContext subcontext(scope, context);
81-
if (command){
82-
command(subcontext);
83-
}
80+
ControllerContext subcontext(scope, context);
81+
command(subcontext);
8482
subcontext.wait_for_all_requests();
8583
},
8684
callbacks,
@@ -90,7 +88,7 @@ int run_until(
9088
}
9189

9290

93-
#if 0
91+
#if 1
9492
// Same as "run_until()", but will cancel the commands and return if a timeout
9593
// is reached.
9694
//
@@ -100,9 +98,9 @@ int run_until(
10098
// - -2 if timed out. Nothing triggered, and command did not finish.
10199
//
102100
int run_until_with_time_limit(
103-
ProgramEnvironment& env, VideoStream& stream, BotBaseContext& context,
101+
ProgramEnvironment& env, VideoStream& stream, CancellableScope& scope,
104102
WallClock deadline,
105-
std::function<void(BotBaseContext& context)>&& command,
103+
std::function<void(CancellableScope& scope)>&& command,
106104
const std::vector<PeriodicInferenceCallback>& callbacks,
107105
std::chrono::milliseconds default_video_period,
108106
std::chrono::milliseconds default_audio_period

SerialPrograms/Source/CommonFramework/Main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include "Globals.h"
2121
#include "GlobalSettingsPanel.h"
2222
//#include "Windows/DpiScaler.h"
23-
#include "SetupSettings.h"
24-
#include "NewVersionCheck.h"
23+
#include "Startup/SetupSettings.h"
24+
#include "Startup/NewVersionCheck.h"
2525
#include "Windows/MainWindow.h"
2626

2727

0 commit comments

Comments
 (0)