#ifndef _FRAME_HANDLER_H_ #define _FRAME_HANDLER_H_ #undef _HAS_STD_BYTE #include #include #include #include #include #include #include "common/HeaderInfo.h" #include "messages/Frame.pb.h" using namespace std::chrono; namespace Kinova { namespace Api { struct CallbackTimeoutStruct { uint32_t m_timeout; steady_clock::time_point m_registeredTimePoint; explicit CallbackTimeoutStruct(uint32_t timeout) : m_timeout(timeout) { m_registeredTimePoint = time_point_cast(steady_clock::now()); } bool checkExpiry() { uint32_t duration = duration_cast(steady_clock::now() - m_registeredTimePoint).count(); return (duration >= m_timeout); } }; typedef std::function MessageCallback; typedef std::queue > TimeoutClockByMeesageCallbackQueue; class FrameHandler { public: explicit FrameHandler(uint32_t maxCallbackTimeout = 60000); ~FrameHandler() = default; std::future registerMessage(uint32_t msgId); // TODO sfforget 2018-05-30 Add error callback void registerMessageCallback(uint32_t msgId, const MessageCallback &callback); Error manageReceivedMessage(Frame &msgFrame); void setMessageException(HeaderInfo &headerInfo, Error &error); private: void cleanDanglingCallback(); std::mutex m_mutex; uint32_t m_maxCallbackTimeout; // IdGenerator m_apiIdGenerator; // TODO cleanup messages without response from server to avoid memory leak. // todoErr add the validation information beside the message promise std::unordered_map > > m_messagePromises; TimeoutClockByMeesageCallbackQueue m_timeoutClockByCallback; std::unordered_map m_messageCallbacks; }; } } #endif // _MESSAGE_MANAGER_H_