-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
68 lines (51 loc) · 1.41 KB
/
main.cpp
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
#include <iostream>
#include <vector>
#include "lindainterface.h"
using namespace std;
const size_t CLIENT_COUNT = 4;
int in_pipes[CLIENT_COUNT];
int out_pipes[CLIENT_COUNT];
extern int client_main(LindaInterface);
int main(int argc, char *argv[])
{
for(size_t i=0; i<CLIENT_COUNT; i++)
{
int in_pipefd[2];
int out_pipefd[2];
if(pipe(in_pipefd) == -1)
break; // error
if(pipe(out_pipefd) == -1)
break; // error
in_pipes[i] = in_pipefd[0];
out_pipes[i] = out_pipefd[1];
pid_t c_pid = fork();
if(c_pid < 0)
break; // error
if(c_pid == 0)
{
// Client init
close(in_pipefd[0]);
close(out_pipefd[1]);
return client_main(LindaInterface(out_pipefd[0], in_pipefd[1]));
}
// Here is server code
close(in_pipefd[1]);
close(out_pipefd[0]);
}
// Server code
fd_set req_descriptors;
FD_ZERO(&req_descriptors);
for(size_t i=0; i<CLIENT_COUNT; i++)
FD_SET(in_pipes[i], &req_descriptors);
while(1)
{
if(select(CLIENT_COUNT, &req_descriptors, nullptr, nullptr, nullptr) == -1)
break; // error
for(size_t i=0; i<CLIENT_COUNT; i++)
if(FD_ISSET(in_pipes[i], &req_descriptors))
{
// receiving request
}
}
return 0;
}