增强了时钟模块

This commit is contained in:
Saturneic 2019-02-02 21:55:17 +08:00
parent e0733eae26
commit 0c3749ad99
5 changed files with 81 additions and 20 deletions

View File

@ -17,6 +17,9 @@
9277A18921FEB21D009C5F11 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 9277A18821FEB21D009C5F11 /* libsqlite3.dylib */; }; 9277A18921FEB21D009C5F11 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 9277A18821FEB21D009C5F11 /* libsqlite3.dylib */; };
9277A18C220076EE009C5F11 /* sql.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A18A220076EE009C5F11 /* sql.cpp */; }; 9277A18C220076EE009C5F11 /* sql.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A18A220076EE009C5F11 /* sql.cpp */; };
9277A190220079DB009C5F11 /* cproj_proj.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A18F220079DB009C5F11 /* cproj_proj.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 */ /* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */ /* Begin PBXCopyFilesBuildPhase section */
@ -33,7 +36,6 @@
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
9221D9EB21EA5142007310A7 /* Net */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Net; sourceTree = BUILT_PRODUCTS_DIR; }; 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 = "<group>"; };
9277A14621FD7246009C5F11 /* cmap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = cmap.h; path = include/cmap.h; sourceTree = "<group>"; }; 9277A14621FD7246009C5F11 /* cmap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = cmap.h; path = include/cmap.h; sourceTree = "<group>"; };
9277A14721FD7246009C5F11 /* server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = server.h; path = include/server.h; sourceTree = "<group>"; }; 9277A14721FD7246009C5F11 /* server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = server.h; path = include/server.h; sourceTree = "<group>"; };
9277A14821FD7246009C5F11 /* net.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = net.h; path = include/net.h; sourceTree = "<group>"; }; 9277A14821FD7246009C5F11 /* net.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = net.h; path = include/net.h; sourceTree = "<group>"; };
@ -56,8 +58,6 @@
9277A15A21FD725F009C5F11 /* cpart.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = cpart.cpp; path = src/cpart.cpp; sourceTree = "<group>"; }; 9277A15A21FD725F009C5F11 /* cpart.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = cpart.cpp; path = src/cpart.cpp; sourceTree = "<group>"; };
9277A15B21FD725F009C5F11 /* memory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = memory.cpp; path = src/memory.cpp; sourceTree = "<group>"; }; 9277A15B21FD725F009C5F11 /* memory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = memory.cpp; path = src/memory.cpp; sourceTree = "<group>"; };
9277A15C21FD725F009C5F11 /* clock.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = clock.cpp; path = src/clock.cpp; sourceTree = "<group>"; }; 9277A15C21FD725F009C5F11 /* clock.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = clock.cpp; path = src/clock.cpp; sourceTree = "<group>"; };
9277A16A21FE034F009C5F11 /* a.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = a.cpp; path = build/Debug/PCS/srcs/a.cpp; sourceTree = "<group>"; };
9277A16C21FE0357009C5F11 /* da.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = da.cpp; path = build/Debug/PCS/srcs/da.cpp; sourceTree = "<group>"; };
9277A18821FEB21D009C5F11 /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = build/Debug/Libraries/libsqlite3.dylib; sourceTree = "<group>"; }; 9277A18821FEB21D009C5F11 /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = build/Debug/Libraries/libsqlite3.dylib; sourceTree = "<group>"; };
9277A18A220076EE009C5F11 /* sql.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = sql.cpp; path = src/sql.cpp; sourceTree = "<group>"; }; 9277A18A220076EE009C5F11 /* sql.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = sql.cpp; path = src/sql.cpp; sourceTree = "<group>"; };
9277A18D22007758009C5F11 /* sql.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = sql.h; path = include/sql.h; sourceTree = "<group>"; }; 9277A18D22007758009C5F11 /* sql.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = sql.h; path = include/sql.h; sourceTree = "<group>"; };
@ -97,11 +97,8 @@
9221DA0E21EB5FAD007310A7 /* Net */ = { 9221DA0E21EB5FAD007310A7 /* Net */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
9277A16C21FE0357009C5F11 /* da.cpp */,
9277A16A21FE034F009C5F11 /* a.cpp */,
925A13AA21EC989500CBD427 /* include */, 925A13AA21EC989500CBD427 /* include */,
9277A15121FD724C009C5F11 /* src */, 9277A15121FD724C009C5F11 /* src */,
9221DA1621EB8C02007310A7 /* pcs.map */,
); );
name = Net; name = Net;
sourceTree = "<group>"; sourceTree = "<group>";
@ -219,6 +216,9 @@
9277A16021FD725F009C5F11 /* md5.cpp in Sources */, 9277A16021FD725F009C5F11 /* md5.cpp in Sources */,
9277A16521FD725F009C5F11 /* cmap.cpp in Sources */, 9277A16521FD725F009C5F11 /* cmap.cpp in Sources */,
9277A16221FD725F009C5F11 /* main.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; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -16,6 +16,16 @@ struct clock_register{
void *(*func)(void *); void *(*func)(void *);
bool if_thread; bool if_thread;
int click; 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 setThreadsClock(void);
//时钟滴答调用函数 //时钟滴答调用函数
void threadsClock(int); void threadsClock(int);
//时钟线程完结前调用此函数进行标记
void clockThreadFinish(uint32_t tid);
void newClock(clock_register ncr);
#endif /* clock_h */ #endif /* clock_h */

View File

@ -9,9 +9,11 @@
#include "type.h" #include "type.h"
#include "clock.h" #include "clock.h"
#define CLOCKESE 15 #define CLOCKESE 30
list<clock_register> clocks_list; list<clock_register> clocks_list;
map<uint32_t,clock_thread_info *> clocks_thread_map;
list<uint32_t> clock_thread_finished;
static struct itimerval oitrl, itrl; static struct itimerval oitrl, itrl;
static uint64_t clock_erase = CLOCKESE; static uint64_t clock_erase = CLOCKESE;
@ -31,27 +33,51 @@ void setThreadsClock(void){
setitimer(ITIMER_REAL, &itrl, &oitrl); setitimer(ITIMER_REAL, &itrl, &oitrl);
} }
void newClock(clock_register ncr){
clocks_list.push_back(ncr);
}
//时钟滴答调用函数 //时钟滴答调用函数
void threadsClock(int n){ void threadsClock(int n){
// 删除到期时钟 // 删除到期时钟
if(clock_erase == 0){ if(clock_erase == 0){
for(auto i = clocks_list.begin(); i != clocks_list.end();){ 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++; else i++;
} }
// 重设总滴答数 // 重设总滴答数
clock_erase = CLOCKESE; clock_erase = CLOCKESE;
} }
else clock_erase--; 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){ for(auto &clock : clocks_list){
if(clock.click == 0){ if(clock.click == 0){
if(clock.if_thread){ if(clock.if_thread){
pthread_t ptd = 0; clock_thread_info *pncti = new clock_thread_info();
pthread_create(&ptd, NULL, clock.func, NULL); 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{ else{
clock.func(NULL); clock.func(clock.arg);
} }
// 标记时钟到期 // 标记时钟到期
clock.click = -1; clock.click = -1;
@ -63,4 +89,7 @@ void threadsClock(int n){
} }
void clockThreadFinish(uint32_t tid){
clock_thread_finished.push_back(tid);
}

View File

@ -17,11 +17,12 @@
int update(string instruct, vector<string> &configs, vector<string> &lconfigs, vector<string> &targets); int update(string instruct, vector<string> &configs, vector<string> &lconfigs, vector<string> &targets);
int construct(string instruct,vector<string> &config, vector<string> &lconfig, vector<string> &target); int construct(string instruct,vector<string> &config, vector<string> &lconfig, vector<string> &target);
int server(string instruct, vector<string> &configs, vector<string> &lconfigs, vector<string> &targets);
struct instructions{ struct instructions{
int (*unpack)(string instruct,vector<string> &, vector<string> &, vector<string> &) = NULL; int (*unpack)(string instruct,vector<string> &, vector<string> &, vector<string> &) = NULL;
int (*construct)(string instruct,vector<string> &, vector<string> &, vector<string> &) = NULL; int (*construct)(string instruct,vector<string> &, vector<string> &, vector<string> &) = NULL;
int (*update)(string instruct,vector<string> &, vector<string> &, vector<string> &) = NULL; int (*update)(string instruct,vector<string> &, vector<string> &, vector<string> &) = NULL;
int (*server)(string instruct,vector<string> &, vector<string> &, vector<string> &) = NULL;
}; };
int main(int argc, const char *argv[]){ int main(int argc, const char *argv[]){
@ -33,10 +34,11 @@ int main(int argc, const char *argv[]){
vector<string> long_config; vector<string> long_config;
// 目标 // 目标
vector<string> target; vector<string> target;
// 设置函数 // 注册函数
struct instructions istns; struct instructions istns;
istns.construct = construct; istns.construct = construct;
istns.update = update; istns.update = update;
istns.server = server;
// 解析命令 // 解析命令
int if_instruct = 1; 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); if(istns.update != nullptr) istns.update(instruct,config,long_config,target);
else printf("Function not found.\n"); 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{ else{
printf("Instruction \"%s\" doesn't make sense.\n",instruct.data()); printf("Instruction \"%s\" doesn't make sense.\n",instruct.data());
} }
@ -81,6 +87,15 @@ bool config_search(vector<string> &configs,string tfg){
return false; return false;
} }
int server(string instruct, vector<string> &configs, vector<string> &lconfigs, vector<string> &targets){
initClock();
setThreadsClock();
Server nsvr;
setServerClock(&nsvr, 2);
while(1) usleep(10000);
return 0;
}
int update(string instruct, vector<string> &configs, vector<string> &lconfigs, vector<string> &targets){ int update(string instruct, vector<string> &configs, vector<string> &lconfigs, vector<string> &targets){
try { try {
Proj nproj(targets[0], "netc.proj"); Proj nproj(targets[0], "netc.proj");

View File

@ -14,8 +14,10 @@ extern list<clock_register> clocks_list;
void setServerClock(Server *psvr, int clicks){ void setServerClock(Server *psvr, int clicks){
clock_register ncr; clock_register ncr;
ncr.if_thread = true; ncr.if_thread = true;
ncr.click = 2; ncr.click = 3;
ncr.rawclick = 3;
ncr.func = serverDeamon; ncr.func = serverDeamon;
ncr.arg = (void *)psvr;
clocks_list.push_back(ncr); clocks_list.push_back(ncr);
} }
@ -210,26 +212,28 @@ raw_data Server::ProcessSignedRawMsg(char *p_rdt, ssize_t size){
return trdt; return trdt;
} }
void *serverDeamon(void *psvr){ void *serverDeamon(void *pvcti){
Server svr = *((Server *) psvr); clock_thread_info *pcti = (clock_thread_info *) pvcti;
Server *psvr = (Server *) pcti->args;
//cout<<"Server Deamon Checked."<<endl; //cout<<"Server Deamon Checked."<<endl;
Addr f_addr; Addr f_addr;
int prm = svr.packet_max; int prm = psvr->packet_max;
ssize_t tlen; ssize_t tlen;
char *str = nullptr; char *str = nullptr;
printf("Checking Packet.\n"); printf("Checking Packet.\n");
do{ do{
tlen = svr.socket.RecvRAW(&str); tlen = psvr->socket.RecvRAW(&str);
if(tlen > 0){ if(tlen > 0){
// 记录有效数据包 // 记录有效数据包
if(Server::CheckRawMsg(str, tlen)){ if(Server::CheckRawMsg(str, tlen)){
raw_data trdt = Server::ProcessSignedRawMsg(str, tlen); raw_data trdt = Server::ProcessSignedRawMsg(str, tlen);
svr.rawdata_in.push_back(trdt); psvr->rawdata_in.push_back(trdt);
} }
} }
free(str); free(str);
}while (tlen && prm-- > 0); }while (tlen && prm-- > 0);
setServerClock(&svr, 2); pcti->if_reset = true;
clockThreadFinish(pcti->tid);
pthread_exit(NULL); pthread_exit(NULL);
} }