-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (104 loc) · 2.86 KB
/
main.cpp
File metadata and controls
115 lines (104 loc) · 2.86 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
#include <iostream>
#include "common.h"
#ifdef __linux__
#include <sys/prctl.h>
#endif
#include <signal.h>
using namespace std;
std::string LOGNAME = "console";
extern int main_gm(string ip, u_int16_t port, const ZZ &psk, int lambda);
int main_m(string ip, u_int16_t port, string id, const ZZ &psk);
int main(int argc, char *argv[]) {
// Console logger with color
// usage https://github.com/gabime/spdlog
stdout_color_mt(LOGNAME);
INFO("Program started");
int oc; /*选项字符 */
char *ip = nullptr;
char *name = nullptr;
string psk;
string log_level;
bool type = false;
bool separate_gm = false;
while ((oc = getopt(argc, argv, "sgmhi:n:p:l:")) != -1) {
switch (oc) {
case 's':
separate_gm = true;
break;
case 'g': // GM
type = true;
break;
case 'm': // Member
type = false;
break;
case 'i': //IP
ip = optarg;
break;
case 'n': //name
name = optarg;
break;
case 'p': //psk
psk = optarg;
break;
case 'l': //log_level
log_level = optarg;
break;
case 'h':
cout << "usage: {-g|-m} [-i <ip>] [-n <id>] -p <PSK> [-l <log_level>]" << endl;
return 0;
default:
cout << "usage: {-g|-m} [-i <ip>] [-n <id>] -p <PSK> [-l <log_level>]" << endl;
break;
}
}
if (psk.empty()) {
CRITICAL("Wrong usage: no psk");
return -1;
}
ZZ _psk = conv<ZZ>(atoi(psk.c_str()));
set_level(level::debug);
if (log_level == "debug")
set_level(level::debug);
if (log_level == "info")
set_level(level::info);
if (log_level == "warn")
set_level(level::warn);
if (log_level == "err")
set_level(level::err);
if (log_level == "critical")
set_level(level::critical);
if (separate_gm) {
LOGNAME = "GM";
stdout_color_mt(LOGNAME);
main_gm("0.0.0.0", 9999, _psk, 64);
return 0;
}
if (!name) {
CRITICAL("Wrong usage: no id");
return -1;
}
LOGNAME = name;
stdout_color_mt(LOGNAME);
pid_t pid;
if (type) {//GM
pid = fork();
if (pid == 0) {
#ifdef __linux__
prctl(PR_SET_PDEATHSIG, SIGHUP);
#endif
LOGNAME = "GM";
stdout_color_mt(LOGNAME);
main_gm("0.0.0.0", 9999, _psk, 64);
return 0;
}
sleep(1);
main_m("127.0.0.1", 9999, name, _psk);
} else {
if (!ip) {
CRITICAL("Wrong usage: no ip");
return -1;
}
main_m(ip, 9999, name, _psk);
}
return 0;
}