11// sherpa-onnx/csrc/online-websocket-server.cc
22//
33// Copyright (c) 2022-2023 Xiaomi Corporation
4+ // Copyright (c) 2025 Uniphore (Author: Manickavela A)
45
5- #include " asio.hpp "
6- #include " sherpa-onnx/csrc/macros.h "
7- # include " sherpa-onnx/csrc/online-websocket-server-impl.h "
8- #include " sherpa-onnx/csrc/parse-options .h"
6+ #include < string >
7+ #include < csignal >
8+
9+ #include " sherpa-onnx/csrc/online-websocket-server .h"
910
1011static constexpr const char *kUsageMessage = R"(
1112Automatic speech recognition with sherpa-onnx using websocket.
@@ -30,92 +31,121 @@ Please refer to
3031for a list of pre-trained models to download.
3132)" ;
3233
33- class OnlineWebsocketServerApp {
34- public:
35- OnlineWebsocketServerApp (int32_t argc, char *argv[]) : argc_(argc), argv_(argv) {}
3634
37- void Run () {
38- sherpa_onnx::ParseOptions po (kUsageMessage );
39- sherpa_onnx::OnlineWebsocketServerConfig config;
35+ // Global server instance pointer for signal handling
36+ OnlineWebsocketServerApp *global_server_instance = nullptr ;
4037
41- // the server will listen on this port
42- int32_t port = 6006 ;
38+ // Signal handler to stop the server
39+ void SignalHandler (int signal) {
40+ if (signal == SIGINT || signal == SIGTERM) {
41+ SHERPA_ONNX_LOGE (" \n Signal %d received. Stopping server..." , signal);
42+ if (global_server_instance) {
43+ global_server_instance->Stop ();
44+ }
45+ }
46+ }
4347
44- // size of the thread pool for handling network connections
45- int32_t num_io_threads = 1 ;
48+ OnlineWebsocketServerApp::OnlineWebsocketServerApp (
49+ int32_t argc, char *argv[]) : argc_(argc), argv_(argv) {}
4650
47- // size of the thread pool for neural network computation and decoding
48- int32_t num_work_threads = 3 ;
51+ void OnlineWebsocketServerApp::Run () {
52+ sherpa_onnx::ParseOptions po (kUsageMessage );
53+ sherpa_onnx::OnlineWebsocketServerConfig config;
4954
50- po. Register ( " num-io-threads " , &num_io_threads,
51- " Thread pool size for network connections. " ) ;
55+ // the server will listen on this port
56+ int32_t port = 6006 ;
5257
53- po.Register (" num-work-threads" , &num_work_threads,
54- " Thread pool size for for neural network "
55- " computation and decoding." );
58+ // size of the thread pool for handling network connections
59+ int32_t num_io_threads = 1 ;
5660
57- po.Register (" port" , &port, " The port on which the server will listen." );
61+ // size of the thread pool for neural network computation and decoding
62+ int32_t num_work_threads = 3 ;
5863
59- config.Register (&po);
64+ po.Register (" num-io-threads" , &num_io_threads,
65+ " Thread pool size for network connections." );
6066
61- if (argc_ == 1 ) {
62- po.PrintUsage ();
63- exit (EXIT_FAILURE);
64- }
67+ po.Register (" num-work-threads" , &num_work_threads,
68+ " Thread pool size for for neural network "
69+ " computation and decoding." );
6570
66- po.Read (argc_, argv_ );
71+ po.Register ( " port " , &port, " The port on which the server will listen. " );
6772
68- if (po.NumArgs () != 0 ) {
69- SHERPA_ONNX_LOGE (" Unrecognized positional arguments!" );
70- po.PrintUsage ();
71- exit (EXIT_FAILURE);
72- }
73+ config.Register (&po);
7374
74- config.Validate ();
75+ if (argc_ == 1 ) {
76+ po.PrintUsage ();
77+ exit (EXIT_FAILURE);
78+ }
7579
76- asio::io_context io_conn; // for network connections
77- asio::io_context io_work; // for neural network and decoding
80+ po.Read (argc_, argv_);
7881
79- sherpa_onnx::OnlineWebsocketServer server (io_conn, io_work, config);
80- server.Run (port);
82+ if (po.NumArgs () != 0 ) {
83+ SHERPA_ONNX_LOGE (" Unrecognized positional arguments!" );
84+ po.PrintUsage ();
85+ exit (EXIT_FAILURE);
86+ }
8187
82- SHERPA_ONNX_LOGE (" Started!" );
83- SHERPA_ONNX_LOGE (" Listening on: %d" , port);
84- SHERPA_ONNX_LOGE (" Number of work threads: %d" , num_work_threads);
88+ config.Validate ();
8589
86- // give some work to do for the io_work pool
87- auto work_guard = asio::make_work_guard (io_work) ;
90+ // Set the global instance for signal handling
91+ global_server_instance = this ;
8892
89- std::vector<std::thread> io_threads;
93+ // Handle SIGINT and SIGTERM
94+ std::signal (SIGINT, SignalHandler);
95+ std::signal (SIGTERM, SignalHandler);
9096
91- // decrement since the main thread is also used for network communications
92- for (int32_t i = 0 ; i < num_io_threads - 1 ; ++i) {
93- io_threads.emplace_back ([&io_conn]() { io_conn.run (); });
94- }
97+ // io_conn for network connections
98+ // io_work for neural network and decoding
9599
96- std::vector<std::thread> work_threads;
97- for (int32_t i = 0 ; i < num_work_threads; ++i) {
98- work_threads.emplace_back ([&io_work]() { io_work.run (); });
99- }
100+ sherpa_onnx::OnlineWebsocketServer server (io_conn_, io_work_, config);
101+ server.Run (port);
100102
101- io_conn.run ();
103+ SHERPA_ONNX_LOGE (" Started!" );
104+ SHERPA_ONNX_LOGE (" Listening on: %d" , port);
105+ SHERPA_ONNX_LOGE (" Number of work threads: %d" , num_work_threads);
102106
103- for (auto &t : io_threads) {
104- t.join ();
105- }
107+ // give some work to do for the io_work pool
108+ auto work_guard = asio::make_work_guard (io_work_);
106109
107- for (auto &t : work_threads) {
108- t.join ();
109- }
110+ std::vector<std::thread> io_threads;
111+
112+ // decrement since the main thread is also used for network communications
113+ for (int32_t i = 0 ; i < num_io_threads - 1 ; ++i) {
114+ io_threads.emplace_back ([this ]() { io_conn_.run (); });
110115 }
111116
112- private:
113- int32_t argc_;
114- char **argv_;
115- };
117+ std::vector<std::thread> work_threads;
118+ for (int32_t i = 0 ; i < num_work_threads; ++i) {
119+ work_threads.emplace_back ([this ]() { io_work_.run (); });
120+ }
121+
122+ // Main thread handles IO
123+ io_conn_.run ();
124+
125+ for (auto &t : io_threads) {
126+ t.join ();
127+ }
128+
129+ for (auto &t : work_threads) {
130+ t.join ();
131+ }
132+ SHERPA_ONNX_LOGE (" Server shut down gracefully." );
133+ }
134+
135+ void OnlineWebsocketServerApp::Stop () {
136+ shutdown_requested_.store (true );
137+ io_conn_.stop ();
138+ io_work_.stop ();
139+ SHERPA_ONNX_LOGE (" Server stopping..." );
140+ }
116141
117142int32_t main (int32_t argc, char *argv[]) {
118143 OnlineWebsocketServerApp app (argc, argv);
119144 app.Run ();
120145 return 0 ;
121146}
147+
148+ void StartServer (int32_t argc, char *argv[]) {
149+ OnlineWebsocketServerApp app (argc, argv);
150+ app.Run ();
151+ }
0 commit comments