Commit 1116447e746c09db31c7544bcce53f75e58666bb

Authored by Hu Chunming
1 parent fe625b06

代码逻辑有花

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