Commit 1116447e746c09db31c7544bcce53f75e58666bb
1 parent
fe625b06
代码逻辑有花
Showing
2 changed files
with
41 additions
and
8 deletions
src/decoder/gb28181/sip/SipServer.cpp
@@ -124,7 +124,7 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) { | @@ -124,7 +124,7 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) { | ||
124 | this->clearClientMap(); | 124 | this->clearClientMap(); |
125 | break; | 125 | break; |
126 | case EXOSIP_MESSAGE_NEW://23 | 126 | case EXOSIP_MESSAGE_NEW://23 |
127 | - LOG_INFO("EXOSIP_MESSAGE_NEW type={}",evtp->type); | 127 | + // LOG_INFO("EXOSIP_MESSAGE_NEW type={}",evtp->type); |
128 | 128 | ||
129 | if (MSG_IS_REGISTER(evtp->request)) { | 129 | if (MSG_IS_REGISTER(evtp->request)) { |
130 | this->response_register(evtp); | 130 | this->response_register(evtp); |
@@ -300,9 +300,18 @@ void SipServer::response_register(eXosip_event_t *evtp) { | @@ -300,9 +300,18 @@ void SipServer::response_register(eXosip_event_t *evtp) { | ||
300 | 300 | ||
301 | LOG_INFO("Camera registration succee,ip={},port={},device={}",client->getIp(),client->getPort(),client->getDevice()); | 301 | LOG_INFO("Camera registration succee,ip={},port={},device={}",client->getIp(),client->getPort(),client->getDevice()); |
302 | 302 | ||
303 | + std::lock_guard<std::mutex> l(m_client_map_mtx); | ||
304 | + | ||
305 | + auto it_client = mClientMap.find(client->getDevice()); | ||
306 | + if (it_client != mClientMap.end()) { | ||
307 | + Client* c = it_client->second; | ||
308 | + delete c; | ||
309 | + c = nullptr; | ||
310 | + mClientMap.erase(it_client); | ||
311 | + } | ||
312 | + | ||
303 | mClientMap.insert(std::make_pair(client->getDevice(),client)); | 313 | mClientMap.insert(std::make_pair(client->getDevice(),client)); |
304 | 314 | ||
305 | - //this->request_invite(client); | ||
306 | // NVR注册成功,立即请求设备目录 | 315 | // NVR注册成功,立即请求设备目录 |
307 | RequestCatalog(client); | 316 | RequestCatalog(client); |
308 | 317 | ||
@@ -370,14 +379,14 @@ void SipServer::response_message(eXosip_event_t *evtp) { | @@ -370,14 +379,14 @@ void SipServer::response_message(eXosip_event_t *evtp) { | ||
370 | parse_xml(body->body, "<DeviceID>", false, "</DeviceID>", false, DeviceID); | 379 | parse_xml(body->body, "<DeviceID>", false, "</DeviceID>", false, DeviceID); |
371 | } | 380 | } |
372 | 381 | ||
373 | - // LOG_INFO("CmdType={},DeviceID={}", CmdType, DeviceID); | ||
374 | - | ||
375 | if(!strcmp(CmdType, "Catalog")) { | 382 | if(!strcmp(CmdType, "Catalog")) { |
376 | this->response_message_answer(evtp,200); | 383 | this->response_message_answer(evtp,200); |
377 | // 需要根据对方的Catelog请求,做一些相应的应答请求 | 384 | // 需要根据对方的Catelog请求,做一些相应的应答请求 |
378 | CCatalogParser catPaser; | 385 | CCatalogParser catPaser; |
379 | std::vector<DeviceInfo> vec_device = catPaser.Decode_Catlog(body->body); | 386 | std::vector<DeviceInfo> vec_device = catPaser.Decode_Catlog(body->body); |
380 | printDevice(vec_device); | 387 | printDevice(vec_device); |
388 | + | ||
389 | + std::lock_guard<std::mutex> l(m_device_map_mtx); | ||
381 | for (size_t i = 0; i < vec_device.size(); i++) { | 390 | for (size_t i = 0; i < vec_device.size(); i++) { |
382 | DeviceInfo info = vec_device[i]; | 391 | DeviceInfo info = vec_device[i]; |
383 | m_device_map[info.id] = info; | 392 | m_device_map[info.id] = info; |
@@ -385,12 +394,19 @@ void SipServer::response_message(eXosip_event_t *evtp) { | @@ -385,12 +394,19 @@ void SipServer::response_message(eXosip_event_t *evtp) { | ||
385 | } | 394 | } |
386 | else if(!strcmp(CmdType, "Keepalive")){ | 395 | else if(!strcmp(CmdType, "Keepalive")){ |
387 | this->response_message_answer(evtp,200); | 396 | this->response_message_answer(evtp,200); |
397 | + // LOG_INFO("CmdType={},DeviceID={}", CmdType, DeviceID); | ||
398 | + std::lock_guard<std::mutex> l_c(m_client_map_mtx); | ||
399 | + Client* client = mClientMap[DeviceID]; | ||
400 | + if (client != nullptr) { | ||
401 | + client->updateHeartBeat(UtilTools::get_cur_time_ms()); | ||
402 | + } | ||
388 | }else{ | 403 | }else{ |
389 | this->response_message_answer(evtp,200); | 404 | this->response_message_answer(evtp,200); |
390 | } | 405 | } |
391 | } | 406 | } |
392 | 407 | ||
393 | bool SipServer::check_device_status(string id) { | 408 | bool SipServer::check_device_status(string id) { |
409 | + std::lock_guard<std::mutex> l(m_device_map_mtx); | ||
394 | auto it_info = m_device_map.find(id); | 410 | auto it_info = m_device_map.find(id); |
395 | if (it_info == m_device_map.end()) { | 411 | if (it_info == m_device_map.end()) { |
396 | return false; | 412 | return false; |
@@ -407,11 +423,14 @@ bool SipServer::check_device_status(string id) { | @@ -407,11 +423,14 @@ bool SipServer::check_device_status(string id) { | ||
407 | } | 423 | } |
408 | 424 | ||
409 | Client* SipServer::get_parent_by_id(string id) { | 425 | Client* SipServer::get_parent_by_id(string id) { |
426 | + std::lock_guard<std::mutex> l(m_device_map_mtx); | ||
410 | auto it_info = m_device_map.find(id); | 427 | auto it_info = m_device_map.find(id); |
411 | if (it_info == m_device_map.end()) { | 428 | if (it_info == m_device_map.end()) { |
412 | return nullptr; | 429 | return nullptr; |
413 | } | 430 | } |
414 | string parent_id = it_info->second.parentid; | 431 | string parent_id = it_info->second.parentid; |
432 | + | ||
433 | + std::lock_guard<std::mutex> l_c(m_client_map_mtx); | ||
415 | auto it_client = mClientMap.find(parent_id); | 434 | auto it_client = mClientMap.find(parent_id); |
416 | if (it_client == mClientMap.end()) { | 435 | if (it_client == mClientMap.end()) { |
417 | return nullptr; | 436 | return nullptr; |
@@ -564,6 +583,9 @@ int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) { | @@ -564,6 +583,9 @@ int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) { | ||
564 | } | 583 | } |
565 | 584 | ||
566 | void SipServer::cacheCatalog() { | 585 | void SipServer::cacheCatalog() { |
586 | + | ||
587 | + std::lock_guard<std::mutex> l(m_client_map_mtx); | ||
588 | + | ||
567 | if (mClientMap.size() <= 0){ | 589 | if (mClientMap.size() <= 0){ |
568 | cout << "no IPC" << endl; | 590 | cout << "no IPC" << endl; |
569 | return ; | 591 | return ; |
@@ -608,6 +630,7 @@ void SipServer::RequestCatalog(Client* client) { | @@ -608,6 +630,7 @@ void SipServer::RequestCatalog(Client* client) { | ||
608 | } | 630 | } |
609 | 631 | ||
610 | int SipServer::clearClientMap(){ | 632 | int SipServer::clearClientMap(){ |
633 | + std::lock_guard<std::mutex> l(m_client_map_mtx); | ||
611 | for (auto iter=mClientMap.begin(); iter!=mClientMap.end(); iter++) { | 634 | for (auto iter=mClientMap.begin(); iter!=mClientMap.end(); iter++) { |
612 | delete iter->second; | 635 | delete iter->second; |
613 | iter->second = nullptr; | 636 | iter->second = nullptr; |
@@ -618,6 +641,7 @@ int SipServer::clearClientMap(){ | @@ -618,6 +641,7 @@ int SipServer::clearClientMap(){ | ||
618 | } | 641 | } |
619 | 642 | ||
620 | void SipServer::deleteClientByDevice(string device) { | 643 | void SipServer::deleteClientByDevice(string device) { |
644 | + std::lock_guard<std::mutex> l(m_client_map_mtx); | ||
621 | auto it = mClientMap.find(device); | 645 | auto it = mClientMap.find(device); |
622 | if (it == mClientMap.end()) { | 646 | if (it == mClientMap.end()) { |
623 | return ; | 647 | return ; |
src/decoder/gb28181/sip/SipServer.h
@@ -14,6 +14,7 @@ extern "C" { | @@ -14,6 +14,7 @@ extern "C" { | ||
14 | #include <string> | 14 | #include <string> |
15 | #include <thread> | 15 | #include <thread> |
16 | #include <chrono> | 16 | #include <chrono> |
17 | +#include <mutex> | ||
17 | 18 | ||
18 | #include "./Message/CatalogParser.h" | 19 | #include "./Message/CatalogParser.h" |
19 | 20 | ||
@@ -101,15 +102,21 @@ public: | @@ -101,15 +102,21 @@ public: | ||
101 | return mPort; | 102 | return mPort; |
102 | } | 103 | } |
103 | 104 | ||
105 | + unsigned long getHeartBeat() { | ||
106 | + return mHeartBeatTime; | ||
107 | + } | ||
108 | + | ||
109 | + void updateHeartBeat(unsigned long ts) { | ||
110 | + mHeartBeatTime = ts; | ||
111 | + } | ||
112 | + | ||
104 | private: | 113 | private: |
105 | - // step1 | ||
106 | string mIp; // client ip | 114 | string mIp; // client ip |
107 | int mPort; // client port | 115 | int mPort; // client port |
108 | string mDevice;// 340200000013200000024 | 116 | string mDevice;// 340200000013200000024 |
109 | - // step2 | ||
110 | bool mIsReg; | 117 | bool mIsReg; |
111 | - // step3 | ||
112 | - int mRtpPort{}; | 118 | + int mRtpPort; |
119 | + unsigned long mHeartBeatTime{0}; | ||
113 | 120 | ||
114 | }; | 121 | }; |
115 | 122 | ||
@@ -169,7 +176,9 @@ private: | @@ -169,7 +176,9 @@ private: | ||
169 | ServerInfo mInfo; | 176 | ServerInfo mInfo; |
170 | 177 | ||
171 | std::map<std::string, Client *> mClientMap;// <DeviceID,SipClient> | 178 | std::map<std::string, Client *> mClientMap;// <DeviceID,SipClient> |
179 | + mutex m_client_map_mtx; | ||
172 | std::map<std::string, DeviceInfo> m_device_map; | 180 | std::map<std::string, DeviceInfo> m_device_map; |
181 | + mutex m_device_map_mtx; | ||
173 | 182 | ||
174 | thread* m_event_loop_thread; | 183 | thread* m_event_loop_thread; |
175 | }; | 184 | }; |