Well, this code isnt written in C, its C++. I am using C-Bindings to make functions calls to opensplice. This i realize, might be the cause of the issue, since i dont believe it is something that is supported. I am going to paste the entirety of my type and topic code. maybe that will help.
//type.cpp
namespace test
{
template<typename T>
Type<T>::Type()
{
if (!isInitialized())
{
initialize();
}
//create type
mTypeSupport = Typedefs<T>::newTypeSupport();
chkPtr(mTypeSupport, std::string("Failed to create ")+
typeid(mTypeSupport).name());
//register type
DDS_ReturnCode_t ret(Typedefs<T>::registerType(mTypeSupport, dp));
chkRetcode(ret, std::string("Failed to register ")+
typeid(mTypeSupport).name());
}
template<typename T>
Type<T>::~Type()
{
typename TopicMap::iterator it(mTopicMap.begin());
while (it != mTopicMap.end())
{
delete it->second;
it->second = 0;
mTopicMap.erase(it++);
}
DDS_free(mTypeSupport);
}
template<typename T>
Topic<T>* Type<T>::createTopic(const std::string& name)
{
Topic<T>* ret(new Topic<T>(mTypeSupport, name));
mTopicMap.insert(std::pair<std::string, Topic<T>*>(name, ret));
return ret;
}
template<typename T>
void Type<T>::publish(T& data, const std::string& topicname)
{
Topic<T>* topic(findTopic(topicname, true));
if (topic)
{
topic->publish(data);
}
}
template<typename T>
bool Type<T>::read(T& data, const std::string& topicname)
{
bool ret(false);
Topic<T>* topic(findTopic(topicname, true));
if (topic)
{
ret = topic->read(data);
}
return ret;
}
template<typename T>
void Type<T>::subscribe(Callback<T>& callback, const std::string& topicname)
{
Topic<T>* topic(findTopic(topicname, true));
if (topic)
{
topic->subscribe(callback);
}
}
template<typename T>
void Type<T>::unsubscribe(const std::string& topicname)
{
Topic<T>* topic(findTopic(topicname, true));
if (topic)
{
topic->unsubscribe();
}
}
template<typename T>
Topic<T>* Type<T>::findTopic(const std::string& name, bool create)
{
Topic<T>* ret(0);
typename TopicMap::const_iterator it(mTopicMap.find(name));
if (it == mTopicMap.end() && create)
{
ret = createTopic(name);
}
else if (it != mTopicMap.end())
{
ret = it->second;
}
return ret;
}
}
Topic.cpp
namespace test
{
template<typename T>
inline void onDataAvailable(void* listenerData, DDS_DataReader )
{
ListenerData<T>* ld(reinterpret_cast<ListenerData<T>*>(listenerData));
if (ld && ld->topic && ld->callback)
{
ld->topic->read(ld->callback->mData);
ld->callback->onData();
}
}
template<typename T>
Topic<T>::Topic(typename Typedefs<T>::TypeSupport ts, const std::string& name)
{
//check pointer passed into constructor
chkPtr(ts, "Topic::Topic: ts is null pointer", true);
//create topic
mTopic = DDS_DomainParticipant_create_topic(dp,
name.c_str(),
Typedefs<T>::typeName(ts),
topicQos,
0,
DDS_STATUS_MASK_NONE);
chkPtr(mTopic, std::string("Topic::Topic: Failed to create topic for ")+
typeid(typename Typedefs<T>::Type).name());
//data writer
mDataWriter = DDS_Publisher_create_datawriter(publisher,
mTopic,
DDS_DATAWRITER_QOS_USE_TOPIC_QOS,
0,
DDS_STATUS_MASK_NONE);
chkPtr(mDataWriter, std::string("Topic::Topic: Failed to create datawriter for ")+
typeid(typename Typedefs<T>::Type).name());
//data reader
mDataReader = DDS_Subscriber_create_datareader(subscriber,
mTopic,
DDS_DATAREADER_QOS_USE_TOPIC_QOS,
0,
DDS_STATUS_MASK_NONE);
chkPtr(mDataReader, std::string("Topic::Topic: Failed to create datareader for ")+
typeid(typename Typedefs<T>::Type).name());
//data listener
mDataListener = DDS_DataReaderListener__alloc();
chkPtr(mDataListener, std::string("Topic::Topic: Failed to create datalistener for ")+
typeid(typename Typedefs<T>::Type).name());
mDataListener->on_data_available = onDataAvailable<T>;
mDataListener->on_requested_deadline_missed = 0;
mDataListener->on_requested_incompatible_qos = 0;
mDataListener->on_sample_rejected = 0;
mDataListener->on_liveliness_changed = 0;
mDataListener->on_subscription_matched = 0;
mDataListener->on_sample_lost = 0;
mDataListener->listener_data = &mListenerData;
mListenerData.topic = this;
}
template<typename T>
Topic<T>::~Topic()
{
DDS_free(mDataListener);
DDS_Publisher_delete_datawriter(publisher, mDataWriter);
DDS_Subscriber_delete_datareader(subscriber, mDataReader);
DDS_DomainParticipant_delete_topic(dp, mTopic);
}
template<typename T>
std::string Topic<T>::name() const
{
std::string ret;
if (mTopic)
{
ret = DDS_Topic_get_name(mTopic);
}
return ret;
}
template<typename T>
void Topic<T>::publish(T& data)
{
//send data
DDS_ReturnCode_t ret2(DDS_Publisher_resume_publications(publisher));
DDS_ReturnCode_t ret(Typedefs<T>::write(mDataWriter, &data));
chkRetcode(ret, std::string("Failed to send ")+typeid(data).name());
}
template<typename T>
bool Topic<T>::read(T& data)
{
bool ret(false);
//gather sequences
typename Typedefs<T>::Seq* seq(Typedefs<T>::newSequence());
seq->_length = 0;
seq->_maximum = 1;
seq->_release = true;
seq->_buffer = &data;
DDS_SampleInfoSeq* infoSeq(DDS_SampleInfoSeq__alloc());
infoSeq->_length = seq->_length;
infoSeq->_maximum = seq->_maximum;
infoSeq->_release = seq->_release;
infoSeq->_buffer = DDS_SampleInfoSeq_allocbuf(1);
//read
DDS_ReturnCode_t r(Typedefs<T>::read(mDataReader, seq, infoSeq));
chkRetcode(r, std::string("Failed to read ")+typeid(data).name());
if (seq->_length)
{
if (r == DDS_RETCODE_OK)
{
ret = true;
}
}
DDS_free(infoSeq);
DDS_free(seq);
return ret;
}
template<typename T>
void Topic<T>::subscribe(Callback<T>& callback)
{
mListenerData.callback = &callback;
DDS_DataReader_set_listener(mDataReader, mDataListener, DDS_DATA_AVAILABLE_STATUS);
}
template<typename T>
void Topic<T>::unsubscribe()
{
DDS_DataReader_set_listener(mDataReader, 0, DDS_DATA_AVAILABLE_STATUS);
}
}