gpt4 book ai didi

在 DDS 中为多个主题创建多个数据写入器

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:25 36 4
gpt4 key购买 nike

我正在 Linux 32 位的 OpenSplice DDS Community Edition 上工作。在这里我可以有多个主题,数据编写者可以写入这个主题,发布者将发布这些主题。我尝试修改可用的源代码,我试图在其中获取如下所示的输出:

    Chatter 1: Hi there, I will send you 10 more exxpert messages.
Chatter 1: Message no. 1
Chatter 1: Message no. 2
Chatter 1: Message no. 3
Chatter 1: Message no. 4
Chatter 1: Message no. 4
Chatter 1: Message no. 5
Chatter 1: Message no. 6
Chatter 1: Message no. 7
Chatter 1: Message no. 8
Chatter 1: Message no. 9
Chatter 1: Message no. 10

源码如下:

/* Initialize the chat messages on Heap. */
msg = Chat_ChatMessage__alloc();
checkHandle(msg, "Chat_ChatMessage__alloc");
msg->userID = ownID;
msg->index = 0;
msg->content = DDS_string_alloc(MAX_MSG_LEN);
checkHandle(msg->content, "DDS_string_alloc");
if (ownID == TERMINATION_MESSAGE) {
snprintf (msg->content, MAX_MSG_LEN, "Termination message.");
} else {
snprintf (msg->content, MAX_MSG_LEN, "Hi there, I will send you %d more messages.", NUM_MSG);
}
printf("Writing message: %s\n", msg->content);

//Initialize the chat messages on Heap//

/* Register a chat message for this user (pre-allocating resources for it!!) */
userHandle = Chat_ChatMessageDataWriter_register_instance(talker, msg);

/* Write a message using the pre-generated instance handle. */
status = Chat_ChatMessageDataWriter_write(talker, msg, userHandle);
checkStatus(status, "Chat_ChatMessageDataWriter_write");

/* Register a chat message for this user (pre-allocating resources for it!!) */
userHandle = Chat_ChatMessageDataWriter_register_instance(chatterbox, msg);

/* Write a message using the pre-generated instance handle. */
status = Chat_ChatMessageDataWriter_write(chatterbox, msg, userHandle);
checkStatus(status, "Chat_ChatMessageDataWriter_write");

sleep (1); /* do not run so fast! */
/* Write any number of messages, re-using the existing string-buffer: no leak!!. */
for (i = 1; i <= NUM_MSG && ownID != TERMINATION_MESSAGE; i++) {
msg->index = i;
snprintf ( msg->content, MAX_MSG_LEN, "Message no. %d", msg->index);
printf("Writing message: %s\n", msg->content);
status = Chat_ChatMessageDataWriter_write(talker, msg, userHandle);
checkStatus(status, "Chat_ChatMessageDataWriter_write");
sleep (1); /* do not run so fast! */
}

/* Write any number of messages, re-using the existing string-buffer: no leak!!. */
for (i = 1; i <= NUM_MSG && ownID != TERMINATION_MESSAGE; i++) {
msg->index = i;
snprintf (msg->content, MAX_MSG_LEN, "Hi there, I will send you %d more exxpert_messages.", NUM_MSG);
snprintf ( msg->content, MAX_MSG_LEN, "Message no. %d", msg->index);
printf("Writing message: %s\n", msg->content);
status = Chat_ChatMessageDataWriter_write(chatterbox, msg, userHandle);
checkStatus(status, "Chat_ChatMessageDataWriter_write");
}

但我对上述代码的输出如下:

    Writing message: Hi there, I will send you 10 more messages.
Writing message: Message no. 1
Error in Chat_ChatMessageDataWriter_write: DDS_RETCODE_PRECONDITION_NOT_MET

最佳答案

当您使用 DataWriter 注册一个实例时,将返回一个 InstanceHandle 类型的值,就像您在此处的代码中一样:

userHandle = Chat_ChatMessageDataWriter_register_instance(chatterbox, msg);

您只能将该 userHandle 变量用于后续 write() 以及您注册的相同 DataWriter。但是,您尝试执行以下操作:

status = Chat_ChatMessageDataWriter_write(talker, msg, userHandle);

注意不同的变量 chatterboxtalkeruserHandle 变量不属于 talker DataWriter,因此 PRECONDITION_NOT_MET 返回值失败。

关于在 DDS 中为多个主题创建多个数据写入器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37394958/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com