#ifndef __UT_DDS_TOPIC_CHANNEL_HPP__ #define __UT_DDS_TOPIC_CHANNEL_HPP__ #include namespace unitree { namespace common { /* * @brief: DdsTopicChannelAbstract */ class DdsTopicChannelAbstract { public: virtual bool Write(const void* message, int64_t waitMicrosec) = 0; virtual int64_t GetLastDataAvailableTime() const = 0; }; using DdsTopicChannelAbstractPtr = std::shared_ptr; #define UT_DDS_WAIT_MATCHED_TIME_MICRO_SEC 100000 /* * @brief: DdsTopicChannel */ template class DdsTopicChannel : public DdsTopicChannelAbstract { public: explicit DdsTopicChannel() {} ~DdsTopicChannel() {} void SetTopic(const DdsParticipantPtr& participant, const std::string& name, const DdsTopicQos& qos) { mTopic = DdsTopicPtr(new DdsTopic(participant, name, qos)); } void SetWriter(const DdsPublisherPtr& publisher, const DdsWriterQos& qos) { mWriter = DdsWriterPtr(new DdsWriter(publisher, mTopic, qos)); MicroSleep(UT_DDS_WAIT_MATCHED_TIME_MICRO_SEC); } void SetReader(const DdsSubscriberPtr& subscriber, const DdsReaderQos& qos, const DdsReaderCallback& cb, int32_t queuelen) { mReader = DdsReaderPtr(new DdsReader(subscriber, mTopic, qos)); mReader->SetListener(cb, queuelen); } DdsWriterPtr GetWriter() const { return mWriter; } DdsReaderPtr GetReader() const { return mReader; } bool Write(const void* message, int64_t waitMicrosec) { return Write(*(const MSG*)message, waitMicrosec); } bool Write(const MSG& message, int64_t waitMicrosec) { return mWriter->Write(message, waitMicrosec); } int64_t GetLastDataAvailableTime() const { if (mReader) { return mReader->GetLastDataAvailableTime(); } return 0; } private: DdsTopicPtr mTopic; DdsWriterPtr mWriter; DdsReaderPtr mReader; }; template using DdsTopicChannelPtr = std::shared_ptr>; } } #endif//__UT_DDS_TOPIC_CHANNEL_HPP__