【原创】基于简单的内存数据库实现一个异步回调的通信...

[复制链接]
1721|9
 楼主| keer_zu 发表于 2020-4-14 10:26 | 显示全部楼层 |阅读模式
本帖最后由 keer_zu 于 2020-4-17 16:54 编辑

前面实现了一个简单的内存数据库,接下来就基于这个简单的内存数据库实现一个异步回调的通信框架。

zxcom


@紫剑

@dirtwillfly @海中水
@icecut @yyy71cj


参看之前帖子:


【原创】一个超简单的key-value内存数据库

992445e9520c45f0e6.png

库的使用实例:
main.c

  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include "ucomlib.h"
  4. #include <stdio.h>
  5. #include <sys/socket.h>
  6. #include <sys/un.h>
  7. #include <string.h>
  8. #include "zxcom.h"
  9. #include <pthread.h>
  10. #include <errno.h>
  11. #include <arpa/inet.h>
  12. #include <stdlib.h>
  13. pthread_mutex_t g_mutex;

  14. int server_fd = -1;
  15. int client_fd = -1;


  16. char * server_filename = "/tmp/socket-server";
  17. char * client_filename = "/tmp/socket-client";

  18. void ENTER_CRITICAL()
  19. {
  20.         pthread_mutex_lock(&g_mutex);
  21. }


  22. void EXIT_CRITICAL()
  23. {
  24.         pthread_mutex_unlock(&g_mutex);
  25. }

  26. int CRITICAL_INIT()
  27. {
  28.         pthread_mutex_init(&g_mutex, NULL);
  29. }

  30. int CRITICAL_DEINIT()
  31. {
  32.         pthread_mutex_destroy(&g_mutex);
  33. }

  34. void *thread_timer(void* arg)
  35. {
  36.         //printf("new thread,thread is :%u,pid:%d\n",pthread_self(),getpid());
  37.         while(1) {
  38.                 sleep(2);
  39.                 OnTimer();
  40.         }
  41. }

  42. struct sockaddr_un srv_un_s = {0};
  43. struct sockaddr_un srv_un_c = {0};


  44. void *thread_run_c(void* arg)
  45. {
  46.         int ret;
  47.         char packet[100],buf[100];
  48.         socklen_t addre_len;
  49.         int socketfd = *(int *)arg;
  50.         char *param = "hello";
  51.         //struct sockaddr_un srv_un = {0};        
  52.         struct sockaddr_un remote_un = {0};        

  53.         while (1) {
  54.                 sleep(2);
  55.                 ZxcomOnSendMsg(1,param,strlen(param),packet);
  56.                 if (sendto(socketfd,packet,strlen(param) + 4, 0,
  57.                         (struct sockaddr *)&srv_un_s, sizeof(srv_un_s)) == -1) {
  58.                         perror("send");
  59.                         close(socketfd);
  60.                         socketfd = -1;
  61.                         pthread_exit(NULL);
  62.                 }

  63.                 memset(buf, 0, sizeof(buf));
  64.                 ret = recvfrom(socketfd, buf, sizeof(buf), 0,
  65.                         (struct sockaddr*)&remote_un, &addre_len);
  66.                 if (ret == -1) {
  67.                         perror("error when recvfrom, ");
  68.                         continue;
  69.                         
  70.                 }
  71.                 ZxcomOnPacket(buf,ret);

  72.                 buf[ret] = 0;
  73.                 printf("cli ret:%d, contrl:0x%x    %s\n", ret,*(int*)buf,buf+4);
  74.         }
  75. }



  76. void *thread_run_s(void* arg)
  77. {
  78.         int ret;
  79.         int i =0;
  80.         socklen_t addre_len;
  81.         struct sockaddr_un remote_un = {0};        
  82.         char buf[100] = {0};
  83.         int server_fd = *(int *)arg;
  84.         for(;;) {
  85.                 ++i;
  86.                 memset(buf, 0, sizeof(buf));
  87.                 ret = recvfrom(server_fd, buf, sizeof(buf), 0,
  88.                         (struct sockaddr*)&remote_un, &addre_len);
  89.                 if (ret == -1) {
  90.                         perror("error when recvfrom, ");
  91.                         continue;
  92.                         
  93.                 }
  94.                 ZxcomOnPacket(buf,ret);

  95.                 buf[ret] = 0;
  96.                 printf("ret:%d, contrl:0x%x    %s\n", ret,*(int*)buf,buf+4);
  97.                 usleep(500000);
  98.         }   

  99.         //close(server_fd);
  100.         //server_fd = -1;
  101. }





  102. int cmd1_handler(void *param)   //server
  103. {
  104.         printf("handler param: %s\n",(char*)param);
  105.         //struct sockaddr_un srv_un = {0};
  106.         char *res = "go away";
  107.         char packet[100];
  108.         ZxcomOnSendResponse(1,res, strlen(res), packet);

  109.         if(server_fd < 0) {
  110.                 return -1;
  111.         }

  112.         if (sendto(server_fd,packet,strlen(res) + 4, 0,
  113.                         (struct sockaddr *)&srv_un_c, sizeof(srv_un_c)) == -1) {
  114.                         perror("send");
  115.                         close(server_fd);
  116.                         server_fd = -1;
  117.                         pthread_exit(NULL);
  118.         }
  119. }


  120. int response1_handler(void *param)
  121. {
  122.         printf("response handler param: %s\n",(char*)param);
  123. }


  124. int main(int argc, char **argv)
  125. {
  126.         int ret;
  127.         
  128.         pthread_t tid_timer,tid_run1,tid_run2;

  129.         char obuf[100];
  130.         //struct sockaddr_un srv_un_c = { 0 };
  131.         

  132.         
  133.         ZxcomInit();
  134.         ZxcomAddCommand(1,cmd1_handler);
  135.         ZxcomAddResponse(1,response1_handler);

  136.         srv_un_c.sun_family = AF_UNIX;
  137.         strncpy(srv_un_c.sun_path, client_filename, sizeof(srv_un_c.sun_path));

  138.         srv_un_s.sun_family = AF_UNIX;
  139.         strncpy(srv_un_s.sun_path, server_filename, sizeof(srv_un_s.sun_path));

  140.         

  141.         
  142.         

  143.         char mode = 's';
  144.         if (argc > 1)
  145.                 mode = argv[1][0];
  146.         if (mode == 's'){
  147.                 server_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  148.                 if (server_fd < 0) {
  149.                         printf("socket error %s errno: %d\n", strerror(errno), errno);
  150.                 }
  151.         
  152.                 unlink(srv_un_s.sun_path);

  153.                 if (bind(server_fd, (struct sockaddr *)&srv_un_s, sizeof(srv_un_s)) == -1) {
  154.                         printf("bind server err");
  155.                         exit(1);
  156.                 }

  157.                 pthread_create(&tid_run2,NULL,thread_run_s,&server_fd);
  158.         } else {
  159.                 client_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  160.                 if (client_fd < 0) {
  161.                         printf("socket error %s errno: %d\n", strerror(errno), errno);
  162.                 }
  163.                 unlink(srv_un_c.sun_path);
  164.                
  165.                 if (bind(client_fd, (struct sockaddr *)&srv_un_c, sizeof(srv_un_c)) == -1) {
  166.                         perror("bind client");
  167.                         exit(1);
  168.                 }
  169.                
  170.                 pthread_create(&tid_run1,NULL,thread_run_c,&client_fd);
  171.         }

  172.         pthread_create(&tid_timer,NULL,thread_timer,NULL);

  173.         while(1){
  174.                 sleep(10);
  175.         }
  176.         
  177.         return 0;
  178. }



debug已经完成,可以直接从github上拉去代码,编译,运行在linux上面。  

@dirtwillfly @icecut @yyy71cj

在linux下:

  1. git clone https://github.com/KevinZu/zxcom.git

  2. cd zxcom

  3. checkout -b develop

  4. git pull origin develop

  5. mkdir build

  6. cd build

  7. cmake ..

  8. make



然后执行服务端:

  1. ./main


再执行客户端:
  1. ./main c

服务端:





客户端:


  
icecut 发表于 2020-4-14 17:22 | 显示全部楼层
哈哈哈。浓浓的互联网味道。 用邮箱不能实现么?或者消息队列?
 楼主| keer_zu 发表于 2020-4-14 17:38 | 显示全部楼层
icecut 发表于 2020-4-14 17:22
哈哈哈。浓浓的互联网味道。 用邮箱不能实现么?或者消息队列?

要mcu也能用才行。

评论

@dirtwillfly 可以直接从github上拉去代码,编译,运行在linux上面。  发表于 2020-4-16 10:31
 楼主| keer_zu 发表于 2020-4-15 10:23 | 显示全部楼层
yyy71cj 发表于 2020-4-15 10:05
我想请教一个问题:给安卓手机编程,你用什么编程环境和工具?我正想转战手机编程,但是一直在选择用什么而 ...

你要做哪一层开发?
内核也是linux,开发当然用C语言。


如果做APP,那就是java,java我之前用eclipse或者myeclipse。

java是跨平台的,不必过多考虑平台的差异。
icecut 发表于 2020-4-16 10:04 | 显示全部楼层
keer_zu 发表于 2020-4-14 17:38
要mcu也能用才行。

ucos里面有吧
 楼主| keer_zu 发表于 2020-4-16 10:19 | 显示全部楼层

ucos不管通信吧
 楼主| keer_zu 发表于 2020-4-19 13:24 | 显示全部楼层
yyy71cj 发表于 2020-4-18 12:08
有没有类似VS这种的工具软件?

eclipse就是类似VS啊,用的人挺多的。
 楼主| keer_zu 发表于 2020-4-20 09:57 | 显示全部楼层
yyy71cj 发表于 2020-4-19 20:17
我试试。记得以前装过,需要配置不少东西,蛮繁琐的。

像eclipse,sublime这样的工具通常支持很多语言,针对特定语言和功能需要安装若干插件,通常安装很方便。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1488

主题

12953

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部