Commit 4efb624075ee655666e2bf6177a5ac6fba3f921c

Authored by Hu Chunming
1 parent e918c2a7

call release 自动重新invite 补交

sip/WebSocketServer.cpp
... ... @@ -25,13 +25,11 @@ int WebSocketServer::parse_invite(vector<string>& vec_msg, std::string ip) {
25 25 } else if (StringTools::to_lower(vec_msg[1]) == "tcp") {
26 26 net_type = "tcp";
27 27 }
28   - std::cout << "net type: " << vec_msg[1] << std::endl;
29   -
30   - std::cout << "invite " << vec_msg[2] << ":" << vec_msg[3] << std::endl;
  28 +
  29 + LOG_INFO("invite {}:{} net type: {}", vec_msg[2], vec_msg[3], vec_msg[1]);
31 30 int ret = -100;
32 31 if ("udp" == net_type) {
33 32 ret = sip_server.RequestInvite_UDP(vec_msg[2].c_str(), ip.c_str(), atoi(vec_msg[3].c_str()));
34   - // ret = sip_server.RequestInvite_UDP(vec_data[0].c_str(), "176.10.0.3", 30026);
35 33 } else if ("tcp" == net_type)
36 34 {
37 35 /* code */
... ... @@ -45,7 +43,7 @@ string WebSocketServer::get_ip_from_hdl(websocketpp::connection_hdl hdl) {
45 43 // 获取客户端IP地址
46 44 auto addr = con->get_socket().remote_endpoint().address().to_string();
47 45 std::string ip = addr.substr(addr.find_last_of(":")+1);
48   - std::cout << "ip:" << ip << endl;
  46 + LOG_DEBUG("ip:{}", ip);
49 47 return ip;
50 48 }
51 49  
... ... @@ -58,26 +56,57 @@ int WebSocketServer::msg_parser(websocketpp::connection_hdl hdl, string msg) {
58 56 string ip = get_ip_from_hdl(hdl);
59 57  
60 58 string response_msg = "";
61   -
62 59 int ret = -100;
63 60 if ( StringTools::to_lower(vec_msg[0]) == "invite") {
64 61 ret = parse_invite(vec_msg, ip);
65   - response_msg = vec_msg[2] + "_" + vec_msg[3] +"|" + vec_msg[0] + "|" + vec_msg[1] + "|" + to_string(ret);
  62 + string strKey = vec_msg[2] + "_" + vec_msg[3];
  63 + if (ret > 0) {
  64 + std::lock_guard<std::mutex> l_hdl(m_channelport_hdl_map_mtx);
  65 + m_channelport_hdl_map[strKey] = hdl;
  66 + }
  67 +
  68 + response_msg = strKey +"|" + vec_msg[0] + "|" + vec_msg[1] + "|" + to_string(ret);
66 69 ws_server.send(hdl, response_msg, websocketpp::frame::opcode::text);
67 70 } else if ( StringTools::to_lower(vec_msg[0]) == "bye") {
68 71 if (StringTools::to_lower(vec_msg[1]) == "invite") {
69   - std::cout << "bye invite: " << vec_msg[2] << std::endl;
  72 + LOG_INFO("BYE invite: {}", vec_msg[2]);
  73 +
  74 + std::lock_guard<std::mutex> l_hdl(m_channelport_hdl_map_mtx);
  75 + string strKey = vec_msg[2] + "_" + vec_msg[3];
  76 + auto it = m_channelport_hdl_map.find(strKey);
  77 + if (it != m_channelport_hdl_map.end()) {
  78 + m_channelport_hdl_map.erase(it);
  79 + }
  80 +
70 81 ret = sip_server.ByeInvite(vec_msg[2], ip, atoi(vec_msg[3].c_str()));
71 82 }
72 83 } else {
73   - std::cout << "on_message called with hdl: " << hdl.lock().get()
74   - << " and message: " << msg
75   - << std::endl;
  84 + LOG_ERROR("on_message called with hdl: {} and message:{}", hdl.lock().get(), msg);
76 85 }
77 86  
78 87 return ret;
79 88 }
80 89  
  90 +void WebSocketServer::response_client(std::string sip_channel_id, int rtp_port, std::string net_type, int ret) {
  91 +
  92 + std::lock_guard<std::mutex> l_hdl(m_channelport_hdl_map_mtx);
  93 +
  94 + string strKey = sip_channel_id + "_" + to_string(rtp_port);
  95 + auto it = m_channelport_hdl_map.find(strKey);
  96 + if (it == m_channelport_hdl_map.end()) {
  97 + return;
  98 + }
  99 +
  100 + try {
  101 + // invite
  102 + string response_msg = strKey +"|invite|" + net_type + "|" + to_string(ret);
  103 + ws_server.send(it->second, response_msg, websocketpp::frame::opcode::text);
  104 + } catch(const std::exception& e) {
  105 + // 存在一种可能:连接已经断开,但是sip那边请求失败,这种时候应该是会抛出异常。所以这里用try-cache,屏蔽此情况造成的问题
  106 + LOG_WARN("response failed because::{}", e.what());
  107 + }
  108 +}
  109 +
81 110 // Define a callback to handle incoming messages
82 111 void WebSocketServer::on_message(websocketpp::connection_hdl hdl, message_ptr msg) {
83 112  
... ... @@ -87,29 +116,37 @@ void WebSocketServer::on_message(websocketpp::connection_hdl hdl, message_ptr ms
87 116 }
88 117  
89 118 try {
90   - int ret = msg_parser(hdl,msg->get_payload());
91   - // 返回执行结果
92   - // ws_server.send(hdl, to_string(ret), websocketpp::frame::opcode::text);
  119 + msg_parser(hdl,msg->get_payload());
93 120 } catch (websocketpp::exception const & e) {
94   - std::cout << "Echo failed because: " << "(" << e.what() << ")" << std::endl;
  121 + LOG_ERROR("Echo failed because::{}", e.what());
95 122 ws_server.send(hdl, to_string(-101), websocketpp::frame::opcode::text);
96 123 }
97 124 }
98 125  
99 126 void WebSocketServer::on_open(websocketpp::connection_hdl hdl)
100 127 {
101   - // std::string msg = "hello";
102   - // c.send(hdl, msg, websocketpp::frame::opcode::text);
103   - // c.get_alog().write(websocketpp::log::alevel::app, "Tx: " + msg);
104   - std::cout << "连接sip服务器成功!" << std::endl;
  128 + LOG_INFO("连接sip服务器成功!");
105 129 }
106 130  
107 131 void WebSocketServer::on_fail(websocketpp::connection_hdl h){
108   - std::cout << "连接sip服务器失败!" << std::endl;
  132 + LOG_WARN("连接sip服务器失败!");
109 133 }
110 134  
111 135 void WebSocketServer::on_close(websocketpp::connection_hdl h){
112   - std::cout << "on_close" << std::endl;
  136 + LOG_INFO("on_close:{}", h.lock().get());
  137 +
  138 + std::lock_guard<std::mutex> l_hdl(m_channelport_hdl_map_mtx);
  139 +
  140 + for (auto it = m_channelport_hdl_map.begin(); it != m_channelport_hdl_map.end(); )
  141 + {
  142 + server::connection_ptr con = ws_server.get_con_from_hdl(it->second);
  143 + server::connection_ptr con_std = ws_server.get_con_from_hdl(h);
  144 + if (con == con_std) {
  145 + it = m_channelport_hdl_map.erase(it);
  146 + } else {
  147 + it++;
  148 + }
  149 + }
113 150 }
114 151  
115 152  
... ... @@ -123,7 +160,7 @@ void WebSocketServer::start() {
123 160  
124 161 ServerInfo info = pConfig->getServerCfg();
125 162  
126   - sip_server.Init(&info);
  163 + sip_server.Init(&info, this);
127 164  
128 165 try {
129 166 // Set logging settings
... ... @@ -141,7 +178,7 @@ void WebSocketServer::start() {
141 178  
142 179 // Listen on port 9002
143 180 int port = info.getWsPort();
144   - std::cout << "websocket server listen:" << port << std::endl;
  181 + LOG_INFO("websocket server listen:{}", port);
145 182 ws_server.listen(port);
146 183  
147 184 // Start the server accept loop
... ... @@ -150,8 +187,8 @@ void WebSocketServer::start() {
150 187 // Start the ASIO io_service run loop
151 188 ws_server.run();
152 189 } catch (websocketpp::exception const & e) {
153   - std::cout << e.what() << std::endl;
  190 + LOG_ERROR("websocket start failed:{}", e.what());
154 191 } catch (...) {
155   - std::cout << "other exception" << std::endl;
  192 + LOG_ERROR("websocket start failed: other exception");
156 193 }
157 194 }
... ...
sip/WebSocketServer.h
  1 +#ifndef __WEB_SOCKET_SERVER__
  2 +#define __WEB_SOCKET_SERVER__
  3 +
1 4 #include <websocketpp/config/asio_no_tls.hpp>
2 5 #include <websocketpp/server.hpp>
3 6  
4 7 #include "SipServer.h"
  8 +#include <map>
  9 +#include <mutex>
5 10  
6 11 typedef websocketpp::server<websocketpp::config::asio> server;
7 12  
... ... @@ -24,6 +29,8 @@ public:
24 29  
25 30 void start();
26 31  
  32 + void response_client(std::string sip_channel_id, int rtp_port, std::string net_type, int ret);
  33 +
27 34 private:
28 35 int msg_parser(websocketpp::connection_hdl hdl, string msg);
29 36 int parse_invite(vector<string>& vec_msg, std::string ip);
... ... @@ -33,4 +40,9 @@ private:
33 40 private:
34 41 server ws_server;
35 42 SipServer sip_server;
36   -};
37 43 \ No newline at end of file
  44 +
  45 + std::map<string, websocketpp::connection_hdl> m_channelport_hdl_map;
  46 + std::mutex m_channelport_hdl_map_mtx;
  47 +};
  48 +
  49 +#endif // __WEB_SOCKET_SERVER__
38 50 \ No newline at end of file
... ...