From 8284df1d749fa7adb334fe4f43da77bfc9c05a71 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 24 十二月 2020 11:35:02 +0800
Subject: [PATCH] add error message method

---
 src/socket/shm_socket.c        |   20 +++++-
 src/bus_error.c                |   77 +++++++++++++++++++++++++
 src/socket/bus_server_socket.c |    3 
 src/socket/shm_socket.h        |    7 --
 src/bus_error.h                |   15 +++++
 test_net_socket/heart_beat.c   |   16 ++---
 6 files changed, 116 insertions(+), 22 deletions(-)

diff --git a/src/bus_error.c b/src/bus_error.c
new file mode 100644
index 0000000..d94e129
--- /dev/null
+++ b/src/bus_error.c
@@ -0,0 +1,77 @@
+#include "usg_common.h"
+#include "bus_error.h"
+
+#define MAX_ERROR_LEN 256           /* Maximum length of string in per-thread
+                                       buffer returned by strerror() */
+#define _bus_nerr  256
+
+int bus_errno;
+
+static pthread_once_t once = PTHREAD_ONCE_INIT;
+static pthread_key_t strerrorKey;
+
+
+static char *_bus_errlist[_bus_nerr] = {
+  "\0",
+  "timeout",
+  "the other end is not inline"
+
+};
+
+static void                         /* Free thread-specific data buffer */
+destructor(void *buf)
+{
+  free(buf);
+}
+
+static void                         /* One-time key creation function */
+createKey(void)
+{
+  int s;
+
+  /* Allocate a unique thread-specific data key and save the address
+     of the destructor for thread-specific data buffers */
+
+  s = pthread_key_create(&strerrorKey, destructor);
+  if (s != 0)
+    err_exit(s, "pthread_key_create");
+}
+
+char *
+bus_strerror(int err)
+{
+  int s;
+  char *buf;
+
+  /* Make first caller allocate key for thread-specific data */
+
+  s = pthread_once(&once, createKey);
+  if (s != 0)
+    err_exit(s, "pthread_once");
+
+  buf = (char *)pthread_getspecific(strerrorKey);
+  if (buf == NULL)
+  {
+    /* If first call from this thread, allocate
+                                   buffer for thread, and save its location */
+    buf = (char *)malloc(MAX_ERROR_LEN);
+    if (buf == NULL)
+      err_exit(errno, "malloc");
+
+    s = pthread_setspecific(strerrorKey, buf);
+    if (s != 0)
+      err_exit(s, "pthread_setspecific");
+  }
+
+  if (err < 0 || err >= _bus_nerr || _bus_errlist[err] == NULL)
+  {
+    snprintf(buf, MAX_ERROR_LEN, "Unknown error %d", err);
+  }
+  else
+  {
+    strncpy(buf, _bus_errlist[err], MAX_ERROR_LEN - 1);
+    buf[MAX_ERROR_LEN - 1] = '\0';          /* Ensure null termination */
+  }
+
+  return buf;
+}
diff --git a/src/bus_error.h b/src/bus_error.h
new file mode 100644
index 0000000..af7426a
--- /dev/null
+++ b/src/bus_error.h
@@ -0,0 +1,15 @@
+#ifndef __BUS_ERROR_H__
+#define __BUS_ERROR_H__
+
+
+
+
+
+#define EBUS_TIMEOUT 1
+#define EBUS_CLOSED 2
+
+extern int bus_errno;
+
+char *bus_strerror(int eno) ;
+
+#endif
\ No newline at end of file
diff --git a/src/socket/bus_server_socket.c b/src/socket/bus_server_socket.c
index 0f4e52e..9bd61b0 100644
--- a/src/socket/bus_server_socket.c
+++ b/src/socket/bus_server_socket.c
@@ -1,6 +1,7 @@
 
 #include "bus_server_socket.h"
 #include "shm_mod_socket.h"
+#include "bus_error.h"
 
 static Logger *logger = LoggerFactory::getLogger();
 
@@ -212,7 +213,7 @@
 		for(set_iter = subscripter_set->begin(); set_iter != subscripter_set->end(); set_iter++) {
 			send_key = *set_iter;
  // printf("_proxy_pub send before %d \n", send_key);
-			if (shm_sendto(shm_socket, buf, size, send_key, &timeout) == SHM_SOCKET_ECONNFAILED ) {
+			if (shm_sendto(shm_socket, buf, size, send_key, &timeout) == EBUS_CLOSED ) {
 				//瀵规柟宸插叧闂殑杩炴帴鏀惧埌寰呭垹闄ら槦鍒楅噷銆傚鏋滅洿鎺ュ垹闄や細璁﹊ter鎸囬拡鍑虹幇閿欎贡
 				subscripter_to_del.push_back(send_key);
 			} else {
diff --git a/src/socket/shm_socket.c b/src/socket/shm_socket.c
index c1ac3c8..f53b520 100644
--- a/src/socket/shm_socket.c
+++ b/src/socket/shm_socket.c
@@ -2,6 +2,7 @@
 #include "hashtable.h"
 #include "logger_factory.h"
 #include <map>
+#include "bus_error.h"
 
 static Logger *logger = LoggerFactory::getLogger();
 
@@ -323,8 +324,9 @@
 
   SHMQueue<shm_msg_t> *remoteQueue;
   if ((remoteQueue = _attach_remote_queue(key)) == NULL) {
-  	logger->error( "shm_sendto failed, the other end has been closed, or has not been opened!");
-    return SHM_SOCKET_ECONNFAILED;
+    bus_errno = EBUS_CLOSED;
+    logger->error("sendto key %d failed, %s", key, bus_strerror(bus_errno));
+    return EBUS_CLOSED;
   }
 
   shm_msg_t dest;
@@ -351,8 +353,16 @@
   } else {
     delete remoteQueue;
     mm_free(dest.buf);
-    logger->error(errno, "sendto key %d failed!", key);
-    return -1;
+    if(errno == EAGAIN) {
+      bus_errno = EBUS_TIMEOUT;
+      logger->error("sendto key %d failed, %s", key, bus_strerror(bus_errno));
+      return EBUS_TIMEOUT;
+    } else {
+      logger->error(errno, "sendto key %d failed!", key);
+      return -1;
+    }
+   
+   
   }
 }
 
@@ -532,7 +542,7 @@
 
   hashtable_t *hashtable = mm_get_hashtable();
   if (hashtable_get(hashtable, key) == NULL) {
-    logger->error("shm_socket._remote_queue_attach锛歝onnet at key %d  failed!", key);
+    //logger->error("shm_socket._remote_queue_attach锛歝onnet at key %d  failed!", key);
     return NULL;
   }
 
diff --git a/src/socket/shm_socket.h b/src/socket/shm_socket.h
index cf6737f..8667796 100644
--- a/src/socket/shm_socket.h
+++ b/src/socket/shm_socket.h
@@ -11,13 +11,6 @@
   SHM_MSG_NOWAIT = 2
 };
 
-
-
-enum shm_socket_error_type_t {
-	SHM_SOCKET_ECONNFAILED = 1,
-	SHM_SOCKET_ETIMEOUT = 2	
-};
-
 enum shm_connection_status_t {
 	SHM_CONN_CLOSED=1,
 	SHM_CONN_LISTEN=2,
diff --git a/test_net_socket/heart_beat.c b/test_net_socket/heart_beat.c
index 562cb23..d7f5ed3 100644
--- a/test_net_socket/heart_beat.c
+++ b/test_net_socket/heart_beat.c
@@ -28,14 +28,12 @@
   char sendbuf[512];
   int rv;
   int remote_port;
-  while (true) {
-    if ((rv =  net_mod_socket_recvfrom(serv, &recvbuf, &size, &remote_port) ) == 0) {
-      printf( "RECEIVED HREARTBEAT FROM %d: %s\n", remote_port, recvbuf);
-      net_mod_socket_sendto(serv, "suc", strlen("suc")+1, remote_port);
-      free(recvbuf);
-    }
-    
+  while (net_mod_socket_recvfrom(serv, &recvbuf, &size, &remote_port) == 0) {
+    printf( "RECEIVED HREARTBEAT FROM %d: %s\n", remote_port, recvbuf);
+    net_mod_socket_sendto(serv, "suc", strlen("suc")+1, remote_port);
+    free(recvbuf);
   }
+  // sleep(1000);
   net_mod_socket_close(serv);
 }
 
@@ -45,7 +43,7 @@
   int size;
   char sendbuf[512];
   long i = 0;
-  net_node_t node_arr[] = {"", 0, 100};
+  net_node_t node_arr[] = {"", 0, port};
   int node_arr_size = 1;
 
   int recv_arr_size;
@@ -53,7 +51,7 @@
   while (true) {
     sprintf(sendbuf, "%d", i);
     printf("SEND HEART:%s\n", sendbuf);
-    rv = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf)+1, NULL, NULL);
+    rv = net_mod_socket_sendandrecv_timeout(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf)+1, NULL, NULL, 1000);
    // sleep(1);
     i++;
   }

--
Gitblit v1.8.0