parent
2da666757d
commit
a33642226c
145
Main/gserver.cpp
145
Main/gserver.cpp
|
@ -11,13 +11,13 @@
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
#define GSERVER_PORT_FILE "bin/.gserver_port"
|
//#define GSERVER_PORT_FILE "bin/.gserver_port"
|
||||||
#define GSERVER_PORT_SWAP "bin/.gserver_port.swap"
|
//#define GSERVER_PORT_SWAP "bin/.gserver_port.swap"
|
||||||
#define GSERVER_LOG "logs/gserver.log"
|
//#define GSERVER_LOG "logs/gserver.log"
|
||||||
|
|
||||||
bool isOnlyProcess(const char* argv0);
|
bool isOnlyProcess(const char* argv0);
|
||||||
void checkSwap();
|
void checkSwap();
|
||||||
bool startServer();
|
bool startServer(bool _debug);
|
||||||
bool stopServer();
|
bool stopServer();
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
|
@ -39,7 +39,7 @@ int main(int argc, char* argv[])
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == "-h" || mode == "--help") {
|
else if (mode == "-h" || mode == "--help") {
|
||||||
cout << endl;
|
cout << endl;
|
||||||
cout << "gStore Server (gServer)" << endl;
|
cout << "gStore Server (gServer)" << endl;
|
||||||
cout << endl;
|
cout << endl;
|
||||||
|
@ -52,6 +52,7 @@ int main(int argc, char* argv[])
|
||||||
cout << "\t-r,--restart\t\tRestart gServer." << endl;
|
cout << "\t-r,--restart\t\tRestart gServer." << endl;
|
||||||
cout << "\t-p,--port [PORT=" << Socket::DEFAULT_CONNECT_PORT << "]\tChange connection port configuration, takes effect after restart if gServer running." << endl;
|
cout << "\t-p,--port [PORT=" << Socket::DEFAULT_CONNECT_PORT << "]\tChange connection port configuration, takes effect after restart if gServer running." << endl;
|
||||||
cout << "\t-P,--printport\t\tDisplay current connection port configuration." << endl;
|
cout << "\t-P,--printport\t\tDisplay current connection port configuration." << endl;
|
||||||
|
cout << "\t-d,--debug\t\tStart gServer in debug mode (keep gServer in the foreground)." << endl;
|
||||||
cout << "\t-k,--kill\t\tKill existing gServer process(es), ONLY use when out of normal procedures." << endl;
|
cout << "\t-k,--kill\t\tKill existing gServer process(es), ONLY use when out of normal procedures." << endl;
|
||||||
cout << endl;
|
cout << endl;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -70,7 +71,8 @@ int main(int argc, char* argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isOnlyProcess(argv[0])) {
|
if (!isOnlyProcess(argv[0])) {
|
||||||
ofstream out(GSERVER_PORT_SWAP, ios::out);
|
//ofstream out(GSERVER_PORT_SWAP, ios::out);
|
||||||
|
ofstream out(Util::gserver_port_swap.c_str());
|
||||||
if (!out) {
|
if (!out) {
|
||||||
cout << "Failed to change port!" << endl;
|
cout << "Failed to change port!" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -80,7 +82,8 @@ int main(int argc, char* argv[])
|
||||||
cout << "Port will be changed to " << port << " after the current server stops or restarts." << endl;
|
cout << "Port will be changed to " << port << " after the current server stops or restarts." << endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
ofstream out(GSERVER_PORT_FILE, ios::out);
|
//ofstream out(GSERVER_PORT_FILE, ios::out);
|
||||||
|
ofstream out(Util::gserver_port_file.c_str());
|
||||||
if (!out) {
|
if (!out) {
|
||||||
cout << "Failed to change port!" << endl;
|
cout << "Failed to change port!" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -91,12 +94,17 @@ int main(int argc, char* argv[])
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == "-s" || mode == "--start") {
|
else if (mode == "-s" || mode == "--start") {
|
||||||
if (!isOnlyProcess(argv[0])) {
|
if (!isOnlyProcess(argv[0])) {
|
||||||
cout << "gServer already running!" << endl;
|
cout << "gServer already running!" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (startServer()) {
|
if (startServer(false)) {
|
||||||
|
sleep(1);
|
||||||
|
if (isOnlyProcess(argv[0])) {
|
||||||
|
cerr << "Server stopped unexpectedly. Check for port conflicts!" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -104,7 +112,7 @@ int main(int argc, char* argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == "-t" || mode == "--stop") {
|
else if (mode == "-t" || mode == "--stop") {
|
||||||
if (isOnlyProcess(argv[0])) {
|
if (isOnlyProcess(argv[0])) {
|
||||||
cout << "gServer not running!" << endl;
|
cout << "gServer not running!" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -117,7 +125,7 @@ int main(int argc, char* argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == "-r" || mode == "--restart") {
|
else if (mode == "-r" || mode == "--restart") {
|
||||||
if (isOnlyProcess(argv[0])) {
|
if (isOnlyProcess(argv[0])) {
|
||||||
cout << "gServer not running!" << endl;
|
cout << "gServer not running!" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -125,22 +133,32 @@ int main(int argc, char* argv[])
|
||||||
if (!stopServer()) {
|
if (!stopServer()) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (!startServer()) {
|
if (startServer(false)) {
|
||||||
|
sleep(1);
|
||||||
|
if (isOnlyProcess(argv[0])) {
|
||||||
|
cerr << "Server stopped unexpectedly. Check for port conflicts!" << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == "-P" || mode == "--printport") {
|
else if (mode == "-P" || mode == "--printport") {
|
||||||
unsigned short port = Socket::DEFAULT_CONNECT_PORT;
|
unsigned short port = Socket::DEFAULT_CONNECT_PORT;
|
||||||
ifstream in(GSERVER_PORT_FILE);
|
//ifstream in(GSERVER_PORT_FILE);
|
||||||
|
ifstream in(Util::gserver_port_file.c_str());
|
||||||
if (in) {
|
if (in) {
|
||||||
in >> port;
|
in >> port;
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
cout << "Current connection port is " << port << '.' << endl;
|
cout << "Current connection port is " << port << '.' << endl;
|
||||||
unsigned short portSwap = 0;
|
unsigned short portSwap = 0;
|
||||||
ifstream inSwap(GSERVER_PORT_SWAP);
|
//ifstream inSwap(GSERVER_PORT_SWAP);
|
||||||
|
ifstream inSwap(Util::gserver_port_swap.c_str());
|
||||||
if (inSwap) {
|
if (inSwap) {
|
||||||
inSwap >> portSwap;
|
inSwap >> portSwap;
|
||||||
inSwap.close();
|
inSwap.close();
|
||||||
|
@ -151,7 +169,7 @@ int main(int argc, char* argv[])
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == "-k" || mode == "--kill") {
|
else if (mode == "-k" || mode == "--kill") {
|
||||||
if (isOnlyProcess(argv[0])) {
|
if (isOnlyProcess(argv[0])) {
|
||||||
cout << "No process to kill!" << endl;
|
cout << "No process to kill!" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -160,8 +178,10 @@ int main(int argc, char* argv[])
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "Invalid arguments! Input \"bin/gserver -h\" for help." << endl;
|
else {
|
||||||
return -1;
|
cerr << "Invalid arguments! Type \"bin/gserver -h\" for help." << endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isOnlyProcess(const char* argv0) {
|
bool isOnlyProcess(const char* argv0) {
|
||||||
|
@ -169,10 +189,12 @@ bool isOnlyProcess(const char* argv0) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkSwap() {
|
void checkSwap() {
|
||||||
if (access(GSERVER_PORT_SWAP, 00) != 0) {
|
//if (access(GSERVER_PORT_SWAP, 00) != 0) {
|
||||||
|
if (access(Util::gserver_port_swap.c_str(), 00) != 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ifstream in(GSERVER_PORT_SWAP, ios::in);
|
//ifstream in(GSERVER_PORT_SWAP, ios::in);
|
||||||
|
ifstream in(Util::gserver_port_swap.c_str());
|
||||||
if (!in) {
|
if (!in) {
|
||||||
cout << "Failed in checkSwap(), port may not be changed." << endl;
|
cout << "Failed in checkSwap(), port may not be changed." << endl;
|
||||||
return;
|
return;
|
||||||
|
@ -180,27 +202,33 @@ void checkSwap() {
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
in >> port;
|
in >> port;
|
||||||
in.close();
|
in.close();
|
||||||
ofstream out(GSERVER_PORT_FILE, ios::out);
|
//ofstream out(GSERVER_PORT_FILE, ios::out);
|
||||||
|
ofstream out(Util::gserver_port_file.c_str());
|
||||||
if (!out) {
|
if (!out) {
|
||||||
cout << "Failed in checkSwap(), port may not be changed." << endl;
|
cout << "Failed in checkSwap(), port may not be changed." << endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
out << port;
|
out << port;
|
||||||
out.close();
|
out.close();
|
||||||
chmod(GSERVER_PORT_FILE, 0644);
|
//chmod(GSERVER_PORT_FILE, 0644);
|
||||||
string cmd = string("rm ") + GSERVER_PORT_SWAP;
|
chmod(Util::gserver_port_file.c_str(), 0644);
|
||||||
|
//string cmd = string("rm ") + GSERVER_PORT_SWAP;
|
||||||
|
string cmd = string("rm ") + Util::gserver_port_swap;
|
||||||
system(cmd.c_str());
|
system(cmd.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool startServer() {
|
bool startServer(bool _debug) {
|
||||||
unsigned short port = Socket::DEFAULT_CONNECT_PORT;
|
unsigned short port = Socket::DEFAULT_CONNECT_PORT;
|
||||||
ifstream in(GSERVER_PORT_FILE, ios::in);
|
//ifstream in(GSERVER_PORT_FILE, ios::in);
|
||||||
|
ifstream in(Util::gserver_port_file.c_str());
|
||||||
if (!in) {
|
if (!in) {
|
||||||
ofstream out(GSERVER_PORT_FILE, ios::out);
|
//ofstream out(GSERVER_PORT_FILE, ios::out);
|
||||||
|
ofstream out(Util::gserver_port_file.c_str());
|
||||||
if (out) {
|
if (out) {
|
||||||
out << port;
|
out << port;
|
||||||
out.close();
|
out.close();
|
||||||
chmod(GSERVER_PORT_FILE, 0644);
|
//chmod(GSERVER_PORT_FILE, 0644);
|
||||||
|
chmod(Util::gserver_port_file.c_str(), 0644);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -208,6 +236,17 @@ bool startServer() {
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_debug) {
|
||||||
|
Server server(port);
|
||||||
|
if (!server.createConnection()) {
|
||||||
|
cerr << Util::getTimeString() << "Failed to create connection at port " << port << '.' << endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
cout << Util::getTimeString() << "Server started at port " << port << '.' << endl;
|
||||||
|
server.listen();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
pid_t fpid = fork();
|
pid_t fpid = fork();
|
||||||
|
|
||||||
// child
|
// child
|
||||||
|
@ -215,23 +254,51 @@ bool startServer() {
|
||||||
if (!Util::dir_exist("logs")) {
|
if (!Util::dir_exist("logs")) {
|
||||||
Util::create_dir("logs");
|
Util::create_dir("logs");
|
||||||
}
|
}
|
||||||
freopen(GSERVER_LOG, "a", stdout);
|
freopen(Util::gserver_log.c_str(), "a", stdout);
|
||||||
freopen(GSERVER_LOG, "a", stderr);
|
freopen(Util::gserver_log.c_str(), "a", stderr);
|
||||||
Server server(port);
|
|
||||||
if (!server.createConnection()) {
|
int status;
|
||||||
cout << Util::getTimeString() << "Failed to create connection at port " << port << '.' << endl;
|
|
||||||
return false;
|
while (true) {
|
||||||
|
fpid = fork();
|
||||||
|
|
||||||
|
// child, main process
|
||||||
|
if (fpid == 0) {
|
||||||
|
Server server(port);
|
||||||
|
if (!server.createConnection()) {
|
||||||
|
cerr << Util::getTimeString() << "Failed to create connection at port " << port << '.' << endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
cout << Util::getTimeString() << "Server started at port " << port << '.' << endl;
|
||||||
|
server.listen();
|
||||||
|
exit(0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// parent, deamon process
|
||||||
|
else if (fpid > 0) {
|
||||||
|
waitpid(fpid, &status, 0);
|
||||||
|
if (WIFEXITED(status)) {
|
||||||
|
exit(0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
cerr << Util::getTimeString() << "Server stopped abnormally, restarting server..." << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// fork failure
|
||||||
|
else {
|
||||||
|
cerr << Util::getTimeString() << "Failed to start server: deamon fork failure." << endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cout << Util::getTimeString() << "Server started at port " << port << '.' << endl;
|
|
||||||
server.listen();
|
|
||||||
exit(0);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// parent
|
// parent
|
||||||
else if (fpid > 0) {
|
else if (fpid > 0) {
|
||||||
cout << "Server started at port " << port << '.' << endl;
|
cout << "Server started at port " << port << '.' << endl;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// fork failure
|
// fork failure
|
||||||
else {
|
else {
|
||||||
cout << "Failed to start server at port " << port << '.' << endl;
|
cout << "Failed to start server at port " << port << '.' << endl;
|
||||||
|
@ -241,7 +308,8 @@ bool startServer() {
|
||||||
|
|
||||||
bool stopServer() {
|
bool stopServer() {
|
||||||
unsigned short port = Socket::DEFAULT_CONNECT_PORT;
|
unsigned short port = Socket::DEFAULT_CONNECT_PORT;
|
||||||
ifstream in(GSERVER_PORT_FILE, ios::in);
|
//ifstream in(GSERVER_PORT_FILE, ios::in);
|
||||||
|
ifstream in(Util::gserver_port_file.c_str());
|
||||||
if (in) {
|
if (in) {
|
||||||
in >> port;
|
in >> port;
|
||||||
in.close();
|
in.close();
|
||||||
|
@ -262,3 +330,4 @@ bool stopServer() {
|
||||||
checkSwap();
|
checkSwap();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue