From bbd05bcb8dad934834a3cd51b1af673ade52679b Mon Sep 17 00:00:00 2001 From: dijunkun Date: Tue, 10 Sep 2024 22:32:59 +0800 Subject: [PATCH] [fix] fix crash due to accessing to invalid virtual function --- src/pc/peer_connection.cpp | 3 +- src/pc/peer_connection.h | 4 +- src/rtc/x_inner.cpp | 2 +- src/transmission/ice_transmission.cpp | 3 +- src/transmission/ice_transmission.h | 6 +-- src/ws/{ws_core.cpp => ws_client.cpp} | 78 +++++++++++++++------------ src/ws/{ws_core.h => ws_client.h} | 25 +++++---- src/ws/ws_transmission.cpp | 24 --------- src/ws/ws_transmission.h | 22 -------- 9 files changed, 67 insertions(+), 100 deletions(-) rename src/ws/{ws_core.cpp => ws_client.cpp} (59%) rename src/ws/{ws_core.h => ws_client.h} (78%) delete mode 100644 src/ws/ws_transmission.cpp delete mode 100644 src/ws/ws_transmission.h diff --git a/src/pc/peer_connection.cpp b/src/pc/peer_connection.cpp index f0f4960..499a334 100644 --- a/src/pc/peer_connection.cpp +++ b/src/pc/peer_connection.cpp @@ -233,8 +233,7 @@ int PeerConnection::Init(PeerConnectionParams params, } }; - ws_transport_ = - std::make_shared(on_receive_ws_msg_, on_ws_status_); + ws_transport_ = std::make_shared(on_receive_ws_msg_, on_ws_status_); uri_ = "ws://" + cfg_signal_server_ip_ + ":" + cfg_signal_server_port_; if (ws_transport_) { ws_transport_->Connect(uri_); diff --git a/src/pc/peer_connection.h b/src/pc/peer_connection.h index b1f8901..f1d368f 100644 --- a/src/pc/peer_connection.h +++ b/src/pc/peer_connection.h @@ -10,7 +10,7 @@ #include "ice_transmission.h" #include "video_decoder_factory.h" #include "video_encoder_factory.h" -#include "ws_transmission.h" +#include "ws_client.h" #include "x.h" typedef void (*OnReceiveBuffer)(const char *, size_t, const char *, @@ -140,7 +140,7 @@ class PeerConnection { bool try_rejoin_with_turn_ = true; private: - std::shared_ptr ws_transport_ = nullptr; + std::shared_ptr ws_transport_ = nullptr; std::function on_receive_ws_msg_ = nullptr; std::function on_ws_status_ = nullptr; unsigned int ws_connection_id_ = 0; diff --git a/src/rtc/x_inner.cpp b/src/rtc/x_inner.cpp index 4dd936d..7bb384b 100644 --- a/src/rtc/x_inner.cpp +++ b/src/rtc/x_inner.cpp @@ -5,7 +5,7 @@ #include "ice_agent.h" #include "log.h" -#include "ws_transmission.h" +#include "ws_client.h" #include "x.h" using nlohmann::json; diff --git a/src/transmission/ice_transmission.cpp b/src/transmission/ice_transmission.cpp index 36789b7..282c4d1 100644 --- a/src/transmission/ice_transmission.cpp +++ b/src/transmission/ice_transmission.cpp @@ -14,8 +14,7 @@ using nlohmann::json; IceTransmission::IceTransmission( bool enable_turn, bool trickle_ice, bool offer_peer, std::string &transmission_id, std::string &user_id, - std::string &remote_user_id, - std::shared_ptr ice_ws_transmission, + std::string &remote_user_id, std::shared_ptr ice_ws_transmission, std::function on_ice_status_change) : enable_turn_(enable_turn), trickle_ice_(trickle_ice), diff --git a/src/transmission/ice_transmission.h b/src/transmission/ice_transmission.h index 71de389..150514e 100644 --- a/src/transmission/ice_transmission.h +++ b/src/transmission/ice_transmission.h @@ -21,7 +21,7 @@ #include "rtp_packet.h" #include "rtp_video_receiver.h" #include "rtp_video_sender.h" -#include "ws_transmission.h" +#include "ws_client.h" class IceTransmission { public: @@ -38,7 +38,7 @@ class IceTransmission { IceTransmission(bool enable_turn, bool trickle_ice, bool offer_peer, std::string &transmission_id, std::string &user_id, std::string &remote_user_id, - std::shared_ptr ice_ws_transmission, + std::shared_ptr ice_ws_transmission, std::function on_ice_status_change); ~IceTransmission(); @@ -124,7 +124,7 @@ class IceTransmission { private: std::unique_ptr ice_agent_ = nullptr; - std::shared_ptr ice_ws_transport_ = nullptr; + std::shared_ptr ice_ws_transport_ = nullptr; CongestionControl *congestion_control_ = nullptr; std::function on_receive_video_ = nullptr; diff --git a/src/ws/ws_core.cpp b/src/ws/ws_client.cpp similarity index 59% rename from src/ws/ws_core.cpp rename to src/ws/ws_client.cpp index 1cbf184..e3e5968 100644 --- a/src/ws/ws_core.cpp +++ b/src/ws/ws_client.cpp @@ -1,4 +1,4 @@ -#include "ws_core.h" +#include "ws_client.h" #include #include @@ -6,7 +6,9 @@ #include "log.h" -WsCore::WsCore() { +WsClient::WsClient(std::function on_receive_msg_cb, + std::function on_ws_status_cb) + : on_receive_msg_(on_receive_msg_cb), on_ws_status_(on_ws_status_cb) { m_endpoint_.clear_access_channels(websocketpp::log::alevel::all); m_endpoint_.clear_error_channels(websocketpp::log::elevel::all); @@ -16,7 +18,7 @@ WsCore::WsCore() { m_thread_ = std::thread(&client::run, &m_endpoint_); } -WsCore::~WsCore() { +WsClient::~WsClient() { destructed_ = true; running_ = false; @@ -33,7 +35,7 @@ WsCore::~WsCore() { } } -int WsCore::Connect(std::string const &uri) { +int WsClient::Connect(std::string const &uri) { uri_ = uri; websocketpp::lib::error_code ec; @@ -47,51 +49,53 @@ int WsCore::Connect(std::string const &uri) { return -1; } - con->set_open_handler(websocketpp::lib::bind( - &WsCore::OnOpen, this, &m_endpoint_, websocketpp::lib::placeholders::_1)); - con->set_fail_handler(websocketpp::lib::bind( - &WsCore::OnFail, this, &m_endpoint_, websocketpp::lib::placeholders::_1)); + con->set_open_handler( + websocketpp::lib::bind(&WsClient::OnOpen, this, &m_endpoint_, + websocketpp::lib::placeholders::_1)); + con->set_fail_handler( + websocketpp::lib::bind(&WsClient::OnFail, this, &m_endpoint_, + websocketpp::lib::placeholders::_1)); con->set_close_handler( - websocketpp::lib::bind(&WsCore::OnClose, this, &m_endpoint_, + websocketpp::lib::bind(&WsClient::OnClose, this, &m_endpoint_, websocketpp::lib::placeholders::_1)); con->set_ping_handler(websocketpp::lib::bind( - &WsCore::OnPing, this, websocketpp::lib::placeholders::_1, + &WsClient::OnPing, this, websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2)); con->set_pong_handler(websocketpp::lib::bind( - &WsCore::OnPong, this, websocketpp::lib::placeholders::_1, + &WsClient::OnPong, this, websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2)); con->set_pong_timeout(1000); con->set_pong_timeout_handler(websocketpp::lib::bind( - &WsCore::OnPongTimeout, this, websocketpp::lib::placeholders::_1, + &WsClient::OnPongTimeout, this, websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2)); con->set_message_handler(websocketpp::lib::bind( - &WsCore::OnMessage, this, websocketpp::lib::placeholders::_1, + &WsClient::OnMessage, this, websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2)); m_endpoint_.connect(con); ws_status_ = WsStatus::WsOpening; - OnWsStatus(WsStatus::WsOpening); + on_ws_status_(WsStatus::WsOpening); return 0; } -void WsCore::Close(websocketpp::close::status::value code, std::string reason) { +void WsClient::Close(websocketpp::close::status::value code, + std::string reason) { websocketpp::lib::error_code ec; - + running_ = false; m_endpoint_.close(connection_handle_, code, reason, ec); if (ec) { LOG_ERROR("Initiating close error: {}", ec.message()); } - OnWsStatus(WsStatus::WsClosed); } -void WsCore::Send(std::string message) { +void WsClient::Send(std::string message) { websocketpp::lib::error_code ec; m_endpoint_.send(connection_handle_, message, @@ -102,7 +106,7 @@ void WsCore::Send(std::string message) { } } -void WsCore::Ping(websocketpp::connection_hdl hdl) { +void WsClient::Ping(websocketpp::connection_hdl hdl) { while (running_) { { std::unique_lock lock(mtx_); @@ -110,6 +114,10 @@ void WsCore::Ping(websocketpp::connection_hdl hdl) { [this] { return !running_; }); } + if (!running_) { + break; + } + if (hdl.expired()) { LOG_WARN("Websocket connection expired, reconnecting..."); } else { @@ -126,52 +134,52 @@ void WsCore::Ping(websocketpp::connection_hdl hdl) { } } -WsStatus WsCore::GetStatus() { return ws_status_; } +WsStatus WsClient::GetStatus() { return ws_status_; } -void WsCore::OnOpen(client *c, websocketpp::connection_hdl hdl) { +void WsClient::OnOpen(client *c, websocketpp::connection_hdl hdl) { ws_status_ = WsStatus::WsOpened; - OnWsStatus(WsStatus::WsOpened); + on_ws_status_(WsStatus::WsOpened); if (!heartbeat_started_) { heartbeat_started_ = true; running_ = true; - ping_thread_ = std::thread(&WsCore::Ping, this, hdl); + ping_thread_ = std::thread(&WsClient::Ping, this, hdl); } else { running_ = false; cond_var_.notify_one(); if (ping_thread_.joinable()) { ping_thread_.join(); running_ = true; - ping_thread_ = std::thread(&WsCore::Ping, this, hdl); + ping_thread_ = std::thread(&WsClient::Ping, this, hdl); } } } -void WsCore::OnFail(client *c, websocketpp::connection_hdl hdl) { +void WsClient::OnFail(client *c, websocketpp::connection_hdl hdl) { ws_status_ = WsStatus::WsFailed; - OnWsStatus(WsStatus::WsFailed); - + on_ws_status_(WsStatus::WsFailed); Connect(uri_); } -void WsCore::OnClose(client *c, websocketpp::connection_hdl hdl) { +void WsClient::OnClose(client *c, websocketpp::connection_hdl hdl) { ws_status_ = WsStatus::WsServerClosed; + on_ws_status_(WsStatus::WsServerClosed); + if (running_) { - OnWsStatus(WsStatus::WsServerClosed); // try to reconnect Connect(uri_); } } -bool WsCore::OnPing(websocketpp::connection_hdl hdl, std::string msg) { +bool WsClient::OnPing(websocketpp::connection_hdl hdl, std::string msg) { return true; } -bool WsCore::OnPong(websocketpp::connection_hdl hdl, std::string msg) { +bool WsClient::OnPong(websocketpp::connection_hdl hdl, std::string msg) { return true; } -void WsCore::OnPongTimeout(websocketpp::connection_hdl hdl, std::string msg) { +void WsClient::OnPongTimeout(websocketpp::connection_hdl hdl, std::string msg) { if (timeout_count_ < 2) { timeout_count_++; return; @@ -181,10 +189,10 @@ void WsCore::OnPongTimeout(websocketpp::connection_hdl hdl, std::string msg) { // m_endpoint_.close(hdl, websocketpp::close::status::normal, // "OnPongTimeout"); ws_status_ = WsStatus::WsReconnecting; - OnWsStatus(WsStatus::WsReconnecting); + on_ws_status_(WsStatus::WsReconnecting); m_endpoint_.reset(); } -void WsCore::OnMessage(websocketpp::connection_hdl, client::message_ptr msg) { - OnReceiveMessage(msg->get_payload()); +void WsClient::OnMessage(websocketpp::connection_hdl, client::message_ptr msg) { + on_receive_msg_(msg->get_payload()); } \ No newline at end of file diff --git a/src/ws/ws_core.h b/src/ws/ws_client.h similarity index 78% rename from src/ws/ws_core.h rename to src/ws/ws_client.h index 1eaadd2..bf11d5a 100644 --- a/src/ws/ws_core.h +++ b/src/ws/ws_client.h @@ -1,5 +1,11 @@ -#ifndef _WS_CORE_H_ -#define _WS_CORE_H_ +/* + * @Author: DI JUNKUN + * @Date: 2024-09-10 + * Copyright (c) 2023 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _WS_CLIENT_H_ +#define _WS_CLIENT_H_ #include #include @@ -24,12 +30,14 @@ enum WsStatus { WsServerClosed }; -class WsCore { +class WsClient { public: - WsCore(); + WsClient(std::function on_receive_msg_cb, + std::function on_ws_status_cb); - virtual ~WsCore(); + ~WsClient(); + public: int Connect(std::string const &uri); void Close(websocketpp::close::status::value code = @@ -57,10 +65,6 @@ class WsCore { void OnMessage(websocketpp::connection_hdl hdl, client::message_ptr msg); - virtual void OnReceiveMessage(const std::string &msg) = 0; - - virtual void OnWsStatus(WsStatus ws_status) = 0; - private: client m_endpoint_; websocketpp::connection_hdl connection_handle_; @@ -76,6 +80,9 @@ class WsCore { int timeout_count_ = 0; std::string uri_; bool destructed_ = false; + + std::function on_receive_msg_ = nullptr; + std::function on_ws_status_ = nullptr; }; #endif \ No newline at end of file diff --git a/src/ws/ws_transmission.cpp b/src/ws/ws_transmission.cpp deleted file mode 100644 index cb32c7b..0000000 --- a/src/ws/ws_transmission.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "ws_transmission.h" - -#include "log.h" - -WsTransmission::WsTransmission( - std::function on_receive_msg_cb, - std::function on_ws_status_cb) - : on_receive_msg_(on_receive_msg_cb), on_ws_status_(on_ws_status_cb) {} - -WsTransmission::~WsTransmission() {} - -void WsTransmission::OnReceiveMessage(const std::string &msg) { - // LOG_INFO("Receive msg: {}", msg); - if (on_receive_msg_) { - on_receive_msg_(msg); - } -} - -void WsTransmission::OnWsStatus(WsStatus ws_status) { - // LOG_INFO("Receive msg: {}", msg); - if (on_ws_status_) { - on_ws_status_(ws_status); - } -} \ No newline at end of file diff --git a/src/ws/ws_transmission.h b/src/ws/ws_transmission.h deleted file mode 100644 index 5a9a6f7..0000000 --- a/src/ws/ws_transmission.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef _WS_TRANSMISSION_H_ -#define _WS_TRANSMISSION_H_ - -#include "ws_core.h" - -class WsTransmission : public WsCore { - public: - WsTransmission(std::function on_receive_msg_cb, - std::function on_ws_status_cb); - ~WsTransmission(); - - public: - void OnReceiveMessage(const std::string &msg); - - void OnWsStatus(WsStatus ws_status); - - private: - std::function on_receive_msg_ = nullptr; - std::function on_ws_status_ = nullptr; -}; - -#endif \ No newline at end of file