-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
81 lines (72 loc) · 2.14 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bnidia <bnidia@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 08:42:42 by bnidia #+# #+# */
/* Updated: 2022/03/29 19:43:16 by bnidia ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
static void handler(int signum, siginfo_t *sig, void *context);
static volatile sig_atomic_t g_response[2] = {};
int main(void)
{
struct sigaction act;
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = &handler;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGUSR1);
sigaddset(&act.sa_mask, SIGUSR2);
if (-1 == sigaction(SIGUSR1, &act, NULL)
|| -1 == sigaction(SIGUSR2, &act, NULL))
{
ft_printf("Error setting the signal disposition\n");
return (0);
}
ft_printf("Server started successfully and now waiting for client\n");
ft_printf("PID is: %i\n", getpid());
while (42)
{
while (g_response[0] != 1)
;
g_response[0] = 0;
kill(g_response[1], SIGUSR1);
}
}
static void string_assembly(unsigned char c)
{
static unsigned char str[4096];
static int str_i;
str[str_i++] = c;
if (str_i == 4096)
{
write(1, &str, str_i);
str_i = 0;
}
if (c == 0)
{
write(1, &str, str_i);
write(1, "\n", 1);
str_i = 0;
}
}
static void handler(int signum, siginfo_t *sig, void *context)
{
static unsigned char letter;
static int i;
(void)context;
if ((SIGUSR1 ^ signum) == 0)
letter |= (1 << i);
i++;
if ((8 ^ i) == 0)
{
string_assembly(letter);
letter = 0;
i = 0;
}
g_response[0] = 1;
g_response[1] = sig->si_pid;
}