-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_server.c
102 lines (92 loc) · 2.35 KB
/
main_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main_server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amontign <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/31 11:03:52 by amontign #+# #+# */
/* Updated: 2023/07/25 10:49:01 by amontign ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h"
char *g_bin_tab;
void bin_to_dec(char *g_bin_tab, int iter)
{
int res;
int puissance;
int i;
int j;
j = 0;
while (j < iter)
{
res = 0;
puissance = 1;
i = 7 + (j * 8);
while (i >= (j * 8))
{
res += (g_bin_tab[i] - 48) * puissance;
puissance *= 2;
i--;
}
write(1, &res, 1);
j++;
}
}
int check_end(char *g_bin_tab, int signal_count)
{
int i;
i = signal_count - 8;
while (i < signal_count)
{
if (g_bin_tab[i] == 49)
return (0);
i++;
}
bin_to_dec(g_bin_tab, (signal_count / 8));
return (1);
}
void complete_bin_tab(int signal_num, int signal_count)
{
if (signal_num == SIGUSR1)
g_bin_tab[signal_count] = 48;
else
g_bin_tab[signal_count] = 49;
}
void sig_handler(int signal_num, siginfo_t *info, void *ptr)
{
static int signal_count;
static int client;
if (client != 0 && client != (int)info->si_pid)
return (kill((int)info->si_pid, SIGUSR2), (void)ptr);
if ((int)info->si_pid == getpid())
return ;
client = (int)info->si_pid;
g_bin_tab = ft_realloc(g_bin_tab, signal_count);
if (!g_bin_tab)
exit(1);
complete_bin_tab(signal_num, signal_count);
g_bin_tab[++signal_count] = 0;
if (signal_count % 8 == 0)
{
if (check_end(g_bin_tab, signal_count))
{
if (g_bin_tab)
free(g_bin_tab);
g_bin_tab = NULL;
sig_handler_norme(&signal_count, &client);
}
}
usleep(20);
kill(client, SIGUSR1);
}
void sig_int_handler(int sig)
{
(void)sig;
if (g_bin_tab)
{
free(g_bin_tab);
g_bin_tab = NULL;
}
exit(0);
}