-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
113 lines (107 loc) · 2.82 KB
/
Copy pathserver.c
File metadata and controls
113 lines (107 loc) · 2.82 KB
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
104
105
106
107
108
109
110
111
112
113
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mzdrodow <mzdrodow@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/28 20:28:11 by mzdrodow #+# #+# */
/* Updated: 2025/11/30 22:04:55 by mzdrodow ### ########.fr */
/* */
/* ************************************************************************** */
#include "mini_talk.h"
t_buffer g_buffer;
int file_fd;
int downloaded_bytes = 0;
int filesize_todownload;
int filename_flag = 0;
int filename_index = 0;
int filesize_flag = 0;
int filesize_index = 0;
char filename[256];
char filesize[256];
char *path;
void signal_handler(int sig, siginfo_t *info, void *context)
{
int client_pid;
(void)context;
client_pid = info->si_pid;
g_buffer.size++;
if (sig == SIGUSR1)
{
g_buffer.buffer = (g_buffer.buffer << 1) | 1;
}
else if (sig == SIGUSR2)
{
g_buffer.buffer = (g_buffer.buffer << 1) | 0;
}
if (g_buffer.size == 8)
{
if (filename_flag == 0)
{
if (g_buffer.buffer != '\0')
{
filename[filename_index++] = g_buffer.buffer;
}
else
{
filename_flag = 1;
filename[filename_index] = '\0';
path = ft_strjoin("./downloads/", filename);
file_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
free(path);
}
}
else if (filesize_flag == 0)
{
if (g_buffer.buffer != '\0')
{
filesize[filesize_index++] = g_buffer.buffer;
}
else
{
filesize_flag = 1;
filesize[filesize_index] = '\0';
filesize_todownload = ft_atoi(filesize);
ft_printf("downloading %s (%s bytes)\n", filename, filesize);
}
}
else
{
write(file_fd, &g_buffer.buffer, 1);
downloaded_bytes++;
}
if (downloaded_bytes == filesize_todownload && filesize_todownload != 0)
{
filename_flag = 0;
filesize_flag = 0;
downloaded_bytes = 0;
filesize_todownload = 0;
filename_index = 0;
filesize_index = 0;
ft_printf("download finished\n");
}
g_buffer.buffer = 0;
g_buffer.size = 0;
}
kill(client_pid, SIGUSR1);
}
int main(void)
{
int pid;
struct sigaction sa;
g_buffer.buffer = 0;
g_buffer.size = 0;
pid = getpid();
ft_printf("pid: %d\n", pid);
sa.sa_sigaction = signal_handler;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
while (1)
{
pause();
}
return (0);
}