SHARE
TWEET

architecte

a guest Feb 27th, 2020 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ** EPITECH PROJECT, 2020
  3. ** navy
  4. ** File description:
  5. ** sigin
  6. */
  7.  
  8. #include "../../include/navy.h"
  9. #include "../../include/util.h"
  10.  
  11. int read_int(navy_t *navy)
  12. {
  13.     int move;
  14.  
  15.     while (navy->sig[6] != '\0')
  16.         usleep(100);
  17.     move = bin_to_int(navy->sig);
  18.     navy->sig[6] = '0';
  19.     navy->sig[0] = '\0';
  20.     return (move);
  21. }
  22.  
  23. void sigusr_handler(int sig, siginfo_t *info, void *context)
  24. {
  25.     int i = 0;
  26.  
  27.     if (instance->state > 0 && instance->epid == info->si_pid) {
  28.         for (; instance->sig[i]; ++i);
  29.         if ('\0' != instance->sig[6]) {
  30.             instance->sig[i] = '0' + (sig == SIGUSR2);
  31.             instance->sig[i + 1] = '\0';
  32.         }
  33.     }
  34.     if (0 == instance->state)
  35.         if (instance->epid != NULL) {
  36.             instance->state = 2 + my_putstr("successfully connected\n\n");
  37.         } else {
  38.             instance->epid = info->si_pid;
  39.             instance->state = 1 + my_putstr("enemy connected\n\n");
  40.             send_int(instance, 1);
  41.         }
  42. }
  43.  
  44. void init_signals(navy_t *navy)
  45. {
  46.     struct sigaction act = {0};
  47.  
  48.     instance = navy;
  49.     navy->sig = malloc(sizeof(char) * 7);
  50.     navy->sig[0] = '\0';
  51.     navy->sig[6] = '1';
  52.     act.sa_sigaction = &sigusr_handler;
  53.     act.sa_flags = SA_SIGINFO;
  54.     sigaction(SIGUSR1, &act, NULL);
  55.     sigaction(SIGUSR2, &act, NULL);
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top