Browse Source

replace socketpair with pipe

ithewei 3 năm trước cách đây
mục cha
commit
5bc99d454f
5 tập tin đã thay đổi với 28 bổ sung7 xóa
  1. 2 0
      CMakeLists.txt
  2. 2 4
      base/hsocket.c
  3. 2 0
      configure
  4. 14 3
      event/hloop.c
  5. 8 0
      hconfig.h

+ 2 - 0
CMakeLists.txt

@@ -57,6 +57,8 @@ check_function("gettimeofday" "sys/time.h")
 check_function("pthread_spin_lock" "pthread.h")
 check_function("pthread_mutex_timedlock" "pthread.h")
 check_function("sem_timedwait" "semaphore.h")
+check_function("pipe" "unistd.h")
+check_function("socketpair" "sys/socket.h")
 
 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/hconfig.h.in ${CMAKE_CURRENT_SOURCE_DIR}/hconfig.h)
 

+ 2 - 4
base/hsocket.c

@@ -357,10 +357,8 @@ int ConnectUnixTimeout(const char* path, int ms) {
 #endif
 
 int Socketpair(int family, int type, int protocol, int sv[2]) {
-#ifdef OS_UNIX
-    if (family == AF_UNIX) {
-        return socketpair(family, type, protocol, sv);
-    }
+#if defined(OS_UNIX) && HAVE_SOCKETPAIR
+    return socketpair(family, type, protocol, sv);
 #endif
     if (family != AF_INET || type != SOCK_STREAM) {
         return -1;

+ 2 - 0
configure

@@ -245,6 +245,8 @@ function=gettimeofday && header=sys/time.h && check_function
 function=pthread_spin_lock && header=pthread.h && check_function
 function=pthread_mutex_timedlock && header=pthread.h && check_function
 function=sem_timedwait && header=semaphore.h && check_function
+function=pipe && header=unistd.h && check_function
+function=socketpair && header=sys/socket.h && check_function
 
 # Checks for options
 source config.mk 2>/dev/null

+ 14 - 3
event/hloop.c

@@ -17,8 +17,8 @@
 #define IO_ARRAY_INIT_SIZE              1024
 #define CUSTOM_EVENT_QUEUE_INIT_SIZE    16
 
-#define SOCKPAIR_WRITE_INDEX    0
-#define SOCKPAIR_READ_INDEX     1
+#define SOCKPAIR_READ_INDEX     0
+#define SOCKPAIR_WRITE_INDEX    1
 
 static void __hidle_del(hidle_t* idle);
 static void __htimer_del(htimer_t* timer);
@@ -208,10 +208,17 @@ unlock:
 }
 
 static int hloop_create_sockpair(hloop_t* loop) {
+#if defined(OS_UNIX) && HAVE_PIPE
+    if (pipe(loop->sockpair) != 0) {
+        hloge("pipe create failed!");
+        return -1;
+    }
+#else
     if (Socketpair(AF_INET, SOCK_STREAM, 0, loop->sockpair) != 0) {
         hloge("socketpair create failed!");
         return -1;
     }
+#endif
     hio_t* io = hread(loop, loop->sockpair[SOCKPAIR_READ_INDEX], loop->readbuf.base, loop->readbuf.len, sockpair_read_cb);
     io->priority = HEVENT_HIGH_PRIORITY;
     // NOTE: Avoid duplication closesocket in hio_cleanup
@@ -243,7 +250,11 @@ void hloop_post_event(hloop_t* loop, hevent_t* ev) {
             goto unlock;
         }
     }
-    nsend = send(loop->sockpair[SOCKPAIR_WRITE_INDEX], "e", 1, 0);
+#if defined(OS_UNIX) && HAVE_PIPE
+    nsend = write(loop->sockpair[SOCKPAIR_WRITE_INDEX], "e", 1);
+#else
+    nsend =  send(loop->sockpair[SOCKPAIR_WRITE_INDEX], "e", 1, 0);
+#endif
     if (nsend != 1) {
         hloge("send failed!");
         goto unlock;

+ 8 - 0
hconfig.h

@@ -65,6 +65,14 @@
 #define HAVE_SEM_TIMEDWAIT 0
 #endif
 
+#ifndef HAVE_PIPE
+#define HAVE_PIPE 1
+#endif
+
+#ifndef HAVE_SOCKETPAIR
+#define HAVE_SOCKETPAIR 1
+#endif
+
 /* #undef WITH_OPENSSL */
 /* #undef WITH_GNUTLS */
 /* #undef WITH_MBEDTLS */