Răsfoiți Sursa

optimize start|stop

ithewei 3 ani în urmă
părinte
comite
b5cff05977
4 a modificat fișierele cu 37 adăugiri și 7 ștergeri
  1. 2 2
      evpp/TcpClient.h
  2. 21 3
      evpp/TcpServer.h
  3. 7 1
      evpp/UdpClient.h
  4. 7 1
      evpp/UdpServer.h

+ 2 - 2
evpp/TcpClient.h

@@ -69,7 +69,7 @@ public:
     }
     }
 
 
     int startConnect() {
     int startConnect() {
-        assert(channel != NULL);
+        if (channel == NULL) return -1;
         if (connect_timeout) {
         if (connect_timeout) {
             channel->setConnectTimeout(connect_timeout);
             channel->setConnectTimeout(connect_timeout);
         }
         }
@@ -243,7 +243,7 @@ public:
 
 
     // stop thread-safe
     // stop thread-safe
     void stop(bool wait_threads_stopped = true) {
     void stop(bool wait_threads_stopped = true) {
-        TcpClientTmpl::setReconnect(NULL);
+        TcpClientEventLoopTmpl<TSocketChannel>::closesocket();
         EventLoopThread::stop(wait_threads_stopped);
         EventLoopThread::stop(wait_threads_stopped);
     }
     }
 };
 };

+ 21 - 3
evpp/TcpServer.h

@@ -39,7 +39,12 @@ public:
     // closesocket thread-safe
     // closesocket thread-safe
     void closesocket() {
     void closesocket() {
         if (listenfd >= 0) {
         if (listenfd >= 0) {
-            hio_close_async(hio_get(acceptor_loop->loop(), listenfd));
+            hloop_t* loop = acceptor_loop->loop();
+            if (loop) {
+                hio_t* listenio = hio_get(loop, listenfd);
+                assert(listenio != NULL);
+                hio_close_async(listenio);
+            }
             listenfd = -1;
             listenfd = -1;
         }
         }
     }
     }
@@ -58,8 +63,11 @@ public:
     }
     }
 
 
     int startAccept() {
     int startAccept() {
-        assert(listenfd >= 0);
-        hio_t* listenio = haccept(acceptor_loop->loop(), listenfd, onAccept);
+        if (listenfd < 0) return -1;
+        hloop_t* loop = acceptor_loop->loop();
+        if (loop == NULL) return -2;
+        hio_t* listenio = haccept(loop, listenfd, onAccept);
+        assert(listenio != NULL);
         hevent_set_userdata(listenio, this);
         hevent_set_userdata(listenio, this);
         if (tls) {
         if (tls) {
             hio_enable_ssl(listenio);
             hio_enable_ssl(listenio);
@@ -67,6 +75,15 @@ public:
         return 0;
         return 0;
     }
     }
 
 
+    int stopAccept() {
+        if (listenfd < 0) return -1;
+        hloop_t* loop = acceptor_loop->loop();
+        if (loop == NULL) return -2;
+        hio_t* listenio = hio_get(loop, listenfd);
+        assert(listenio != NULL);
+        return hio_del(listenio, HV_READ);
+    }
+
     // start thread-safe
     // start thread-safe
     void start(bool wait_threads_started = true) {
     void start(bool wait_threads_started = true) {
         if (worker_threads.threadNum() > 0) {
         if (worker_threads.threadNum() > 0) {
@@ -76,6 +93,7 @@ public:
     }
     }
     // stop thread-safe
     // stop thread-safe
     void stop(bool wait_threads_stopped = true) {
     void stop(bool wait_threads_stopped = true) {
+        closesocket();
         if (worker_threads.threadNum() > 0) {
         if (worker_threads.threadNum() > 0) {
             worker_threads.stop(wait_threads_stopped);
             worker_threads.stop(wait_threads_stopped);
         }
         }

+ 7 - 1
evpp/UdpClient.h

@@ -43,7 +43,7 @@ public:
     }
     }
 
 
     int startRecv() {
     int startRecv() {
-        assert(channel != NULL);
+        if (channel == NULL) return -1;
         channel->onread = [this](Buffer* buf) {
         channel->onread = [this](Buffer* buf) {
             if (onMessage) {
             if (onMessage) {
                 onMessage(channel, buf);
                 onMessage(channel, buf);
@@ -62,6 +62,11 @@ public:
         return channel->startRead();
         return channel->startRead();
     }
     }
 
 
+    int stopRecv() {
+        if (channel == NULL) return -1;
+        return channel->stopRead();
+    }
+
     // start thread-safe
     // start thread-safe
     void start() {
     void start() {
         loop_->runInLoop(std::bind(&UdpClientEventLoopTmpl::startRecv, this));
         loop_->runInLoop(std::bind(&UdpClientEventLoopTmpl::startRecv, this));
@@ -134,6 +139,7 @@ public:
 
 
     // stop thread-safe
     // stop thread-safe
     void stop(bool wait_threads_stopped = true) {
     void stop(bool wait_threads_stopped = true) {
+        UdpClientEventLoopTmpl<TSocketChannel>::closesocket();
         EventLoopThread::stop(wait_threads_stopped);
         EventLoopThread::stop(wait_threads_stopped);
     }
     }
 };
 };

+ 7 - 1
evpp/UdpServer.h

@@ -42,7 +42,7 @@ public:
     }
     }
 
 
     int startRecv() {
     int startRecv() {
-        assert(channel != NULL);
+        if (channel == NULL) return -1;
         channel->onread = [this](Buffer* buf) {
         channel->onread = [this](Buffer* buf) {
             if (onMessage) {
             if (onMessage) {
                 onMessage(channel, buf);
                 onMessage(channel, buf);
@@ -61,6 +61,11 @@ public:
         return channel->startRead();
         return channel->startRead();
     }
     }
 
 
+    int stopRecv() {
+        if (channel == NULL) return -1;
+        return channel->stopRead();
+    }
+
     // start thread-safe
     // start thread-safe
     void start() {
     void start() {
         loop_->runInLoop(std::bind(&UdpServerEventLoopTmpl::startRecv, this));
         loop_->runInLoop(std::bind(&UdpServerEventLoopTmpl::startRecv, this));
@@ -118,6 +123,7 @@ public:
 
 
     // stop thread-safe
     // stop thread-safe
     void stop(bool wait_threads_stopped = true) {
     void stop(bool wait_threads_stopped = true) {
+        UdpServerEventLoopTmpl<TSocketChannel>::closesocket();
         EventLoopThread::stop(wait_threads_stopped);
         EventLoopThread::stop(wait_threads_stopped);
     }
     }
 };
 };