2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* Interprocess.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <boost/interprocess/sync/scoped_lock.hpp>
|
|
|
|
#include <boost/interprocess/sync/interprocess_mutex.hpp>
|
|
|
|
#include <boost/interprocess/sync/interprocess_condition.hpp>
|
|
|
|
#include <boost/interprocess/mapped_region.hpp>
|
|
|
|
#include <boost/interprocess/shared_memory_object.hpp>
|
2009-04-15 17:03:31 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2008-09-12 11:51:46 +03:00
|
|
|
struct ServerReady
|
|
|
|
{
|
|
|
|
bool ready;
|
2016-12-04 11:54:26 +02:00
|
|
|
uint16_t port; //ui16?
|
2017-06-04 14:59:11 +02:00
|
|
|
boost::interprocess::interprocess_mutex mutex;
|
|
|
|
boost::interprocess::interprocess_condition cond;
|
2008-09-12 11:51:46 +03:00
|
|
|
|
|
|
|
ServerReady()
|
|
|
|
{
|
|
|
|
ready = false;
|
2016-12-04 11:54:26 +02:00
|
|
|
port = 0;
|
2008-09-12 11:51:46 +03:00
|
|
|
}
|
|
|
|
|
2017-06-04 14:59:11 +02:00
|
|
|
void waitTillReady()
|
2008-09-12 11:51:46 +03:00
|
|
|
{
|
2017-06-04 14:59:11 +02:00
|
|
|
boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> slock(mutex);
|
|
|
|
while(!ready)
|
2012-02-20 00:03:43 +03:00
|
|
|
{
|
2017-06-04 14:59:11 +02:00
|
|
|
cond.wait(slock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setToReadyAndNotify(const uint16_t Port)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boost::unique_lock<boost::interprocess::interprocess_mutex> lock(mutex);
|
2012-02-20 00:03:43 +03:00
|
|
|
ready = true;
|
2016-12-04 11:54:26 +02:00
|
|
|
port = Port;
|
2012-02-20 00:03:43 +03:00
|
|
|
}
|
2008-09-12 11:51:46 +03:00
|
|
|
cond.notify_all();
|
|
|
|
}
|
|
|
|
};
|
2009-01-11 00:08:18 +02:00
|
|
|
|
2017-06-04 14:59:11 +02:00
|
|
|
struct SharedMemory
|
2009-01-11 00:08:18 +02:00
|
|
|
{
|
2017-07-12 17:32:22 +02:00
|
|
|
std::string name;
|
2009-01-11 00:08:18 +02:00
|
|
|
boost::interprocess::shared_memory_object smo;
|
2017-06-04 14:59:11 +02:00
|
|
|
boost::interprocess::mapped_region * mr;
|
|
|
|
ServerReady * sr;
|
2017-07-12 17:32:22 +02:00
|
|
|
|
|
|
|
SharedMemory(const std::string & Name, bool initialize = false)
|
|
|
|
: name(Name)
|
2009-01-11 00:08:18 +02:00
|
|
|
{
|
2017-06-04 14:59:11 +02:00
|
|
|
if(initialize)
|
|
|
|
{
|
|
|
|
//if the application has previously crashed, the memory may not have been removed. to avoid problems - try to destroy it
|
2017-07-12 17:32:22 +02:00
|
|
|
boost::interprocess::shared_memory_object::remove(name.c_str());
|
2017-06-04 14:59:11 +02:00
|
|
|
}
|
2017-07-12 17:32:22 +02:00
|
|
|
smo = boost::interprocess::shared_memory_object(boost::interprocess::open_or_create, name.c_str(), boost::interprocess::read_write);
|
2009-01-11 00:08:18 +02:00
|
|
|
smo.truncate(sizeof(ServerReady));
|
2009-01-31 00:23:13 +02:00
|
|
|
mr = new boost::interprocess::mapped_region(smo,boost::interprocess::read_write);
|
2017-06-04 14:59:11 +02:00
|
|
|
if(initialize)
|
|
|
|
sr = new(mr->get_address())ServerReady();
|
|
|
|
else
|
|
|
|
sr = reinterpret_cast<ServerReady*>(mr->get_address());
|
2009-01-11 00:08:18 +02:00
|
|
|
};
|
2017-06-04 14:59:11 +02:00
|
|
|
|
|
|
|
~SharedMemory()
|
2009-01-30 23:28:02 +02:00
|
|
|
{
|
2009-01-31 00:23:13 +02:00
|
|
|
delete mr;
|
2017-07-12 17:32:22 +02:00
|
|
|
boost::interprocess::shared_memory_object::remove(name.c_str());
|
2009-01-30 23:28:02 +02:00
|
|
|
}
|
2016-01-31 17:01:58 +02:00
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|