標籤:

面向對象 & 回調函數 模板

typedef struct{ s_begin begin; //啟動掃描 s_end end; //終止掃描 SCAN_CFG cfg_msg; //掃描配置參數 SCAN_RES res_msg; //掃描結果}SCAN_BUILDER; //提供給前端使用的數據結構typedef struct{ s_begin begin; //啟動掃描 s_end end; //終止掃描 SCAN_CFG cfg_msg; //掃描配置參數 SCAN_RES res_msg; //掃描結果 void * data; s_stu_act status_act; //狀態變更時的響應動作 s_fini_act fini_act; //掃描結束時的響應動作 exception_act exceptionscall;//異常時候的響應動作 pthread_mutex_t mutex;//使用互斥鎖同步文件的操作 c_map cmap; hash_map *ip_map; FILE *fp;//數據存儲文件指針 volatile int quitflag;//終止掃描標記 char env[PATH_MAX];//環境變數 int quitcase;//記錄退出原因 1.代表正常退出 2.代表超時退出 3.代表ctrl+c退出 char str_cahce[512]; char _s1[3+1]; char _s2[3+1]; char _s3[3+1]; char _s4[3+1]; }SCANER_OBJ;//自己使用的數據結構typedef void(*v_fini_act)(DEAL_TYPE, VERTIFY_RES,void *);//驗證結束時的回調動作typedef void(*s_fini_act)(DEAL_TYPE, SCAN_RES,void *);//掃描結束時的回調動作typedef void(*v_stu_act)(DEAL_TYPE, char *, uint32_t, uint32_t,int threadseq,void *,double);//驗證狀態回調動作typedef void(*s_stu_act)(DEAL_TYPE, char *, uint32_t, uint32_t,int threadseq,void *,double );//掃描狀態回調動作typedef void(*exception_act)(DEAL_TYPE,Exception_type, char *,int threadseq);//異常回調函數typedef void(*s_begin)(void * scan_builder,ERROR_CODE * errcode);//開始掃描typedef void(*s_end)(void * scan_builder,ERROR_CODE * errcode);//結束掃描typedef void(*v_begin)(void * vertify_builder,ERROR_CODE * errcode);//開始驗證typedef void(*v_end)(void * vertify_builder,ERROR_CODE * errcode); //結束驗證------------------------------------------------------------------------------用法:int main(){ SCAN_BUILDER * ntpscaner; ntpscaner = scan_new(scan_cfgntp,&errnp); //調用註冊函數 if_scan_status_reg(ntpscaner,status_scan,&errnp,&tomcat);//回調函數是庫的使用者在本地定義的 if_scan_fini_reg(ntpscaner,finish_scan,&errnp,&tomcat); if_scan_exception_reg(ntpscaner,scan_exception,&errnp); ntpscaner->begin(ntpscaner,&errnp); destroy_scanbuilder(ntpscaner); ntpscaner = NULL;}----------------------------------------------------庫的作者SCAN_BUILDER * scan_new(SCAN_CFG scan_cfg,ERROR_CODE * error_code){ //創建掃描器對象指針 char localip[100]={0}; char devname[100]={0}; get_local_ip(localip,devname); int link = check_nic(devname); if (link) { printf("interface: %s is up!
", devname); } else { printf("interface: %s is down!
", devname); if(error_code != NULL) { *error_code = ERR_ETH_DISABLE; return NULL; } return NULL; } //做指針轉換 返回對象 SCAN_BUILDER * scan_builder = (SCAN_BUILDER *)malloc(sizeof(SCANER_OBJ)); if(scan_builder == NULL) { if(error_code != NULL) { *error_code = ERR_SCAN_NEW; } return scan_builder; } memset(scan_builder, 0x0, sizeof(SCANER_OBJ)); //填充掃描器欄位 scan_builder->cfg_msg = scan_cfg; scan_builder->begin = scan_start; scan_builder->end = scan_terminate; if(error_code != NULL) { *error_code = ERR_SUCC; } return scan_builder;}void scan_start(void * scan_builder,ERROR_CODE * errcode){ SCANER_OBJ * pscaner = (SCANER_OBJ *)scan_builder;//指針轉換 //logic code}
推薦閱讀:

為什麼你聽了許多的道理,卻依然過不好這一生? ——說說上凸增函數與邊際效用遞減原理
一起認識COUNTIF函數(應用篇)
一起認識SUMIF函數
你會用Sum函數嗎?
集齊Vlookup函數 Lookup函數的全部用法

TAG:函數 | 模板 |