Browse Source

#221: save host and port for restart

ithewei 3 years ago
parent
commit
aba82b91f8
9 changed files with 126 additions and 28 deletions
  1. 3 5
      evpp/Channel.h
  2. 12 8
      evpp/TcpClient.h
  3. 16 2
      evpp/TcpClient_test.cpp
  4. 13 2
      evpp/TcpServer.h
  5. 15 2
      evpp/TcpServer_test.cpp
  6. 17 2
      evpp/UdpClient.h
  7. 15 2
      evpp/UdpClient_test.cpp
  8. 20 3
      evpp/UdpServer.h
  9. 15 2
      evpp/UdpServer_test.cpp

+ 3 - 5
evpp/Channel.h

@@ -144,11 +144,9 @@ public:
 
     // close thread-safe
     int close(bool async = false) {
-        if (!isOpened()) return -1;
-        if (async) {
-            return hio_close_async(io_);
-        }
-        return hio_close(io_);
+        if (isClosed()) return -1;
+        status = CLOSED;
+        return async ? hio_close_async(io_) : hio_close(io_);
     }
 
 public:

+ 12 - 8
evpp/TcpClient.h

@@ -62,14 +62,23 @@ public:
     }
     // closesocket thread-safe
     void closesocket() {
-        setReconnect(NULL);
         if (channel) {
+            setReconnect(NULL);
             channel->close(true);
         }
     }
 
     int startConnect() {
-        if (channel == NULL) return -1;
+        if (channel == NULL || channel->isClosed()) {
+            int connfd = createsocket(&remote_addr.sa);
+            if (connfd < 0) {
+                hloge("createsocket %s:%d return %d!\n", remote_host.c_str(), remote_port, connfd);
+                return connfd;
+            }
+        }
+        if (channel == NULL || channel->status >= SocketChannel::CONNECTING) {
+            return -1;
+        }
         if (connect_timeout) {
             channel->setConnectTimeout(connect_timeout);
         }
@@ -111,10 +120,6 @@ public:
             // reconnect
             if (reconn_setting) {
                 startReconnect();
-            } else {
-                channel = NULL;
-                // NOTE: channel should be destroyed,
-                // so in this lambda function, no code should be added below.
             }
         };
         return channel->startConnect();
@@ -126,7 +131,6 @@ public:
         uint32_t delay = reconn_setting_calc_delay(reconn_setting);
         loop_->setTimeout(delay, [this](TimerID timerID){
             hlogi("reconnect... cnt=%d, delay=%d", reconn_setting->cur_retry_cnt, reconn_setting->cur_delay);
-            if (createsocket(&remote_addr.sa) < 0) return;
             startConnect();
         });
         return 0;
@@ -221,7 +225,7 @@ template<class TSocketChannel = SocketChannel>
 class TcpClientTmpl : private EventLoopThread, public TcpClientEventLoopTmpl<TSocketChannel> {
 public:
     TcpClientTmpl(EventLoopPtr loop = NULL)
-        : EventLoopThread()
+        : EventLoopThread(loop)
         , TcpClientEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
     {}
     virtual ~TcpClientTmpl() {

+ 16 - 2
evpp/TcpClient_test.cpp

@@ -7,6 +7,8 @@
  *
  */
 
+#include <iostream>
+
 #include "TcpClient.h"
 #include "htime.h"
 
@@ -72,8 +74,20 @@ int main(int argc, char* argv[]) {
 
     cli.start();
 
-    // press Enter to stop
-    while (getchar() != '\n');
+    std::string str;
+    while (std::getline(std::cin, str)) {
+        if (str == "close") {
+            cli.closesocket();
+        } else if (str == "start") {
+            cli.start();
+        } else if (str == "stop") {
+            cli.stop();
+            break;
+        } else {
+            if (!cli.isConnected()) break;
+            cli.send(str);
+        }
+    }
 
     return 0;
 }

+ 13 - 2
evpp/TcpServer.h

@@ -34,6 +34,9 @@ public:
     //@retval >=0 listenfd, <0 error
     int createsocket(int port, const char* host = "0.0.0.0") {
         listenfd = Listen(port, host);
+        if (listenfd < 0) return listenfd;
+        this->host = host;
+        this->port = port;
         return listenfd;
     }
     // closesocket thread-safe
@@ -63,7 +66,13 @@ public:
     }
 
     int startAccept() {
-        if (listenfd < 0) return -1;
+        if (listenfd < 0) {
+            listenfd = createsocket(port, host.c_str());
+            if (listenfd < 0) {
+                hloge("createsocket %s:%d return %d!\n", host.c_str(), port, listenfd);
+                return listenfd;
+            }
+        }
         hloop_t* loop = acceptor_loop->loop();
         if (loop == NULL) return -2;
         hio_t* listenio = haccept(loop, listenfd, onAccept);
@@ -227,6 +236,8 @@ private:
     }
 
 public:
+    std::string             host;
+    int                     port;
     int                     listenfd;
     bool                    tls;
     unpack_setting_t        unpack_setting;
@@ -252,7 +263,7 @@ template<class TSocketChannel = SocketChannel>
 class TcpServerTmpl : private EventLoopThread, public TcpServerEventLoopTmpl<TSocketChannel> {
 public:
     TcpServerTmpl(EventLoopPtr loop = NULL)
-        : EventLoopThread()
+        : EventLoopThread(loop)
         , TcpServerEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
     {}
     virtual ~TcpServerTmpl() {

+ 15 - 2
evpp/TcpServer_test.cpp

@@ -7,6 +7,8 @@
  *
  */
 
+#include <iostream>
+
 #include "TcpServer.h"
 
 using namespace hv;
@@ -55,8 +57,19 @@ int main(int argc, char* argv[]) {
 
     srv.start();
 
-    // press Enter to stop
-    while (getchar() != '\n');
+    std::string str;
+    while (std::getline(std::cin, str)) {
+        if (str == "close") {
+            srv.closesocket();
+        } else if (str == "start") {
+            srv.start();
+        } else if (str == "stop") {
+            srv.stop();
+            break;
+        } else {
+            srv.broadcast(str.data(), str.size());
+        }
+    }
 
     return 0;
 }

+ 17 - 2
evpp/UdpClient.h

@@ -32,6 +32,8 @@ public:
     int createsocket(int remote_port, const char* remote_host = "127.0.0.1") {
         hio_t* io = hloop_create_udp_client(loop_->loop(), remote_host, remote_port);
         if (io == NULL) return -1;
+        this->remote_host = remote_host;
+        this->remote_port = remote_port;
         channel.reset(new TSocketChannel(io));
         return channel->fd();
     }
@@ -43,7 +45,16 @@ public:
     }
 
     int startRecv() {
-        if (channel == NULL) return -1;
+        if (channel == NULL || channel->isClosed()) {
+            int sockfd = createsocket(remote_port, remote_host.c_str());
+            if (sockfd < 0) {
+                hloge("createsocket %s:%d return %d!\n", remote_host.c_str(), remote_port, sockfd);
+                return sockfd;
+            }
+        }
+        if (channel == NULL || channel->isClosed()) {
+            return -1;
+        }
         channel->onread = [this](Buffer* buf) {
             if (onMessage) {
                 onMessage(channel, buf);
@@ -99,6 +110,10 @@ public:
 
 public:
     TSocketChannelPtr       channel;
+
+    std::string             remote_host;
+    int                     remote_port;
+
 #if WITH_KCP
     bool                    enable_kcp;
     kcp_setting_t           kcp_setting;
@@ -117,7 +132,7 @@ template<class TSocketChannel = SocketChannel>
 class UdpClientTmpl : private EventLoopThread, public UdpClientEventLoopTmpl<TSocketChannel> {
 public:
     UdpClientTmpl(EventLoopPtr loop = NULL)
-        : EventLoopThread()
+        : EventLoopThread(loop)
         , UdpClientEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
     {}
     virtual ~UdpClientTmpl() {

+ 15 - 2
evpp/UdpClient_test.cpp

@@ -7,6 +7,8 @@
  *
  */
 
+#include <iostream>
+
 #include "UdpClient.h"
 #include "htime.h"
 
@@ -38,8 +40,19 @@ int main(int argc, char* argv[]) {
         cli.sendto(str);
     });
 
-    // press Enter to stop
-    while (getchar() != '\n');
+    std::string str;
+    while (std::getline(std::cin, str)) {
+        if (str == "close") {
+            cli.closesocket();
+        } else if (str == "start") {
+            cli.start();
+        } else if (str == "stop") {
+            cli.stop();
+            break;
+        } else {
+            cli.sendto(str);
+        }
+    }
 
     return 0;
 }

+ 20 - 3
evpp/UdpServer.h

@@ -31,6 +31,8 @@ public:
     int createsocket(int port, const char* host = "0.0.0.0") {
         hio_t* io = hloop_create_udp_server(loop_->loop(), host, port);
         if (io == NULL) return -1;
+        this->host = host;
+        this->port = port;
         channel.reset(new TSocketChannel(io));
         return channel->fd();
     }
@@ -42,7 +44,16 @@ public:
     }
 
     int startRecv() {
-        if (channel == NULL) return -1;
+        if (channel == NULL || channel->isClosed()) {
+            int bindfd = createsocket(port, host.c_str());
+            if (bindfd < 0) {
+                hloge("createsocket %s:%d return %d!\n", host.c_str(), port, bindfd);
+                return bindfd;
+            }
+        }
+        if (channel == NULL || channel->isClosed()) {
+            return -1;
+        }
         channel->onread = [this](Buffer* buf) {
             if (onMessage) {
                 onMessage(channel, buf);
@@ -86,6 +97,8 @@ public:
     }
 
 public:
+    std::string             host;
+    int                     port;
     TSocketChannelPtr       channel;
 #if WITH_KCP
     bool                    enable_kcp;
@@ -105,7 +118,7 @@ template<class TSocketChannel = SocketChannel>
 class UdpServerTmpl : private EventLoopThread, public UdpServerEventLoopTmpl<TSocketChannel> {
 public:
     UdpServerTmpl(EventLoopPtr loop = NULL)
-        : EventLoopThread()
+        : EventLoopThread(loop)
         , UdpServerEventLoopTmpl<TSocketChannel>(EventLoopThread::loop())
     {}
     virtual ~UdpServerTmpl() {
@@ -118,7 +131,11 @@ public:
 
     // start thread-safe
     void start(bool wait_threads_started = true) {
-        EventLoopThread::start(wait_threads_started, std::bind(&UdpServerTmpl::startRecv, this));
+        if (isRunning()) {
+            UdpServerEventLoopTmpl<TSocketChannel>::start();
+        } else {
+            EventLoopThread::start(wait_threads_started, std::bind(&UdpServerTmpl::startRecv, this));
+        }
     }
 
     // stop thread-safe

+ 15 - 2
evpp/UdpServer_test.cpp

@@ -7,6 +7,8 @@
  *
  */
 
+#include <iostream>
+
 #include "UdpServer.h"
 
 using namespace hv;
@@ -31,8 +33,19 @@ int main(int argc, char* argv[]) {
     };
     srv.start();
 
-    // press Enter to stop
-    while (getchar() != '\n');
+    std::string str;
+    while (std::getline(std::cin, str)) {
+        if (str == "close") {
+            srv.closesocket();
+        } else if (str == "start") {
+            srv.start();
+        } else if (str == "stop") {
+            srv.stop();
+            break;
+        } else {
+            srv.sendto(str);
+        }
+    }
 
     return 0;
 }