-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_bonus.c
65 lines (59 loc) · 2.07 KB
/
server_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ysaito <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/05 13:10:47 by ysaito #+# #+# */
/* Updated: 2021/11/12 20:20:02 by ysaito ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "minitalk_utils_bonus.h"
static void server_handler(int signo, siginfo_t *info, void *context)
{
static char received_str[BUF_SIZE];
static int bit_counter;
static size_t index;
ucontext_t *ucontext;
ucontext = (ucontext_t *)context;
received_str[index] <<= 1;
if (signo == SIGUSR2)
received_str[index] |= 0x01;
if (bit_counter++ != CHAR_BIT_INDEX)
return ;
if (received_str[index] == '\0')
{
ft_putendl_fd(received_str, STDOUT_FILENO);
kill(info->si_pid, SIGUSR1);
index = -1;
}
else if (index == (BUF_SIZE - 2))
{
received_str[++index] = '\0';
ft_putstr_fd(received_str, STDOUT_FILENO);
index = -1;
}
bit_counter = 0;
++index;
}
static void put_ownpid(void)
{
ft_putstr_fd("server PID: ", STDOUT_FILENO);
ft_putnbr_fd(getpid(), STDOUT_FILENO);
ft_putchar_fd('\n', STDOUT_FILENO);
}
int main(void)
{
struct sigaction act;
put_ownpid();
init_sigaction(&act, (SA_RESTART | SA_SIGINFO), &server_handler);
if (sigaction(SIGUSR1, &act, NULL) == -1)
put_error_and_exit("Error sigaction function(SIGUSR1).");
if (sigaction(SIGUSR2, &act, NULL) == -1)
put_error_and_exit("Error sigaction function(SIGUSR2).");
while (1)
{}
return (EXIT_SUCCESS);
}