diff --git a/LibCarla/source/carla/multigpu/listener.cpp b/LibCarla/source/carla/multigpu/listener.cpp index 751b06835..65ccf2f80 100644 --- a/LibCarla/source/carla/multigpu/listener.cpp +++ b/LibCarla/source/carla/multigpu/listener.cpp @@ -24,10 +24,15 @@ namespace multigpu { } Listener::~Listener() { + Stop(); } void Listener::Stop() { _acceptor.cancel(); + _acceptor.close(); + // _acceptor.get_io_service().stop(); + _io_context.stop(); + _io_context.reset(); } void Listener::OpenSession( @@ -40,14 +45,20 @@ namespace multigpu { auto session = std::make_shared(_io_context, timeout, *this); auto self = shared_from_this(); - _acceptor.async_accept(session->_socket, [=](error_code ec) { - if (!ec) { - session->Open(on_opened, on_closed, on_response); - } else { - log_error("tcp accept error:", ec.message()); - } + + auto handle_query = [on_opened, on_closed, on_response, session](const error_code &ec) { + if (!ec) { + session->Open(std::move(on_opened), std::move(on_closed), , std::move(on_response)); + } else { + self->_acceptor.get_io_service().stop(); + log_error("tcp accept secondary error:", ec.message()); + } + }; - self->OpenSession(timeout, on_opened, on_closed, on_response); + _acceptor.async_accept(session->_socket, [=](error_code ec) { + // Handle query and open a new session immediately. + boost::asio::post(_io_context, [=]() { handle_query(ec); }); + OpenSession(timeout, on_opened, on_closed, on_response); }); } diff --git a/LibCarla/source/carla/multigpu/router.cpp b/LibCarla/source/carla/multigpu/router.cpp index 6ea700903..fca7a8c0e 100644 --- a/LibCarla/source/carla/multigpu/router.cpp +++ b/LibCarla/source/carla/multigpu/router.cpp @@ -16,8 +16,13 @@ Router::Router(void) : _next(0) { } Router::~Router() { + Stop(); +} + +void Router::Stop() { ClearSessions(); _listener->Stop(); + _listener.reset(); _pool.Stop(); } diff --git a/LibCarla/source/carla/multigpu/router.h b/LibCarla/source/carla/multigpu/router.h index d840d8713..62a60a4f7 100644 --- a/LibCarla/source/carla/multigpu/router.h +++ b/LibCarla/source/carla/multigpu/router.h @@ -38,9 +38,10 @@ namespace multigpu { Router(void); explicit Router(uint16_t port); ~Router(); - + void Write(MultiGPUCommand id, Buffer &&buffer); std::future WriteToNext(MultiGPUCommand id, Buffer &&buffer); + void Stop(); void SetCallbacks(); void AsyncRun(size_t worker_threads); diff --git a/LibCarla/source/carla/streaming/detail/tcp/Server.cpp b/LibCarla/source/carla/streaming/detail/tcp/Server.cpp index c1363d4da..a1c0c6fbd 100644 --- a/LibCarla/source/carla/streaming/detail/tcp/Server.cpp +++ b/LibCarla/source/carla/streaming/detail/tcp/Server.cpp @@ -35,7 +35,7 @@ namespace tcp { if (!ec) { session->Open(std::move(on_opened), std::move(on_closed)); } else { - log_error("tcp accept error:", ec.message()); + log_error("tcp accept stream error:", ec.message()); } }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/DataStream.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/DataStream.h index 2053ac249..5002f3e05 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/DataStream.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/DataStream.h @@ -52,6 +52,7 @@ public: bool AreClientsListening() { + check(Stream.has_value()); return Stream ? Stream->AreClientsListening() : false; } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp index 4c10a4385..5d2d96e7a 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/CarlaServer.cpp @@ -2310,7 +2310,9 @@ void FCarlaServer::FPimpl::BindActions() FCarlaServer::FCarlaServer() : Pimpl(nullptr) {} -FCarlaServer::~FCarlaServer() {} +FCarlaServer::~FCarlaServer() { + Stop(); +} FDataMultiStream FCarlaServer::Start(uint16_t RPCPort, uint16_t StreamingPort, uint16_t SecondaryPort) { @@ -2395,8 +2397,11 @@ bool FCarlaServer::TickCueReceived() void FCarlaServer::Stop() { - check(Pimpl != nullptr); - Pimpl->Server.Stop(); + if (Pimpl) + { + Pimpl->Server.Stop(); + Pimpl->SecondaryServer->Stop(); + } } FDataStream FCarlaServer::OpenStream() const