-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (56 loc) · 1.56 KB
/
main.cpp
File metadata and controls
61 lines (56 loc) · 1.56 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
#include <iostream>
#include "server.h"
#include "helpers.h"
void handleChildMode(char** argv);
void handleParentMode(int argc, char** argv);
int main(int argc, char** argv)
{
std::cerr << "Process started" << std::endl;
if (std::string(argv[0]) == "child")
handleChildMode(argv);
else
handleParentMode(argc, argv);
std::cerr << "Process terminated" << std::endl;
return 0;
}
void handleChildMode(char** argv)
{
std::cerr << "This is child process " << argv[1] << std::endl;
helpers::SharedMemoryDescriptor desc = helpers::openSharedMemory(sizeof(ChildProcessData), argv[1]);
ChildProcessData* data = (ChildProcessData*)desc.memory;
Server server("noexec", true);
Socket sock(data->socket);
if (std::string(argv[2]) == "tcp")
server.TcpConnectionHandler(std::make_shared<TCPClient>(sock));
else
server.UdpConnectionHandler(std::move(sock));
helpers::removeSharedMemory(desc);
}
void handleParentMode(int argc, char **argv)
{
long port = 2115;
std::string ip = "127.0.0.1";
Server server(argv[0]);
std::string str;
if (argc > 2)
{
ip = argv[1];
port = std::strtol(argv[2], nullptr, 10);
}
if (!server.Listen(port, ip))
{
std::cerr << "Cannot bind socket" << std::endl;
return;
}
while (1)
{
std::cin >> str;
if (str == "exit")
{
server.Shutdown();
break;
}
if (str == "mp" || str == "enable multiprocessing")
server.EnableMultiprocessMode();
}
}