| 332 | } |
| 333 | |
| 334 | void stop() { |
| 335 | bool needStop = false; |
| 336 | { |
| 337 | std::lock_guard<std::mutex> guard(_shutdownLock); |
| 338 | needStop = !_stopped; |
| 339 | } |
| 340 | if (needStop) { |
| 341 | auto connections = snapshotConnections(); |
| 342 | for (const auto& connection : connections) { |
| 343 | if (!connection) continue; |
| 344 | std::lock_guard<std::mutex> guard(connection->lock); |
| 345 | if (connection->webSocket && connection->webSocket->is_open()) { |
| 346 | connection->webSocket->close(httplib::ws::CloseStatus::GoingAway, "shutting down"s); |
| 347 | } |
| 348 | } |
| 349 | _server.stop(); |
| 350 | std::unique_lock<std::mutex> lock(_shutdownLock); |
| 351 | _waitForShutdown.wait(lock, [this]() { |
| 352 | return _stopped; |
| 353 | }); |
| 354 | } |
| 355 | LogHandler -= std::make_pair(this, &WebSocketServer::sendLog); |
| 356 | } |
| 357 | |
| 358 | private: |
| 359 | std::vector<ConnectionPtr> snapshotConnections() const { |