-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_server.c
101 lines (93 loc) · 3.37 KB
/
ft_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ael-khni <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/09 16:02:02 by ael-khni #+# #+# */
/* Updated: 2021/12/15 19:43:08 by ael-khni ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_minitalk.h"
void display_banner(int pid)
{
ft_printf("\n\t%s███╗ ███╗██╗███╗ ██╗██╗████████╗ █████╗ ██╗ ██╗██╗\
%s\n", YELLOW, END);
ft_printf("\t%s██╔████╔██║██║██╔██╗ ██║██║ ██║ ███████║██║ █████╔╝\
%s \n", YELLOW, END);
ft_printf("\t%s██║╚██╔╝██║██║██║╚██╗██║██║ ██║ ██╔══██║██║ ██╔═██╗\
%s \n", YELLOW, END);
ft_printf("\t%s██║ ╚═╝ ██║██║██║ ╚████║██║ ██║ ██║ ██║███████╗██║ ██╗\
%s\n", YELLOW, END);
ft_printf("\t%s╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝\
%s\n", YELLOW, END);
ft_printf("%s\n\t\tPID: %d%s\t\t\t%sBy: ael-khni%s\n", RED, pid, END,
YELLOW, END);
ft_printf("\t⊱ ────────────────────── {.⋅ ✯ ⋅.} ─────────────────────── ⊰\
\n\n");
}
void signal_error(void)
{
ft_printf("\n%sserver: unexpected error.%s\n", RED, END);
exit(EXIT_FAILURE);
}
void extended_action(char *c, int *received, int *client_pid, int *bit)
{
ft_printf("%c", *c);
if (*c == '\0')
{
ft_printf("\n%s%d signal recieved from client PID: %d%s\n",
GREEN, *received, *client_pid, END);
*received = 0;
*c = 0;
if (kill(*client_pid, SIGUSR1) == -1)
signal_error();
return ;
}
*bit = 0;
}
void action(int sig, siginfo_t *info, void *context)
{
static int client_pid;
static int bit;
static char c;
static int received;
static int current_pid;
(void)context;
if (!client_pid)
client_pid = info->si_pid;
current_pid = info->si_pid;
if (client_pid != current_pid)
{
client_pid = current_pid;
bit = 0;
c = 0;
received = 0;
}
c |= (sig == SIGUSR2);
received++;
bit++;
if (bit == 8)
extended_action(&c, &received, &client_pid, &bit);
c <<= 1;
usleep(100);
kill(client_pid, SIGUSR2);
}
int main(void)
{
int pid;
struct sigaction act;
pid = getpid();
display_banner(pid);
act.sa_sigaction = action;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
while (1)
{
sigaction(SIGUSR1, &act, 0);
sigaction(SIGUSR2, &act, 0);
pause();
}
return (EXIT_FAILURE);
}