c887a0f0
Hu Chunming
提交初成版代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include <iostream>
#include "WebSocketServer.h"
#include "./Utils/StringTools.hpp"
#include "ConfigParser.hpp"
#include "./Utils/logger.hpp"
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
WebSocketServer::WebSocketServer(/* args */)
{
}
WebSocketServer::~WebSocketServer()
{
}
int WebSocketServer::parse_invite(vector<string>& vec_msg, std::string ip) {
string net_type = "";
if (StringTools::to_lower(vec_msg[1]) == "udp") {
net_type = "udp";
} else if (StringTools::to_lower(vec_msg[1]) == "tcp") {
net_type = "tcp";
}
|
4efb6240
Hu Chunming
call release 自动重新...
|
28
29
|
LOG_INFO("invite {}:{} net type: {}", vec_msg[2], vec_msg[3], vec_msg[1]);
|
c887a0f0
Hu Chunming
提交初成版代码
|
30
31
32
|
int ret = -100;
if ("udp" == net_type) {
ret = sip_server.RequestInvite_UDP(vec_msg[2].c_str(), ip.c_str(), atoi(vec_msg[3].c_str()));
|
3a5bc7be
Hu Chunming
添加无鉴权;支持摄像头直连
|
33
34
|
} else if ("tcp" == net_type) {
ret = sip_server.RequestInvite_TCP_a(vec_msg[2].c_str(), ip.c_str(), atoi(vec_msg[3].c_str()));
|
c887a0f0
Hu Chunming
提交初成版代码
|
35
36
37
38
39
40
41
42
43
44
|
}
return ret;
}
string WebSocketServer::get_ip_from_hdl(websocketpp::connection_hdl hdl) {
server::connection_ptr con = ws_server.get_con_from_hdl(hdl);
// 获取客户端IP地址
auto addr = con->get_socket().remote_endpoint().address().to_string();
std::string ip = addr.substr(addr.find_last_of(":")+1);
|
4efb6240
Hu Chunming
call release 自动重新...
|
45
|
LOG_DEBUG("ip:{}", ip);
|
c887a0f0
Hu Chunming
提交初成版代码
|
46
47
48
49
50
51
52
53
54
55
56
|
return ip;
}
int WebSocketServer::msg_parser(websocketpp::connection_hdl hdl, string msg) {
vector<string> vec_msg = StringTools::split(msg, "|");
if (vec_msg.size() <= 0) {
return -1;
}
string ip = get_ip_from_hdl(hdl);
|
10821ac3
Hu Chunming
1.定时刷新设备目录;
|
57
|
string response_msg = "";
|
c887a0f0
Hu Chunming
提交初成版代码
|
58
59
60
|
int ret = -100;
if ( StringTools::to_lower(vec_msg[0]) == "invite") {
ret = parse_invite(vec_msg, ip);
|
4efb6240
Hu Chunming
call release 自动重新...
|
61
62
63
64
65
66
67
|
string strKey = vec_msg[2] + "_" + vec_msg[3];
if (ret > 0) {
std::lock_guard<std::mutex> l_hdl(m_channelport_hdl_map_mtx);
m_channelport_hdl_map[strKey] = hdl;
}
response_msg = strKey +"|" + vec_msg[0] + "|" + vec_msg[1] + "|" + to_string(ret);
|
10821ac3
Hu Chunming
1.定时刷新设备目录;
|
68
|
ws_server.send(hdl, response_msg, websocketpp::frame::opcode::text);
|
c887a0f0
Hu Chunming
提交初成版代码
|
69
70
|
} else if ( StringTools::to_lower(vec_msg[0]) == "bye") {
if (StringTools::to_lower(vec_msg[1]) == "invite") {
|
4efb6240
Hu Chunming
call release 自动重新...
|
71
72
73
74
75
76
77
78
79
|
LOG_INFO("BYE invite: {}", vec_msg[2]);
std::lock_guard<std::mutex> l_hdl(m_channelport_hdl_map_mtx);
string strKey = vec_msg[2] + "_" + vec_msg[3];
auto it = m_channelport_hdl_map.find(strKey);
if (it != m_channelport_hdl_map.end()) {
m_channelport_hdl_map.erase(it);
}
|
c887a0f0
Hu Chunming
提交初成版代码
|
80
81
82
|
ret = sip_server.ByeInvite(vec_msg[2], ip, atoi(vec_msg[3].c_str()));
}
} else {
|
4efb6240
Hu Chunming
call release 自动重新...
|
83
|
LOG_ERROR("on_message called with hdl: {} and message:{}", hdl.lock().get(), msg);
|
c887a0f0
Hu Chunming
提交初成版代码
|
84
85
86
87
88
|
}
return ret;
}
|
4efb6240
Hu Chunming
call release 自动重新...
|
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
void WebSocketServer::response_client(std::string sip_channel_id, int rtp_port, std::string net_type, int ret) {
std::lock_guard<std::mutex> l_hdl(m_channelport_hdl_map_mtx);
string strKey = sip_channel_id + "_" + to_string(rtp_port);
auto it = m_channelport_hdl_map.find(strKey);
if (it == m_channelport_hdl_map.end()) {
return;
}
try {
// invite
string response_msg = strKey +"|invite|" + net_type + "|" + to_string(ret);
ws_server.send(it->second, response_msg, websocketpp::frame::opcode::text);
} catch(const std::exception& e) {
// 存在一种可能:连接已经断开,但是sip那边请求失败,这种时候应该是会抛出异常。所以这里用try-cache,屏蔽此情况造成的问题
LOG_WARN("response failed because::{}", e.what());
}
}
|
c887a0f0
Hu Chunming
提交初成版代码
|
109
110
111
112
113
114
115
116
117
|
// Define a callback to handle incoming messages
void WebSocketServer::on_message(websocketpp::connection_hdl hdl, message_ptr msg) {
if (msg->get_payload() == "stop-listening") {
ws_server.stop_listening();
return;
}
try {
|
4efb6240
Hu Chunming
call release 自动重新...
|
118
|
msg_parser(hdl,msg->get_payload());
|
c887a0f0
Hu Chunming
提交初成版代码
|
119
|
} catch (websocketpp::exception const & e) {
|
4efb6240
Hu Chunming
call release 自动重新...
|
120
|
LOG_ERROR("Echo failed because::{}", e.what());
|
c887a0f0
Hu Chunming
提交初成版代码
|
121
122
123
124
125
126
|
ws_server.send(hdl, to_string(-101), websocketpp::frame::opcode::text);
}
}
void WebSocketServer::on_open(websocketpp::connection_hdl hdl)
{
|
4efb6240
Hu Chunming
call release 自动重新...
|
127
|
LOG_INFO("连接sip服务器成功!");
|
c887a0f0
Hu Chunming
提交初成版代码
|
128
129
130
|
}
void WebSocketServer::on_fail(websocketpp::connection_hdl h){
|
4efb6240
Hu Chunming
call release 自动重新...
|
131
|
LOG_WARN("连接sip服务器失败!");
|
c887a0f0
Hu Chunming
提交初成版代码
|
132
133
134
|
}
void WebSocketServer::on_close(websocketpp::connection_hdl h){
|
4efb6240
Hu Chunming
call release 自动重新...
|
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
LOG_INFO("on_close:{}", h.lock().get());
std::lock_guard<std::mutex> l_hdl(m_channelport_hdl_map_mtx);
for (auto it = m_channelport_hdl_map.begin(); it != m_channelport_hdl_map.end(); )
{
server::connection_ptr con = ws_server.get_con_from_hdl(it->second);
server::connection_ptr con_std = ws_server.get_con_from_hdl(h);
if (con == con_std) {
it = m_channelport_hdl_map.erase(it);
} else {
it++;
}
}
|
c887a0f0
Hu Chunming
提交初成版代码
|
149
150
151
152
153
154
155
156
157
158
159
160
161
|
}
void WebSocketServer::start() {
ConfigParser* pConfig = ConfigParser::getInstance();
if (!pConfig->init())
{
LOG_ERROR("get config failed!");
return;
}
ServerInfo info = pConfig->getServerCfg();
|
4efb6240
Hu Chunming
call release 自动重新...
|
162
|
sip_server.Init(&info, this);
|
c887a0f0
Hu Chunming
提交初成版代码
|
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
try {
// Set logging settings
ws_server.set_access_channels(websocketpp::log::alevel::all);
ws_server.clear_access_channels(websocketpp::log::alevel::frame_payload);
// Initialize Asio
ws_server.init_asio();
// Register our message handler
ws_server.set_message_handler(bind(&WebSocketServer::on_message,this,::_1,::_2));
ws_server.set_open_handler(bind(&WebSocketServer::on_open, this, _1));
ws_server.set_fail_handler(bind(&WebSocketServer::on_fail, this, _1));
ws_server.set_close_handler(bind(&WebSocketServer::on_close, this, _1));
// Listen on port 9002
int port = info.getWsPort();
|
4efb6240
Hu Chunming
call release 自动重新...
|
180
|
LOG_INFO("websocket server listen:{}", port);
|
c887a0f0
Hu Chunming
提交初成版代码
|
181
182
183
184
185
186
187
188
|
ws_server.listen(port);
// Start the server accept loop
ws_server.start_accept();
// Start the ASIO io_service run loop
ws_server.run();
} catch (websocketpp::exception const & e) {
|
4efb6240
Hu Chunming
call release 自动重新...
|
189
|
LOG_ERROR("websocket start failed:{}", e.what());
|
c887a0f0
Hu Chunming
提交初成版代码
|
190
|
} catch (...) {
|
4efb6240
Hu Chunming
call release 自动重新...
|
191
|
LOG_ERROR("websocket start failed: other exception");
|
c887a0f0
Hu Chunming
提交初成版代码
|
192
193
|
}
}
|