/** @file * @author Edouard DUPIN * @copyright 2016, Edouard DUPIN, all right reserved * @license APACHE v2.0 (see license file) */ #pragma once #include #include #include #include #include #include namespace jus { class TcpString : public eproperty::Interface { private: enet::Tcp m_connection; std::thread* m_thread; bool m_threadRunning; std::chrono::steady_clock::time_point m_lastReceive; std::chrono::steady_clock::time_point m_lastSend; public: using Observer = std::function; //!< Define an Observer: function pointer using ObserverRaw = std::function; //!< Define an Observer: function pointer Observer m_observerElement; ObserverRaw m_observerRawElement; /** * @brief Connect an function member on the signal with the shared_ptr object. * @param[in] _class shared_ptr Object on whe we need to call ==> the object is get in keeped in weak_ptr. * @param[in] _func Function to call. * @param[in] _args Argument optinnal the user want to add. */ template void connect(CLASS_TYPE* _class, void (CLASS_TYPE::*_func)(std::string)) { m_observerElement = [=](std::string _value){ (*_class.*_func)(std::move(_value)); }; } void connectClean() { m_observerElement = nullptr; } template void connectRaw(CLASS_TYPE* _class, void (CLASS_TYPE::*_func)(const jus::Buffer&)) { m_observerRawElement = [=](const jus::Buffer& _value){ (*_class.*_func)(std::move(_value)); }; } void connectCleanRaw() { m_observerRawElement = nullptr; } public: TcpString(); TcpString(enet::Tcp _connection); virtual ~TcpString(); void setInterface(enet::Tcp _connection); void connect(bool _async = false); void disconnect(bool _inThreadStop = false); bool isActive() const; void setInterfaceName(const std::string& _name); int32_t write(const std::string& _data); int32_t writeBinary(const jus::Buffer& _data); std::string asyncRead(); private: std::string read(); private: void threadCallback(); public: const std::chrono::steady_clock::time_point& getLastTimeReceive() { return m_lastReceive; } const std::chrono::steady_clock::time_point& getLastTimeSend() { return m_lastSend; } private: using ActionAsync = std::function; std::mutex m_threadAsyncMutex; std::thread* m_threadAsync; bool m_threadAsyncRunning; std::vector m_threadAsyncList; private: void threadAsyncCallback(); public: void addAsync(ActionAsync _elem) { std::unique_lock lock(m_threadAsyncMutex); m_threadAsyncList.push_back(_elem); } }; }