WriterProxyData,包含以下成员:
GUID_t m_guid;
RemoteLocatorList remote_locators_; //!Holds locator information
InstanceHandle_t m_key; //!GUID_t of the Writer converted to InstanceHandle_t
InstanceHandle_t m_RTPSParticipantKey; //!GUID_t of the participant converted to InstanceHandle
string_255 m_typeName; //!Type name
string_255 m_topicName; //!Topic name
uint16_t m_userDefinedId; //!User defined ID
uint32_t m_typeMaxSerialized; //!Maximum size of the type associated with this Wrtiter, serialized.
TopicKind_t m_topicKind; //!Topic kind
GUID_t persistence_guid_; //!Persistence GUID
TypeIdV1* m_type_id; //!Type Identifier
TypeObjectV1* m_type; //!Type Object
xtypes::TypeInformation* m_type_information; //!Type Information
ParameterPropertyList_t m_properties;
Publisher是一直归属于DomainParticipant。
Publisher* publisher_with_default_qos =
participant->create_publisher(PUBLISHER_QOS_DEFAULT);
if (nullptr == publisher_with_default_qos) {
// Error
return;
}
// First load the XML with the profiles
DomainParticipantFactory::get_instance()->load_XML_profiles_file("profiles.xml");
// Create participant
// Create a Publisher using a profile and no Listener
Publisher* publisher_with_profile = participant->create_publisher_with_profile("publisher_profile");
if (nullptr == publisher_with_profile) {
// Error
return;
}
// Delete the entities the Publisher created.
if (publisher->delete_contained_entities() != ReturnCode_t::RETCODE_OK) {
// Publisher failed to delete the entities it created.
return;
}
// Delete the Publisher
if (participant->delete_publisher(publisher) != ReturnCode_t::RETCODE_OK) {
// Error
return;
}
DataWriter归属于Publisher,对DataWriter来说,Publisher就是一个创建工厂。每个DataWriter在创建之处绑定到一个Topic,所以Topic必须先于DataWriter创建。
默认的DataWriterQos可以通过Publisher实例函数get_default_datawriter_qos()获取
DataWriterListener是一个抽象类,用于响应DataWriter的状态发生变化,有以下成员函数用于回调:
DataWriter* data_writer_with_default_qos = publisher->create_datawriter(topic, DATAWRITER_QOS_DEFAULT);
if (nullptr == data_writer_with_default_qos) {
// Error
return;
}
通过Profile创建:
// First load the XML with the profiles
DomainParticipantFactory::get_instance()->load_XML_profiles_file("profiles.xml");
// Create a DataWriter using a profile and no Listener
DataWriter* data_writer_with_profile = publisher->create_datawriter_with_profile(topic, "data_writer_profile");
if (nullptr == data_writer_with_profile) {
// Error
return;
}
通过自定义PayloadPool创建DataWriter
// A DataWriterQos must be provided to the creation method
DataWriterQos qos;
// Create PayloadPool
std::shared_ptr<eprosima::fastrtps::rtps::IPayloadPool> payload_pool =
std::dynamic_pointer_cast<eprosima::fastrtps::rtps::IPayloadPool>(std::make_shared<CustomPayloadPool>());
DataWriter* data_writer = publisher->create_datawriter(topic, qos, nullptr, StatusMask::all(), payload_pool);
if (nullptr == data_writer) {
// Error
return;
}
删除DataWriter
// Delete the DataWriter
if (publisher->delete_datawriter(data_writer) != ReturnCode_t::RETCODE_OK) {
// Error
return;
}
发送数据
write()函数有两个参数:指向要发送数值的data实例;instance的handler。如果handle是非空,那么它必须要被TypeSupport的getKey()获取到,否则write函数会失败。
// Publish the new value, deduce the instance handle
if (data_writer->write(data, eprosima::fastrtps::rtps::InstanceHandle_t()) != ReturnCode_t::RETCODE_OK) {
// Error
return;
}
减少发送数据的拷贝:通过loan sample,步骤如下:
void* data = nullptr;
if (ReturnCode_t::RETCODE_OK == data_writer->loan_sample(data)) {
bool error = false;
// Fill the data values
// (...)
if (error) {
// Return the loan without publishing
data_writer->discard_loan(data);
return;
}
// Publish the new value
if (data_writer->write(data, eprosima::fastrtps::rtps::InstanceHandle_t()) != ReturnCode_t::RETCODE_OK) {
// Error
return;
}
}
// The data instance can be reused to publish new values,
// but delete it at the end to avoid leaks
custom_type_support->deleteData(data)