Arthur Brandao 6 лет назад
Родитель
Сommit
3708f3caca
2 измененных файлов с 43 добавлено и 3 удалено
  1. 34 3
      Serveur/handler.c
  2. 9 0
      Serveur/handler.h

+ 34 - 3
Serveur/handler.c

@@ -7,10 +7,11 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <pthread.h>
 #include "error.h"
 #include "bomberstudent_server.h"
 #include "client.h"
-#include "game.h"
 #include "player.h"
 #include "handler.h"
 
@@ -19,6 +20,38 @@ extern Game game[MAXGAME];
 extern int nbGame;
 
 /* --- Fonctions privées --- */
+
+void* timer_thread(void* data){
+    timer_data* t;
+    //Recup données
+    t = (timer_data*) data;
+    //Detache le thread
+    if (pthread_detach(pthread_self()) != 0) {
+        adderror("Impossible de détacher le thread Timer");
+        return NULL;
+    }
+    //Attend le temps indiqué
+    sleep(t->second);
+    //Appel callback
+    if(t->callback(&game[t->game], t->player) != SUCCESS){
+        adderror("Erreur callback Timer");
+    }
+    return NULL;
+}
+
+void timer(int second, int game, int player, int(*callback)(Game*, int)){
+    pthread_t thread;
+    timer_data* t = malloc(sizeof(timer_t));
+    t->second = second;
+    t->game = game;
+    t->player = player;
+    t->callback = callback;
+    //Creation thread
+    if (pthread_create(&thread, NULL, timer_thread, t) != 0) {
+        adderror("Impossible de créer le thread Timer");
+    }
+}
+
 /**
  * Cherche dans quel partie est un client
  * @param int L'id du client
@@ -67,8 +100,6 @@ int search_game(char* name){
     return index;
 }
 
-
-
 /* --- Fonctions publiques --- */
 void ini_handler(){
     //Get

+ 9 - 0
Serveur/handler.h

@@ -11,6 +11,15 @@
 /* --- Include --- */
 #include "constante.h"
 #include "json.h"
+#include "game.h"
+
+/* --- Structure --- */
+typedef struct{
+    int second; //Seconde à attendre
+    int game; //Index de la game dans le tableau
+    int player; //Index du joueur dans le tableau de game
+    int(*callback)(Game*, int);
+}timer_data;
 
 /* --- Fonctions --- */
 /**