diff --git a/Source/Carla/Game/CarlaGameController.cpp b/Source/Carla/Game/CarlaGameController.cpp index 954f9edec..01263149b 100644 --- a/Source/Carla/Game/CarlaGameController.cpp +++ b/Source/Carla/Game/CarlaGameController.cpp @@ -27,21 +27,15 @@ void CarlaGameController::Initialize(UCarlaSettings &InCarlaSettings) { CarlaSettings = &InCarlaSettings; - bool bServerStartedSuccessfully = false; - + // Initialize server if missing. if (Server == nullptr) { Server = MakeUnique(CarlaSettings->WorldPort, CarlaSettings->ServerTimeOut); - if (Errc::Success == Server->Connect()) { - if (Errc::Success == Server->ReadNewEpisode(*CarlaSettings, BLOCKING)) { - bServerStartedSuccessfully = true; - } + if ((Errc::Success != Server->Connect()) || + (Errc::Success != Server->ReadNewEpisode(*CarlaSettings, BLOCKING))) { + UE_LOG(LogCarlaServer, Warning, TEXT("Failed to initialize, server needs restart")); + Server = nullptr; } } - - if (!bServerStartedSuccessfully) { - UE_LOG(LogCarlaServer, Warning, TEXT("Failed to initialize, server needs restart")); - Server = nullptr; - } } APlayerStart *CarlaGameController::ChoosePlayerStart( @@ -94,37 +88,42 @@ void CarlaGameController::Tick(float DeltaSeconds) if (Server == nullptr) { UE_LOG(LogCarlaServer, Warning, TEXT("Client disconnected, server needs restart")); RestartLevel(); + return; } // Check if the client requested a new episode. - if (Server != nullptr) { + { auto ec = Server->ReadNewEpisode(*CarlaSettings, NON_BLOCKING); switch (ec) { case Errc::Success: RestartLevel(); - break; + return; case Errc::Error: Server = nullptr; - break; + return; + default: + break; // fallthrough... } } // Send measurements. - if (Server != nullptr) { + { check(GameState != nullptr); if (Errc::Error == Server->SendMeasurements( *GameState, Player->GetPlayerState(), CarlaSettings->bSendNonPlayerAgentsInfo)) { Server = nullptr; + return; } } // Read control, block if the settings say so. - if (Server != nullptr) { + { const bool bShouldBlock = CarlaSettings->bSynchronousMode; if (Errc::Error == Server->ReadControl(*Player, bShouldBlock)) { Server = nullptr; + return; } } } diff --git a/Source/Carla/Game/CarlaServer.cpp b/Source/Carla/Game/CarlaServer.cpp index 3f51e30e1..6c1adbb24 100644 --- a/Source/Carla/Game/CarlaServer.cpp +++ b/Source/Carla/Game/CarlaServer.cpp @@ -85,6 +85,9 @@ CarlaServer::CarlaServer(const uint32 InWorldPort, const uint32 InTimeOut) : CarlaServer::~CarlaServer() { +#ifdef CARLA_SERVER_EXTRA_LOG + UE_LOG(LogCarlaServer, Warning, TEXT("Destroying CarlaServer")); +#endif // CARLA_SERVER_EXTRA_LOG carla_free_server(Server); } diff --git a/Source/Carla/Game/CarlaServer.h b/Source/Carla/Game/CarlaServer.h index a14960c60..2a7497a86 100644 --- a/Source/Carla/Game/CarlaServer.h +++ b/Source/Carla/Game/CarlaServer.h @@ -49,5 +49,5 @@ private: const uint32 TimeOut; - void* Server; + void* const Server; }; diff --git a/Util/PythonClient/carla_client.py b/Util/PythonClient/carla_client.py index 73d7d56ae..16da29a28 100755 --- a/Util/PythonClient/carla_client.py +++ b/Util/PythonClient/carla_client.py @@ -167,7 +167,7 @@ def test_carla_client(): with open(args.ini_file, 'r') as fd: client.write_request_new_episode(fd.read()) else: - logging.info('sending empty ini file') + logging.info('sending default ini file') client.write_request_new_episode(CarlaSettings) while True: @@ -194,7 +194,7 @@ def test_carla_client(): autopilot = random.choice([True, False]) - for x in xrange(0, 1000): + for x in xrange(0, 100): logging.info('waiting for measurements') data = client.read_measurements() if not data: @@ -225,8 +225,8 @@ def test_carla_client(): with open(args.ini_file, 'r') as fd: client.write_request_new_episode(fd.read()) else: - logging.info('sending empty ini file') - client.write_request_new_episode('Dummy empty ini file') + logging.info('sending default ini file') + client.write_request_new_episode(CarlaSettings) client.disconnect_secondary_clients()