瀏覽代碼

feat(gsm): kill process

Bryan Lee 1 年之前
父節點
當前提交
34a7322aad
共有 2 個文件被更改,包括 35 次插入0 次删除
  1. 27 0
      game-server-manager/src/game/kill.rs
  2. 8 0
      game-server-manager/src/game/mod.rs

+ 27 - 0
game-server-manager/src/game/kill.rs

@@ -0,0 +1,27 @@
+use crate::game::GamesData;
+use crate::ServiceKey;
+use actix_web::{error, post, web, HttpResponse};
+
+#[post("/kill/{port}")]
+async fn kill(
+    port: web::Path<u16>,
+    service_key: ServiceKey,
+    games_data: web::Data<GamesData>,
+) -> actix_web::Result<HttpResponse> {
+    service_key.validate()?;
+
+    let mut games = games_data
+        .games
+        .write()
+        .expect("Failed to get write lock on games");
+
+    let Some(game) = games.find_mut_by_port(port.into_inner()) else {
+        return Ok(HttpResponse::NotFound().finish());
+    };
+
+    game.process
+        .terminate()
+        .map_err(error::ErrorInternalServerError)?;
+
+    Ok(HttpResponse::Ok().finish())
+}

+ 8 - 0
game-server-manager/src/game/mod.rs

@@ -52,6 +52,14 @@ impl Games {
         game.into()
         game.into()
     }
     }
 
 
+    pub fn find_mut_by_port(&mut self, port: u16) -> Option<&mut Game> {
+        let game: Option<&mut Option<Game>> = self.0.get_mut((port - BASE_PORT) as usize);
+        let Some(game) = game else {
+            return None;
+        };
+        game.into()
+    }
+
     pub fn get_active_count(&self) -> usize {
     pub fn get_active_count(&self) -> usize {
         self.0.iter().filter(|p| p.is_some()).count()
         self.0.iter().filter(|p| p.is_some()).count()
     }
     }