diff --git a/build.sh b/build.sh index 96311fb..46f1587 100644 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ cd src -#g++ -g -std=c++11 -DZMQ_STATIC -o main -Wl,-dn -lzmq -lmsgpackc -lmysqlcppconn-static -Wl,-dy -lssl -lcrypto -lpthread -lrt -ldl -lm -L ../lib -I ../include/zmq -I ../include/msgpack -I ../include/mysql/ -Wl,-rpath ../lib main.cpp - -g++ -g -std=c++11 -o main -lzmq -lmsgpackc -lmysqlclient -lssl -lcrypto -L ../lib -I ../include/zmq -I ../include/msgpack -I ../include/mysql/mysql -Wl,-rpath ../lib main.cpp \ No newline at end of file +g++ -g -std=c++11 -o gpats -lzmq -lmsgpackc -lmysqlclient -lssl -lcrypto -L ../lib -I ../include/zmq -I ../include/msgpack -I ../include/mysql/mysql -Wl,-rpath ../lib main.cpp +cd .. +mv src/gpats bin \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..3345e2a --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +cd bin +nohup ./gpats >> gpats.log 2>&1 & +cd .. diff --git a/src/main.cpp b/src/main.cpp index fd0c166..c73dce9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,42 +10,57 @@ using namespace std; + + template -static void send_data(zmq::socket_t& socket, const string& tag, const T& data) +bool send_data(zmq::socket_t& socket, const string& tag, const T& data) { msgpack::sbuffer packed; msgpack::pack(&packed, data); //tag zmq::message_t tag_msg(tag.size()); std::memcpy(tag_msg.data(), tag.data(), tag.size()); - socket.send(tag_msg, ZMQ_SNDMORE); + bool ret1 = socket.send(tag_msg, ZMQ_SNDMORE); //data zmq::message_t body_msg(packed.size()); std::memcpy(body_msg.data(), packed.data(), packed.size()); - socket.send(body_msg); + bool ret2 = socket.send(body_msg); + + return ret1 && ret2; + } -static void recv_tag(zmq::socket_t& socket, string& tag) +static bool recv_tag(zmq::socket_t& socket, string& tag) { zmq::message_t tag_msg; - socket.recv(&tag_msg); + bool ret = socket.recv(&tag_msg); //tag - string tag_r(static_cast(tag_msg.data()), tag_msg.size()); - tag = tag_r; + if (ret) + { + tag.assign(static_cast(tag_msg.data()), tag_msg.size()); + } + return ret; } + + template -static void recv_data(zmq::socket_t& socket, T& data) +static bool recv_data(zmq::socket_t& socket, T& data) { zmq::message_t body_msg; - socket.recv(&body_msg); + bool ret = socket.recv(&body_msg); //data - msgpack::unpacked unpacked_body = msgpack::unpack(static_cast(body_msg.data()), body_msg.size()); - msgpack::object deserialized = unpacked_body.get(); - deserialized.convert(data); + if (ret) + { + msgpack::unpacked unpacked_body = msgpack::unpack(static_cast(body_msg.data()), body_msg.size()); + msgpack::object deserialized = unpacked_body.get(); + deserialized.convert(data); + } + return ret; } + static string get_code_from_machine(string machine) { #define MACHINE_CODE_BUFFER_SIZE 21 @@ -87,40 +102,11 @@ int init_db( MYSQL &mysql) cout << mysql_error(&mysql) << endl; return 0; } -#if 0 - string sql = "select * from user"; - //mysql_error(&mysql); - - MYSQL_RES *res; - MYSQL_ROW row; - MYSQL_FIELD *fields; - - my_ulonglong num_rows; - unsigned int num_fields; - if(0 == mysql_real_query(&mysql, sql.c_str(), sql.size())) - { - res = mysql_store_result(&mysql); - if(nullptr != res) - { - num_rows = mysql_num_rows(res); - fields = mysql_fetch_field(res); - while((row = mysql_fetch_row(res))) - { - for(unsigned int i = 0; i < num_fields; i++) - { - cout << fields[i].name << "=" << row[i] << endl; - } - } - } - mysql_free_result(res); - } - mysql_close(&mysql); -#endif return 1; } -static int check_pwd_for_login(MYSQL &mysql, const login_req_pk &login_req) +static int check_user_for_login(MYSQL &mysql, const login_req_pk &login_req) { MYSQL_RES *res; @@ -130,7 +116,7 @@ static int check_pwd_for_login(MYSQL &mysql, const login_req_pk &login_req) unsigned int num_fields; string sql = "select pwd, status from user where user='" + login_req.user + "'"; - cout << sql << endl; + //cout << sql << endl; if(0 == mysql_real_query(&mysql, sql.c_str(), sql.size())) { res = mysql_store_result(&mysql); @@ -157,7 +143,7 @@ static int check_pwd_for_login(MYSQL &mysql, const login_req_pk &login_req) } -static int check_date_for_login(MYSQL &mysql, const login_req_pk &login_req, string &date_limit) +static int check_uuid_for_login(MYSQL &mysql, const login_req_pk &login_req,string &expire_date) { MYSQL_RES *res; MYSQL_ROW row; @@ -165,9 +151,11 @@ static int check_date_for_login(MYSQL &mysql, const login_req_pk &login_req, str my_ulonglong num_rows; unsigned int num_fields; - string db_date; - string sql = "select expire_date from uuid where user='" + login_req.user + "' and uuid='" + login_req.uuid + "'"; - cout << sql << endl; + string db_expire_date; + string db_version; + int db_status; + string sql = "select status,version,expire_date from uuid where user='" + login_req.user + "' and uuid='" + login_req.uuid + "'"; + //cout << sql << endl; if(0 == mysql_real_query(&mysql, sql.c_str(), sql.size())) { res = mysql_store_result(&mysql); @@ -177,7 +165,9 @@ static int check_date_for_login(MYSQL &mysql, const login_req_pk &login_req, str row = mysql_fetch_row(res); if(num_rows == 1) { - db_date = row[0]; + db_status = atoi(row[0]); + db_version = row[1]; + db_expire_date = row[2]; } else { @@ -192,15 +182,30 @@ static int check_date_for_login(MYSQL &mysql, const login_req_pk &login_req, str cout << mysql_error(&mysql) << endl; return 0; } + + //check status + if(db_status != 1 ) + { + return 0; + } + //check version + int db_ver_i = atoi(db_version.c_str()); + int pk_ver_i = atoi(login_req.ver.c_str()); + if(pk_ver_i < db_ver_i) + { + return 0; + } + + //check date struct tm tm_time; - strptime(db_date.c_str(), "%Y-%m-%d %H:%M:%S", &tm_time); - time_t expire_date = mktime(&tm_time); + strptime(db_expire_date.c_str(), "%Y-%m-%d %H:%M:%S", &tm_time); + time_t db_expire_date_t = mktime(&tm_time); time_t current_date = time(NULL); - if(current_date > expire_date) + if(current_date > db_expire_date_t) { return 0; } - date_limit = db_date; + expire_date = db_expire_date; return 1; @@ -218,7 +223,7 @@ static int check_count_for_login(MYSQL &mysql, const login_req_pk &login_req) int count = 0; //get count string sql = "select count from uuid where user='" + login_req.user + "' and uuid='" + login_req.uuid + "'"; - cout << sql << endl; + //cout << sql << endl; if(0 == mysql_real_query(&mysql, sql.c_str(), sql.size())) { res = mysql_store_result(&mysql); @@ -246,7 +251,7 @@ static int check_count_for_login(MYSQL &mysql, const login_req_pk &login_req) //compute count int ccount = 0; sql = "select count(*) from mc where user='" + login_req.user + "' and uuid='" + login_req.uuid + "'"; - cout << sql << endl; + //cout << sql << endl; if(0 == mysql_real_query(&mysql, sql.c_str(), sql.size())) { res = mysql_store_result(&mysql); @@ -275,7 +280,7 @@ static int check_count_for_login(MYSQL &mysql, const login_req_pk &login_req) //if exist int cccount = 0; sql = "select count(*) from mc where user='" + login_req.user + "' and uuid='" + login_req.uuid + "' and mc='" + get_code_from_machine(login_req.mc) + "'"; - cout << sql << endl; + //cout << sql << endl; if(0 == mysql_real_query(&mysql, sql.c_str(), sql.size())) { res = mysql_store_result(&mysql); @@ -315,7 +320,7 @@ static int check_count_for_login(MYSQL &mysql, const login_req_pk &login_req) } -static int add_mc_for_login(MYSQL &mysql, const login_req_pk &login_req) +static int add_or_update_mc_for_login(MYSQL &mysql, const login_req_pk &login_req) { MYSQL_RES *res; @@ -327,7 +332,7 @@ static int add_mc_for_login(MYSQL &mysql, const login_req_pk &login_req) //if exist int count = 0; string sql = "select count(*) from mc where user='" + login_req.user + "' and uuid='" + login_req.uuid + "' and mc='" + get_code_from_machine(login_req.mc) + "'"; - cout << sql << endl; + //cout << sql << endl; if(0 == mysql_real_query(&mysql, sql.c_str(), sql.size())) { res = mysql_store_result(&mysql); @@ -355,7 +360,7 @@ static int add_mc_for_login(MYSQL &mysql, const login_req_pk &login_req) //insert into mc (user,uuid,mc,status,pub_ip,pri_ip,ver,login_date) values ('15011457740','db1ac97cf2bb5bab8481b0614346852f','zdfwdertaf',1,'2.2.2.2','192.168.1.1','1.0.0',now()) if(0 == count) { - sql = "insert into mc (user,uuid,mc,status,mn,pub_ip,pri_ip,ver,login_date) values('" + sql = "insert into mc (user,uuid,mc,status,mn,pub_ip,pri_ip,ver,login_date,create_date) values('" + login_req.user + "','" + login_req.uuid + "','" + get_code_from_machine(login_req.mc) @@ -364,8 +369,9 @@ static int add_mc_for_login(MYSQL &mysql, const login_req_pk &login_req) + "'," + "''" + ",'" + login_req.ip + "','" + login_req.ver - + "'," + "now())"; - cout << sql << endl; + + "'," + "now()" + + "," + "now())"; + //cout << sql << endl; if(0 != mysql_real_query(&mysql, sql.c_str(), sql.size())) { cout << mysql_error(&mysql) << endl; @@ -376,8 +382,8 @@ static int add_mc_for_login(MYSQL &mysql, const login_req_pk &login_req) //update mc set login_date=now() where user=15011457740 and uuid='db1ac97cf2bb5bab8481b0614346852f' and mc='zdfwdertaf' else if(1 == count) { - sql = "update mc set login_date=now() where user='" + login_req.user + "' and uuid='" + login_req.uuid + "' and mc='" + get_code_from_machine(login_req.mc) + "'"; - cout << sql << endl; + sql = "update mc set status=1, login_date=now() where user='" + login_req.user + "' and uuid='" + login_req.uuid + "' and mc='" + get_code_from_machine(login_req.mc) + "'"; + //cout << sql << endl; if(0 != mysql_real_query(&mysql, sql.c_str(), sql.size())) { cout << mysql_error(&mysql) << endl; @@ -390,28 +396,83 @@ static int add_mc_for_login(MYSQL &mysql, const login_req_pk &login_req) } +static int update_mc_for_logout(MYSQL &mysql, const logout_req_pk &logout_req) +{ -static void proc_login(MYSQL &mysql, zmq::socket_t& socket, const login_req_pk &login_req) + MYSQL_RES *res; + MYSQL_ROW row; + + my_ulonglong num_rows; + unsigned int num_fields; + + //if exist + int count = 0; + string sql = "select count(*) from mc where user='" + logout_req.user + "' and uuid='" + logout_req.uuid + "' and mc='" + get_code_from_machine(logout_req.mc) + "'"; + //cout << sql << endl; + if(0 == mysql_real_query(&mysql, sql.c_str(), sql.size())) + { + res = mysql_store_result(&mysql); + if(nullptr != res) + { + num_rows = mysql_num_rows(res); + row = mysql_fetch_row(res); + if(num_rows == 1) + { + count = atoi(row[0]); + } + else + { + mysql_free_result(res); + return 0; + } + mysql_free_result(res); + } + } + else + { + cout << mysql_error(&mysql) << endl; + return 0; + } + if(1 == count) + + { + sql = "update mc set status=0, logout_date=now() where user='" + logout_req.user + "' and uuid='" + logout_req.uuid + "' and mc='" + get_code_from_machine(logout_req.mc) + "'"; + //cout << sql << endl; + if(0 != mysql_real_query(&mysql, sql.c_str(), sql.size())) + { + cout << mysql_error(&mysql) << endl; + return 0; + } + + } + return 1; + + +} + +static int proc_login(MYSQL &mysql, zmq::socket_t& socket, const login_req_pk &login_req) { const string tag_rsp = "login"; - string date_limit; + string expire_date; - if(0 == check_pwd_for_login(mysql, login_req)) + if(0 == check_user_for_login(mysql, login_req)) { login_rsp_pk login_rsp; login_rsp.err_code = 0; - login_rsp.err_msg = "pwd error"; + login_rsp.err_msg = "user error"; send_data(socket, tag_rsp, login_rsp); - return; + cout << "login user error" << ":" << login_req.user << "-" << login_req.uuid << "-" << login_req.mc << "-" << login_req.mn << "-" << login_req.ip << "-" << login_req.ver << endl; + return 0; } - if(0 == check_date_for_login(mysql, login_req, date_limit)) + if(0 == check_uuid_for_login(mysql, login_req, expire_date)) { login_rsp_pk login_rsp; login_rsp.err_code = 0; - login_rsp.err_msg = "date expire"; + login_rsp.err_msg = "uuid error"; send_data(socket, tag_rsp, login_rsp); - return; + cout << "login uuid error" << ":" << login_req.user << "-" << login_req.uuid << "-" << login_req.mc << "-" << login_req.mn << "-" << login_req.ip << "-" << login_req.ver << endl; + return 0; } if(0 == check_count_for_login(mysql, login_req)) { @@ -419,27 +480,37 @@ static void proc_login(MYSQL &mysql, zmq::socket_t& socket, const login_req_pk & login_rsp.err_code = 0; login_rsp.err_msg = "count limit"; send_data(socket, tag_rsp, login_rsp); - return; + cout << "login count limit" << ":" << login_req.user << "-" << login_req.uuid << "-" << login_req.mc << "-" << login_req.mn << "-" << login_req.ip << "-" << login_req.ver << endl; + return 0; } - add_mc_for_login(mysql, login_req); + add_or_update_mc_for_login(mysql, login_req); // login_rsp_pk login_rsp; login_rsp.err_code = 1; login_rsp.err_msg = "login sucess"; - login_rsp.date = date_limit; + login_rsp.date = expire_date; send_data(socket, tag_rsp, login_rsp); - + cout << "login sucess" << ":" << login_req.user << "-" << login_req.uuid << "-" << login_req.mc << "-" << login_req.mn << "-" << login_req.ip << "-" << login_req.ver << endl; + return 1; } -static void proc_logout(MYSQL &mysql, zmq::socket_t& socket, const logout_req_pk &logout) +static int proc_logout(MYSQL &mysql, zmq::socket_t& socket, const logout_req_pk &logout_req) { + const string tag_rsp = "logout"; - + update_mc_for_logout(mysql, logout_req); + // + logout_rsp_pk logout_rsp; + logout_rsp.err_code = 1; + logout_rsp.err_msg = "logout sucess"; + send_data(socket, tag_rsp, logout_rsp); + cout << "logout sucess" << ":" << logout_req.user << "-" << logout_req.uuid << "-" << logout_req.mc << "-" << logout_req.mn << "-" << logout_req.ip << "-" << logout_req.ver << endl; + return 1; } -int main () +void main_loop () { //init db MYSQL mysql; @@ -447,6 +518,10 @@ int main () //init socket zmq::context_t context (1); zmq::socket_t socket (context, ZMQ_REP); + int sendtimeout = 3000; + socket.setsockopt(ZMQ_SNDTIMEO, &sendtimeout, sizeof(sendtimeout)); + int lingertime = 0; + socket.setsockopt(ZMQ_LINGER, &lingertime, sizeof(lingertime)); socket.bind ("tcp://*:8787"); //loop process package while (true) { @@ -457,15 +532,13 @@ int main () { login_req_pk login_req; recv_data(socket, login_req); - cout << tag_req << ":" << login_req.user << "-" << login_req.uuid << endl; - proc_login(mysql, socket, login_req); + int ret = proc_login(mysql, socket, login_req); } else if(tag_req == "logout") { logout_req_pk logout_req; recv_data(socket, logout_req); - cout << tag_req << ":" << logout_req.user << "-" << logout_req.uuid << endl; - proc_logout(mysql, socket, logout_req); + int ret = proc_logout(mysql, socket, logout_req); } else { @@ -473,8 +546,22 @@ int main () } } - return 0; } +int main () +{ + while(true) + { + try + { + main_loop(); + } + catch (exception& e) + { + cout << "exception:" << e.what() <