From 77f0fd3d9e6dbfb8442cdde7b4d0080ed170f708 Mon Sep 17 00:00:00 2001 From: Saturneic Date: Tue, 5 Mar 2019 20:27:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Net.xcodeproj/project.pbxproj | 4 +- include/sql.h | 2 + src/client.cpp | 4 ++ src/controller.cpp | 8 ++- src/cproj_proj.cpp | 94 +++++++++++++++++++++++++++++++++-- src/main.cpp | 4 ++ src/model.cpp | 14 ++++-- src/server.cpp | 17 ++++--- src/socket.cpp | 2 +- src/sql.cpp | 6 +++ 10 files changed, 136 insertions(+), 19 deletions(-) diff --git a/Net.xcodeproj/project.pbxproj b/Net.xcodeproj/project.pbxproj index 2ea6d2d..680f13c 100644 --- a/Net.xcodeproj/project.pbxproj +++ b/Net.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 9227C804220D6E4D00AE694C /* model.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9227C803220D6E4D00AE694C /* model.cpp */; }; 9227C85B220D702800AE694C /* controller.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9227C85A220D702800AE694C /* controller.cpp */; }; 9227C860220DAD9B00AE694C /* aes.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9227C85F220DAD9B00AE694C /* aes.cpp */; }; + 92436CB7222E68C2000443B2 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 9277A18821FEB21D009C5F11 /* libsqlite3.dylib */; }; 926E0965220B17FA00AD5D5B /* client.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 926E0964220B17FA00AD5D5B /* client.cpp */; }; 9277A16021FD725F009C5F11 /* md5.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15421FD725F009C5F11 /* md5.cpp */; }; 9277A16121FD725F009C5F11 /* cproj_cpt.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15521FD725F009C5F11 /* cproj_cpt.cpp */; }; @@ -18,7 +19,6 @@ 9277A16621FD725F009C5F11 /* cpart.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15A21FD725F009C5F11 /* cpart.cpp */; }; 9277A16721FD725F009C5F11 /* memory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15B21FD725F009C5F11 /* memory.cpp */; }; 9277A16821FD725F009C5F11 /* clock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15C21FD725F009C5F11 /* clock.cpp */; }; - 9277A18921FEB21D009C5F11 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 9277A18821FEB21D009C5F11 /* libsqlite3.dylib */; }; 9277A18C220076EE009C5F11 /* sql.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A18A220076EE009C5F11 /* sql.cpp */; }; 9277A190220079DB009C5F11 /* cproj_proj.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A18F220079DB009C5F11 /* cproj_proj.cpp */; }; 92C34C272205C63A00AB38D3 /* server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15821FD725F009C5F11 /* server.cpp */; }; @@ -90,7 +90,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 9277A18921FEB21D009C5F11 /* libsqlite3.dylib in Frameworks */, + 92436CB7222E68C2000443B2 /* libsqlite3.dylib in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/include/sql.h b/include/sql.h index 89000c3..2f8c6f5 100644 --- a/include/sql.h +++ b/include/sql.h @@ -41,6 +41,8 @@ namespace sql { string string_type(string str); // 输出错误信息 void printError(sqlite3 *psql); +// 执行SQL语句 + int exec(sqlite3 *psql, string sql); } #endif /* sql_h */ diff --git a/src/client.cpp b/src/client.cpp index c97ad2e..28d9eb5 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -18,6 +18,8 @@ Client::Client(int port, string send_ip,int send_port):socket(port),send_socket( sqlite3_stmt *psqlsmt; const char *pzTail; sqlite3_open("info.db", &psql); + + sql::exec(psql, "BEGIN DEFERRED;"); string sql_quote = "select name,tag,msqes_key from client_info where rowid = 1;"; sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); sqlite3_step(psqlsmt); @@ -25,6 +27,8 @@ Client::Client(int port, string send_ip,int send_port):socket(port),send_socket( tag = (const char *)sqlite3_column_text(psqlsmt, 1); sqe_key = (const char *)sqlite3_column_text(psqlsmt, 2); sqlite3_finalize(psqlsmt); + sql::exec(psql, "COMMIT;"); + } //客户端请求接收守护进程 diff --git a/src/controller.cpp b/src/controller.cpp index e9f745a..e9d51b4 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -443,7 +443,7 @@ int client(string instruct, vector &configs, vector &lconfigs, v //等待广场服务器回应 while (if_wait == 1) { - sleep(10); + usleep(1000); } if(!if_wait){ #ifdef DEBUG @@ -584,6 +584,7 @@ int client(string instruct, vector &configs, vector &lconfigs, v //成功注册 if (!if_wait) { sqlite3_stmt *psqlsmt; + sql::exec(psql, "BEGIN IMMEDIATE;"); sql::insert_info(psql, &psqlsmt, "client_register_info", { {"name","?1"}, {"tag","?2"}, @@ -595,6 +596,7 @@ int client(string instruct, vector &configs, vector &lconfigs, v sqlite3_bind_blob(psqlsmt, 3, nclt.post_key.GetKey(), sizeof(uint64_t) * 4, SQLITE_TRANSIENT); sqlite3_step(psqlsmt); sqlite3_finalize(psqlsmt); + sql::exec(psql, "COMMIT;"); } else if (~!if_wait) { error::printError("fail to do register."); @@ -726,6 +728,7 @@ int client(string instruct, vector &configs, vector &lconfigs, v } else if (cmdstr == "info") { sqlite3_open("info.db",&psql); + sql::exec(psql, "BEGIN DEFERRED;"); string sql_quote = "SELECT * from client_register_info where rowid = 1;"; sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); sqlite3_step(psqlsmt); @@ -746,7 +749,8 @@ int client(string instruct, vector &configs, vector &lconfigs, v error::printError("[NONE INFO]"); } sqlite3_finalize(psqlsmt); - + sql::exec(psql, "COMMIT;"); + sqlite3_close(psql); } else if (cmdstr == "quit") { //关闭所有打开的文件描述符 diff --git a/src/cproj_proj.cpp b/src/cproj_proj.cpp index 25a9da0..bee2420 100644 --- a/src/cproj_proj.cpp +++ b/src/cproj_proj.cpp @@ -246,7 +246,10 @@ void Proj::write_lib_info(void){ #ifdef DEBUG printf("Writing Libfiles Information Into Database.\n"); #endif + + for(auto lib : lib_index){ + sql::exec(psql, "BEGIN;"); // 链接数据 sqlite3_bind_int(psqlsmt, 1, idx++); sqlite3_bind_text(psqlsmt, 2, lib.second.data(), -1, SQLITE_TRANSIENT); @@ -264,8 +267,11 @@ void Proj::write_lib_info(void){ } sqlite3_reset(psqlsmt); sqlite3_clear_bindings(psqlsmt); + sql::exec(psql, "COMMIT;"); } sqlite3_finalize(psqlsmt); + + } // 写入入口函数信息到数据库中 @@ -284,6 +290,7 @@ void Proj::write_func_info(void){ for(auto func : func_index){ string src_file = func.second->funcs_src.find(func.first)->second; string lib_file = lib_index.find(src_file)->second; + sql::exec(psql, "BEGIN;"); string sql_quote = "SELECT id FROM srcfiles WHERE name = "+sql::string_type(src_file)+";"; // 查找源文件信息在数据库中的ID SQLCallBack *psqlcb = sql::sql_exec(psql, sql_quote); @@ -304,7 +311,9 @@ void Proj::write_func_info(void){ throw "database abnormal"; } delete psqlcb; + sql::exec(psql, "COMMIT;"); // 查找动态链接库信息在数据库中的ID + sql::exec(psql, "BEGIN;"); sql_quote = "SELECT id FROM libfiles WHERE name = "+sql::string_type(lib_file)+";"; psqlcb = sql::sql_exec(psql, sql_quote); if(psqlcb->size != 1){ @@ -324,6 +333,9 @@ void Proj::write_func_info(void){ throw "database abnormal"; } delete psqlcb; + sql::exec(psql, "COMMIT;"); + + sql::exec(psql, "BEGIN;"); // 链接数据 sqlite3_bind_int(psqlsmt, 1, idx++); sqlite3_bind_text(psqlsmt, 2, func.first.data(), -1, SQLITE_TRANSIENT); @@ -344,6 +356,8 @@ void Proj::write_func_info(void){ } sqlite3_reset(psqlsmt); sqlite3_clear_bindings(psqlsmt); + sql::exec(psql, "COMMIT;"); + } sqlite3_finalize(psqlsmt); } @@ -353,8 +367,10 @@ void Proj::write_func_info(string func_name, Cpt *pcpt){ string sql_quote2 = "SELECT * FROM functions ORDER BY id DESC limit 0,1;"; const char *pzTail; sqlite3_stmt *psqlsmt2; + sql::exec(psql, "BEGIN;"); sqlite3_prepare(psql, sql_quote2.data(), -1, &psqlsmt2, &pzTail); sqlite3_step(psqlsmt2); + sql::exec(psql, "COMMIT;"); int idx = sqlite3_column_int(psqlsmt2, 0); sqlite3_finalize(psqlsmt2); @@ -370,6 +386,8 @@ void Proj::write_func_info(string func_name, Cpt *pcpt){ #endif string src_file = pcpt->funcs_src.find(func_name)->second; string lib_file = lib_index.find(src_file)->second; + + sql::exec(psql, "BEGIN;"); string sql_quote = "SELECT id FROM srcfiles WHERE name = "+sql::string_type(src_file)+";"; // 查找源文件信息在数据库中的ID SQLCallBack *psqlcb = sql::sql_exec(psql, sql_quote); @@ -390,6 +408,9 @@ void Proj::write_func_info(string func_name, Cpt *pcpt){ throw "database abnormal"; } delete psqlcb; + sql::exec(psql, "COMMIT;"); + + sql::exec(psql, "BEGIN;"); // 查找动态链接库信息在数据库中的ID sql_quote = "SELECT id FROM libfiles WHERE name = "+sql::string_type(lib_file)+";"; psqlcb = sql::sql_exec(psql, sql_quote); @@ -410,6 +431,9 @@ void Proj::write_func_info(string func_name, Cpt *pcpt){ throw "database abnormal"; } delete psqlcb; + sql::exec(psql, "COMMIT;"); + + sql::exec(psql, "BEGIN;"); // 链接数据 sqlite3_bind_int(psqlsmt, 1, ++idx); sqlite3_bind_text(psqlsmt, 2, func_name.data(), -1, SQLITE_TRANSIENT); @@ -431,6 +455,7 @@ void Proj::write_func_info(string func_name, Cpt *pcpt){ sqlite3_reset(psqlsmt); sqlite3_clear_bindings(psqlsmt); sqlite3_finalize(psqlsmt); + sql::exec(psql, "COMMIT;"); } //写入口函数入输入输出参数信息到数据库中 @@ -482,7 +507,10 @@ void Proj::write_args_info_no_create_table(string func_name, Cpt *pcpt){ printf("Writing Function Parameter (IN) Information Into Database (%s)\n",func_name.data()); #endif // 写入输入参数 + for(auto arg : *pfin){ + + sql::exec(psql, "BEGIN;"); // 连接数据 sqlite3_bind_text(psqlsmt, 1, arg.type.data(), -1, SQLITE_TRANSIENT); sqlite3_bind_int(psqlsmt, 2, idx++); @@ -504,14 +532,18 @@ void Proj::write_args_info_no_create_table(string func_name, Cpt *pcpt){ } sqlite3_reset(psqlsmt); sqlite3_clear_bindings(psqlsmt); + sql::exec(psql, "COMMIT;"); + } + #ifdef DEBUG printf("Writing Function Parameter (OUT) Information Into Database (%s)\n",func_name.data()); #endif idx = 0; // 写入输入参数 for(auto arg : *pfout){ + sql::exec(psql, "BEGIN;"); // 连接数据 sqlite3_bind_text(psqlsmt, 1, arg.type.data(), -1, SQLITE_TRANSIENT); sqlite3_bind_int(psqlsmt, 2, idx++); @@ -534,6 +566,7 @@ void Proj::write_args_info_no_create_table(string func_name, Cpt *pcpt){ } sqlite3_reset(psqlsmt); sqlite3_clear_bindings(psqlsmt); + sql::exec(psql, "COMMIT;"); } sqlite3_finalize(psqlsmt); } @@ -558,6 +591,8 @@ void Proj::write_cpt_info(void){ }; string buff; read_settings(treal_path, buff); + + sql::exec(psql, "BEGIN;"); sqlite3_bind_int(psqlsmt, 1, idx++); string md5; ComputeFile(treal_path.data(), md5); @@ -578,6 +613,8 @@ void Proj::write_cpt_info(void){ } sqlite3_reset(psqlsmt); sqlite3_clear_bindings(psqlsmt); + sql::exec(psql, "COMMIT;"); + } sqlite3_finalize(psqlsmt); } @@ -601,6 +638,8 @@ void Proj::write_proj_info(void){ sqlite3_bind_blob(psqlsmt, 2, content.data(), (int)content.size(), SQLITE_TRANSIENT); string md5; ComputeFile(proj_path+"netc.proj", md5); + + sql::exec(psql, "BEGIN;"); sqlite3_bind_text(psqlsmt, 3, md5.data(), -1, SQLITE_TRANSIENT); sqlite3_bind_text(psqlsmt, 4, lib_path.data(), -1, SQLITE_TRANSIENT); // 执行SQL语句 @@ -618,7 +657,7 @@ void Proj::write_proj_info(void){ sqlite3_reset(psqlsmt); sqlite3_clear_bindings(psqlsmt); sqlite3_finalize(psqlsmt); - + sql::exec(psql, "COMMIT;"); } void Proj::check_database(void){ @@ -633,6 +672,7 @@ void Proj::check_database(void){ #endif const char *tables[] = {"srcfiles","cptfiles","functions","projfile","libfiles","projfile"}; for(auto strc : tables){ + sql::exec(psql, "BEGIN;"); sqlite3_bind_text(psqlsmt, 1, strc, -1, SQLITE_STATIC); // 执行SQL语句 int rtn = sqlite3_step(psqlsmt); @@ -656,6 +696,7 @@ void Proj::check_database(void){ } sqlite3_reset(psqlsmt); sqlite3_clear_bindings(psqlsmt); + sql::exec(psql, "COMMIT;"); } sqlite3_finalize(psqlsmt); @@ -688,7 +729,7 @@ void Proj::check_database(void){ }; check_table(3, sctc, psqlsmt); sqlite3_finalize(psqlsmt); - + // 检查数据库表cptfiles其项及其类型是否正确 sql_quote = "PRAGMA table_info( cptfiles );"; sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); @@ -737,9 +778,11 @@ void Proj::check_database(void){ printf("Succeed In Checking Table Columns\n"); #endif + sql::exec(psql, "BEGIN;"); sql_quote = "SELECT project_name FROM projfile;"; sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); string tname = (char *)sqlite3_column_text(psqlsmt, 0); if(name != tname){ #ifdef DEBUG @@ -748,6 +791,8 @@ void Proj::check_database(void){ throw "database conflict"; } sqlite3_finalize(psqlsmt); + + #ifdef DEBUG printf("Succeed In Checking Project Information\n"); #endif @@ -755,7 +800,9 @@ void Proj::check_database(void){ } void Proj::check_table(int cnum,vector sctc,sqlite3_stmt *psqlsmt){ + sql::exec(psql, "BEGIN;"); sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); int tcnum = sqlite3_data_count(psqlsmt); if(tcnum != 6){ #ifdef DEBUG @@ -775,7 +822,9 @@ void Proj::check_table(int cnum,vector sctc,sqlite3_stmt *ps if(tctc.name != sctc[i-1].name || tctc.type != sctc[i-1].type || tctc.notnull != sctc[i-1].notnull || tctc.pk != sctc[i-1].pk){ throw "table abnormal"; } + sql::exec(psql, "BEGIN;"); sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); } } @@ -1093,12 +1142,14 @@ void Proj::UpdateProcess(void){ CheckCptInfo(); BuildFuncIndex(); + sql::exec(psql, "BEGIN;"); // 编译新增源文件并将记录添加到数据库中 // 查找最大id string sql_quote2 = "SELECT * FROM srcfiles ORDER BY id DESC limit 0,1;"; sqlite3_stmt *psqlsmt2; sqlite3_prepare(psql, sql_quote2.data(), -1, &psqlsmt2, &pzTail); int rtn = sqlite3_step(psqlsmt2); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ } @@ -1109,6 +1160,7 @@ void Proj::UpdateProcess(void){ } int last_id = sqlite3_column_int(psqlsmt2, 0); sqlite3_finalize(psqlsmt2); + sql::insert_info(psql, &psqlsmt2, "srcfiles", { {"id","?1"}, {"name","?2"}, @@ -1116,8 +1168,10 @@ void Proj::UpdateProcess(void){ {"md5","?4"} }); for(auto usrc : used_srcfiles){ + sql::exec(psql, "BEGIN;"); sqlite3_bind_text(psqlsmt, 1, usrc.first.data(), -1, SQLITE_TRANSIENT); int rtn = sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ } @@ -1126,9 +1180,12 @@ void Proj::UpdateProcess(void){ int errorcode = sqlite3_extended_errcode(psql); printf("\033[31mSQL Error: [%d]%s\n\033[0m",errorcode,error); } + + int if_find = sqlite3_column_int(psqlsmt, 0); if(!if_find){ compile_srcfile(usrc.first, src_paths[usrc.second]); + sql::exec(psql, "BEGIN;"); sqlite3_bind_int(psqlsmt2, 1, ++last_id); sqlite3_bind_text(psqlsmt2, 2, usrc.first.data(), -1, SQLITE_TRANSIENT); sqlite3_bind_text(psqlsmt2, 3, src_paths[usrc.second].data(), -1, SQLITE_TRANSIENT); @@ -1137,6 +1194,7 @@ void Proj::UpdateProcess(void){ sqlite3_bind_text(psqlsmt2, 4, md5.data(), -1, SQLITE_TRANSIENT); // 添加入口函数 int rtn = sqlite3_step(psqlsmt2); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ } @@ -1147,6 +1205,7 @@ void Proj::UpdateProcess(void){ } sqlite3_reset(psqlsmt2); sqlite3_clear_bindings(psqlsmt2); + } sqlite3_reset(psqlsmt); sqlite3_clear_bindings(psqlsmt); @@ -1154,9 +1213,11 @@ void Proj::UpdateProcess(void){ sqlite3_finalize(psqlsmt); sqlite3_finalize(psqlsmt2); + sql::exec(psql, "BEGIN;"); sql_quote2 = "SELECT * FROM libfiles ORDER BY id DESC limit 0,1;"; sqlite3_prepare(psql, sql_quote2.data(), -1, &psqlsmt2, &pzTail); rtn = sqlite3_step(psqlsmt2); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ } @@ -1167,16 +1228,22 @@ void Proj::UpdateProcess(void){ } last_id = sqlite3_column_int(psqlsmt2, 0); sqlite3_finalize(psqlsmt2); + + + sql::insert_info(psql, &psqlsmt2, "libfiles", { {"id","?1"}, {"name","?2"}, {"content","?3"} }); for(auto lib : lib_index){ + + sql::exec(psql, "BEGIN;"); sql_quote = "SELECT count(*) FROM libfiles WHERE name = ?1"; sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); sqlite3_bind_text(psqlsmt, 1, lib.second.data(), -1, SQLITE_TRANSIENT); int rtn = sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ int if_find = sqlite3_column_int(psqlsmt, 0); sqlite3_finalize(psqlsmt); @@ -1188,11 +1255,12 @@ void Proj::UpdateProcess(void){ printf("\033[31mSQL Error: [%d]%s\n\033[0m",errorcode,error); throw error; } - + sql::exec(psql, "BEGIN;"); sqlite3_bind_int(psqlsmt2, 1, ++last_id); sqlite3_bind_text(psqlsmt2, 2, lib.second.data(), -1, SQLITE_TRANSIENT); // 添加动态链接库 rtn = sqlite3_step(psqlsmt2); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ } @@ -1214,8 +1282,10 @@ void Proj::UpdateProcess(void){ sqlite3_prepare(psql, sql_quote2.data(), -1, &psqlsmt2, &pzTail); for(auto usrc : used_srcfiles){ + sql::exec(psql, "BEGIN;"); sqlite3_bind_text(psqlsmt, 1, usrc.first.data(), -1, SQLITE_TRANSIENT); int rtn = sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ } @@ -1230,11 +1300,14 @@ void Proj::UpdateProcess(void){ ComputeFile(tsrc_file.data(), tmd5); if(dmd5 != tmd5){ compile_srcfile(usrc.first, src_paths[usrc.second]); + + sql::exec(psql, "BEGIN;"); // 更新md5 sqlite3_bind_text(psqlsmt2, 1, tmd5.data(), -1, SQLITE_TRANSIENT); sqlite3_bind_text(psqlsmt2, 2, usrc.first.data(), -1, SQLITE_TRANSIENT); int rtn = sqlite3_step(psqlsmt2); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ } @@ -1258,8 +1331,10 @@ void Proj::UpdateProcess(void){ sql_quote = "SELECT count(*) FROM functions WHERE name = ?1;"; sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); for(auto func : func_index){ + sql::exec(psql, "BEGIN;"); sqlite3_bind_text(psqlsmt, 1, func.first.data(), -1, SQLITE_TRANSIENT); int rtn = sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ int if_find = sqlite3_column_int(psqlsmt, 0); @@ -1285,8 +1360,11 @@ void Proj::UpdateProcess(void){ // 检查更新参数列表 for(auto func : func_index){ sql_quote = "SELECT * FROM fargs_" + func.first+";"; + + sql::exec(psql, "BEGIN;"); sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); int rtn = sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ } @@ -1318,7 +1396,9 @@ void Proj::UpdateProcess(void){ break; } idx++; + sql::exec(psql, "BEGIN;"); sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); } idx = 0; for(auto farg_out : func.second->fargs_out.find(func.first)->second){ @@ -1339,7 +1419,9 @@ void Proj::UpdateProcess(void){ break; } idx++; + sql::exec(psql, "BEGIIN;"); sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); } sqlite3_finalize(psqlsmt); if(if_changed){ @@ -1347,8 +1429,10 @@ void Proj::UpdateProcess(void){ printf("\033[33mFunction Args Changed %s\n\033[0m",func.first.data()); #endif sql_quote2 = "DELETE FROM fargs_"+func.first+";"; + sql::exec(psql, "BEGIN;"); sqlite3_prepare(psql, sql_quote2.data(), -1, &psqlsmt2, &pzTail); sqlite3_step(psqlsmt2); + sql::exec(psql, "COMMIT;"); sqlite3_finalize(psqlsmt2); write_args_info_no_create_table(func.first,func.second); } @@ -1356,8 +1440,10 @@ void Proj::UpdateProcess(void){ } sql_quote = "DELETE FROM cptfiles"; + sql::exec(psql, "BEGIN;"); sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); rtn = sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ } @@ -1370,8 +1456,10 @@ void Proj::UpdateProcess(void){ write_cpt_info(); sql_quote = "DELETE FROM projfile"; + sql::exec(psql, "BEGIN;"); sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); rtn = sqlite3_step(psqlsmt); + sql::exec(psql, "COMMIT;"); if(rtn == SQLITE_DONE || rtn == SQLITE_ROW){ } diff --git a/src/main.cpp b/src/main.cpp index cd942f5..96cecbe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -80,6 +80,10 @@ int main(int argc, const char *argv[]){ if (istns.update != nullptr) rtn = istns.client(instruct, config, long_config, target); else error::printError("Function not found."); } + else if (instruct == "autoinit") { + //自动配置 + + } else { printf("\033[33mInstruction \"%s\" doesn't make sense.\n\033[0m", instruct.data()); } diff --git a/src/model.cpp b/src/model.cpp index 4547f2d..a595933 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -41,11 +41,13 @@ void getSQEPublicKey(respond *pres,void *args){ sqlite3 *psql = (sqlite3 *)args; sqlite3_stmt *psqlsmt; const char *pzTail; + sql::exec(psql, "BEGIN;"); string sql_quote = "update client_info set msqes_rsa_public = ?1 where rowid = 1;"; sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); sqlite3_bind_blob(psqlsmt, 1, npbc, sizeof(public_key_class), SQLITE_TRANSIENT); sqlite3_step(psqlsmt); sqlite3_finalize(psqlsmt); + sql::exec(psql, "COMMIT;"); if_wait = 0; } else if_wait = -1; @@ -99,10 +101,11 @@ void registerSQECallback(respond *pres,void *args){ sqlite3_stmt *psqlsmt; const char *pzTail; string sql_quote = "delete from client_register_info;"; - + sql::exec(psql, "BEGIN;"); sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); sqlite3_step(psqlsmt); sqlite3_finalize(psqlsmt); + sql::exec(psql, "COMMIT;"); } } if_wait = 0; @@ -163,9 +166,9 @@ void *connectionDeamon(void *args){ if (!memcmp(pcntl->write_buff, "SDAT", sizeof(uint32_t))) { uint32_t nsrwd_size = 0; Byte buff[BUFSIZ]; - memcpy(&nsrwd_size, pcntl->write_buff + sizeof(uint32_t), sizeof(uint32_t)); - if (!memcmp(pcntl->write_buff + 3 * sizeof(uint32_t) + nsrwd_size, "TADS", sizeof(uint32_t))) { - memcpy(buff, pcntl->write_buff + 3 * sizeof(uint32_t), nsrwd_size); + memcpy(&nsrwd_size, ((Byte *)pcntl->write_buff + sizeof(uint32_t)), sizeof(uint32_t)); + if (!memcmp((Byte *)pcntl->write_buff + 3 * sizeof(uint32_t) + nsrwd_size, "TADS", sizeof(uint32_t))) { + memcpy(buff, (Byte *)pcntl->write_buff + 3 * sizeof(uint32_t), nsrwd_size); send(pcntl->data_sfd, buff, nsrwd_size, 0); } else error::printError("buffer error."); @@ -307,6 +310,7 @@ void *clientServiceDeamon(void *arg) { pthread_attr_destroy(&attr); usleep(1000); } + pthread_exit(NULL); } void gets_s(char *buff, uint32_t size) { @@ -316,4 +320,4 @@ void gets_s(char *buff, uint32_t size) { buff[i++] = ch; } buff[i] = '\0'; -} \ No newline at end of file +} diff --git a/src/server.cpp b/src/server.cpp index 78be55a..bb5dade 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -291,7 +291,7 @@ void Client::SendRawData(raw_data *trdt){ int Server::SentRawdata(struct raw_data *trdt){ // 对大包进行拆分发送 - int rtn = 0; + ssize_t rtn = 0; if(trdt->msg_size > 256){ uint64_t aidx = 0,bidx = 0; int64_t alls = trdt->msg_size; @@ -317,7 +317,7 @@ int Server::SentRawdata(struct raw_data *trdt){ else { rtn = send_socket.SendRAW(trdt->msg, trdt->msg_size); } - return rtn; + return (int)rtn; } void net_box::FreeNetBox(void){ @@ -648,6 +648,7 @@ SQEServer::SQEServer(int port):Server(port){ sqlite3_stmt *psqlsmt; const char *pzTail; + sql::exec(psql, "BEGIN;"); // 从数据库获得服务器的公私钥及服务器名 string sql_quote = "select sqes_public,sqes_private,name from server_info where rowid = 1;"; sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); @@ -662,17 +663,20 @@ SQEServer::SQEServer(int port):Server(port){ memcpy(&prc, tbyt, sizeof(private_key_class)); name = (const char *)sqlite3_column_blob(psqlsmt, 2); sqlite3_finalize(psqlsmt); + sql::exec(psql, "COMMIT;"); //打印关键信息 error::printSuccess("Server Name: "+name); error::printSuccess("Listen Port: " + std::to_string(port)); - + sql::exec(psql, "BEGIN;"); sql_quote = "select count(name) from sqlite_master where name = \"register_info\""; sqlite3_prepare(psql, sql_quote.data(), -1, &psqlsmt, &pzTail); if (sqlite3_step(psqlsmt) != SQLITE_ROW) { sql::printError(psql); + sql::exec(psql, "COMMIT;"); throw "database is abnormal"; } + sql::exec(psql, "COMMIT;"); int if_find = sqlite3_column_int(psqlsmt,0); if (!if_find) { sql::table_create(psql, "register_info", { @@ -689,6 +693,7 @@ SQEServer::SQEServer(int port):Server(port){ error::printInfo("create table register_info."); } sqlite3_finalize(psqlsmt); + } void SQEServer::Packet2Request(packet &pkt, request &req){ @@ -863,7 +868,7 @@ void SQEServer::ProcessRequset(void){ pclr->t_addr.SetPort(sqlite3_column_int(psqlsmt, 6)); pclr->psql = psql; memcpy((void *)pclr->key.GetKey(), sqlite3_column_blob(psqlsmt, 3), sizeof(uint64_t) * 4); - printf("Login successfully %s[%s]:%s\n", pclr->name.data(), pclr->tag.data()); + printf("Login successfully %s[%s]\n", pclr->name.data(), pclr->tag.data()); //注册客户端联络守护进程 clock_register *pncr = new clock_register(); @@ -1253,7 +1258,7 @@ void *clientListener(void *args){ free(buff); } else if(size < 0){ - pcltl->if_connected == false; + pcltl->if_connected = false; break; } usleep(1000); @@ -1475,4 +1480,4 @@ void encrypt_post::GetJSON(string &json) { Writer writer(sb); edoc.Accept(writer); json = sb.GetString(); -} \ No newline at end of file +} diff --git a/src/socket.cpp b/src/socket.cpp index 8b7080f..4ec56c6 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -139,7 +139,7 @@ ssize_t SocketTCPClient::SendRAW(char *buff, unsigned long size){ send(client_sfd, vbuff, 2 * sizeof(uint32_t) + nidx - idx + 1, 0); - int grtn = recv(client_sfd, gbuff, BUFSIZ,0); + ssize_t grtn = recv(client_sfd, gbuff, BUFSIZ,0); if (grtn > 0 && !memcmp(gbuff, "DGET", sizeof(uint32_t))); else { return -1; diff --git a/src/sql.cpp b/src/sql.cpp index edbc13f..294bc46 100644 --- a/src/sql.cpp +++ b/src/sql.cpp @@ -71,7 +71,9 @@ namespace sql { } sql_quote += ");"; // 执行SQL语句 + sql::exec(psql, "BEGIN;"); SQLCallBack *pscb = sql_exec(psql, sql_quote); + sql::exec(psql, "COMMIT;"); if(pscb->sql_rtn != SQLITE_OK){ #ifdef DEBUG printf("[Error]Fail To Create Table %s\n",name.data()); @@ -136,4 +138,8 @@ namespace sql { } } + int exec(sqlite3 *psql, string sql){ + return sqlite3_exec(psql, sql.data(), NULL, NULL, NULL); + } + }