[BitMessage] Shutdown workaround

This starts a thread in the game's shutdown routine
and destroys BitMRC in a separate thread to prevent deadlocks.
This is not a good fix
This commit is contained in:
momo5502 2016-11-12 12:55:20 +01:00
parent 72c19cb968
commit ea3660bbf8
2 changed files with 59 additions and 14 deletions

View File

@ -8,6 +8,7 @@ using namespace Utils;
namespace Components
{
std::thread BitMessage::ShutDownThread;
BitMRC* BitMessage::BMClient;
BitMessage::BitMessage()
@ -16,6 +17,15 @@ namespace Components
Logger::Print("Initializing BitMessage...\n");
#endif // DEBUG
QuickPatch::OnShutdown([] ()
{
BitMessage::ShutDownThread = std::thread(BitMessage::ShutDown);
if (BitMessage::ShutDownThread.joinable())
{
BitMessage::ShutDownThread.join();
}
});
BitMessage::BMClient = new BitMRC(BITMESSAGE_OBJECT_STORAGE_FILENAME, BITMESSAGE_KEYS_FILENAME);
BitMessage::BMClient->init();
BitMessage::BMClient->defaultTTL = 1 * 60 * 60; // 1 hour
@ -70,6 +80,8 @@ namespace Components
Command::Add("bm_check_messages", [] (Command::Params)
{
if (!BitMessage::BMClient) return;
while (BitMessage::BMClient->new_messages.size() > 0)
{
auto msg = BitMessage::BMClient->new_messages.pop();
@ -79,11 +91,13 @@ namespace Components
Command::Add("bm_check_connections", [] (Command::Params)
{
if (!BitMessage::BMClient) return;
std::shared_lock<std::shared_timed_mutex> mlock(BitMessage::BMClient->mutex_nodes);
for (auto& node : BitMessage::BMClient->Nodes)
{
switch (node->state) {
switch (node->state)
{
case 0: // Not connected
Logger::Print("%s: Disconnected\n", node->Ip.data());
break;
@ -104,6 +118,7 @@ namespace Components
Command::Add("bm_check_privatekey", [] (Command::Params)
{
if (!BitMessage::BMClient) return;
std::shared_lock<std::shared_timed_mutex> mlock(BitMessage::BMClient->mutex_priv);
if (BitMessage::BMClient->PrivAddresses.empty())
@ -123,6 +138,7 @@ namespace Components
Command::Add("bm_check_publickey", [] (Command::Params)
{
if (!BitMessage::BMClient) return;
std::shared_lock<std::shared_timed_mutex> mlock(BitMessage::BMClient->mutex_pub);
if (BitMessage::BMClient->PubAddresses.empty())
@ -145,6 +161,7 @@ namespace Components
Command::Add("bm_address_public", [] (Command::Params params)
{
if (!BitMessage::BMClient) return;
if (params.Length() < 2) return;
ustring addre;
@ -165,6 +182,7 @@ namespace Components
Command::Add("bm_address_broadcast", [] (Command::Params params)
{
if (!BitMessage::BMClient) return;
if (params.Length() < 2) return;
ustring addre;
@ -183,22 +201,31 @@ namespace Components
#endif
}
BitMessage::~BitMessage()
void BitMessage::ShutDown()
{
BitMessage::Save();
delete BitMessage::BMClient;
BitMessage::BMClient = nullptr;
}
BitMessage::~BitMessage()
{
if (BitMessage::BMClient)
{
BitMessage::ShutDown();
}
}
void BitMessage::SetDefaultTTL(time_t ttl)
{
if (!BitMessage::BMClient) return;
BitMessage::BMClient->defaultTTL = ttl;
}
bool BitMessage::RequestPublicKey(std::string targetAddress)
{
if (!BitMessage::BMClient) return false;
// Convert to ustring
ustring targetAddressU;
targetAddressU.fromString(targetAddress);
@ -217,6 +244,7 @@ namespace Components
PubAddr* BitMessage::FindPublicKey(PubAddr address)
{
if (!BitMessage::BMClient) return nullptr;
std::shared_lock<std::shared_timed_mutex> mlock(BitMessage::BMClient->mutex_pub);
PubAddr* retval = nullptr;
@ -240,6 +268,8 @@ namespace Components
bool BitMessage::WaitForPublicKey(std::string targetAddress)
{
if (!BitMessage::BMClient) return false;
// Convert to ustring
ustring targetAddressU;
targetAddressU.fromString(targetAddress);
@ -280,6 +310,8 @@ namespace Components
bool BitMessage::Subscribe(std::string targetAddress)
{
if (!BitMessage::BMClient) return false;
// Convert to ustring
ustring targetAddressU;
targetAddressU.fromString(targetAddress);
@ -298,6 +330,8 @@ namespace Components
bool BitMessage::SendMsg(std::string targetAddress, std::string message, time_t ttl)
{
if (!BitMessage::BMClient) return false;
// Convert target address to ustring
ustring targetAddressU;
targetAddressU.fromString(targetAddress);
@ -329,6 +363,8 @@ namespace Components
bool BitMessage::SendBroadcast(std::string message, time_t ttl)
{
if (!BitMessage::BMClient) return false;
// Convert message to ustring
ustring messageU;
messageU.fromString(message);
@ -348,6 +384,8 @@ namespace Components
bool BitMessage::InitAddr()
{
if (!BitMessage::BMClient) return false;
#ifdef DEBUG
Logger::Print("Generating BM address...\n");
#endif
@ -362,9 +400,12 @@ namespace Components
}
void BitMessage::Save()
{
if (BitMessage::BMClient)
{
BitMessage::BMClient->save();
}
}
}
#endif

View File

@ -28,8 +28,12 @@ namespace Components
static BitMRC* BMClient;
private:
static std::thread ShutDownThread;
static PubAddr* FindPublicKey(PubAddr addr);
static bool InitAddr();
static void ShutDown();
};
}