From f600bb176c1d2f0eeb5180637bdc09605b3d21bd Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期三, 14 十二月 2022 14:58:28 +0800 Subject: [PATCH] restruct for easy use v1.1 --- message.h | 177 +++++++++++++++++++++++++++++++--------------------------- 1 files changed, 94 insertions(+), 83 deletions(-) diff --git a/message.h b/message.h index e22be44..aa5b703 100644 --- a/message.h +++ b/message.h @@ -3,42 +3,20 @@ #include <stddef.h> -struct cstr{ - char* str; // 瀛楃涓插唴瀹� - size_t size; // 瀛楃涓查暱搴� -}; -// 杩涚▼淇℃伅 -struct cproc{ - struct cstr name; // 杩涚▼鍚嶅瓧 - struct cstr id; // 杩涚▼id - struct cstr info; // 杩涚▼鍏朵粬淇℃伅锛岀洰鍓嶆病鐢� -}; +/* + 娉ㄥ唽杩涚▼鐨刾roc淇℃伅锛屽寘鎷繘绋嬪悕[name], 杩涚▼[id],绛� +*/ +typedef struct cproc cproc; +/* + 杩涚▼娉ㄥ唽淇℃伅, 鍖呮嫭杩涚▼鐨刾roc淇℃伅 cproc + 寰呮敞鍐岀殑鏈繘绋嬫彁渚涜姹傚搷搴旂殑涓婚 topics[s], char** rep, 涓婚鏁伴噺 repcnt + 寰呮敞鍐岀殑鏈繘绋嬪彂甯冪殑涓婚 topics[s], char** pub, 涓婚鏁伴噺 pubcnt + 寰呮敞鍐岀殑鏈繘绋嬭闃呯殑涓婚 topics[s], char** sub, 涓婚鏁伴噺 subcnt + 寰呮敞鍐岀殑鏈繘绋嬭闃呯殑[缃戠粶銆佽繙绋嬩富鏈篯涓婚 topics[s], char** subnet, 涓婚鏁伴噺 subnetcnt +*/ +typedef struct creg creg; -struct cstrarr{ - struct cstr* arr; // 瀛楃涓叉暟缁� - size_t count; // 瀛楃涓叉暟缁勯暱搴� -}; -// 杩涚▼娉ㄥ唽淇℃伅 -struct creg{ - struct cproc* pinfo; // 闇�瑕佹敞鍐岀殑杩涚▼淇℃伅 - - cstrarr channel; // 杩涚▼鎻愪緵鐨勮姹傚搷搴旀湇鍔$殑涓婚 - cstrarr topic_pub; // 杩涚▼鎻愪緵鐨勫彂甯冭闃呯殑鍙戝竷涓婚 - cstrarr topic_sub; // 杩涚▼闇�瑕佽闃呯殑涓婚 - cstrarr topic_sub_net; // 杩涚▼闇�瑕佽闃呯殑缃戠粶涓婚锛岀洰鍓嶆病鐢� -}; - -// 鍏朵粬杩涚▼淇℃伅 -struct cclient{ - struct creg* rinfo; // 浠h〃鍏朵粬杩涚▼鐨勮繘绋嬩俊鎭� - - int replykey; // 娌$敤锛屼笂涓�涓増鏈敤鍏变韩鍐呭瓨锛屾涓簁ey - int hbcnt; // 蹇冭烦娆℃暟锛熷彲鑳芥病鐢� - int dcnt; // deadcount锛屽彲鑳芥病鐢紵 - int status; // 杩涚▼鐘舵�侊紝鍙兘娌$敤锛� -}; - -//TableChanged enum +//TableChanged enum pollcontrol 鎺ユ敹鍒扮殑璁㈤槄娑堟伅锛屾暟鎹簱鐨勫彉鍖� enum TableChanged { T_Camera = 0, //鎽勫儚鏈哄彉鍖� T_CameraRule = 1, //鎽勫儚鏈轰换鍔″弬鏁板彉鍖� @@ -58,6 +36,7 @@ T_CameraPolygonRelation = 15, //鎽勫儚鏈哄尯鍩熺殑鍏宠仈鍏崇郴 T_Voice = 16, //鎶ヨ澹伴煶鍙戠敓鍙樺寲 }; +// pollcontrol 鎺ユ敹鍒扮殑璁㈤槄娑堟伅锛屾暟鎹簱鐨勬搷浣� enum DbAction { Insert = 0, //Insert db Update = 1, //Update db @@ -66,19 +45,33 @@ // 鏁版嵁搴撳彉鍖栦俊鎭� struct DbChangeMsg{ - TableChanged table; //鍙樺寲鐨勮〃 - struct cstr id; //鍙樺寲鏁版嵁id + TableChanged table; //鍙樺寲鐨勮〃 + char* id; //鍙樺寲鏁版嵁id + size_t idl; // id len DbAction action; //action/ DbAction[Insert/Update/Delete] - struct cstr info; //鍙樺寲鍐呭 + char* info; //鍙樺寲鍐呭 + size_t infol; }; -// 璁㈤槄娑堟伅 +// 璁㈤槄娑堟伅, 鍖呮嫭鏁版嵁搴撴秷鎭拰鍏朵粬杩涚▼鐨勬秷鎭� struct csubmsg{ - struct cstr topic; // 鏀跺埌鐨勮闃呮秷鎭殑涓婚锛屽尯鍒嗛偅绉嶈闃呮秷鎭� + char* topic; // 鏀跺埌鐨勮闃呮秷鎭殑涓婚锛屽尯鍒嗛偅绉嶈闃呮秷鎭� + size_t topicl; // private // enum MsgT {NONE=0, DB, PROCLIST} type; - struct cstr msg; // 鏀跺埌鐨勮闃呮秷鎭殑body + char* msg; // 鏀跺埌鐨勮闃呮秷鎭殑body + size_t msgl; +}; + +// 鍏朵粬杩涚▼淇℃伅, pollcontrol鍙兘渚濊禆鍏朵粬杩涚▼鐨勬湇鍔★紝鍙兘闇�瑕佺瓑鍏朵粬杩涚▼鍚姩鍐嶅紑濮嬪伐浣� +struct cclient{ + creg* rinfo; // 浠h〃鍏朵粬杩涚▼鐨勮繘绋嬩俊鎭� + + int replykey; // 娌$敤锛屼笂涓�涓増鏈敤鍏变韩鍐呭瓨锛屾涓簁ey + int hbcnt; // 蹇冭烦娆℃暟锛熷彲鑳芥病鐢� + int dcnt; // deadcount锛屽彲鑳芥病鐢紵 + int status; // 杩涚▼鐘舵�侊紝鍙兘娌$敤锛� }; // 鍏朵粬娉ㄥ唽杩涚▼鍒楄〃锛屽鏁版嵁搴撹繘绋嬮渶瑕佸惎鍔ㄥ啀杩愯pollcontrol閫昏緫 @@ -94,16 +87,20 @@ 鎺ユ敹request娑堟伅浼氬甫鏈夊彂鍑簉equest娑堟伅鐨勮繘绋嬬殑id锛宲rocid */ struct creqmsg{ - struct cstr procid; // 鍙戦�乺equest娑堟伅鐨勮繘绋媔d - struct cstr msg; // request娑堟伅浣� + char* procid; // 鍙戦�乺equest娑堟伅鐨勮繘绋媔d + size_t procidl; + char* msg; // request娑堟伅浣� + size_t msgl; }; // decode stack err msg /* pollcontrol浼氭帴鏀舵暟鎹爤瑙g爜鍙戦�佺殑瑙g爜澶辫触娑堟伅锛岀敤浜庣疆缃戦〉鐘舵�� */ struct cstackmsgerr{ - struct cstr stackid; // 瑙g爜澶辫触鐨勬暟鎹爤id - struct cstr fileid; // 瑙g爜澶辫触鐨勬枃浠秈d + char* stackid; // 瑙g爜澶辫触鐨勬暟鎹爤id + size_t stackidl; + char* fileid; // 瑙g爜澶辫触鐨勬枃浠秈d + size_t fileidl; }; // stack file /* @@ -111,9 +108,13 @@ 鐪嬩唬鐮佸簲璇ュ彧闇�瑕佽В鏋愬嚭鐨勫嚑涓� */ struct cstackfile{ - struct cstr id; // 鏂囦欢id - struct cstr name; // 鏂囦欢name - struct cstr path; // 鏂囦欢璺緞 + char* id; // 鏂囦欢id + size_t idl; + char* name; // 鏂囦欢name + size_t namel; + char* path; // 鏂囦欢璺緞 + size_t pathl; + int type; // 鏂囦欢绫诲瀷 1锛歷ideo锛�2锛歱icture void* noused; // 鏈娇鐢� }; @@ -123,8 +124,10 @@ */ struct cstackmsg{ int procnum; // decoder 鍚姩鐨勮繘绋嬪彿锛屾暟鎹爤鍙兘浼氭湁鏁颁釜decoder鍚屾椂杩愯 - struct cstr stackid; // 鏁版嵁鏍� id - struct cstr stackname; // 鏁版嵁鏍� name + char* stackid; // 鏁版嵁鏍� id + size_t stackidl; + char* stackname; // 鏁版嵁鏍� name + size_t stacknamel; int type; // 鏁版嵁鏍堢被鍨� video picture int shmkey; // 鏁版嵁鏍堜娇鐢ㄧ殑鍏变韩鍐呭瓨key int width; // 鍒嗚鲸鐜� @@ -138,45 +141,59 @@ // 瀵瑰簲 bhome_msg.MsgRequestTopicReply struct crepmsg{ int errcode; // 鐩稿簲request璇锋眰鐨勬秷鎭紝閿欒鐮� - struct cstr errmsg; // 閿欒娑堟伅 - struct cstr data; // 娑堟伅浣� + char* errmsg; // 閿欒娑堟伅 + size_t errmsgl; + char* data; // 娑堟伅浣� + size_t datal; }; #ifdef __cplusplus extern "C"{ #endif -/* - 灏佽浜咰鎺ュ彛鐨剆tring - cstr_new 鍒涘缓涓�涓猻tring锛屽寘鎷唴瀛樺湴鍧�鍜岄暱搴︼紝浼氭嫹璐濆弬鏁� - 蹇呴』浣跨敤cstr_free閲婃斁 -*/ -struct cstr cstr_new(const char* str, const size_t len); -void cstr_free(struct cstr str); -/* - 灏佽瀛楃涓叉暟缁勶紝鍏朵腑鏄竴涓猻truct cstr鏁扮粍锛屽寘鎷寚鍚戞暟缁勭殑鎸囬拡鍜宑ount - 閫氳繃cstr_arr_add娣诲姞瀛楃涓诧紝鍐呴儴浼氭嫹璐濆瓧绗︿覆 - 蹇呴』浣跨敤cstr_arr_free閲婃斁 -*/ -struct cstrarr cstr_arr_new(const size_t count); -void cstr_arr_add(struct cstrarr* arr, const char* data, const size_t len, const size_t idx); -void cstr_arr_free(struct cstrarr arr); /* - 鍒涘缓struct cproc 缁撴瀯锛屽搴攑rocinfo锛屼繚瀛榩roc鐨刵ame锛宨d锛宨nfo[褰撳墠娌℃湁浣跨敤] - 蹇呴』浣跨敤free_proc_info閲婃斁 + 鍐呴儴浣跨敤锛屾繁鎷疯礉 cproc */ -struct cproc* make_proc_info(const struct cstr name, const struct cstr id, const struct cstr info); +cproc* internal_clone_cproc(const cproc* proc); +void internal_cproc_free(cproc* proc); /* - 浠庡凡瀛樺湪鐨刾roc鍏嬮殕锛屼細鎷疯礉锛屼娇鐢╢ree_proc_info閲婃斁 + 杩斿洖涓�涓� cproc 缁撴瀯锛屼粎鎷疯礉杈撳叆鍙傛暟 + 鐢熷懡鍛ㄦ湡 cproc < name[id] */ -struct cproc* clone_proc_info(const struct cproc* pi); -void free_proc_info(struct cproc* pi); +cproc* make_cproc(const char* name, const char* id); +/* + 浠庡凡瀛樺湪鐨� cproc 涓幏鍙� name 鍜� id锛屼粎浠呮槸 cproc 鍐呴儴鐨勫紩鐢紝鏃犻渶閲婃斁 + 鐢熷懡鍛ㄦ湡 name[id] < cproc +*/ +char* cproc_name(const cproc* proc); +char* cproc_id(const cproc* proc); +/* + 閲婃斁 cproc 鎸囬拡 +*/ +void cproc_free(cproc* proc); /* - 閲婃斁creg缁撴瀯鎸囬拡 - creg缁撴瀯鍙互浣跨敤涓婅堪make_proc_info銆乧str_arr_new銆乧str_new鍑芥暟鍒涘缓 + 杩斿洖涓�涓� creg 缁撴瀯锛屼粎鎷疯礉杈撳叆鍙傛暟 + 杈撳叆鍙傛暟鐢熷懡鍛ㄦ湡闇�瑕嗙洊杩斿洖鍊肩敓鍛藉懆鏈� */ -void free_creg(struct creg* reg); +creg* make_creg(const cproc* proc, const char** rep, const size_t repcnt, + const char** pub, const size_t pubcnt, + const char** sub, const size_t subcnt, + const char** subnet, const size_t subnetcnt); +/* + 鑾峰彇宸插瓨鍦ㄧ殑 creg 涓殑 cproc 鎴栬�呮敞鍐岀殑涓婚锛屾棤闇�閲婃斁 + 鐢熷懡鍛ㄦ湡 < creg +*/ +const cproc* creg_proc(const creg* reg); +char** creg_rep_topic(const creg* reg, size_t* count); +char** creg_pub_topic(const creg* reg, size_t* count); +char** creg_sub_topic(const creg* reg, size_t* count); +char** creg_subnet_topic(const creg* reg, size_t* count); +/* + 閲婃斁 creg 鎸囬拡 +*/ +void creg_free(creg* reg); + // 璁㈤槄娑堟伅鐩稿叧锛岃闃呮暟鎹簱db娑堟伅鍜岃繘绋嬪垪琛╬roclist娑堟伅 /* cbhomeclient.cpp涓娇鐢紝灏嗘帴鏀跺埌鐨剆ubmsg瑙e寘鎴恈submsg @@ -248,14 +265,8 @@ // reply msg /* - no use 灏唕eply娑堟伅搴忓垪鍖栦负json锛岀洰鍓嶆病鏈変娇鐢� - 浣跨敤 cstr_free 閲婃斁 -*/ -struct cstr make_reply_msg_json(const int success, const char* msg, const size_t msgl, - const char* data, const size_t datal); -/* 鍒涘缓 creqmsg 鍖呮嫭errcode銆乪rrmsg鍜屾秷鎭綋data - 浣跨敤 free_reply_msg 閲婃斁 + 蹇呴』浣跨敤 free_reply_msg 閲婃斁 */ struct crepmsg* make_reply_msg(const int errcode, const char* errmsg, const size_t emsgl, const char* data, const size_t datal); -- Gitblit v1.8.0