mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Opus codec module test pass
This commit is contained in:
@@ -20,7 +20,7 @@ WsCore::WsCore() {
|
||||
WsCore::~WsCore() {
|
||||
m_endpoint_.stop_perpetual();
|
||||
|
||||
if (GetStatus() != "Open") {
|
||||
if (GetStatus() != WsStatus::WsOpened) {
|
||||
// Only close open connections
|
||||
return;
|
||||
}
|
||||
@@ -42,6 +42,8 @@ WsCore::~WsCore() {
|
||||
}
|
||||
|
||||
int WsCore::Connect(std::string const &uri) {
|
||||
uri_ = uri;
|
||||
|
||||
websocketpp::lib::error_code ec;
|
||||
|
||||
client::connection_ptr con = m_endpoint_.get_connection(uri, ec);
|
||||
@@ -81,6 +83,9 @@ int WsCore::Connect(std::string const &uri) {
|
||||
|
||||
m_endpoint_.connect(con);
|
||||
|
||||
ws_status_ = WsStatus::WsOpening;
|
||||
OnWsStatus(WsStatus::WsOpening);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -118,21 +123,26 @@ void WsCore::Ping(websocketpp::connection_hdl hdl) {
|
||||
}
|
||||
}
|
||||
|
||||
const std::string &WsCore::GetStatus() { return connection_status_; }
|
||||
WsStatus WsCore::GetStatus() { return ws_status_; }
|
||||
|
||||
void WsCore::OnOpen(client *c, websocketpp::connection_hdl hdl) {
|
||||
connection_status_ = "Open";
|
||||
ws_status_ = WsStatus::WsOpened;
|
||||
OnWsStatus(WsStatus::WsOpened);
|
||||
|
||||
ping_thread_ = websocketpp::lib::make_shared<websocketpp::lib::thread>(
|
||||
&WsCore::Ping, this, hdl);
|
||||
}
|
||||
|
||||
void WsCore::OnFail(client *c, websocketpp::connection_hdl hdl) {
|
||||
connection_status_ = "Failed";
|
||||
ws_status_ = WsStatus::WsFailed;
|
||||
OnWsStatus(WsStatus::WsFailed);
|
||||
|
||||
Connect(uri_);
|
||||
}
|
||||
|
||||
void WsCore::OnClose(client *c, websocketpp::connection_hdl hdl) {
|
||||
connection_status_ = "Closed";
|
||||
ws_status_ = WsStatus::WsClosed;
|
||||
OnWsStatus(WsStatus::WsClosed);
|
||||
}
|
||||
|
||||
bool WsCore::OnPing(websocketpp::connection_hdl hdl, std::string msg) {
|
||||
@@ -152,6 +162,8 @@ void WsCore::OnPongTimeout(websocketpp::connection_hdl hdl, std::string msg) {
|
||||
LOG_WARN("Pong timeout, reset connection");
|
||||
// m_endpoint_.close(hdl, websocketpp::close::status::normal,
|
||||
// "OnPongTimeout");
|
||||
ws_status_ = WsStatus::WsReconnecting;
|
||||
OnWsStatus(WsStatus::WsReconnecting);
|
||||
m_endpoint_.reset();
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
|
||||
typedef websocketpp::client<websocketpp::config::asio_client> client;
|
||||
|
||||
enum WsStatus { WsOpening = 0, WsOpened, WsFailed, WsClosed, WsReconnecting };
|
||||
|
||||
class WsCore {
|
||||
public:
|
||||
WsCore();
|
||||
@@ -27,7 +29,7 @@ class WsCore {
|
||||
|
||||
void Ping(websocketpp::connection_hdl hdl);
|
||||
|
||||
const std::string &GetStatus();
|
||||
WsStatus GetStatus();
|
||||
|
||||
// Callback
|
||||
void OnOpen(client *c, websocketpp::connection_hdl hdl);
|
||||
@@ -46,14 +48,17 @@ class WsCore {
|
||||
|
||||
virtual void OnReceiveMessage(const std::string &msg) = 0;
|
||||
|
||||
virtual void OnWsStatus(WsStatus ws_status) = 0;
|
||||
|
||||
private:
|
||||
client m_endpoint_;
|
||||
websocketpp::connection_hdl connection_handle_;
|
||||
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread_;
|
||||
websocketpp::lib::shared_ptr<websocketpp::lib::thread> ping_thread_;
|
||||
|
||||
std::string connection_status_ = "Connecting";
|
||||
WsStatus ws_status_ = WsStatus::WsClosed;
|
||||
int timeout_count_ = 0;
|
||||
std::string uri_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -3,8 +3,9 @@
|
||||
#include "log.h"
|
||||
|
||||
WsTransmission::WsTransmission(
|
||||
std::function<void(const std::string &)> on_receive_msg_cb)
|
||||
: on_receive_msg_(on_receive_msg_cb) {}
|
||||
std::function<void(const std::string &)> on_receive_msg_cb,
|
||||
std::function<void(WsStatus)> on_ws_status_cb)
|
||||
: on_receive_msg_(on_receive_msg_cb), on_ws_status_(on_ws_status_cb) {}
|
||||
|
||||
WsTransmission::~WsTransmission() {}
|
||||
|
||||
@@ -13,4 +14,11 @@ void WsTransmission::OnReceiveMessage(const std::string &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);
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,18 @@
|
||||
|
||||
class WsTransmission : public WsCore {
|
||||
public:
|
||||
WsTransmission(std::function<void(const std::string &)> on_receive_msg_cb);
|
||||
WsTransmission(std::function<void(const std::string &)> on_receive_msg_cb,
|
||||
std::function<void(WsStatus)> on_ws_status_cb);
|
||||
~WsTransmission();
|
||||
|
||||
public:
|
||||
void OnReceiveMessage(const std::string &msg);
|
||||
|
||||
void OnWsStatus(WsStatus ws_status);
|
||||
|
||||
private:
|
||||
std::function<void(const std::string &)> on_receive_msg_ = nullptr;
|
||||
std::function<void(WsStatus)> on_ws_status_ = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user