-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
49 lines (41 loc) · 1.24 KB
/
main.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
#include "global.h"
#include "network.h"
#include "utils.h"
void messageLoop() {
while (1) {
fd_select = fd_read;
// select function that monitor all sockets
int error = select(maxfd + 1, &fd_select, NULL, NULL, &timeout);
if(error < 0) {
perror("fail to select");
return;
}
if(FD_ISSET(socket_fd, &fd_select)) {
acceptConnection(socket_fd, "commandCon", -1);
}
// check all readable connections of command
for (int i = 0; i < MAX_CON; ++i) {
commandResponse(i);
}
for (int i = 0; i < MAX_CON; ++i) {
if (FD_ISSET(clients[i].listenCon, &fd_select)) {
acceptConnection(clients[i].listenCon, "fileTransferCon", i);
disconnectListen(i);
}
}
// check all readable connections of file transfer
for (int i = 0; i < MAX_CON; ++i) {
fileTransferResponse(i);
}
// check all writable connections of data
for (int i = 0; i < MAX_CON; ++i) {
dataResponse(i);
}
}
}
int main(int argc, const char *argv[]) {
if (!initServer(argc, argv))
return 1;
messageLoop();
return 0;
}