websocket-server.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
  3. * Reserved. MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. /* 2022-2023 by zhaomingwork */
  6. // websocket server for asr engine
  7. // take some ideas from https://github.com/k2-fsa/sherpa-onnx
  8. // online-websocket-server-impl.cc, thanks. The websocket server has two threads
  9. // pools, one for handle network data and one for asr decoder.
  10. // now only support offline engine.
  11. #ifndef WEBSOCKET_SERVER_H_
  12. #define WEBSOCKET_SERVER_H_
  13. #include <iostream>
  14. #include <map>
  15. #include <memory>
  16. #include <string>
  17. #include <thread>
  18. #include <utility>
  19. #define ASIO_STANDALONE 1 // not boost
  20. #include <glog/logging.h>
  21. #include <fstream>
  22. #include <functional>
  23. #include <websocketpp/common/thread.hpp>
  24. #include <websocketpp/config/asio.hpp>
  25. #include <websocketpp/server.hpp>
  26. #include "asio.hpp"
  27. #include "com-define.h"
  28. #include "funasrruntime.h"
  29. #include "nlohmann/json.hpp"
  30. #include "tclap/CmdLine.h"
  31. typedef websocketpp::server<websocketpp::config::asio> server;
  32. typedef websocketpp::server<websocketpp::config::asio_tls> wss_server;
  33. typedef server::message_ptr message_ptr;
  34. using websocketpp::lib::bind;
  35. using websocketpp::lib::placeholders::_1;
  36. using websocketpp::lib::placeholders::_2;
  37. typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
  38. typedef websocketpp::lib::unique_lock<websocketpp::lib::mutex> unique_lock;
  39. typedef websocketpp::lib::shared_ptr<websocketpp::lib::asio::ssl::context>
  40. context_ptr;
  41. typedef struct {
  42. std::string msg;
  43. float snippet_time;
  44. } FUNASR_RECOG_RESULT;
  45. typedef struct {
  46. nlohmann::json msg;
  47. std::shared_ptr<std::vector<char>> samples;
  48. } FUNASR_MESSAGE;
  49. // See https://wiki.mozilla.org/Security/Server_Side_TLS for more details about
  50. // the TLS modes. The code below demonstrates how to implement both the modern
  51. enum tls_mode { MOZILLA_INTERMEDIATE = 1, MOZILLA_MODERN = 2 };
  52. class WebSocketServer {
  53. public:
  54. WebSocketServer(asio::io_context& io_decoder, bool is_ssl, server* server,
  55. wss_server* wss_server, std::string& s_certfile,
  56. std::string& s_keyfile)
  57. : io_decoder_(io_decoder),
  58. is_ssl(is_ssl),
  59. server_(server),
  60. wss_server_(wss_server) {
  61. if (is_ssl) {
  62. std::cout << "certfile path is " << s_certfile << std::endl;
  63. wss_server->set_tls_init_handler(
  64. bind<context_ptr>(&WebSocketServer::on_tls_init, this,
  65. MOZILLA_INTERMEDIATE, ::_1, s_certfile, s_keyfile));
  66. wss_server_->set_message_handler(
  67. [this](websocketpp::connection_hdl hdl, message_ptr msg) {
  68. on_message(hdl, msg);
  69. });
  70. // set open handle
  71. wss_server_->set_open_handler(
  72. [this](websocketpp::connection_hdl hdl) { on_open(hdl); });
  73. // set close handle
  74. wss_server_->set_close_handler(
  75. [this](websocketpp::connection_hdl hdl) { on_close(hdl); });
  76. // begin accept
  77. wss_server_->start_accept();
  78. // not print log
  79. wss_server_->clear_access_channels(websocketpp::log::alevel::all);
  80. } else {
  81. // set message handle
  82. server_->set_message_handler(
  83. [this](websocketpp::connection_hdl hdl, message_ptr msg) {
  84. on_message(hdl, msg);
  85. });
  86. // set open handle
  87. server_->set_open_handler(
  88. [this](websocketpp::connection_hdl hdl) { on_open(hdl); });
  89. // set close handle
  90. server_->set_close_handler(
  91. [this](websocketpp::connection_hdl hdl) { on_close(hdl); });
  92. // begin accept
  93. server_->start_accept();
  94. // not print log
  95. server_->clear_access_channels(websocketpp::log::alevel::all);
  96. }
  97. }
  98. void do_decoder(const std::vector<char>& buffer,
  99. websocketpp::connection_hdl& hdl, const nlohmann::json& msg);
  100. void initAsr(std::map<std::string, std::string>& model_path, int thread_num);
  101. void on_message(websocketpp::connection_hdl hdl, message_ptr msg);
  102. void on_open(websocketpp::connection_hdl hdl);
  103. void on_close(websocketpp::connection_hdl hdl);
  104. context_ptr on_tls_init(tls_mode mode, websocketpp::connection_hdl hdl,
  105. std::string& s_certfile, std::string& s_keyfile);
  106. private:
  107. void check_and_clean_connection();
  108. asio::io_context& io_decoder_; // threads for asr decoder
  109. // std::ofstream fout;
  110. FUNASR_HANDLE asr_hanlde; // asr engine handle
  111. bool isonline = false; // online or offline engine, now only support offline
  112. bool is_ssl = true;
  113. server* server_; // websocket server
  114. wss_server* wss_server_; // websocket server
  115. // use map to keep the received samples data from one connection in offline
  116. // engine. if for online engline, a data struct is needed(TODO)
  117. std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,
  118. std::owner_less<websocketpp::connection_hdl>>
  119. data_map;
  120. websocketpp::lib::mutex m_lock; // mutex for sample_map
  121. };
  122. #endif // WEBSOCKET_SERVER_H_