本来打算用boost的message queue,后来发现有个很严重的bug,在win7 64位的环境下,client无法打开queue,
有人建议打开#define BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME 能打开,但是这终究不是个办法。
有人建议打开#define BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME 能打开,但是这终究不是个办法。
解决方案 »
- 在MFC中如何可以使进度条的矩形框变圆滑?求高手指点
- 树控件节点展开跳动问题
- 扫描出的矩形图片变成圆
- 项目中关于VC6和VS2003的矛盾
- 求LPBINDSTATUSCALLBACK在VB里的写法
- ATL怎样返回一个日期时间类型?
- 急需“虚拟打印驱动”的技术资料和源代码
- 100分之100:为什么在windows xp下好多驱动程序都不用安装,而 win98,2000等下要?
- 如何在单文档程序启动后自动调用函数?
- help,急急!!!为何在程序中添加CDaoRecordest类,编译时Vc不认???程序是基于对话框的,数据库后加入.
- VC串口不能接收数据
- _tcscpy_s,_tcstok_s 等等函数VC 6.0下如何调用
#include "zlogger.h"
#include "mq.h"const std::size_t MQ_MAX_NUM_MSG = 32, MQ_MAX_MSG_SIZE = 16;
const char *MESSAGE_QUEUE_NAME_UP = "zhplayer_mq_up";
const char *MESSAGE_QUEUE_NAME_DOWN = "zhplayer_mq_down";
const int MQ_OP_TIMEOUT = 1000;ZHMQ::ZHMQ()
{
sibling_ = true;
}ZHMQ::~ZHMQ()
{
if (!sibling_) remove();
}bool ZHMQ::init()
{
// Assume that we are sibling
sibling_ = true; // Try to open or create named message queue
if (!reset()) return false; // Opened successfully. Trying to ping
if (ping_root())
{
sibling_ = true;
return true;
} // Queues opened but not pinging? That's The Main Root Instance
sibling_ = false; // Reopen queues to clear it's contents
remove();
return reset();
}bool ZHMQ::reset()
{
try
{
mqu_.reset(new ipc::message_queue(ipc::open_or_create, MESSAGE_QUEUE_NAME_UP,
MQ_MAX_NUM_MSG, MQ_MAX_MSG_SIZE));
mqd_.reset(new ipc::message_queue(ipc::open_or_create, MESSAGE_QUEUE_NAME_DOWN,
MQ_MAX_NUM_MSG, MQ_MAX_MSG_SIZE)); return true;
} catch (ipc::interprocess_exception)
{
remove();
ZLOGE("Unable to open or create boost shared message queue");
return false;
}
}void ZHMQ::remove() const
{
ipc::message_queue::remove(MESSAGE_QUEUE_NAME_UP);
ipc::message_queue::remove(MESSAGE_QUEUE_NAME_DOWN);
}bool ZHMQ::ping_root()
{
if (!send("ping")) return false; std::string pong;
if (!receive(pong)) return false; return pong == "pong";
}bool ZHMQ::send(const std::string msg) const
{
if (!is_valid()) return false; try
{
boost::posix_time::ptime expires =
boost::posix_time::microsec_clock::universal_time() +
boost::posix_time::milliseconds(MQ_OP_TIMEOUT); if (sibling_)
return mqu_->timed_send(msg.c_str(), msg.length(), 0, expires);
else
return mqd_->timed_send(msg.c_str(), msg.length(), 0, expires);
} catch (...)
{
return false;
}
}bool ZHMQ::receive(std::string &msg)
{
if (!is_valid()) return false; char buffer[MQ_MAX_MSG_SIZE + 4]; std::size_t received;
unsigned int priority; msg.clear(); try
{
boost::posix_time::ptime expires =
boost::posix_time::microsec_clock::universal_time() +
boost::posix_time::milliseconds(MQ_OP_TIMEOUT); if (sibling_)
{
if (!mqd_->timed_receive(buffer, MQ_MAX_MSG_SIZE + 4, received, priority, expires))
return false;
} else
{
if (!mqu_->timed_receive(buffer, MQ_MAX_MSG_SIZE + 4, received, priority, expires))
return false;
} buffer[received] = 0;
msg = buffer;
return true;
} catch (...)
{
return false;
}
}std::size_t ZHMQ::msg_count() const
{
if (!is_valid()) return 0; if (sibling_)
return mqd_->get_num_msg();
else
return mqu_->get_num_msg();
}