Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion Cleanup.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cd %~dp0\ClientProject
@call Cleanup.cmd

cd %~dp0\NativePrograms
@call !Cleanup.cmd
@call Cleanup.cmd

cd %~dp0\GeneratorSource
@call Cleanup.cmd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,17 @@
<ClInclude Include="..\..\ClientSource\Connection\SerialConnectionPOSIX.h" />
<ClInclude Include="..\..\ClientSource\Connection\SerialConnectionWinAPI.h" />
<ClInclude Include="..\..\ClientSource\Connection\StreamInterface.h" />
<ClInclude Include="..\..\ClientSource\Libraries\Compiler.h" />
<ClInclude Include="..\..\ClientSource\Libraries\Logging.h" />
<ClInclude Include="..\..\ClientSource\Libraries\MessageConverter.h" />
<ClInclude Include="..\..\ClientSource\Libraries\Utilities.h" />
<ClInclude Include="..\..\Common\Clientside\PrettyPrint.h" />
<ClInclude Include="..\..\Common\Clientside\AsyncDispatcher.h" />
<ClInclude Include="..\..\Common\Clientside\SpinLock.h" />
<ClInclude Include="..\..\Common\Clientside\Unicode.h" />
<ClInclude Include="..\..\Common\Compiler.h" />
<ClInclude Include="..\..\Common\Cpp\AsyncDispatcher.h" />
<ClInclude Include="..\..\Common\Cpp\Exception.h" />
<ClInclude Include="..\..\Common\Cpp\FixedLimitVector.h" />
<ClInclude Include="..\..\Common\Cpp\PanicDump.h" />
<ClInclude Include="..\..\Common\Cpp\PrettyPrint.h" />
<ClInclude Include="..\..\Common\Cpp\SpinLock.h" />
<ClInclude Include="..\..\Common\Cpp\Unicode.h" />
<ClInclude Include="..\..\Common\CRC32.h" />
<ClInclude Include="..\..\Common\MessageProtocol.h" />
<ClInclude Include="..\..\Common\PokemonSwSh\PokemonProgramIDs.h" />
Expand Down Expand Up @@ -193,9 +196,11 @@
<ClCompile Include="..\..\ClientSource\Programs\FriendDelete.cpp" />
<ClCompile Include="..\..\ClientSource\Programs\TurboA.cpp" />
<ClCompile Include="..\..\ClientSource\Programs\DateSpam-WattFarmer.cpp" />
<ClCompile Include="..\..\Common\Clientside\PrettyPrint.cpp" />
<ClCompile Include="..\..\Common\Clientside\AsyncDispatcher.cpp" />
<ClCompile Include="..\..\Common\Clientside\Unicode.cpp" />
<ClCompile Include="..\..\Common\Cpp\AsyncDispatcher.cpp" />
<ClCompile Include="..\..\Common\Cpp\Exception.cpp" />
<ClCompile Include="..\..\Common\Cpp\PanicDump.cpp" />
<ClCompile Include="..\..\Common\Cpp\PrettyPrint.cpp" />
<ClCompile Include="..\..\Common\Cpp\Unicode.cpp" />
<ClCompile Include="..\..\Common\CRC32.c">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
<Filter Include="Source Files\Common">
<UniqueIdentifier>{227cd5ac-1fca-4f18-a5c4-e517dd526f05}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Common\Clientside">
<UniqueIdentifier>{8d23dd57-0446-4723-8b44-05293f49c650}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Common\PokemonSwSh">
<UniqueIdentifier>{ca670832-1439-443e-a6b2-6bf5b48ad8d3}</UniqueIdentifier>
</Filter>
Expand All @@ -39,6 +36,9 @@
<Filter Include="Source Files\Common\SwitchFramework">
<UniqueIdentifier>{10ade231-7f71-4505-a596-091dfe263106}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Common\Cpp">
<UniqueIdentifier>{8d23dd57-0446-4723-8b44-05293f49c650}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\ClientSource\Connection\SerialConnection.h">
Expand All @@ -65,27 +65,12 @@
<ClInclude Include="..\..\ClientSource\Libraries\Logging.h">
<Filter>Source Files\ClientSource\Libraries</Filter>
</ClInclude>
<ClInclude Include="..\..\ClientSource\Libraries\Compiler.h">
<Filter>Source Files\ClientSource\Libraries</Filter>
</ClInclude>
<ClInclude Include="..\..\ClientSource\Libraries\Utilities.h">
<Filter>Source Files\ClientSource\Libraries</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Clientside\PrettyPrint.h">
<Filter>Source Files\Common\Clientside</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Clientside\Unicode.h">
<Filter>Source Files\Common\Clientside</Filter>
</ClInclude>
<ClInclude Include="..\..\ClientSource\Connection\BotBase.h">
<Filter>Source Files\ClientSource\Connection</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Clientside\AsyncDispatcher.h">
<Filter>Source Files\Common\Clientside</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Clientside\SpinLock.h">
<Filter>Source Files\Common\Clientside</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\PokemonSwSh\PokemonProgramIDs.h">
<Filter>Source Files\Common\PokemonSwSh</Filter>
</ClInclude>
Expand Down Expand Up @@ -128,6 +113,30 @@
<ClInclude Include="..\..\Common\SwitchFramework\Switch_PushButtons.h">
<Filter>Source Files\Common\SwitchFramework</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Cpp\AsyncDispatcher.h">
<Filter>Source Files\Common\Cpp</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Cpp\Exception.h">
<Filter>Source Files\Common\Cpp</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Cpp\FixedLimitVector.h">
<Filter>Source Files\Common\Cpp</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Cpp\PanicDump.h">
<Filter>Source Files\Common\Cpp</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Cpp\PrettyPrint.h">
<Filter>Source Files\Common\Cpp</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Cpp\SpinLock.h">
<Filter>Source Files\Common\Cpp</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Cpp\Unicode.h">
<Filter>Source Files\Common\Cpp</Filter>
</ClInclude>
<ClInclude Include="..\..\Common\Compiler.h">
<Filter>Source Files\Common</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\ClientSource\Connection\PABotBaseConnection.cpp">
Expand Down Expand Up @@ -169,15 +178,6 @@
<ClCompile Include="..\..\ClientSource\Programs\DateSpam-WattFarmer.cpp">
<Filter>Source Files\ClientSource\Programs</Filter>
</ClCompile>
<ClCompile Include="..\..\Common\Clientside\PrettyPrint.cpp">
<Filter>Source Files\Common\Clientside</Filter>
</ClCompile>
<ClCompile Include="..\..\Common\Clientside\Unicode.cpp">
<Filter>Source Files\Common\Clientside</Filter>
</ClCompile>
<ClCompile Include="..\..\Common\Clientside\AsyncDispatcher.cpp">
<Filter>Source Files\Common\Clientside</Filter>
</ClCompile>
<ClCompile Include="..\..\Common\PokemonSwSh\PokemonSettings.c">
<Filter>Source Files\Common\PokemonSwSh</Filter>
</ClCompile>
Expand Down Expand Up @@ -220,5 +220,20 @@
<ClCompile Include="..\..\Common\SwitchFramework\Switch_PushButtons.cpp">
<Filter>Source Files\Common\SwitchFramework</Filter>
</ClCompile>
<ClCompile Include="..\..\Common\Cpp\PanicDump.cpp">
<Filter>Source Files\Common\Cpp</Filter>
</ClCompile>
<ClCompile Include="..\..\Common\Cpp\PrettyPrint.cpp">
<Filter>Source Files\Common\Cpp</Filter>
</ClCompile>
<ClCompile Include="..\..\Common\Cpp\Unicode.cpp">
<Filter>Source Files\Common\Cpp</Filter>
</ClCompile>
<ClCompile Include="..\..\Common\Cpp\AsyncDispatcher.cpp">
<Filter>Source Files\Common\Cpp</Filter>
</ClCompile>
<ClCompile Include="..\..\Common\Cpp\Exception.cpp">
<Filter>Source Files\Common\Cpp</Filter>
</ClCompile>
</ItemGroup>
</Project>
83 changes: 74 additions & 9 deletions ClientSource/Connection/BotBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <stdint.h>
#include <string>
#include <atomic>
#include "Common/MessageProtocol.h"

namespace PokemonAutomation{
Expand Down Expand Up @@ -44,63 +45,127 @@ class BotBase{
virtual ~BotBase() = default;
virtual State state() const = 0;
virtual void wait_for_all_requests() = 0;
virtual void stop_all_commands() = 0;

public:
// Request Dispatch

// Return if request cannot be dispatched immediately.
template <uint8_t SendType, typename SendParams>
bool try_issue_request(SendParams& send_params);
bool try_issue_request(
const std::atomic<bool>* cancelled,
SendParams& send_params
);

// Block the thread until the request is sent.
template <uint8_t SendType, typename SendParams>
void issue_request(SendParams& send_params);
void issue_request(
const std::atomic<bool>* cancelled,
SendParams& send_params
);

// Block the thread until the request is send and the response is received.
// Block the thread until the request is sent and the response is received.
template <
uint8_t SendType, uint8_t RecvType,
typename SendParams, typename RecvParams
>
void issue_request_and_wait(SendParams& send_params, RecvParams& recv_params);
void issue_request_and_wait(
const std::atomic<bool>* cancelled,
SendParams& send_params,
RecvParams& recv_params
);


protected:
virtual bool try_issue_request(
const std::atomic<bool>* cancelled,
uint8_t send_type, char* send_params, size_t send_bytes
) = 0;
virtual void issue_request(
const std::atomic<bool>* cancelled,
uint8_t send_type, char* send_params, size_t send_bytes
) = 0;
virtual void issue_request_and_wait(
const std::atomic<bool>* cancelled,
uint8_t send_type, char* send_params, size_t send_bytes,
uint8_t recv_type, char* recv_params, size_t recv_bytes
) = 0;
};



// A wrapper for BotBase that allows for asynchronous cancelling.
class BotBaseContext{
public:
BotBaseContext(BotBase& botbase)
: m_botbase(&botbase)
, m_cancelled(false)
{}

BotBase& botbase() const{ return *m_botbase; }
// operator BotBase&() const{
// return *m_botbase;
// }
BotBase* operator->() const{
check_cancelled();
return m_botbase;
}
void check_cancelled() const{
if (m_cancelled.load(std::memory_order_acquire)){
throw CancelledException();
}
}
void cancel(){
m_cancelled.store(true, std::memory_order_release);
m_botbase->stop_all_commands();
}

const std::atomic<bool>& cancelled_bool() const{
return m_cancelled;
}

private:
BotBase* m_botbase;
std::atomic<bool> m_cancelled;
};






// Implementations

template <uint8_t SendType, typename SendParams>
bool BotBase::try_issue_request(SendParams& send_params){
bool BotBase::try_issue_request(
const std::atomic<bool>* cancelled,
SendParams& send_params
){
static_assert(sizeof(SendParams) <= PABB_MAX_MESSAGE_SIZE, "Message is too large.");
return try_issue_request(SendType, (char*)&send_params, sizeof(SendParams));
return try_issue_request(cancelled, SendType, (char*)&send_params, sizeof(SendParams));
}
template <uint8_t SendType, typename SendParams>
void BotBase::issue_request(SendParams& send_params){
void BotBase::issue_request(
const std::atomic<bool>* cancelled,
SendParams& send_params
){
static_assert(sizeof(SendParams) <= PABB_MAX_MESSAGE_SIZE, "Message is too large.");
issue_request(SendType, (char*)&send_params, sizeof(SendParams));
issue_request(cancelled, SendType, (char*)&send_params, sizeof(SendParams));
}
template <
uint8_t SendType, uint8_t RecvType,
typename SendParams, typename RecvParams
>
void BotBase::issue_request_and_wait(SendParams& send_params, RecvParams& recv_params){
void BotBase::issue_request_and_wait(
const std::atomic<bool>* cancelled,
SendParams& send_params,
RecvParams& recv_params
){
static_assert(sizeof(SendParams) <= PABB_MAX_MESSAGE_SIZE, "Message is too large.");
static_assert(sizeof(RecvParams) <= PABB_MAX_MESSAGE_SIZE, "Message is too large.");
static_assert(PABB_MSG_IS_REQUEST(SendType), "Message must be a request.");
issue_request_and_wait(
cancelled,
SendType, (char*)&send_params, sizeof(SendParams),
RecvType, (char*)&recv_params, sizeof(RecvParams)
);
Expand Down
Loading