Commit 10821ac3a7ff876618b681cb54c40f23b48b8959

Authored by Hu Chunming
1 parent db9bcb04

1.定时刷新设备目录;

2.无鉴权注册;
3.修复注册时client为null导致崩溃的问题
4.返回invite返回值
sip/SipServer.cpp
... ... @@ -66,6 +66,9 @@ SipServer::SipServer():
66 66 }
67 67 #endif // WIN32
68 68  
  69 + mClientMap.clear();
  70 + m_device_map.clear();
  71 +
69 72 }
70 73 SipServer::~SipServer() {
71 74 LOG_INFO("~SipServer");
... ... @@ -253,7 +256,7 @@ void SipServer::event_loop() {
253 256  
254 257 LOG_INFO("sip server init succeed: {}:{}", mInfo.getIp(), mInfo.getPort());
255 258  
256   - // thread* timing_getcatlog_threadptr = new std::thread(timing_getcatlog_thread, this);
  259 + thread* timing_getcatlog_threadptr = new std::thread(timing_getcatlog_thread, this);
257 260  
258 261 while(!mQuit) {
259 262 eXosip_event_t *evtp = eXosip_event_wait(mSipCtx, 0, 20);
... ... @@ -268,11 +271,11 @@ void SipServer::event_loop() {
268 271 }
269 272  
270 273 mQuit = true;
271   - // if (timing_getcatlog_threadptr) {
272   - // timing_getcatlog_threadptr->join();
273   - // delete timing_getcatlog_threadptr;
274   - // timing_getcatlog_threadptr = nullptr;
275   - // }
  274 + if (timing_getcatlog_threadptr) {
  275 + timing_getcatlog_threadptr->join();
  276 + delete timing_getcatlog_threadptr;
  277 + timing_getcatlog_threadptr = nullptr;
  278 + }
276 279 }
277 280  
278 281 void SipServer::Close() {
... ... @@ -374,7 +377,7 @@ void SipServer::response_register(eXosip_event_t *evtp) {
374 377  
375 378 if (!memcmp(calc_response, Response, HASHHEXLEN)) {
376 379 this->response_message_answer(evtp,200);
377   - cacheClient(contact->url);
  380 + cacheClient(contact->url, expire);
378 381 } else {
379 382 this->response_message_answer(evtp,401);
380 383 LOG_INFO("Camera registration error, p={},port={},device={}", strdup(contact->url->host), atoi(contact->url->port), strdup(username));
... ... @@ -388,22 +391,28 @@ void SipServer::response_register(eXosip_event_t *evtp) {
388 391 osip_free(uri);
389 392 } else {
390 393 response_register_401unauthorized(evtp);
391   - cacheClient(contact->url);
  394 + cacheClient(contact->url, expire);
392 395 }
393 396 }
394 397  
395   -void SipServer::cacheClient(osip_uri_t *url) {
  398 +void SipServer::cacheClient(osip_uri_t *url, int expiry) {
396 399  
397 400 string sip_id = strdup(url->username);
398 401  
  402 + long cur_ts = Utools::get_cur_time_ms();
  403 +
399 404 // 已经注册的不再注册
400 405 std::lock_guard<std::mutex> l(m_client_map_mtx);
401 406 auto it = mClientMap.find(sip_id);
402 407 if (it != mClientMap.end()) {
  408 + it->second->setExpiry(expiry);
  409 + it->second->updateHeartBeat(cur_ts);
403 410 return ;
404 411 }
405 412  
406 413 Client* client = new Client(strdup(url->host), atoi(url->port), strdup(url->username));
  414 + client->setExpiry(expiry);
  415 + client->updateHeartBeat(cur_ts);
407 416  
408 417 LOG_INFO("Camera registration succee,ip={},port={},device={}",client->getIp(),client->getPort(),client->getDevice());
409 418  
... ... @@ -474,9 +483,10 @@ void SipServer::response_message(eXosip_event_t *evtp) {
474 483 this->response_message_answer(evtp,200);
475 484 // LOG_INFO("CmdType={},DeviceID={}", CmdType, DeviceID);
476 485 std::lock_guard<std::mutex> l_c(m_client_map_mtx);
477   - Client* client = mClientMap[DeviceID];
478   - if (client != nullptr) {
479   - client->updateHeartBeat(Utools::get_cur_time_ms());
  486 + auto it = mClientMap.find(DeviceID);
  487 + if (it != mClientMap.end()) {
  488 + it->second->updateHeartBeat(Utools::get_cur_time_ms());
  489 + return ;
480 490 }
481 491 }else{
482 492 this->response_message_answer(evtp,200);
... ... @@ -783,9 +793,8 @@ void SipServer::dump_request(eXosip_event_t *evtp) {
783 793 if (s) {
784 794 LOG_INFO("\nprint request start\ntype={}\n{}\nprint request end\n",evtp->type,s);
785 795 }
786   -
787   - LOG_INFO("REQUEST cid:{} did: {}",evtp->cid, evtp->did);
788 796 }
  797 +
789 798 void SipServer::dump_response(eXosip_event_t *evtp) {
790 799 char *s = nullptr;
791 800 size_t len;
... ... @@ -794,7 +803,4 @@ void SipServer::dump_response(eXosip_event_t *evtp) {
794 803 {
795 804 LOG_INFO("\nprint response start\ntype={}\n{}\nprint response end\n",evtp->type,s);
796 805 }
797   -
798   -
799   - LOG_INFO("RESPONSE cid:{} did: {}",evtp->cid, evtp->did);
800 806 }
801 807 \ No newline at end of file
... ...
sip/SipServer.h
... ... @@ -55,6 +55,14 @@ public:
55 55 mHeartBeatTime = ts;
56 56 }
57 57  
  58 + long getExpiry() {
  59 + return mExipry;
  60 + }
  61 +
  62 + void setExpiry(long expiry) {
  63 + mExipry = expiry;
  64 + }
  65 +
58 66 private:
59 67 string mIp; // client ip
60 68 int mPort; // client port
... ... @@ -62,7 +70,7 @@ private:
62 70 bool mIsReg;
63 71 int mRtpPort;
64 72 unsigned long mHeartBeatTime{0};
65   -
  73 + long mExipry{0};
66 74 };
67 75  
68 76  
... ... @@ -92,7 +100,7 @@ private:
92 100 void RequestCatalog(Client* client);
93 101 void cacheCatalog();
94 102  
95   - void cacheClient(osip_uri_t *url);
  103 + void cacheClient(osip_uri_t *url, int expiry);
96 104  
97 105 void response_message_answer(eXosip_event_t *evtp,int code);
98 106 void response_register(eXosip_event_t *evtp);
... ...
sip/WebSocketServer.cpp
... ... @@ -57,9 +57,13 @@ int WebSocketServer::msg_parser(websocketpp::connection_hdl hdl, string msg) {
57 57  
58 58 string ip = get_ip_from_hdl(hdl);
59 59  
  60 + string response_msg = "";
  61 +
60 62 int ret = -100;
61 63 if ( StringTools::to_lower(vec_msg[0]) == "invite") {
62 64 ret = parse_invite(vec_msg, ip);
  65 + response_msg = vec_msg[2] + "_" + vec_msg[3] +"|" + vec_msg[0] + "|" + vec_msg[1] + "|" + to_string(ret);
  66 + ws_server.send(hdl, response_msg, websocketpp::frame::opcode::text);
63 67 } else if ( StringTools::to_lower(vec_msg[0]) == "bye") {
64 68 if (StringTools::to_lower(vec_msg[1]) == "invite") {
65 69 std::cout << "bye invite: " << vec_msg[2] << std::endl;
... ... @@ -85,7 +89,7 @@ void WebSocketServer::on_message(websocketpp::connection_hdl hdl, message_ptr ms
85 89 try {
86 90 int ret = msg_parser(hdl,msg->get_payload());
87 91 // 返回执行结果
88   - ws_server.send(hdl, to_string(ret), websocketpp::frame::opcode::text);
  92 + // ws_server.send(hdl, to_string(ret), websocketpp::frame::opcode::text);
89 93 } catch (websocketpp::exception const & e) {
90 94 std::cout << "Echo failed because: " << "(" << e.what() << ")" << std::endl;
91 95 ws_server.send(hdl, to_string(-101), websocketpp::frame::opcode::text);
... ...