Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1040210
  • 博文数量: 46
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1701
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-24 10:06
文章分类
文章存档

2014年(19)

2013年(27)

分类: C/C++

2014-03-03 17:31:27



easymc是一个C语言开发的简易的消息通道库,目前提供请求/回应、订阅/发布两种通信模式,同机器的通信采取IPC方式,跨机的通信采取TCP方式,源码包含开源项目jemalloc库。


项目主页:

代码地址:


local端使用例子:

struct para{
   int device;
   int exit;
};

static emc_result_t EMC_CALL OnRecvMsg(void *p){
   void *msg=NULL;
   struct para *pa=(struct para *)p;
   int device=pa->device;
   while(!pa->exit){
       if(0==emc_recv(device,(void **)&msg)){
//            printf("recv length=%ld\n",emc_msg_length(msg));
           if(EMC_SUB==emc_msg_get_mode(msg)){
               emc_msg_set_mode(msg,EMC_PUB);
           }else if(EMC_REQ==emc_msg_get_mode(msg)){
               emc_msg_set_mode(msg,EMC_REP);
           }
           emc_send(device,msg,0);
           emc_msg_free(msg);
       }
   }
   printf("OnRecvMsg exit\n");
   return 0;
}

static emc_result_t EMC_CALL OnMonitorDevice(void *p){
   struct para *pa=(struct para *)p;
   int device=pa->device;
   struct monitor_data data={0};
   while(!pa->exit){
       if(0==emc_monitor(device,&data)){
           switch(data.events){
           case EMC_EVENT_ACCEPT:
               printf("client connected server,ip=%s,port=%ld,id=%ld\n",data.ip,data.port,data.id);
               break;
           case EMC_EVENT_CLOSED:
               printf("client disconnected server,ip=%s,port=%ld,id=%ld\n",data.ip,data.port,data.id);
               break;
           case EMC_EVENT_SNDSUCC:
//                printf("server send successful,ip=%s,port=%ld,id=%ld\n",data.ip,data.port,data.id);
               break;
           case EMC_EVENT_SNDFAIL:
//                printf("server send failed,ip=%s,port=%ld,id=%ld\n",data.ip,data.port,data.id);
               break;
           }
       }
   }

   return 0;
}

int main(int argc, char* argv[]){
   struct para pa={0};
   int monitor=1,ch=0;
   int device=emc_device();
   pa.device=device;
   pa.exit=0;
   emc_set(device,EMC_OPT_MONITOR|EMC_OPT_CONTROL,&monitor,sizeof(int));
   printf("Input a port to bind:");
   scanf("%ld",&ch);
   if(emc_bind(device,NULL,ch) < 0){
       printf("emc_bing fail\n");
   }
   emc_thread(OnRecvMsg,(void *)&pa);
   emc_thread(OnMonitorDevice,(void *)&pa);
   printf("Input C or c to close a connection,Q or q to exit\n");
   while(1){
       ch=getchar();
       if('C'==ch || 'c'==ch){
           printf("Input connection id:");
           scanf("%ld",&ch);
           emc_control(device,ch,EMC_CTL_CLOSE);
       }else if('Q'==ch || 'q'==ch){
           break;
       }
   }
   getchar();
   getchar();
   pa.exit=1;
   emc_destory(device);
   getchar();
   return 0;
}


remote端使用例子:

struct para{
   int device;
   int exit;
};

static emc_result_t EMC_CALL OnRecvMsg(void *p){
   struct para *pa=(struct para *)p;
   int device=pa->device;
   void *msg=NULL;
   while(!pa->exit){
       if(0==emc_recv(device,(void **)&msg)){
           printf("recv length=%ld\n",emc_msg_length(msg));
           emc_msg_free(msg);
       }
   }
   return 0;
}

static emc_result_t EMC_CALL OnMonitorDevice(void *p){
   struct para *pa=(struct para *)p;
   int device=pa->device;
   struct monitor_data data={0};
   while(!pa->exit){
       if(0==emc_monitor(device,&data)){
           switch(data.events){
           case EMC_EVENT_CONNECT:
               printf("client connected server,ip=%s,port=%ld,id=%ld\n",data.ip,data.port,data.id);
               break;
           case EMC_EVENT_CLOSED:
               printf("client disconnected server,ip=%s,port=%ld,id=%ld\n",data.ip,data.port,data.id);
               break;
           case EMC_EVENT_SNDSUCC:
               printf("server send successful,ip=%s,port=%ld,id=%ld\n",data.ip,data.port,data.id);
               break;
           case EMC_EVENT_SNDFAIL:
               printf("server send failed,ip=%s,port=%ld,id=%ld\n",data.ip,data.port,data.id);
               break;
           }
       }
   }

   return 0;
}

int main(int argc, char* argv[]){
   int ch=0;int device=-1;
   char ip[16]={0};
   int monitor=1,length=0,port=0;
   void *msg=NULL;void *msg_=NULL;
   struct para pa={0};

   device=emc_device();
   pa.device=device;
   pa.exit=0;
   printf("Input serve ip:");
   scanf("%s",ip);
   printf("Input server port:");
   scanf("%ld",&port);
   emc_thread(OnMonitorDevice,(void *)&pa);
   emc_set(device,EMC_OPT_MONITOR,&monitor,sizeof(int));
   printf("Input mode(1-req,8-sub):");
   scanf("%ld",&ch);
   emc_connect(device,ch,ip,port);
   emc_thread(OnRecvMsg,(void *)&pa);
   printf("Input send data length[Bytes]:");
   scanf("%ld",&length);
   if(EMC_REQ==ch){
       printf("You choose REQREP mode,type S or s to send data and type Q or q to quit\n");
       while(1){
           ch=getchar();
           if('S'==ch || 's'==ch){
               msg=emc_msg_alloc(NULL,length);
               emc_msg_set_mode(msg,EMC_REQ);
               emc_send(device,msg,0);
               emc_msg_free(msg);
           }else if('Q'==ch || 'q'==ch){
               pa.exit=1;
               break;
           }
       }
   }else if(EMC_SUB==ch){
       printf("You choose PUBSUB mode,type S or s to send data and type Q or q to quit\n");
       while(1){
           ch=getchar();
           if('S'==ch || 's'==ch){
               msg=emc_msg_alloc(NULL,length);
               emc_msg_set_mode(msg,EMC_SUB);
               emc_send(device,msg,0);
               emc_msg_free(msg);
           }else if('Q'==ch || 'q'==ch){
               pa.exit=1;
               break;
           }
       }
   }
   getchar();
   getchar();
   pa.exit=1;
   emc_destory(device);
   return 0;
}

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