本来打算用boost的message queue,后来发现有个很严重的bug,在win7 64位的环境下,client无法打开queue,
有人建议打开#define BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME 能打开,但是这终究不是个办法。

解决方案 »

  1.   

    顺便贴个程序,看看是否有人看出这个是哪个开源的软件里面的代码?
    #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();
    }