Fix issue server was destroyed on every reset

This commit is contained in:
nsubiron 2017-09-13 16:12:30 +02:00
parent 1f0cfdf9aa
commit 6e57c18e5b
4 changed files with 23 additions and 21 deletions

View File

@ -27,21 +27,15 @@ void CarlaGameController::Initialize(UCarlaSettings &InCarlaSettings)
{ {
CarlaSettings = &InCarlaSettings; CarlaSettings = &InCarlaSettings;
bool bServerStartedSuccessfully = false; // Initialize server if missing.
if (Server == nullptr) { if (Server == nullptr) {
Server = MakeUnique<CarlaServer>(CarlaSettings->WorldPort, CarlaSettings->ServerTimeOut); Server = MakeUnique<CarlaServer>(CarlaSettings->WorldPort, CarlaSettings->ServerTimeOut);
if (Errc::Success == Server->Connect()) { if ((Errc::Success != Server->Connect()) ||
if (Errc::Success == Server->ReadNewEpisode(*CarlaSettings, BLOCKING)) { (Errc::Success != Server->ReadNewEpisode(*CarlaSettings, BLOCKING))) {
bServerStartedSuccessfully = true; 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( APlayerStart *CarlaGameController::ChoosePlayerStart(
@ -94,37 +88,42 @@ void CarlaGameController::Tick(float DeltaSeconds)
if (Server == nullptr) { if (Server == nullptr) {
UE_LOG(LogCarlaServer, Warning, TEXT("Client disconnected, server needs restart")); UE_LOG(LogCarlaServer, Warning, TEXT("Client disconnected, server needs restart"));
RestartLevel(); RestartLevel();
return;
} }
// Check if the client requested a new episode. // Check if the client requested a new episode.
if (Server != nullptr) { {
auto ec = Server->ReadNewEpisode(*CarlaSettings, NON_BLOCKING); auto ec = Server->ReadNewEpisode(*CarlaSettings, NON_BLOCKING);
switch (ec) { switch (ec) {
case Errc::Success: case Errc::Success:
RestartLevel(); RestartLevel();
break; return;
case Errc::Error: case Errc::Error:
Server = nullptr; Server = nullptr;
break; return;
default:
break; // fallthrough...
} }
} }
// Send measurements. // Send measurements.
if (Server != nullptr) { {
check(GameState != nullptr); check(GameState != nullptr);
if (Errc::Error == Server->SendMeasurements( if (Errc::Error == Server->SendMeasurements(
*GameState, *GameState,
Player->GetPlayerState(), Player->GetPlayerState(),
CarlaSettings->bSendNonPlayerAgentsInfo)) { CarlaSettings->bSendNonPlayerAgentsInfo)) {
Server = nullptr; Server = nullptr;
return;
} }
} }
// Read control, block if the settings say so. // Read control, block if the settings say so.
if (Server != nullptr) { {
const bool bShouldBlock = CarlaSettings->bSynchronousMode; const bool bShouldBlock = CarlaSettings->bSynchronousMode;
if (Errc::Error == Server->ReadControl(*Player, bShouldBlock)) { if (Errc::Error == Server->ReadControl(*Player, bShouldBlock)) {
Server = nullptr; Server = nullptr;
return;
} }
} }
} }

View File

@ -85,6 +85,9 @@ CarlaServer::CarlaServer(const uint32 InWorldPort, const uint32 InTimeOut) :
CarlaServer::~CarlaServer() CarlaServer::~CarlaServer()
{ {
#ifdef CARLA_SERVER_EXTRA_LOG
UE_LOG(LogCarlaServer, Warning, TEXT("Destroying CarlaServer"));
#endif // CARLA_SERVER_EXTRA_LOG
carla_free_server(Server); carla_free_server(Server);
} }

View File

@ -49,5 +49,5 @@ private:
const uint32 TimeOut; const uint32 TimeOut;
void* Server; void* const Server;
}; };

View File

@ -167,7 +167,7 @@ def test_carla_client():
with open(args.ini_file, 'r') as fd: with open(args.ini_file, 'r') as fd:
client.write_request_new_episode(fd.read()) client.write_request_new_episode(fd.read())
else: else:
logging.info('sending empty ini file') logging.info('sending default ini file')
client.write_request_new_episode(CarlaSettings) client.write_request_new_episode(CarlaSettings)
while True: while True:
@ -194,7 +194,7 @@ def test_carla_client():
autopilot = random.choice([True, False]) autopilot = random.choice([True, False])
for x in xrange(0, 1000): for x in xrange(0, 100):
logging.info('waiting for measurements') logging.info('waiting for measurements')
data = client.read_measurements() data = client.read_measurements()
if not data: if not data:
@ -225,8 +225,8 @@ def test_carla_client():
with open(args.ini_file, 'r') as fd: with open(args.ini_file, 'r') as fd:
client.write_request_new_episode(fd.read()) client.write_request_new_episode(fd.read())
else: else:
logging.info('sending empty ini file') logging.info('sending default ini file')
client.write_request_new_episode('Dummy empty ini file') client.write_request_new_episode(CarlaSettings)
client.disconnect_secondary_clients() client.disconnect_secondary_clients()