-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
64 lines (58 loc) · 1.96 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ysaito <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/02 00:24:01 by ysaito #+# #+# */
/* Updated: 2021/11/12 20:19:42 by ysaito ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "minitalk_utils.h"
void server_handler(int signo)
{
static char received_str[BUF_SIZE];
static int bit_counter;
static size_t index;
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);
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();
sigemptyset(&act.sa_mask);
act.sa_handler = server_handler;
act.sa_flags = SA_RESTART;
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);
}