Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过Apache Qpid发送用户定义的结构_C++_Struct_Qpid - Fatal编程技术网

C++ 通过Apache Qpid发送用户定义的结构

C++ 通过Apache Qpid发送用户定义的结构,c++,struct,qpid,C++,Struct,Qpid,我尝试将一个包含AMQP子结构的用户定义结构从一个节点发送到另一个节点。目前我正在使用ApacheQPID库。 (在为其他设备重建代码之前,我目前仍在电脑上测试代码) 我当前的方法包括从结构到bytestring的转换,并通过AMQP发送该转换以在另一端解构它。 我做以下几点 //user defined struct enum Quality { /// <summary>Value is reliable.</summary> QUALITY_GOOD

我尝试将一个包含AMQP子结构的用户定义结构从一个节点发送到另一个节点。目前我正在使用ApacheQPID库。 (在为其他设备重建代码之前,我目前仍在电脑上测试代码)

我当前的方法包括从结构到bytestring的转换,并通过AMQP发送该转换以在另一端解构它。 我做以下几点

//user defined struct
enum Quality
{
    /// <summary>Value is reliable.</summary>
    QUALITY_GOOD,
    /// <summary>Value not reliable.</summary>
    /// <remarks>
    ///     A variable may be declared unreliable if a sensor is not calibrated or
    ///     if the last query failed but older samples are still usable.
    /// </remarks>
    QUALITY_UNCERTAIN,
    /// <summary>Value is invalid.</summary>
    /// <remarks>
    ///     A variable may be declared bad if the measured value is out of range or
    ///     if a timeout occured.
    /// </remarks>
    QUALITY_BAD
};

struct Payload
{
    /// <summary>Identifier that uniquely points to a single instance.</summary>
    DdsInterface::Id id = DdsInterface::Id();
    /// <summary>Human readable short name.</summary>
    std::string name = "default";
    /// <summary>Actual value.</summary>
    long long value;
    /// <summary>Quality of the Value.</summary>
    Quality quality = QUALITY_GOOD;
    /// <summary>Detailed quality of the variable.</summary>
    QualityDetail qualityDetail = 0;
    /// <summary>Unit of measure.</summary>
    PhysicalQuantity quantity = 0;

    Payload();
    Payload(const DdsInterface::Id id, const std::string topic, const uint64_t counter);
};

当我只发送一个字符串而不是它在另一端接收的有效负载时。但由于某些原因,用户定义的结构无法工作。 我希望避免根据qpid映射重新映射所有内容,因为我将丢失Payload.id的深度

如果有人有任何克服这一点的建议,我将不胜感激

提前感谢,, 尼克

我解决了这个问题。 问题是它复制的不是字符串实例而是指针实例。通过设置
std::string name=“default”进入
字符名[20]=“默认”它复制真实的字符串。
这就是发布者和订阅者现在对消息进行编码和解码的方式

void QpidAMQP::AMQPPublish(const Payload& payload, bool durability, bool sync)
{
    // Publish to MQTT broker
    //create stream of bytes to send over the line
    qpid::messaging::Message message;
    message.setDurable(durability);
    std::string b;
    b.resize(sizeof(Payload));
    std::memcpy(const_cast<char*>(b.c_str()), &payload, b.size());
    message.setContent(b);
    std::string temp = message.getContent();
    print_bytes(temp.c_str(), temp.size());
    this->sender.send(message);
    this->session.sync(sync);
}
//receiver functions
void *check_for_incoming_messages(QpidAMQP* amqp_instance) //called via pthread
{
    qpid::messaging::Message message;

    std::cout << "check for incoming messages" << std::endl;
    while(amqp_instance->getReceiver()->fetch(message, qpid::messaging::Duration::FOREVER))
    {
        amqp_instance->on_message(&message);
    }
    return nullptr;
}

void QpidAMQP::on_message(qpid::messaging::Message *message)
{
    /// make sure message topic and payload are copied!!!
    if (this->handler)
    {
        std::string temp = message->getContent();
        print_bytes(temp.c_str(), sizeof (temp)); // used to check the byte data
        Payload payload; //Re-make the struct
        memcpy(&payload, message->getContent().c_str(), message->getContentSize());

        handler->ReceivedIntegerValue(payload.id.variableId, payload.value);
    }
}
void print_bytes(const void *object, size_t size)
{
  // This is for C++; in C just drop the static_cast<>() and assign.
  const unsigned char * const bytes = static_cast<const unsigned char *>(object);
  size_t i;

  printf("[ ");
  for(i = 0; i < size; i++)
  {
    printf("%02x ", bytes[i]);
  }
  printf("]\n");
}
void QpidAMQP::AMQPPublish(const Payload& payload, bool durability, bool sync)
{
    // Publish to MQTT broker
    //create stream of bytes to send over the line
    qpid::messaging::Message message;
    message.setDurable(durability);
    std::string b;
    b.resize(sizeof(Payload));
    std::memcpy(const_cast<char*>(b.c_str()), &payload, b.size());
    message.setContent(b);
    std::string temp = message.getContent();
    print_bytes(temp.c_str(), temp.size());
    this->sender.send(message);
    this->session.sync(sync);
}
void QpidAMQP::on_message(qpid::messaging::Message *message)
{
    /// make sure message topic and payload are copied!!!
    if (this->handler != nullptr)
    {
        const std::string temp = message->getContent();
        print_bytes(temp.c_str(), temp.size());
        Payload payload;
        std::memcpy(&payload, temp.c_str(), temp.size());//sizeof(message->getContentBytes().c_str()));
        handler->ReceivedIntegerValue(payload.id.variableId, payload.value);
    }
}