hewei.it před 4 roky
rodič
revize
0e1358a600
5 změnil soubory, kde provedl 17 přidání a 8 odebrání
  1. 1 0
      evpp/EventLoop.h
  2. 2 2
      evpp/EventLoopThread.h
  3. 3 0
      evpp/EventLoopThreadPool.h
  4. 8 3
      evpp/TcpClient.h
  5. 3 3
      evpp/UdpClient.h

+ 1 - 0
evpp/EventLoop.h

@@ -52,6 +52,7 @@ public:
 
     void stop() {
         if (loop_ == NULL) return;
+        if (status() < kRunning) return;
         setStatus(kStopping);
         hloop_stop(loop_);
         loop_ = NULL;

+ 2 - 2
evpp/EventLoopThread.h

@@ -47,9 +47,9 @@ public:
     void start(bool wait_thread_started = true,
                Functor pre = Functor(),
                Functor post = Functor()) {
+        if (status() >= kStarting && status() < kStopped) return;
         setStatus(kStarting);
 
-        assert(thread_.get() == NULL);
         thread_.reset(new std::thread(&EventLoopThread::loop_thread, this, pre, post));
 
         if (wait_thread_started) {
@@ -61,7 +61,7 @@ public:
 
     // @param wait_thread_started: if ture this method will block until loop_thread stopped.
     void stop(bool wait_thread_stopped = false) {
-        if (status() >= kStopping) return;
+        if (status() < kStarting || status() >= kStopping) return;
         setStatus(kStopping);
 
         loop_->stop();

+ 3 - 0
evpp/EventLoopThreadPool.h

@@ -50,6 +50,7 @@ public:
     void start(bool wait_threads_started = false,
                std::function<void(const EventLoopPtr&)> pre = NULL,
                std::function<void(const EventLoopPtr&)> post = NULL) {
+        if (status() >= kStarting && status() < kStopped) return;
         setStatus(kStarting);
 
         if (thread_num_ == 0) {
@@ -60,6 +61,7 @@ public:
         std::shared_ptr<std::atomic<int>> started_cnt(new std::atomic<int>(0));
         std::shared_ptr<std::atomic<int>> exited_cnt(new std::atomic<int>(0));
 
+        loop_threads_.clear();
         for (int i = 0; i < thread_num_; ++i) {
             EventLoopThreadPtr loop_thread(new EventLoopThread);
             const EventLoopPtr& loop = loop_thread->loop();
@@ -91,6 +93,7 @@ public:
 
     // @param wait_threads_started: if ture this method will block until all loop_threads stopped.
     void stop(bool wait_threads_stopped = false) {
+        if (status() < kStarting || status() >= kStopping) return;
         setStatus(kStopping);
 
         for (auto& loop_thread : loop_threads_) {

+ 8 - 3
evpp/TcpClient.h

@@ -157,18 +157,23 @@ public:
         loop_thread.stop(wait_threads_stopped);
     }
 
+    bool isConnected() {
+        if (channel == NULL) return false;
+        return channel->isConnected();
+    }
+
     int send(const void* data, int size) {
-        if (channel == NULL) return 0;
+        if (!isConnected()) return -1;
         return channel->write(data, size);
     }
 
     int send(Buffer* buf) {
-        if (channel == NULL) return 0;
+        if (!isConnected()) return -1;
         return channel->write(buf);
     }
 
     int send(const std::string& str) {
-        if (channel == NULL) return 0;
+        if (!isConnected()) return -1;
         return channel->write(str);
     }
 

+ 3 - 3
evpp/UdpClient.h

@@ -53,17 +53,17 @@ public:
     }
 
     int sendto(const void* data, int size) {
-        if (channel == NULL) return 0;
+        if (channel == NULL) return -1;
         return channel->write(data, size);
     }
 
     int sendto(Buffer* buf) {
-        if (channel == NULL) return 0;
+        if (channel == NULL) return -1;
         return channel->write(buf);
     }
 
     int sendto(const std::string& str) {
-        if (channel == NULL) return 0;
+        if (channel == NULL) return -1;
         return channel->write(str);
     }