Chinaunix首页 | 论坛 | 博客
  • 博客访问: 568775
  • 博文数量: 117
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 359
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-13 21:58
个人简介

爱上香烟

文章分类

全部博文(117)

文章存档

2018年(3)

2017年(8)

2016年(65)

2015年(41)

我的朋友

分类: C/C++

2016-04-13 10:58:11


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "list.h"

  5. struct Handler_entry{
  6.     struct hlist_node hnode;
  7.     int handling;
  8.     int type;
  9.     void (*handler)(int type, int data_len, char *data);
  10. };

  11. typedef struct {
  12.     char *buf;
  13.     int offset;
  14.     unsigned short type;//max is 2^16=65535, if you want to bigger, change type to unsigned int
  15.     unsigned short len;
  16. }ControlMessag;

  17. #define FAIL 0
  18. #define SUCC 1
  19. #define TYPE1 1
  20. #define TYPE2 2
  21. #define TYPE3 3
  22. #define TYPE4 4
  23. #define UNKNOWN_TYPE 5
  24. #define HASH_SIZE 64

  25. struct msgHandlersHashTable{    
  26.     struct hlist_head table[HASH_SIZE];
  27.     pthread_mutex_t lock;
  28. };
  29. struct msgHandlersHashTable g_HandlersHash;

  30. typedef struct{
  31.     int type;
  32.     void (*handler)(int type, int data_len, char *data);
  33. }ElemMsgHandler;

  34. void handle_type1 (int type, int data_len, char *data);
  35. void handle_type2 (int type, int data_len, char *data);
  36. void handle_type3 (int type, int data_len, char *data);
  37. int addElemMsg(ControlMessag *msg, int type, int len, void *data);
  38. void handleElemMsg(int type, int data_len, char *data);
  39. int __handleMessage( char *recvbuf, int recvbuf_size, void (*handleElemMsg)(int type, int data_len, char *data));
  40. int handleMessage(ControlMessag *msg, void (*handleElemMsg)(int type, int data_len, char *data));
  41. int __regHandler(ElemMsgHandler msgHandler);
  42. int regHandler(int type, void (*handler )(int type, int data_len, char *data));
  43. int __expandMessage(ControlMessag *msg, int data_len);

  44. #define __DEBUG__
  45. #ifdef __DEBUG__
  46. #define DEBUG(format,...) printf("Fun: %s, Line: %d: "format"\n",__FUNCTION__, __LINE__, ##__VA_ARGS__)
  47. #else
  48. #define DEBUG(format,...)
  49. #endif
  50. void myAssertReport( const char *file_name, const char *function_name, unsigned int line_no ){
  51.     printf( "\n[EXAM]Error file_name: %s, function_name: %s, line %u\n"
  52.         , file_name, function_name, line_no);
  53.     abort();
  54. }
  55. #define myAssert(condition) \
  56. do{\
  57.     if(!condition)\
  58.         myAssertReport(__FILE__, __FUNCTION__, __LINE__);\
  59. }while(0)
  60. #define expandMessage(msg,len)    myAssert(SUCC == __expandMessage(msg,len))
  61. #define MessageMalloc(size) malloc(size)
  62. void MessageFree(ControlMessag *msg){
  63.     myAssert(msg);
  64.     if ( msg->buf){
  65.         free(msg->buf);
  66.         msg->buf = NULL;
  67.     }
  68.     return;
  69. }

  70. ElemMsgHandler MsgHandlers[] = {
  71.         {TYPE1, handle_type1},
  72.         {TYPE2, handle_type2},
  73.         {TYPE3, NULL},
  74. };

  75. int __expandMessage(ControlMessag *msg, int data_len){
  76.     myAssert(msg);
  77.     char *p = NULL;
  78.     int size = msg->offset + sizeof(msg->type) + sizeof(msg->len) + data_len;
  79.     p = (char *)MessageMalloc(size);
  80.     if (!p){
  81.         printf("===FUNC=%s err data_len=%d, offset=%d, size=%d===\n",__FUNCTION__, data_len, msg->offset, size);
  82.         return FAIL;
  83.     }
  84.     memset(p, 0, size);
  85.     memcpy(p, msg->buf, msg->offset);
  86.     MessageFree(msg);
  87.     msg->buf = p;
  88.     return SUCC;
  89. }

  90. void msgStroeType(ControlMessag *msg, int val){
  91.     memcpy(&(msg->buf[msg->offset]), (char *)&val, sizeof(msg->type));
  92.     (msg->offset) += sizeof(msg->type);
  93. }
  94. void msgStroeLen(ControlMessag *msg, int val){
  95.     memcpy(&(msg->buf[msg->offset]), (char *)&val, sizeof(msg->len));
  96.     (msg->offset) += sizeof(msg->len);
  97. }
  98. int msgStoreData(ControlMessag *msg, void *data, int data_len){
  99.     memcpy(&(msg->buf[msg->offset]), (char *)data, data_len);
  100.     (msg->offset) += data_len;
  101.     return SUCC;
  102. }

  103. int addElemMsg(ControlMessag *msg, int type, int len, void *data){
  104.     myAssert(msg);
  105.     if (len < 0 || (!data && len))
  106.         return FAIL;
  107.     expandMessage(msg, len);
  108.     msgStroeType(msg, type);
  109.     msgStroeLen(msg, len);
  110.     msgStoreData(msg, data, len);
  111.     return SUCC;
  112. }
  113. int msgEncapMsg(ControlMessag *to, int type, ControlMessag *from){
  114.     if(!to || !from){
  115.         return FAIL;
  116.     }
  117.     return addElemMsg(to, type, from->offset, from->buf);
  118. }
  119. void parseMessage(ControlMessag *msg){
  120.     myAssert(msg);
  121.     memcpy(&msg->type, &(msg->buf[msg->offset]), sizeof(msg->type));
  122.     (msg->offset) += sizeof(msg->type);
  123.     
  124.     memcpy(&msg->len, &(msg->buf[msg->offset]), sizeof(msg->len));
  125.     (msg->offset) += sizeof(msg->len);
  126.     return;
  127. }
  128. void showMsgData(ControlMessag *msg){
  129.     printf("func=%s, this elem type=%d,len=%d,val=%.*s\n\n", __FUNCTION__, msg->type, msg->len, msg->len, &msg->buf[msg->offset]);
  130. }

  131. void handle_type1 (int type, int data_len, char *data){
  132.     DEBUG("type=%d,len=%d,val=%.*s", type, data_len, data_len, data);
  133. }
  134. void handle_type2 (int type, int data_len, char *data){
  135.     DEBUG("type=%d,len=%d,val=%.*s", type, data_len, data_len, data);
  136. }
  137. void handle_type3 (int type, int data_len, char *data){
  138.     DEBUG("type=%d,len=%d", type, data_len);
  139.     
  140.     __handleMessage(data, data_len, NULL);
  141. }
  142. void handle_type4 (int type, int data_len, char *data){
  143.     DEBUG("type=%d,len=%d", type, data_len);
  144.     
  145.     __handleMessage(data, data_len, handle_type3);
  146. }
  147. void initHandlersTable(struct msgHandlersHashTable *handlerHashTable){
  148.     int i;
  149.     for (i = 0; i < sizeof(handlerHashTable->table); i++){
  150.         INIT_HLIST_HEAD(&handlerHashTable->table[i]);        
  151.     }
  152.     pthread_mutex_init(&handlerHashTable->lock, NULL);
  153. }

  154. void handlerHashLock(){
  155.     pthread_mutex_lock(&g_HandlersHash.lock);
  156. }
  157. void handlerHashUnlock(){
  158.     pthread_mutex_unlock(&g_HandlersHash.lock);
  159. }
  160. struct hlist_head *getHashChain(int type){
  161.     int hash_id = type & (HASH_SIZE-1);
  162.     return &g_HandlersHash.table[hash_id];
  163. }
  164. void hlistAddHandler(struct Handler_entry *entry){
  165.     struct hlist_head *chain = getHashChain(entry->type);
  166.     hlist_add_head(&entry->hnode, chain);
  167. }
  168. void hlistDelHandler(struct Handler_entry *entry){
  169.     hlist_del_init(&entry->hnode);
  170. }

  171. struct Handler_entry *findHandlerFromType(int type){
  172.     struct hlist_node *pos, *next;
  173.     struct Handler_entry *entry = NULL;
  174.     struct hlist_head *chain = getHashChain(type);
  175.     hlist_for_each_entry_safe(entry, pos, next, chain, hnode) {
  176.         if (entry->type == type){            
  177.             return entry;
  178.         }
  179.     }    
  180.     return NULL;
  181. }
  182. int regHandler(int type, void (*handler )(int type, int data_len, char *data)){
  183.     ElemMsgHandler h;
  184.     if (type < 0 || !handler)
  185.         return FAIL;
  186.     h.type = type;
  187.     h.handler = handler;
  188.     return __regHandler(h);
  189. }
  190. int __regHandler(ElemMsgHandler msgHandler){    
  191.     struct Handler_entry *entry = NULL;
  192.     handlerHashLock();
  193.     entry = findHandlerFromType(msgHandler.type);
  194.     if (entry){
  195.         entry->handler = msgHandler.handler;
  196.         handlerHashUnlock();
  197.         return SUCC;
  198.     }
  199.     entry = (struct Handler_entry *)malloc(sizeof(struct Handler_entry));
  200.     if (!entry)
  201.         return FAIL;
  202.     memset(entry, 0, sizeof(entry));
  203.     entry->type = msgHandler.type;
  204.     entry->handler = msgHandler.handler;
  205.     hlistAddHandler(entry);
  206.     handlerHashUnlock();
  207.     return SUCC;
  208. }

  209. void unregHandler(int type){
  210.     struct Handler_entry *entry = NULL;
  211.     handlerHashLock();
  212.     entry = findHandlerFromType(type);
  213.     if (entry){
  214.         hlistDelHandler(entry);
  215.         if(!entry->handling)
  216.             free(entry);
  217.     }    
  218.     handlerHashUnlock();
  219.     return;
  220. }

  221. void registerHandlers(ElemMsgHandler msgHandlers[]){
  222.     int i;
  223.     for(i = 0; i < sizeof(msgHandlers); i++){
  224.         if (msgHandlers[i].handler){
  225.             __regHandler(msgHandlers[i]);
  226.         }
  227.     }
  228. }

  229. void handleElemMsg(int type, int data_len, char *data){
  230.     struct Handler_entry *entry = NULL;
  231.     handlerHashLock();
  232.     entry = findHandlerFromType(type);
  233.     if (!entry){
  234.         handlerHashUnlock();
  235.         DEBUG(" no handler: unrecognise this type %d, data_len=%d \n", type, data_len);
  236.         return;
  237.     }
  238.     entry->handling = 1;
  239.     handlerHashUnlock();
  240.     if (entry->handler)
  241.         entry->handler(type, data_len, data);
  242.     handlerHashLock();
  243.     entry->handling = 0;
  244.     if (hlist_unhashed(&entry->hnode))
  245.         free(entry);
  246.     handlerHashUnlock();
  247. }

  248. int handleMessage(ControlMessag *msg, void (*handleElemMsg)(int type, int data_len, char *data)){
  249.     if (!msg){
  250.         DEBUG();
  251.         return FAIL;
  252.     }
  253.     return __handleMessage(msg->buf, msg->offset, handleElemMsg);
  254. }

  255. int __handleMessage( char *recvbuf, int recvbuf_size, void (*handleElemMsg)(int type, int data_len, char *data)){
  256.     DEBUG("recvbuf_size =%d", recvbuf_size);
  257.     if (!recvbuf || !recvbuf_size)
  258.         return FAIL;
  259.     ControlMessag msg;
  260.     memset(&msg, 0, sizeof(msg));
  261.     msg.buf = recvbuf;
  262.     msg.offset = 0;
  263.     int size = recvbuf_size;
  264.     while((size -= sizeof(msg.type)+sizeof(msg.len)) >= 0){
  265.         parseMessage(&msg);
  266.         if ((size -= msg.len) < 0){
  267.             printf(" size %d, this elem type=%d,len=%d, offset =%d \n", size , msg.type, msg.len, msg.offset);
  268.             return FAIL;
  269.         }
  270.         if (handleElemMsg)
  271.             handleElemMsg(msg.type, msg.len, &msg.buf[msg.offset]);
  272.         else{
  273.             //如果在此增加消息处理函数,类型越多,代码会越冗长
  274.             //最好是 处理函数采取注册方式加入
  275.             switch (msg.type){
  276.                 case TYPE1:
  277.                     //TODO:
  278.                     DEBUG(" type %d ",msg.type);
  279.                     showMsgData(&msg);
  280.                     break;
  281.                 case TYPE2:
  282.                     //TODO:
  283.                     DEBUG(" type %d ",msg.type);
  284.                     showMsgData(&msg);
  285.                     break;                
  286.                 default:
  287.                     DEBUG("default: unrecognise this type %d \n",msg.type);
  288.             }
  289.         }
  290.         msg.offset += msg.len;//jump data
  291.     }
  292.     return SUCC;
  293. }

  294. int main(){
  295.     ControlMessag msg;
  296.     ControlMessag msg_all;
  297.     ControlMessag msg_all_all;
  298.     memset(&msg, 0, sizeof(msg));
  299.     memset(&msg_all, 0, sizeof(msg_all));
  300.     memset(&msg_all_all, 0, sizeof(msg_all_all));
  301.     DEBUG();
  302.     initHandlersTable(&g_HandlersHash);
  303.     registerHandlers(MsgHandlers);
  304.     addElemMsg(&msg, TYPE1, 2, "abcd");
  305.     addElemMsg(&msg, TYPE2, 3, "abcd");
  306.     addElemMsg(&msg, UNKNOWN_TYPE, 3, "abcd");
  307.     
  308.     msgEncapMsg(&msg_all, TYPE3, &msg);
  309.     //send msg to ap, and then free msg
  310.     
  311.     regHandler(TYPE3, handle_type3);
  312.     handleMessage(&msg_all, handleElemMsg);
  313.     unregHandler(TYPE2);
  314.     handleMessage(&msg, handleElemMsg);

  315.     msgEncapMsg(&msg_all_all, TYPE4, &msg_all);
  316.     regHandler(TYPE4, handle_type4);
  317.     handleMessage(&msg_all_all, handleElemMsg);

  318.     MessageFree(&msg);
  319.     MessageFree(&msg_all);
  320.     MessageFree(&msg_all_all);
  321.     return 1;
  322. }


阅读(1456) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~