Commit 10821ac3a7ff876618b681cb54c40f23b48b8959
1 parent
db9bcb04
1.定时刷新设备目录;
2.无鉴权注册; 3.修复注册时client为null导致崩溃的问题 4.返回invite返回值
Showing
3 changed files
with
38 additions
and
20 deletions
sip/SipServer.cpp
@@ -66,6 +66,9 @@ SipServer::SipServer(): | @@ -66,6 +66,9 @@ SipServer::SipServer(): | ||
66 | } | 66 | } |
67 | #endif // WIN32 | 67 | #endif // WIN32 |
68 | 68 | ||
69 | + mClientMap.clear(); | ||
70 | + m_device_map.clear(); | ||
71 | + | ||
69 | } | 72 | } |
70 | SipServer::~SipServer() { | 73 | SipServer::~SipServer() { |
71 | LOG_INFO("~SipServer"); | 74 | LOG_INFO("~SipServer"); |
@@ -253,7 +256,7 @@ void SipServer::event_loop() { | @@ -253,7 +256,7 @@ void SipServer::event_loop() { | ||
253 | 256 | ||
254 | LOG_INFO("sip server init succeed: {}:{}", mInfo.getIp(), mInfo.getPort()); | 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 | while(!mQuit) { | 261 | while(!mQuit) { |
259 | eXosip_event_t *evtp = eXosip_event_wait(mSipCtx, 0, 20); | 262 | eXosip_event_t *evtp = eXosip_event_wait(mSipCtx, 0, 20); |
@@ -268,11 +271,11 @@ void SipServer::event_loop() { | @@ -268,11 +271,11 @@ void SipServer::event_loop() { | ||
268 | } | 271 | } |
269 | 272 | ||
270 | mQuit = true; | 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 | void SipServer::Close() { | 281 | void SipServer::Close() { |
@@ -374,7 +377,7 @@ void SipServer::response_register(eXosip_event_t *evtp) { | @@ -374,7 +377,7 @@ void SipServer::response_register(eXosip_event_t *evtp) { | ||
374 | 377 | ||
375 | if (!memcmp(calc_response, Response, HASHHEXLEN)) { | 378 | if (!memcmp(calc_response, Response, HASHHEXLEN)) { |
376 | this->response_message_answer(evtp,200); | 379 | this->response_message_answer(evtp,200); |
377 | - cacheClient(contact->url); | 380 | + cacheClient(contact->url, expire); |
378 | } else { | 381 | } else { |
379 | this->response_message_answer(evtp,401); | 382 | this->response_message_answer(evtp,401); |
380 | LOG_INFO("Camera registration error, p={},port={},device={}", strdup(contact->url->host), atoi(contact->url->port), strdup(username)); | 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,22 +391,28 @@ void SipServer::response_register(eXosip_event_t *evtp) { | ||
388 | osip_free(uri); | 391 | osip_free(uri); |
389 | } else { | 392 | } else { |
390 | response_register_401unauthorized(evtp); | 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 | string sip_id = strdup(url->username); | 400 | string sip_id = strdup(url->username); |
398 | 401 | ||
402 | + long cur_ts = Utools::get_cur_time_ms(); | ||
403 | + | ||
399 | // 已经注册的不再注册 | 404 | // 已经注册的不再注册 |
400 | std::lock_guard<std::mutex> l(m_client_map_mtx); | 405 | std::lock_guard<std::mutex> l(m_client_map_mtx); |
401 | auto it = mClientMap.find(sip_id); | 406 | auto it = mClientMap.find(sip_id); |
402 | if (it != mClientMap.end()) { | 407 | if (it != mClientMap.end()) { |
408 | + it->second->setExpiry(expiry); | ||
409 | + it->second->updateHeartBeat(cur_ts); | ||
403 | return ; | 410 | return ; |
404 | } | 411 | } |
405 | 412 | ||
406 | Client* client = new Client(strdup(url->host), atoi(url->port), strdup(url->username)); | 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 | LOG_INFO("Camera registration succee,ip={},port={},device={}",client->getIp(),client->getPort(),client->getDevice()); | 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,9 +483,10 @@ void SipServer::response_message(eXosip_event_t *evtp) { | ||
474 | this->response_message_answer(evtp,200); | 483 | this->response_message_answer(evtp,200); |
475 | // LOG_INFO("CmdType={},DeviceID={}", CmdType, DeviceID); | 484 | // LOG_INFO("CmdType={},DeviceID={}", CmdType, DeviceID); |
476 | std::lock_guard<std::mutex> l_c(m_client_map_mtx); | 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 | }else{ | 491 | }else{ |
482 | this->response_message_answer(evtp,200); | 492 | this->response_message_answer(evtp,200); |
@@ -783,9 +793,8 @@ void SipServer::dump_request(eXosip_event_t *evtp) { | @@ -783,9 +793,8 @@ void SipServer::dump_request(eXosip_event_t *evtp) { | ||
783 | if (s) { | 793 | if (s) { |
784 | LOG_INFO("\nprint request start\ntype={}\n{}\nprint request end\n",evtp->type,s); | 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 | void SipServer::dump_response(eXosip_event_t *evtp) { | 798 | void SipServer::dump_response(eXosip_event_t *evtp) { |
790 | char *s = nullptr; | 799 | char *s = nullptr; |
791 | size_t len; | 800 | size_t len; |
@@ -794,7 +803,4 @@ void SipServer::dump_response(eXosip_event_t *evtp) { | @@ -794,7 +803,4 @@ void SipServer::dump_response(eXosip_event_t *evtp) { | ||
794 | { | 803 | { |
795 | LOG_INFO("\nprint response start\ntype={}\n{}\nprint response end\n",evtp->type,s); | 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 | \ No newline at end of file | 807 | \ No newline at end of file |
sip/SipServer.h
@@ -55,6 +55,14 @@ public: | @@ -55,6 +55,14 @@ public: | ||
55 | mHeartBeatTime = ts; | 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 | private: | 66 | private: |
59 | string mIp; // client ip | 67 | string mIp; // client ip |
60 | int mPort; // client port | 68 | int mPort; // client port |
@@ -62,7 +70,7 @@ private: | @@ -62,7 +70,7 @@ private: | ||
62 | bool mIsReg; | 70 | bool mIsReg; |
63 | int mRtpPort; | 71 | int mRtpPort; |
64 | unsigned long mHeartBeatTime{0}; | 72 | unsigned long mHeartBeatTime{0}; |
65 | - | 73 | + long mExipry{0}; |
66 | }; | 74 | }; |
67 | 75 | ||
68 | 76 | ||
@@ -92,7 +100,7 @@ private: | @@ -92,7 +100,7 @@ private: | ||
92 | void RequestCatalog(Client* client); | 100 | void RequestCatalog(Client* client); |
93 | void cacheCatalog(); | 101 | void cacheCatalog(); |
94 | 102 | ||
95 | - void cacheClient(osip_uri_t *url); | 103 | + void cacheClient(osip_uri_t *url, int expiry); |
96 | 104 | ||
97 | void response_message_answer(eXosip_event_t *evtp,int code); | 105 | void response_message_answer(eXosip_event_t *evtp,int code); |
98 | void response_register(eXosip_event_t *evtp); | 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,9 +57,13 @@ int WebSocketServer::msg_parser(websocketpp::connection_hdl hdl, string msg) { | ||
57 | 57 | ||
58 | string ip = get_ip_from_hdl(hdl); | 58 | string ip = get_ip_from_hdl(hdl); |
59 | 59 | ||
60 | + string response_msg = ""; | ||
61 | + | ||
60 | int ret = -100; | 62 | int ret = -100; |
61 | if ( StringTools::to_lower(vec_msg[0]) == "invite") { | 63 | if ( StringTools::to_lower(vec_msg[0]) == "invite") { |
62 | ret = parse_invite(vec_msg, ip); | 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 | } else if ( StringTools::to_lower(vec_msg[0]) == "bye") { | 67 | } else if ( StringTools::to_lower(vec_msg[0]) == "bye") { |
64 | if (StringTools::to_lower(vec_msg[1]) == "invite") { | 68 | if (StringTools::to_lower(vec_msg[1]) == "invite") { |
65 | std::cout << "bye invite: " << vec_msg[2] << std::endl; | 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,7 +89,7 @@ void WebSocketServer::on_message(websocketpp::connection_hdl hdl, message_ptr ms | ||
85 | try { | 89 | try { |
86 | int ret = msg_parser(hdl,msg->get_payload()); | 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 | } catch (websocketpp::exception const & e) { | 93 | } catch (websocketpp::exception const & e) { |
90 | std::cout << "Echo failed because: " << "(" << e.what() << ")" << std::endl; | 94 | std::cout << "Echo failed because: " << "(" << e.what() << ")" << std::endl; |
91 | ws_server.send(hdl, to_string(-101), websocketpp::frame::opcode::text); | 95 | ws_server.send(hdl, to_string(-101), websocketpp::frame::opcode::text); |