From 6a59f94278b4d05f5b7715b11b6dd0bfffc28cce Mon Sep 17 00:00:00 2001
From: 志浩 <neo.dzh@alibaba-inc.com>
Date: 星期四, 15 六月 2023 17:12:15 +0800
Subject: [PATCH] Merge branch 'main' into dev_dzh
---
funasr/runtime/websocket/websocket-server.cpp | 80 ++++++++++++++++++++++++++++++++-------
1 files changed, 65 insertions(+), 15 deletions(-)
diff --git a/funasr/runtime/websocket/websocketsrv.cpp b/funasr/runtime/websocket/websocket-server.cpp
similarity index 68%
rename from funasr/runtime/websocket/websocketsrv.cpp
rename to funasr/runtime/websocket/websocket-server.cpp
index b81442c..a311c23 100644
--- a/funasr/runtime/websocket/websocketsrv.cpp
+++ b/funasr/runtime/websocket/websocket-server.cpp
@@ -10,11 +10,48 @@
// pools, one for handle network data and one for asr decoder.
// now only support offline engine.
-#include "websocketsrv.h"
+#include "websocket-server.h"
#include <thread>
#include <utility>
#include <vector>
+
+context_ptr WebSocketServer::on_tls_init(tls_mode mode,
+ websocketpp::connection_hdl hdl,
+ std::string& s_certfile,
+ std::string& s_keyfile) {
+ namespace asio = websocketpp::lib::asio;
+
+ LOG(INFO) << "on_tls_init called with hdl: " << hdl.lock().get();
+ LOG(INFO) << "using TLS mode: "
+ << (mode == MOZILLA_MODERN ? "Mozilla Modern"
+ : "Mozilla Intermediate");
+
+ context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(
+ asio::ssl::context::sslv23);
+
+ try {
+ if (mode == MOZILLA_MODERN) {
+ // Modern disables TLSv1
+ ctx->set_options(
+ asio::ssl::context::default_workarounds |
+ asio::ssl::context::no_sslv2 | asio::ssl::context::no_sslv3 |
+ asio::ssl::context::no_tlsv1 | asio::ssl::context::single_dh_use);
+ } else {
+ ctx->set_options(asio::ssl::context::default_workarounds |
+ asio::ssl::context::no_sslv2 |
+ asio::ssl::context::no_sslv3 |
+ asio::ssl::context::single_dh_use);
+ }
+
+ ctx->use_certificate_chain_file(s_certfile);
+ ctx->use_private_key_file(s_keyfile, asio::ssl::context::pem);
+
+ } catch (std::exception& e) {
+ LOG(INFO) << "Exception: " << e.what();
+ }
+ return ctx;
+}
// feed buffer to asr engine for decoder
void WebSocketServer::do_decoder(const std::vector<char>& buffer,
@@ -40,11 +77,15 @@
jsonresult["wav_name"] = msg["wav_name"];
// send the json to client
- server_->send(hdl, jsonresult.dump(), websocketpp::frame::opcode::text,
- ec);
+ if (is_ssl) {
+ wss_server_->send(hdl, jsonresult.dump(),
+ websocketpp::frame::opcode::text, ec);
+ } else {
+ server_->send(hdl, jsonresult.dump(), websocketpp::frame::opcode::text,
+ ec);
+ }
- std::cout << "buffer.size=" << buffer.size()
- << ",result json=" << jsonresult.dump() << std::endl;
+ LOG(INFO) << "buffer.size=" << buffer.size() << ",result json=" << jsonresult.dump();
if (!isonline) {
// close the client if it is not online asr
// server_->close(hdl, websocketpp::close::status::normal, "DONE", ec);
@@ -67,14 +108,14 @@
data_msg->samples = std::make_shared<std::vector<char>>();
data_msg->msg = nlohmann::json::parse("{}");
data_map.emplace(hdl, data_msg);
- std::cout << "on_open, active connections: " << data_map.size() << std::endl;
+ LOG(INFO) << "on_open, active connections: " << data_map.size();
}
void WebSocketServer::on_close(websocketpp::connection_hdl hdl) {
scoped_lock guard(m_lock);
data_map.erase(hdl); // remove data vector when connection is closed
- std::cout << "on_close, active connections: " << data_map.size() << std::endl;
+ LOG(INFO) << "on_close, active connections: " << data_map.size();
}
// remove closed connection
@@ -83,15 +124,24 @@
auto iter = data_map.begin();
while (iter != data_map.end()) { // loop to find closed connection
websocketpp::connection_hdl hdl = iter->first;
- server::connection_ptr con = server_->get_con_from_hdl(hdl);
- if (con->get_state() != 1) { // session::state::open ==1
- to_remove.push_back(hdl);
+
+ if (is_ssl) {
+ wss_server::connection_ptr con = wss_server_->get_con_from_hdl(hdl);
+ if (con->get_state() != 1) { // session::state::open ==1
+ to_remove.push_back(hdl);
+ }
+ } else {
+ server::connection_ptr con = server_->get_con_from_hdl(hdl);
+ if (con->get_state() != 1) { // session::state::open ==1
+ to_remove.push_back(hdl);
+ }
}
+
iter++;
}
for (auto hdl : to_remove) {
data_map.erase(hdl);
- std::cout << "remove one connection " << std::endl;
+ LOG(INFO)<< "remove one connection ";
}
}
void WebSocketServer::on_message(websocketpp::connection_hdl hdl,
@@ -109,7 +159,7 @@
lock.unlock();
if (sample_data_p == nullptr) {
- std::cout << "error when fetch sample data vector" << std::endl;
+ LOG(INFO) << "error when fetch sample data vector";
return;
}
@@ -124,7 +174,7 @@
if (jsonresult["is_speaking"] == false ||
jsonresult["is_finished"] == true) {
- std::cout << "client done" << std::endl;
+ LOG(INFO) << "client done";
if (isonline) {
// do_close(ws);
@@ -173,9 +223,9 @@
// init model with api
asr_hanlde = FunOfflineInit(model_path, thread_num);
- std::cout << "model ready" << std::endl;
+ LOG(INFO) << "model successfully inited";
} catch (const std::exception& e) {
- std::cout << e.what() << std::endl;
+ LOG(INFO) << e.what();
}
}
--
Gitblit v1.9.1