Skip to content

Commit 313b93d

Browse files
fix ipc process shutdown on childs errors
1 parent a963c6a commit 313b93d

12 files changed

Lines changed: 245 additions & 226 deletions

File tree

config/core.json

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,64 +53,64 @@
5353
},
5454

5555
"npcs": {
56-
"initialNPCCount": 50,
57-
"maxNPCsPerChunk": 5,
58-
"spawnInterval": 5.0,
59-
"despawnDistance": 150.0
56+
"initial_count": 50,
57+
"max_per_chunk": 5,
58+
"spawn_interval": 5.0,
59+
"despawn_distance": 150.0
6060
},
6161

6262
"mobs": {
6363
"enabled": true,
64-
"spawnZones": [
64+
"spawn_zones": [
6565
{
6666
"name": "goblin_forest",
6767
"center": [100.0, 10.0, 100.0],
6868
"radius": 50.0,
69-
"mobType": 0,
70-
"minLevel": 1,
71-
"maxLevel": 5,
72-
"maxMobs": 15,
73-
"respawnTime": 30.0
69+
"type": 0,
70+
"max": 15,
71+
"min_level": 1,
72+
"max_level": 5,
73+
"respawn_time": 30.0
7474
},
7575
{
7676
"name": "orc_camp",
7777
"center": [500.0, 15.0, 500.0],
7878
"radius": 75.0,
79-
"mobType": 1,
80-
"minLevel": 5,
81-
"maxLevel": 15,
82-
"maxMobs": 10,
83-
"respawnTime": 45.0
79+
"type": 1,
80+
"max": 10,
81+
"min_level": 5,
82+
"max_level": 15,
83+
"respawn_time": 45.0
8484
},
8585
{
8686
"name": "dragon_lair",
8787
"center": [1000.0, 30.0, 1000.0],
8888
"radius": 100.0,
89-
"mobType": 2,
90-
"minLevel": 20,
91-
"maxLevel": 30,
92-
"maxMobs": 3,
93-
"respawnTime": 300.0
89+
"type": 2,
90+
"max": 3,
91+
"min_level": 20,
92+
"max_level": 30,
93+
"respawn_time": 300.0
9494
},
9595
{
9696
"name": "slime_swamp",
9797
"center": [-200.0, 5.0, -200.0],
9898
"radius": 60.0,
99-
"mobType": 3,
100-
"minLevel": 1,
101-
"maxLevel": 10,
102-
"maxMobs": 20,
103-
"respawnTime": 20.0
99+
"type": 3,
100+
"max": 20,
101+
"min_level": 1,
102+
"max_level": 10,
103+
"respawn_time": 20.0
104104
}
105105
],
106-
"experienceMultiplier": 1.0,
107-
"lootDropChance": 1.0
106+
"experience_multiplier": 1.0,
107+
"loot_drop_chance": 1.0
108108
},
109109

110110
"collision": {
111111
"enabled": true,
112-
"gridCellSize": 10.0,
113-
"maxCollisionChecks": 1000
112+
"cell_size": 10.0,
113+
"max_checks": 1000
114114
},
115115

116116
"database": {
@@ -120,10 +120,10 @@
120120
"name": "data/game.db",
121121
"user": "gameuser",
122122
"password": "password",
123-
"connection_pool":{
123+
"pool":{
124124
"enabled": false,
125-
"pool_size": 10,
126-
"pool_threads": 2,
125+
"size": 10,
126+
"threads": 2,
127127
"reconnect_attempts": 3,
128128
"min_connections": 5,
129129
"max_connections": 20,
@@ -144,10 +144,12 @@
144144
"console_output": true
145145
},
146146

147-
"pythonScripting": {
148-
"enabled": true,
149-
"scriptDirectory": "scripts",
150-
"hotReload": true
147+
"scripting": {
148+
"python": {
149+
"enabled": true,
150+
"directory": "scripts",
151+
"hot_reload": true
152+
}
151153
},
152154

153155
"game": {

include/config/ConfigManager.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,21 @@ class ConfigManager {
112112
bool GetBool(const std::string& key, bool defaultValue = false) const;
113113
std::string GetString(const std::string& key, const std::string& defaultValue = "") const;
114114
std::vector<std::string> GetStringArray(const std::string& key) const;
115-
nlohmann::json GetJson(const std::string& key) const;
115+
nlohmann::json GetJson(const std::string& key, const nlohmann::json& default_value = nlohmann::json()) const;
116116
bool HasKey(const std::string& key) const;
117117

118118
private:
119+
mutable std::mutex configMutex_;
120+
nlohmann::json config_;
121+
std::string configPath_;
122+
123+
std::pair<std::string, std::optional<size_t>> ParseSegment(const std::string& seg) const;
124+
std::vector<std::string> SplitPath(const std::string& path) const;
125+
119126
ConfigManager() = default;
120127
ConfigManager(const ConfigManager&) = delete;
121128
ConfigManager& operator=(const ConfigManager&) = delete;
122-
129+
123130
bool HasProcessConfig() const;
124131
bool ValidateConfig(const nlohmann::json& config) const;
125-
126-
mutable std::mutex configMutex_;
127-
nlohmann::json config_;
128-
std::string configPath_;
129132
};

include/process/ProcessPool.hpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class ProcessPool {
5757
bool SendToWorker(int workerId, const std::string& message);
5858
std::string ReceiveFromMaster();
5959

60+
bool IsWorkersReady() const;
61+
void WaitForWorkers();
6062
bool IsWorkerAlive(int workerId) const;
6163
void RestartWorker(int workerId);
6264

@@ -69,24 +71,6 @@ class ProcessPool {
6971
void UnblockSignals(const sigset_t* oldset);
7072

7173
private:
72-
struct WorkerInfo {
73-
pid_t pid;
74-
int groupIdx;
75-
int localWorkerId;
76-
WorkerGroupConfig config;
77-
};
78-
79-
void MasterProcess();
80-
void WorkerProcess(int globalWorkerId, const WorkerGroupConfig& config);
81-
void SetupSignalHandlers();
82-
void CleanupDeadWorkers();
83-
void CloseAllPipes();
84-
void CreateWorkerPipe(int globalWorkerId);
85-
86-
bool WriteAll(int fd, const void* buffer, size_t count);
87-
bool ReadAll(int fd, void* buffer, size_t count, bool nonBlocking = false);
88-
bool DrainPipe(int fd, size_t bytesToDrain);
89-
9074
std::vector<WorkerGroupConfig> groups_;
9175
int totalWorkers_;
9276

@@ -99,6 +83,13 @@ class ProcessPool {
9983
std::atomic<bool> running_{false};
10084
std::atomic<bool> shutdownRequested_{false};
10185

86+
struct WorkerInfo {
87+
pid_t pid;
88+
int groupIdx;
89+
int localWorkerId;
90+
WorkerGroupConfig config;
91+
};
92+
10293
std::vector<WorkerInfo> workers_;
10394
std::vector<int> workerPipes_;
10495

@@ -109,4 +100,17 @@ class ProcessPool {
109100

110101
uint32_t maxMessageSize_{1024 * 1024};
111102
uint32_t receiveTimeoutMs_{1000};
103+
104+
std::atomic<bool> workersReady_{false};
105+
106+
void MasterProcess();
107+
void WorkerProcess(int globalWorkerId, const WorkerGroupConfig& config);
108+
void SetupSignalHandlers();
109+
void CleanupDeadWorkers();
110+
void CloseAllPipes();
111+
void CreateWorkerPipe(int globalWorkerId);
112+
113+
bool WriteAll(int fd, const void* buffer, size_t count);
114+
bool ReadAll(int fd, void* buffer, size_t count, bool nonBlocking = false);
115+
bool DrainPipe(int fd, size_t bytesToDrain);
112116
};

0 commit comments

Comments
 (0)