@@ -60,6 +60,21 @@ WSServer::~WSServer()
60
60
stop ();
61
61
}
62
62
63
+ void WSServer::serverRunner ()
64
+ {
65
+ blog (LOG_INFO, " IO thread started." );
66
+ try {
67
+ _server.run ();
68
+ } catch (websocketpp::exception const & e) {
69
+ blog (LOG_ERROR, " websocketpp instance returned an error: %s" , e.what ());
70
+ } catch (const std::exception & e) {
71
+ blog (LOG_ERROR, " websocketpp instance returned an error: %s" , e.what ());
72
+ } catch (...) {
73
+ blog (LOG_ERROR, " websocketpp instance returned an error" );
74
+ }
75
+ blog (LOG_INFO, " IO thread exited." );
76
+ }
77
+
63
78
void WSServer::start (quint16 port, bool lockToIPv4)
64
79
{
65
80
if (_server.is_listening () && (port == _serverPort && _lockToIPv4 == lockToIPv4)) {
@@ -102,11 +117,7 @@ void WSServer::start(quint16 port, bool lockToIPv4)
102
117
103
118
_server.start_accept ();
104
119
105
- QtConcurrent::run ([=]() {
106
- blog (LOG_INFO, " io thread started" );
107
- _server.run ();
108
- blog (LOG_INFO, " io thread exited" );
109
- });
120
+ _serverThread = std::thread (&WSServer::serverRunner, this );
110
121
111
122
blog (LOG_INFO, " server started successfully on port %d" , _serverPort);
112
123
}
@@ -119,7 +130,18 @@ void WSServer::stop()
119
130
120
131
_server.stop_listening ();
121
132
for (connection_hdl hdl : _connections) {
122
- _server.close (hdl, websocketpp::close ::status::going_away, " Server stopping" );
133
+ websocketpp::lib::error_code errorCode;
134
+ _server.pause_reading (hdl, errorCode);
135
+ if (errorCode) {
136
+ blog (LOG_ERROR, " Error: %s" , errorCode.message ().c_str ());
137
+ continue ;
138
+ }
139
+
140
+ _server.close (hdl, websocketpp::close ::status::going_away, " Server stopping" , errorCode);
141
+ if (errorCode) {
142
+ blog (LOG_ERROR, " Error: %s" , errorCode.message ().c_str ());
143
+ continue ;
144
+ }
123
145
}
124
146
125
147
_threadPool.waitForDone ();
@@ -128,6 +150,8 @@ void WSServer::stop()
128
150
std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
129
151
}
130
152
153
+ _serverThread.join ();
154
+
131
155
blog (LOG_INFO, " server stopped successfully" );
132
156
}
133
157
@@ -160,6 +184,11 @@ void WSServer::broadcast(const RpcEvent& event)
160
184
}
161
185
}
162
186
187
+ bool WSServer::isListening ()
188
+ {
189
+ return _server.is_listening ();
190
+ }
191
+
163
192
void WSServer::onOpen (connection_hdl hdl)
164
193
{
165
194
QMutexLocker locker (&_clMutex);
@@ -217,11 +246,12 @@ void WSServer::onClose(connection_hdl hdl)
217
246
218
247
auto conn = _server.get_con_from_hdl (hdl);
219
248
auto localCloseCode = conn->get_local_close_code ();
249
+ auto localCloseReason = conn->get_local_close_reason ();
250
+ QString clientIp = getRemoteEndpoint (hdl);
220
251
221
- if (localCloseCode != websocketpp:: close ::status::going_away) {
222
- QString clientIp = getRemoteEndpoint (hdl);
252
+ blog (LOG_INFO, " Websocket connection with client '%s' closed (disconnected). Code is %d, reason is: '%s' " , clientIp. toUtf8 (). constData (), localCloseCode, localCloseReason. c_str ());
253
+ if (localCloseCode != websocketpp:: close ::status::going_away && _server. is_listening ()) {
223
254
notifyDisconnection (clientIp);
224
- blog (LOG_INFO, " client %s disconnected" , clientIp.toUtf8 ().constData ());
225
255
}
226
256
}
227
257
0 commit comments