diff --git a/src/decoder/gb28181/sip/SipServer.cpp b/src/decoder/gb28181/sip/SipServer.cpp index 8220292..cf86dd1 100644 --- a/src/decoder/gb28181/sip/SipServer.cpp +++ b/src/decoder/gb28181/sip/SipServer.cpp @@ -124,7 +124,7 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) { this->clearClientMap(); break; case EXOSIP_MESSAGE_NEW://23 - LOG_INFO("EXOSIP_MESSAGE_NEW type={}",evtp->type); + // LOG_INFO("EXOSIP_MESSAGE_NEW type={}",evtp->type); if (MSG_IS_REGISTER(evtp->request)) { this->response_register(evtp); @@ -300,9 +300,18 @@ void SipServer::response_register(eXosip_event_t *evtp) { LOG_INFO("Camera registration succee,ip={},port={},device={}",client->getIp(),client->getPort(),client->getDevice()); + std::lock_guard l(m_client_map_mtx); + + auto it_client = mClientMap.find(client->getDevice()); + if (it_client != mClientMap.end()) { + Client* c = it_client->second; + delete c; + c = nullptr; + mClientMap.erase(it_client); + } + mClientMap.insert(std::make_pair(client->getDevice(),client)); - //this->request_invite(client); // NVR注册成功,立即请求设备目录 RequestCatalog(client); @@ -370,14 +379,14 @@ void SipServer::response_message(eXosip_event_t *evtp) { parse_xml(body->body, "", false, "", false, DeviceID); } - // LOG_INFO("CmdType={},DeviceID={}", CmdType, DeviceID); - if(!strcmp(CmdType, "Catalog")) { this->response_message_answer(evtp,200); // 需要根据对方的Catelog请求,做一些相应的应答请求 CCatalogParser catPaser; std::vector vec_device = catPaser.Decode_Catlog(body->body); printDevice(vec_device); + + std::lock_guard l(m_device_map_mtx); for (size_t i = 0; i < vec_device.size(); i++) { DeviceInfo info = vec_device[i]; m_device_map[info.id] = info; @@ -385,12 +394,19 @@ void SipServer::response_message(eXosip_event_t *evtp) { } else if(!strcmp(CmdType, "Keepalive")){ this->response_message_answer(evtp,200); + // LOG_INFO("CmdType={},DeviceID={}", CmdType, DeviceID); + std::lock_guard l_c(m_client_map_mtx); + Client* client = mClientMap[DeviceID]; + if (client != nullptr) { + client->updateHeartBeat(UtilTools::get_cur_time_ms()); + } }else{ this->response_message_answer(evtp,200); } } bool SipServer::check_device_status(string id) { + std::lock_guard l(m_device_map_mtx); auto it_info = m_device_map.find(id); if (it_info == m_device_map.end()) { return false; @@ -407,11 +423,14 @@ bool SipServer::check_device_status(string id) { } Client* SipServer::get_parent_by_id(string id) { + std::lock_guard l(m_device_map_mtx); auto it_info = m_device_map.find(id); if (it_info == m_device_map.end()) { return nullptr; } string parent_id = it_info->second.parentid; + + std::lock_guard l_c(m_client_map_mtx); auto it_client = mClientMap.find(parent_id); if (it_client == mClientMap.end()) { return nullptr; @@ -564,6 +583,9 @@ int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) { } void SipServer::cacheCatalog() { + + std::lock_guard l(m_client_map_mtx); + if (mClientMap.size() <= 0){ cout << "no IPC" << endl; return ; @@ -608,6 +630,7 @@ void SipServer::RequestCatalog(Client* client) { } int SipServer::clearClientMap(){ + std::lock_guard l(m_client_map_mtx); for (auto iter=mClientMap.begin(); iter!=mClientMap.end(); iter++) { delete iter->second; iter->second = nullptr; @@ -618,6 +641,7 @@ int SipServer::clearClientMap(){ } void SipServer::deleteClientByDevice(string device) { + std::lock_guard l(m_client_map_mtx); auto it = mClientMap.find(device); if (it == mClientMap.end()) { return ; diff --git a/src/decoder/gb28181/sip/SipServer.h b/src/decoder/gb28181/sip/SipServer.h index 71a0daf..1332fb5 100644 --- a/src/decoder/gb28181/sip/SipServer.h +++ b/src/decoder/gb28181/sip/SipServer.h @@ -14,6 +14,7 @@ extern "C" { #include #include #include +#include #include "./Message/CatalogParser.h" @@ -101,15 +102,21 @@ public: return mPort; } + unsigned long getHeartBeat() { + return mHeartBeatTime; + } + + void updateHeartBeat(unsigned long ts) { + mHeartBeatTime = ts; + } + private: - // step1 string mIp; // client ip int mPort; // client port string mDevice;// 340200000013200000024 - // step2 bool mIsReg; - // step3 - int mRtpPort{}; + int mRtpPort; + unsigned long mHeartBeatTime{0}; }; @@ -169,7 +176,9 @@ private: ServerInfo mInfo; std::map mClientMap;// + mutex m_client_map_mtx; std::map m_device_map; + mutex m_device_map_mtx; thread* m_event_loop_thread; };