#include "wsTimerThread.h" #include #include "wsClientMgr.h" #include "constdef.h" #include "log.h" namespace YA { wsTimerThread::wsTimerThread() { __Reset(); } wsTimerThread::~wsTimerThread() { Stop(); } void wsTimerThread::Start() { __Running = true; __Thread.reset( new boost::thread( boost::bind( &wsTimerThread::_ThreadFunc, this, this ) ) ); //__Thread->detach(); } void wsTimerThread::Stop() { if(__Thread==nullptr) { return; } __Thread->interrupt(); __Running = false; std::cout << "wsTimerThread::Stop() begin" << std::endl; __Enable = false; if ( __Running ) { boost::unique_lock lock( __ExitMutex ); __ExitCond.wait( lock ); } __Reset(); __Thread=nullptr; std::cout << "wsTimerThread::Stop() end" << std::endl; } void wsTimerThread::__Reset() { __Enable = true; __Running = false; } void wsTimerThread::__ChkConfig() { if ( __Config.SendInterval < MIN_SEND_INTERVAL ) { __Config.SendInterval = MIN_SEND_INTERVAL; } } void wsTimerThread::__SendCardPos() { std::map CardPosList; if(__CardPosList.empty()) return; __CardPosList.copy( CardPosList ); std::string jsCardPos = __jsBuilder.BuildCardPos( CardPosList ); log_info("[pos_map]%s",jsCardPos.c_str()); swsClientMgr.send( JSON_CMD_VALUE_PUSH, jsCardPos ); } void wsTimerThread::_ThreadFunc( wsTimerThread * pOwner ) { while ( pOwner->__Enable ) { std::time_t t = time( 0 ); int seconds = (int)std::difftime( t, __LastSendTime ); if ( seconds >= pOwner->__Config.SendInterval ) { pOwner->__SendCardPos(); __LastSendTime = t; } boost::this_thread::sleep( boost::posix_time::millisec( 1 ) ); } pOwner->__ExitCond.notify_one(); } void wsTimerThread::Init( const _THREAD_CONFIG_ & Config ) { __Config = Config; __ChkConfig(); } void wsTimerThread::upt_card_pos( const _CARD_POS_ & pos ) { //如果已存在就更新,否则插入 uint64_t type = pos.Type; uint64_t id=type<<32|pos.ID; if ( __CardPosList.seek( id ) ) { __CardPosList.update( id, pos ); } else { __CardPosList.insert( id, pos ); } } void wsTimerThread::del_card_pos( const _CARD_POS_ & pos ) { uint64_t type = pos.Type; uint64_t id=type<<32|pos.ID; __CardPosList.erase( id ); } }