-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
154 lines (132 loc) · 4.09 KB
/
Copy pathserver.c
File metadata and controls
154 lines (132 loc) · 4.09 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
#include "utils.h"
#include <signal.h>
#include "cmd.h"
#define BUF_SIZE 256
poll_fds fds;
clients clients_base;
void int_signal()
{
int i;
char s[] = "### Сервер закрывается\n";
mass_send(fds, s, sizeof(s));
for(i = 0; i < get_fds_size(); i++)
{
if(fds[i].fd != -1)
close(fds[i].fd);
}
cleanup(fds, clients_base);
exit(0);
}
int main(int argc, char * argv[])
{
int main_socket, port, events, temp_socket, i;
ssize_t n_read;
char buf[BUF_SIZE + 1];
char *msg;
init_signals();
signal(SIGINT, int_signal);
signal(SIGSTOP, int_signal);
signal(SIGTERM, int_signal);
if(argc < 2)
{
fprintf(stderr,"Необходимо указать номер порта в параметрах\n");
return 1;
}
port = atoi(argv[1]);
set_pswrd();
main_socket = init_socket(port);
fds = init_fds();
clients_base = init_clients();
ban_init();
if(fds == NULL)
{
fprintf(stderr, "%s (%d): Структура не была создана: %s\n",
__FILE__, __LINE__ - 3, strerror(errno));
exit(1);
}
fds = add_fds(fds, main_socket);
for(;;)
{
events = poll(fds, get_fds_size(), 100);
if(events == -1)
{
fprintf(stderr, "%s (%d): Проблемы с poll: %s\n",
__FILE__, __LINE__ - 3, strerror(errno));
exit(1);
}
if(events == 0)
continue;
printf("Events = %d\n",events);
if(fds[0].revents)
{
temp_socket = accept(main_socket, NULL, NULL);
if(temp_socket == -1)
{
fprintf(stderr, "%s (%d): Не удалось принять: %s\n",
__FILE__, __LINE__ - 3, strerror(errno));
exit(1);
}
printf("Клиент %d подсоединился\n", get_fds_size());
fds = add_fds(fds, temp_socket);
clients_base = add_client(clients_base);
write(temp_socket,"server-MZH\n",strlen("server-MZH\n"));
auth(temp_socket);
fds[0].revents = 0;
}
for(i = 1; i < get_fds_size(); i++)
{
if(fds[i].revents)
{
n_read = read(fds[i].fd, buf, BUF_SIZE + 1);
if(n_read == -1)
{
fprintf(stderr, "%s (%d): Ошибка при чтении из сокета: %s\n",
__FILE__, __LINE__ - 3, strerror(errno));
close(fds[i].fd);
fds[i].fd = -1;
}
if(n_read == 0)
{
printf("клиент %d отсоединился\n", i);
i = disconnect(&fds, &clients_base, i);
}
if(n_read > 0)
{
buf[n_read] = '\0';
strip_beg(buf);
if(strcmp(clients_base[i].name, "\0") == 0)
auth2(fds, clients_base, i, buf, fds[i].fd);
else
{
strip_beg(buf);
msg = add_to_buf(clients_base, i, buf);
if(msg == NULL)
continue;
if(msg[0] != '\0')
{
if(cmds(&fds, &clients_base, i, msg))
{
/* Все сделается в cmd */;
}
else
{
msg_everyone(fds, clients_base, i, msg);
}
}
free(msg);
}
}
}
fds[i].revents = 0;
}
}
return 100;
}