Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataWriter fails to write message #461

Open
plament opened this issue Nov 15, 2023 · 1 comment
Open

DataWriter fails to write message #461

plament opened this issue Nov 15, 2023 · 1 comment

Comments

@plament
Copy link

plament commented Nov 15, 2023

I am using 0.10.4 and have the following message.idl

struct Message
{
    @optional int32 a0;
    @optional int32 a1;
};

Then I generate the data types using: idlc -b . -l <path to libcycloneddsidlcxx> -x final message.idl

Then I create 2 messages and try to write them using DataWriter. However sending the second message fails with write failed exception.

Here is my code:

#include <iostream>
#include "dds/dds.hpp"
#include "gen/message.hpp"

using namespace org::eclipse::cyclonedds;

int main() {

    std::cout << "=== [Publisher] Create writer." << std::endl;

    dds::domain::DomainParticipant participant(0);
    dds::topic::Topic<Message> topic(participant, "TOPIC");
    dds::pub::Publisher publisher(participant);
    dds::pub::DataWriter<Message> writer(publisher, topic);

    Message m1;
    m1.a1() = 10;

    Message m2;
    m2.a0() = 10;

    try {
        std::cout << "=== Will write m1" << std::endl;
        writer.write(m1);

        std::cout << "=== Will write m2" << std::endl;
        writer.write(m2);

        std::cout << "=== Write done" << std::endl;
    } catch (const std::exception& ex) {
        std::cerr << "=== [Publisher] Exception: " << ex.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

The output is:

=== [Publisher] Create writer.
=== Will write m1
=== Will write m2
=== [Publisher] Exception: Error Bad Parameter - write failed.
===============================================================================
Context     : void org::eclipse::cyclonedds::pub::AnyDataWriterDelegate::write
Node        : UnknownNode

So writing m2 fails. However if I first write m2 and then m1 then everything works fine.

The actual method which fails is:

bool cdr_stream::bytes_available(size_t N, bool peek)

For some reason there is no space left in the cdr_stream buffer to serialize the second message.

@eboasson

@plament
Copy link
Author

plament commented Nov 15, 2023

Seems the issue is that the serialized size is not calculated properly.

template<typename T, class S>
bool get_serialized_size(const T& sample, bool as_key, size_t &sz)
{
  if (TopicTraits<T>::isSelfContained()) {
    if ((as_key && !get_serialized_fixed_size<T,S,true>(sample, sz)) ||
        (!as_key && !get_serialized_fixed_size<T,S,false>(sample, sz)))
      return false;
  } else {
    S str;
    if (!move(str, sample, as_key))
      return false;
    sz = str.position();
  }

  return true;
}

Here isSelfContained() is true and get_serialized_fixed_size() actually calculates and caches the size and from that moment cyclone uses that cached size value. However serialized size is different depending on which optionals are set.

Actually is it correct that my struct with all optionals is self contained? Because idlc does not detect it as non self contained and that's why it doesn't generate isSelfContained() { return false; }. Maybe an issue in idlc?

ruoruoniao added a commit to ruoruoniao/cyclonedds-cxx that referenced this issue Apr 25, 2024
ruoruoniao added a commit to ruoruoniao/cyclonedds-cxx that referenced this issue Apr 30, 2024
ruoruoniao added a commit to ruoruoniao/cyclonedds-cxx that referenced this issue Apr 30, 2024
except branch idl_is_type_spec(node)
eboasson pushed a commit that referenced this issue May 13, 2024
* repair issue #461

except branch idl_is_type_spec(node)

* if we add an "idl_is_type_spec(node)" branch, it will cause an infinite recursion...

* delete parse by automatic parser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant