-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
103 lines (95 loc) · 3.56 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: relkabou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/19 15:09:47 by aarbaoui #+# #+# */
/* Updated: 2023/05/30 18:03:19 by relkabou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
void ft_print_banner(pid_t pid)
{
ft_printf("%s%s ███▄ ▄███▓██▓███▄ █ ██▄▄▄█████▓▄▄▄ ██▓ ██ ▄█▀\n",
BOLD, RED);
ft_printf("%s%s▓██▒▀█▀ ██▓██▒██ ▀█ █▓██▓ ██▒ ▓▒████▄ ▓██▒ ██▄█▒ \n",
BOLD, RED);
ft_printf("%s%s▓██ ▓██▒██▓██ ▀█ ██▒██▒ ▓██░ ▒▒██ ▀█▄ ▒██░ ▓███▄░ \n",
BOLD, RED);
ft_printf("%s%s▒██ ▒██░██▓██▒ ▐▌██░██░ ▓██▓ ░░██▄▄▄▄██▒██░ ▓██ █▄ \n",
BOLD, RED);
ft_printf("%s%s▒██▒ ░██░██▒██░ ▓██░██░ ▒██▒ ░ ▓█ ▓██░██████▒██▒ █▄\n",
BOLD, RED);
ft_printf("%s%s░ ▒░ ░ ░▓ ░ ▒░ ▒ ▒░▓ ▒ ░░ ▒▒ ▓▒█░ ▒░▓ ▒ ▒▒ ▓▒\n",
BOLD, RED);
ft_printf("%s%s░ ░ ░▒ ░ ░░ ░ ▒░▒ ░ ░ ▒ ▒▒ ░ ░ ▒ ░ ░▒ ▒░\n",
BOLD, RED);
ft_printf("%s%s░ ░ ▒ ░ ░ ░ ░ ▒ ░ ░ ░ ▒ ░ ░ ░ ░░ ░ \n",
BOLD, RED);
ft_printf("%s%s ░ ░ ░ ░ ░ ░ ░ ░ ░ \n\
%s", BOLD, RED, RESET);
ft_printf(" \n");
ft_printf("%s%s by: aarbaoui %s| %sPID: %d%s\n",
BOLD, GREEN, YELLOW, GREEN, pid, RESET);
}
void sigerror(pid_t clientpid)
{
ft_printf("%sError:%s Cannot send recognition signal to client PID: %d",
RED, RESET, clientpid);
exit(-1);
}
void sighandlehelp(char *byte, int *bits, pid_t *clientpid)
{
write(1, byte, 1);
if (*byte == '\0')
{
write(1, "\n", 1);
*byte = 0;
if (kill(*clientpid, SIGUSR1) == -1)
sigerror(*clientpid);
return ;
}
*bits = 0;
}
void sighandle(int sig, siginfo_t *info, void *context)
{
static int bits;
static char bytes;
static pid_t clientpid;
static pid_t workingpid;
(void)context;
if (!clientpid)
clientpid = info->si_pid;
workingpid = info->si_pid;
if (clientpid != workingpid)
{
clientpid = workingpid;
bits = 0;
bytes = 0;
}
bytes = bytes | (sig == SIGUSR2);
bits++;
if (bits == 8)
sighandlehelp(&bytes, &bits, &clientpid);
bytes <<= 1;
usleep(100);
kill(clientpid, SIGUSR2);
}
int main(void)
{
pid_t pid;
struct sigaction sa;
pid = getpid();
ft_print_banner(pid);
sa.sa_sigaction = sighandle;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
while (1)
{
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
pause();
}
}