d8fe994c
Hu Chunming
提交解码代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include "WebsocketClient.h"
#include "../common_header.h"
#include <fstream>
using namespace std;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
void WebsocketClient::on_open(websocketpp::connection_hdl hdl)
{
|
4d446aef
Hu Chunming
日志优化
|
15
|
LOG_INFO( "连接sip服务器成功!");
|
d8fe994c
Hu Chunming
提交解码代码
|
16
17
18
19
|
mbClosed = false;
}
void WebsocketClient::on_fail(websocketpp::connection_hdl h){
|
4d446aef
Hu Chunming
日志优化
|
20
|
LOG_INFO( "连接sip服务器失败!");
|
d8fe994c
Hu Chunming
提交解码代码
|
21
22
23
24
|
mbClosed = true;
}
void WebsocketClient::on_close(websocketpp::connection_hdl h){
|
4d446aef
Hu Chunming
日志优化
|
25
|
LOG_INFO( "on_close");
|
d8fe994c
Hu Chunming
提交解码代码
|
26
27
28
29
30
31
32
33
34
35
|
mbClosed = true;
}
void WebsocketClient::on_message(websocketpp::connection_hdl hdl, message_ptr msg)
{
try {
int ret = msg_parser(hdl,msg->get_payload());
// 返回执行结果
// ws_server.send(hdl, to_string(ret), websocketpp::frame::opcode::text);
} catch (websocketpp::exception const & e) {
|
4d446aef
Hu Chunming
日志优化
|
36
|
LOG_ERROR( "Echo failed because:({})", e.what());
|
d8fe994c
Hu Chunming
提交解码代码
|
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
}
}
int WebsocketClient::msg_parser(websocketpp::connection_hdl hdl, string msg) {
vector<string> vec_msg = StringTools::split(msg, "|");
if (vec_msg.size() <= 0) {
return -1;
}
int ret = -100;
if ( StringTools::to_lower(vec_msg[1]) == "invite") {
string rKey = vec_msg[0];
ret = atoi(vec_msg[3].c_str());
if (ret <= 0) {
LOG_ERROR("invite failed:{}", msg);
response_invite_failed(rKey);
}
} else if ( StringTools::to_lower(vec_msg[0]) == "bye") {
if (StringTools::to_lower(vec_msg[1]) == "invite") {
std::cout << "bye invite: " << vec_msg[2] << std::endl;
}
} else {
|
4d446aef
Hu Chunming
日志优化
|
59
|
LOG_ERROR( "on_message called with hdl:{} and message:{}",hdl.lock().get(), msg);
|
d8fe994c
Hu Chunming
提交解码代码
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
}
return ret;
}
bool WebsocketClient::readCfg() {
std::ifstream cfgFile("./gb28181_cfg.xml");
if(cfgFile.is_open()) {
string strConfig;
string str;
while(cfgFile >> str)
{
strConfig += str;
}
CCatalogParser catPaser;
mInfo = catPaser.DecodeServerConfig(strConfig.c_str());
} else {
LOG_ERROR("read sip server config file failed!");
return false;
}
cfgFile.close();
return true;
}
int WebsocketClient::init()
{
if (!readCfg()) {
return -1;
}
uri = "ws://" + mInfo.getWsIp() + ":" + to_string(mInfo.getWsPort());
|
4d446aef
Hu Chunming
日志优化
|
91
|
LOG_INFO("{}", uri);
|
d8fe994c
Hu Chunming
提交解码代码
|
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
m_client.set_access_channels(websocketpp::log::alevel::all);
m_client.clear_access_channels(websocketpp::log::alevel::frame_payload);
m_client.clear_access_channels(websocketpp::log::alevel::frame_header);
m_client.init_asio();
m_client.set_message_handler(websocketpp::lib::bind(&WebsocketClient::on_message, this,::_1, ::_2));
m_client.set_open_handler(websocketpp::lib::bind(&WebsocketClient::on_open, this, _1));
m_client.set_fail_handler(websocketpp::lib::bind(&WebsocketClient::on_fail, this, _1));
m_client.set_close_handler(websocketpp::lib::bind(&WebsocketClient::on_close, this, _1));
m_client.start_perpetual();
thread_=websocketpp::lib::make_shared<websocketpp::lib::thread>(&client::run,&m_client);
if (connect() != 0) {
return -2;
}
if (mbClosed) {
return -2;
}
return 0;
}
int WebsocketClient::connect()
{
websocketpp::lib::error_code ec;
client::connection_ptr con = m_client.get_connection(uri, ec);
if(ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return -1;
}
m_hdl = con->get_handle();
auto ret = m_client.connect(con);
auto status = con->get_state();;
if (status < 2) {
return 0;
}
return -1;
}
int WebsocketClient::reconnect()
{
websocketpp::lib::error_code ec;
client::connection_ptr con = m_client.get_connection(uri, ec);
if(ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return -1;
}
m_hdl = con->get_handle();
auto ret = m_client.connect(con);
auto status = con->get_state();;
if (status == 1) {
return 0;
}
return -1;
}
int WebsocketClient::check_connect(){
if (mbClosed) {
if (reconnect() == 0) {
return 0;
}
} else {
return 0;
}
return -1;
}
void WebsocketClient::close()
{
m_client.close(m_hdl, websocketpp::close::status::normal, "");
}
void WebsocketClient::terminate()
{
m_client.stop_perpetual();
thread_->join();
}
void WebsocketClient::send_text(std::string msg) {
if (!m_hdl.expired()) {
m_client.send(m_hdl, msg, websocketpp::frame::opcode::text);
}
}
int WebsocketClient::GetMinRtpPort(){
return mInfo.getMinRtpPort();
}
int WebsocketClient::GetMaxRtpPort(){
return mInfo.getMaxRtpPort();
}
|
c027963f
Hu Chunming
ffmpeg6.1.1版本的接收rtp
|
194
|
int WebsocketClient::InviteUdp(std::string sip_channel_id, int rtp_port, RTPReceiver2* r) {
|
d8fe994c
Hu Chunming
提交解码代码
|
195
196
197
198
199
200
201
202
203
204
205
206
|
if (check_connect() < 0) {
return -1;
}
string str_invite = "invite|udp|" + sip_channel_id + "|" + to_string(rtp_port);
send_text(str_invite);
cache_receiver(sip_channel_id, rtp_port, r);
return 0;
}
|
c027963f
Hu Chunming
ffmpeg6.1.1版本的接收rtp
|
207
|
int WebsocketClient::InviteTcp(std::string sip_channel_id, int rtp_port, RTPReceiver2* r) {
|
d8fe994c
Hu Chunming
提交解码代码
|
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
if (check_connect() < 0) {
return -1;
}
string str_invite = "invite|tcp|" + sip_channel_id + "|" + to_string(rtp_port);
send_text(str_invite);
cache_receiver(sip_channel_id, rtp_port, r);
return 0;
}
int WebsocketClient::ByeInvite(std::string sip_channel_id, int rtp_port) {
if (check_connect() < 0) {
return -1;
}
string str_invite = "bye|invite|" + sip_channel_id + "|" + to_string(rtp_port);
send_text(str_invite);
return 0;
}
|
c027963f
Hu Chunming
ffmpeg6.1.1版本的接收rtp
|
231
|
void WebsocketClient::cache_receiver(std::string sip_channel_id, int rtp_port, RTPReceiver2* r) {
|
d8fe994c
Hu Chunming
提交解码代码
|
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
std::lock_guard<std::mutex> l(m_receiver_map_mtx);
string rKey = sip_channel_id + "_" + to_string(rtp_port);
m_receiver_map[rKey] = r;
}
int WebsocketClient::DeleteReceiverPair(std::string sip_channel_id, int rtp_port) {
std::lock_guard<std::mutex> l(m_receiver_map_mtx);
string rKey = sip_channel_id + "_" + to_string(rtp_port);
auto it = m_receiver_map.find(rKey);
if (it != m_receiver_map.end()) {
m_receiver_map.erase(it);
}
}
void WebsocketClient::response_invite_failed(std::string rKey) {
std::lock_guard<std::mutex> l(m_receiver_map_mtx);
auto it = m_receiver_map.find(rKey);
if (it != m_receiver_map.end() && it->second) {
it->second->RequestStreamFailed();
}
}
|