diff --git a/Net.xcodeproj/project.pbxproj b/Net.xcodeproj/project.pbxproj index 500707d..597459f 100644 --- a/Net.xcodeproj/project.pbxproj +++ b/Net.xcodeproj/project.pbxproj @@ -17,6 +17,9 @@ 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 */; }; + 92C34C282205C94600AB38D3 /* addr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15221FD725F009C5F11 /* addr.cpp */; }; + 92C34C292205C95F00AB38D3 /* socket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15321FD725F009C5F11 /* socket.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -33,7 +36,6 @@ /* Begin PBXFileReference section */ 9221D9EB21EA5142007310A7 /* Net */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Net; sourceTree = BUILT_PRODUCTS_DIR; }; - 9221DA1621EB8C02007310A7 /* pcs.map */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; name = pcs.map; path = build/Debug/PCS/pcs.map; sourceTree = ""; }; 9277A14621FD7246009C5F11 /* cmap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = cmap.h; path = include/cmap.h; sourceTree = ""; }; 9277A14721FD7246009C5F11 /* server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = server.h; path = include/server.h; sourceTree = ""; }; 9277A14821FD7246009C5F11 /* net.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = net.h; path = include/net.h; sourceTree = ""; }; @@ -56,8 +58,6 @@ 9277A15A21FD725F009C5F11 /* cpart.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = cpart.cpp; path = src/cpart.cpp; sourceTree = ""; }; 9277A15B21FD725F009C5F11 /* memory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = memory.cpp; path = src/memory.cpp; sourceTree = ""; }; 9277A15C21FD725F009C5F11 /* clock.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = clock.cpp; path = src/clock.cpp; sourceTree = ""; }; - 9277A16A21FE034F009C5F11 /* a.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = a.cpp; path = build/Debug/PCS/srcs/a.cpp; sourceTree = ""; }; - 9277A16C21FE0357009C5F11 /* da.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = da.cpp; path = build/Debug/PCS/srcs/da.cpp; sourceTree = ""; }; 9277A18821FEB21D009C5F11 /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = build/Debug/Libraries/libsqlite3.dylib; sourceTree = ""; }; 9277A18A220076EE009C5F11 /* sql.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = sql.cpp; path = src/sql.cpp; sourceTree = ""; }; 9277A18D22007758009C5F11 /* sql.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = sql.h; path = include/sql.h; sourceTree = ""; }; @@ -97,11 +97,8 @@ 9221DA0E21EB5FAD007310A7 /* Net */ = { isa = PBXGroup; children = ( - 9277A16C21FE0357009C5F11 /* da.cpp */, - 9277A16A21FE034F009C5F11 /* a.cpp */, 925A13AA21EC989500CBD427 /* include */, 9277A15121FD724C009C5F11 /* src */, - 9221DA1621EB8C02007310A7 /* pcs.map */, ); name = Net; sourceTree = ""; @@ -219,6 +216,9 @@ 9277A16021FD725F009C5F11 /* md5.cpp in Sources */, 9277A16521FD725F009C5F11 /* cmap.cpp in Sources */, 9277A16221FD725F009C5F11 /* main.cpp in Sources */, + 92C34C272205C63A00AB38D3 /* server.cpp in Sources */, + 92C34C282205C94600AB38D3 /* addr.cpp in Sources */, + 92C34C292205C95F00AB38D3 /* socket.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/include/clock.h b/include/clock.h index 525df0e..3f7200a 100644 --- a/include/clock.h +++ b/include/clock.h @@ -16,6 +16,16 @@ struct clock_register{ void *(*func)(void *); bool if_thread; int click; + int rawclick; + void *arg; +}; + +struct clock_thread_info{ + uint32_t tid = 0; + pthread_t pht = 0; + bool if_reset = false; + void *args = NULL; + clock_register *pcr; }; //初始化全局时钟 @@ -24,6 +34,9 @@ void initClock(void); void setThreadsClock(void); //时钟滴答调用函数 void threadsClock(int); +//时钟线程完结前调用此函数进行标记 +void clockThreadFinish(uint32_t tid); +void newClock(clock_register ncr); #endif /* clock_h */ diff --git a/src/clock.cpp b/src/clock.cpp index 86443eb..b24a000 100644 --- a/src/clock.cpp +++ b/src/clock.cpp @@ -9,9 +9,11 @@ #include "type.h" #include "clock.h" -#define CLOCKESE 15 +#define CLOCKESE 30 list clocks_list; +map clocks_thread_map; +list clock_thread_finished; static struct itimerval oitrl, itrl; static uint64_t clock_erase = CLOCKESE; @@ -31,27 +33,51 @@ void setThreadsClock(void){ setitimer(ITIMER_REAL, &itrl, &oitrl); } +void newClock(clock_register ncr){ + clocks_list.push_back(ncr); +} + //时钟滴答调用函数 void threadsClock(int n){ // 删除到期时钟 if(clock_erase == 0){ for(auto i = clocks_list.begin(); i != clocks_list.end();){ - if(i->click == 0) i = clocks_list.erase(i); + if(i->click == -1) i = clocks_list.erase(i); else i++; } // 重设总滴答数 clock_erase = CLOCKESE; } else clock_erase--; + + for(auto tid : clock_thread_finished){ + clock_thread_info *tcti = clocks_thread_map.find(tid)->second; + pthread_join(tcti->pht,NULL); + pthread_detach(tcti->pht); + clocks_thread_map.erase(clocks_thread_map.find(tid)); +// 如果时钟需要重置 + if(tcti->if_reset){ + clock_register ncr = *tcti->pcr; + ncr.click = ncr.rawclick; + newClock(ncr); + } + delete tcti; + } + clock_thread_finished.clear(); + // 处理时钟列表 for(auto &clock : clocks_list){ if(clock.click == 0){ if(clock.if_thread){ - pthread_t ptd = 0; - pthread_create(&ptd, NULL, clock.func, NULL); + clock_thread_info *pncti = new clock_thread_info(); + pncti->args = clock.arg; + pncti->pcr = &clock; + pncti->tid = (uint32_t)clocks_thread_map.size()+1; + clocks_thread_map.insert({pncti->tid,pncti}); + pthread_create(&pncti->pht, NULL, clock.func, pncti); } else{ - clock.func(NULL); + clock.func(clock.arg); } // 标记时钟到期 clock.click = -1; @@ -63,4 +89,7 @@ void threadsClock(int n){ } +void clockThreadFinish(uint32_t tid){ + clock_thread_finished.push_back(tid); +} diff --git a/src/main.cpp b/src/main.cpp index 87825fb..e2d24dd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,11 +17,12 @@ int update(string instruct, vector &configs, vector &lconfigs, vector &targets); int construct(string instruct,vector &config, vector &lconfig, vector &target); - +int server(string instruct, vector &configs, vector &lconfigs, vector &targets); struct instructions{ int (*unpack)(string instruct,vector &, vector &, vector &) = NULL; int (*construct)(string instruct,vector &, vector &, vector &) = NULL; int (*update)(string instruct,vector &, vector &, vector &) = NULL; + int (*server)(string instruct,vector &, vector &, vector &) = NULL; }; int main(int argc, const char *argv[]){ @@ -33,10 +34,11 @@ int main(int argc, const char *argv[]){ vector long_config; // 目标 vector target; -// 设置函数 +// 注册函数 struct instructions istns; istns.construct = construct; istns.update = update; + istns.server = server; // 解析命令 int if_instruct = 1; @@ -67,6 +69,10 @@ int main(int argc, const char *argv[]){ if(istns.update != nullptr) istns.update(instruct,config,long_config,target); else printf("Function not found.\n"); } + else if (instruct == "server"){ + if(istns.update != nullptr) istns.server(instruct,config,long_config,target); + else printf("Function not found.\n"); + } else{ printf("Instruction \"%s\" doesn't make sense.\n",instruct.data()); } @@ -81,6 +87,15 @@ bool config_search(vector &configs,string tfg){ return false; } +int server(string instruct, vector &configs, vector &lconfigs, vector &targets){ + initClock(); + setThreadsClock(); + Server nsvr; + setServerClock(&nsvr, 2); + while(1) usleep(10000); + return 0; +} + int update(string instruct, vector &configs, vector &lconfigs, vector &targets){ try { Proj nproj(targets[0], "netc.proj"); diff --git a/src/server.cpp b/src/server.cpp index b07df29..38a6060 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -14,8 +14,10 @@ extern list clocks_list; void setServerClock(Server *psvr, int clicks){ clock_register ncr; ncr.if_thread = true; - ncr.click = 2; + ncr.click = 3; + ncr.rawclick = 3; ncr.func = serverDeamon; + ncr.arg = (void *)psvr; clocks_list.push_back(ncr); } @@ -210,26 +212,28 @@ raw_data Server::ProcessSignedRawMsg(char *p_rdt, ssize_t size){ return trdt; } -void *serverDeamon(void *psvr){ - Server svr = *((Server *) psvr); +void *serverDeamon(void *pvcti){ + clock_thread_info *pcti = (clock_thread_info *) pvcti; + Server *psvr = (Server *) pcti->args; //cout<<"Server Deamon Checked."<packet_max; ssize_t tlen; char *str = nullptr; printf("Checking Packet.\n"); do{ - tlen = svr.socket.RecvRAW(&str); + tlen = psvr->socket.RecvRAW(&str); if(tlen > 0){ // 记录有效数据包 if(Server::CheckRawMsg(str, tlen)){ raw_data trdt = Server::ProcessSignedRawMsg(str, tlen); - svr.rawdata_in.push_back(trdt); + psvr->rawdata_in.push_back(trdt); } } free(str); }while (tlen && prm-- > 0); - setServerClock(&svr, 2); + pcti->if_reset = true; + clockThreadFinish(pcti->tid); pthread_exit(NULL); }