AMQP-CPP library, message.body() returns payload with garbage at end

  amqp, amqp-client, c++

I try to use the AMQP-CPP library, and have now the problem that the received messages contain some gargabe at the end. In wireshark, everything seems fine when looking at the ampq communication. I’ve tried the built-in event callers boost and libuv, both have the same behavior.

My Code looks like this (libuv):

    auto *loop = uv_default_loop();
    // handler for libev
    AMQP::LibUvHandler handler(loop);

    // make a connection
    AMQP::Address address("amqp://10.0.0.10/");
    //AMQP::Address address("amqp://guest:[email protected]:5672");
    AMQP::TcpConnection connection(&handler, address);

    // we need a channel too
    AMQP::TcpChannel channel(&connection);

    //declare queue and bind to exchange
    std::string queuename = "myqueue";
    channel.declareQueue(queuename, AMQP::exclusive + AMQP::autodelete);
    channel.bindQueue("content", queuename, "");

   // Define callbacks and start
    auto messageCb = [&channel](
            const AMQP::Message &message, uint64_t deliveryTag, 
            bool redelivered)
    {
        std::cout << message.body() << std::endl;
        channel.ack(deliveryTag);
    };

    // callback function that is called when the consume operation starts
    auto startCb = [](const std::string &consumertag) {

        std::cout << "consume operation started: " << consumertag << std::endl;
    };

    // callback function that is called when the consume operation failed
    auto errorCb = [](const char* message) {
        std::cout << "consume operation failed:" << *message << std::endl;
    };

    channel.consume(queuename)
        .onReceived(messageCb)
        .onSuccess(startCb)
        .onError(errorCb);

    // run the handler

    //service.run();
    uv_run(loop, UV_RUN_DEFAULT);

    connection.close();

The message contains a JSON. After it it prints some rubbish text, as if some buffer is overwritten with the message and not terminated in the right way. Any idea what can be wrong?

Thanks,
Kind regards

Source: Windows Questions C++

LEAVE A COMMENT