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,22 +27,16 @@ void CarlaGameController::Initialize(UCarlaSettings &InCarlaSettings)
{
CarlaSettings = &InCarlaSettings;
bool bServerStartedSuccessfully = false;
// Initialize server if missing.
if (Server == nullptr) {
Server = MakeUnique<CarlaServer>(CarlaSettings->WorldPort, CarlaSettings->ServerTimeOut);
if (Errc::Success == Server->Connect()) {
if (Errc::Success == Server->ReadNewEpisode(*CarlaSettings, BLOCKING)) {
bServerStartedSuccessfully = true;
}
}
}
if (!bServerStartedSuccessfully) {
if ((Errc::Success != Server->Connect()) ||
(Errc::Success != Server->ReadNewEpisode(*CarlaSettings, BLOCKING))) {
UE_LOG(LogCarlaServer, Warning, TEXT("Failed to initialize, server needs restart"));
Server = nullptr;
}
}
}
APlayerStart *CarlaGameController::ChoosePlayerStart(
const TArray<APlayerStart *> &AvailableStartSpots)
@ -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;
}
}
}

View File

@ -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);
}

View File

@ -49,5 +49,5 @@ private:
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:
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()