From 3ce014732763fd28a7b03d5ce99ec990f830f985 Mon Sep 17 00:00:00 2001
From: cheliequan <liequanche@126.com>
Date: 星期五, 30 十二月 2022 15:59:41 +0800
Subject: [PATCH] 1.客户端支持只获取memfd,只获取数据,同时获取memfd和数据 2.优化代码,所有日志使用mydebug

---
 sample/ipc_server.c |   99 +++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 82 insertions(+), 17 deletions(-)

diff --git a/sample/ipc_server.c b/sample/ipc_server.c
index 0d8178a..9aeab6a 100644
--- a/sample/ipc_server.c
+++ b/sample/ipc_server.c
@@ -5,11 +5,13 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
-
+#include <string.h>
+#include <pthread.h>
 #include "memfd.h"
 #include "ipc_msg.h"
 
-int g_memfd = 0;
+
+//typedef int (*readfunc)(struct user_data*);
 
 void handler(){
 
@@ -43,10 +45,9 @@
 
 
 
-int proc_memfd(struct user_data* rdata)
+int proc_msg(struct user_data* rdata, void * arg, int len)
 {
     int fd = rdata->fd;
-    unsigned int n_size;
     char line[MAX_LEN]= {0};
     int n = 0;
     int nwrite = 0;
@@ -58,21 +59,56 @@
       return -1;
     }
 
-    if(0 == strncmp(rdata->line, HELLO_MSG, strlen(HELLO_MSG)))
+
+    if(0 == strncmp(rdata->line, GET_MEMFD_MSG, strlen(GET_MEMFD_MSG)))
     {
-      memfd_data.memfd = g_memfd;
+      st_custom_data * pst_data = (st_custom_data *)(arg);
       memfd_data.pid = getpid();
+      memfd_data.memfd = pst_data->memfd;
       memfd_data.data = NULL;
       memfd_data.len = 0;
-      data_size = sizeof(memfd_data_st) + memfd_data.len;
+
+      data_size = sizeof(memfd_data_st);
       memcpy(line, &memfd_data, data_size);
+      n = data_size;      
     }
-    else
+    else if(0 == strncmp(rdata->line, GET_DATA_MSG, strlen(GET_DATA_MSG)))
     {
-      return  -1;
+      if((NULL == arg) || (0 == len))
+      {
+        return -1;
+      }
+
+      st_custom_data * pst_data = (st_custom_data *)(arg);
+      memfd_data.pid = 0;
+      memfd_data.memfd = 0;
+      memfd_data.data = pst_data->data;
+      memfd_data.len = pst_data->len;      
+      data_size = pst_data->len;
+      /*copy the arg to line,so it can send to client*/
+      memcpy(line, pst_data->data, data_size);
+      n = data_size;      
     }
-    
-    n = data_size;
+    else if(0 == strncmp(rdata->line, GET_CUSTOM_MSG, strlen(GET_CUSTOM_MSG)))
+    {
+      if((NULL == arg) || (0 == len))
+      {
+        return -1;
+      }
+      st_custom_data * pst_data = (st_custom_data *)(arg);
+      memfd_data.pid = getpid();
+      memfd_data.memfd = pst_data->memfd;
+      memfd_data.data = pst_data->data;
+      memfd_data.len = pst_data->len;
+      data_size = sizeof(memfd_data_st);
+      memcpy(line, &memfd_data, data_size);
+
+      memcpy(line+data_size, memfd_data.data, memfd_data.len);
+      data_size += memfd_data.len;
+      n = data_size;      
+    }
+
+    mydebug("[SERVER] writetask %u sending %d\n", (uint32_t)pthread_self(), rdata->fd);
     while(n>0)
     {
         nwrite = write(rdata->fd, line+data_size-n,n);////ET
@@ -85,12 +121,12 @@
             if (errno == ECONNRESET)
             {
                 close(rdata->fd);
-                printf("[SERVER] Error: send responce failed: %s\n", strerror(errno)); 
+                mydebug("[SERVER] Error: send responce failed: %s\n", strerror(errno)); 
             }
             else if (nwrite == 0)
             {
                 close(rdata->fd);
-                printf("[SERVER] Error: client closed connection.");                  
+                mydebug("[SERVER] Error: client closed connection.");                  
             }
             break;
         }
@@ -101,14 +137,16 @@
     
 }
 
-/*define you our proc_memfd funtion to send your message to other processes锛宱r just put it in shm锛�
+/*define you our proc_msg funtion to send your message to other processes锛�
  notice the file description fd and the pid of creator to other*/
 int main(int argc, char **argv)
 {
   char *name;
   ssize_t  len;
-  char * unixdomain = NULL;
+  char * unix_domain_path = NULL;
   int ret = 0;
+  args_data_st args_data_st={0};
+  int g_memfd = 0;
 
   if (argc < 3) {
     fprintf(stderr, "%s name size [unix domain path]\n", argv[0]);
@@ -117,21 +155,48 @@
 
   name = argv[1];
   len = atoi(argv[2]) * 1024LU * 1024LU * 1024LU;
-  unixdomain = argv[3];
 
   signal(SIGTERM,handler);
   signal(SIGABRT,handler);
   signal(SIGSEGV,handler);
   
  
+#if 1
   g_memfd = basic_shm_create(name, len);
   ret = shm_write(g_memfd);
 
-  ret = basic_create_ipc_server(unixdomain);
+  char data[256] = "test custom usedata\n";
+
+  st_custom_data st_data;
+  st_data.memfd = g_memfd;
+  st_data.data = data;
+  st_data.len = strlen(data)+1; 
+  args_data_st.wfunc = proc_msg;
+  args_data_st.args  = &st_data;
+  args_data_st.len  = sizeof(st_data)+st_data.len;
+
+  unix_domain_path=UNIX_DOMAIN;
+  ret = basic_create_ipc_server(unix_domain_path,&args_data_st);
+  if(ret < 0)
+  {
+     printf("failed to create ipc_server %s\n", unix_domain_path);      
+  }
+ 
+#endif
+
+#if 0
+  char send_buf[256] = "test custom usedata\n";
+  args_data_st.wfunc = proc_msg;
+  args_data_st.args  = &send_buf;
+  args_data_st.len  = strlen(send_buf);
+
+  unix_domain_path="/tmp/unix_domain_usedata";
+  ret = basic_create_ipc_server(unix_domain_path,&args_data_st);
   if(ret < 0)
   {
      printf("failed to create ipc_server\n");      
   }
+#endif
 
  // basic_shm_close(g_memfd);  
   return 0;

--
Gitblit v1.8.0