Forráskód Böngészése

fix reconnect when enable ssl

ithewei 3 éve
szülő
commit
d5847f4bd1

+ 22 - 5
evpp/TcpClient.h

@@ -17,11 +17,14 @@ public:
 
     TcpClientTmpl() {
         connect_timeout = 5000;
+        tls = false;
+        tls_setting = NULL;
         reconn_setting = NULL;
         unpack_setting = NULL;
     }
 
     virtual ~TcpClientTmpl() {
+        HV_FREE(tls_setting);
         HV_FREE(reconn_setting);
         HV_FREE(unpack_setting);
     }
@@ -67,6 +70,12 @@ public:
         if (connect_timeout) {
             channel->setConnectTimeout(connect_timeout);
         }
+        if (tls) {
+            channel->enableSSL();
+            if (tls_setting) {
+                channel->newSslCtx(tls_setting);
+            }
+        }
         channel->onconnect = [this]() {
             if (unpack_setting) {
                 channel->setUnpack(unpack_setting);
@@ -111,7 +120,7 @@ public:
         uint32_t delay = reconn_setting_calc_delay(reconn_setting);
         loop_thread.loop()->setTimeout(delay, [this](TimerID timerID){
             hlogi("reconnect... cnt=%d, delay=%d", reconn_setting->cur_retry_cnt, reconn_setting->cur_delay);
-            createsocket(&peeraddr.sa);
+            if (createsocket(&peeraddr.sa) < 0) return;
             startConnect();
         });
         return 0;
@@ -143,10 +152,16 @@ public:
         return send(str.data(), str.size());
     }
 
-    int withTLS(hssl_ctx_opt_t* opt) {
-        if (!channel) return -1;
-        opt->endpoint = HSSL_CLIENT;
-        return channel->newSslCtx(opt);
+    int withTLS(hssl_ctx_opt_t* opt = NULL) {
+        tls = true;
+        if (opt) {
+            if (tls_setting == NULL) {
+                HV_ALLOC_SIZEOF(tls_setting);
+            }
+            opt->endpoint = HSSL_CLIENT;
+            *tls_setting = *opt;
+        }
+        return 0;
     }
 
     void setConnectTimeout(int ms) {
@@ -183,6 +198,8 @@ public:
 
     sockaddr_u              peeraddr;
     int                     connect_timeout;
+    bool                    tls;
+    hssl_ctx_opt_t*         tls_setting;
     reconn_setting_t*       reconn_setting;
     unpack_setting_t*       unpack_setting;
 

+ 4 - 1
evpp/TcpClient_test.cpp

@@ -1,7 +1,9 @@
 /*
  * TcpClient_test.cpp
  *
- * @build: make evpp
+ * @build   make evpp
+ * @server  bin/TcpServer_test 1234
+ * @client  bin/TcpClient_test 1234
  *
  */
 
@@ -52,6 +54,7 @@ int main(int argc, char* argv[]) {
     };
     // reconnect: 1,2,4,8,10,10,10...
     reconn_setting_t reconn;
+    reconn_setting_init(&reconn);
     reconn.min_delay = 1000;
     reconn.max_delay = 10000;
     reconn.delay_policy = 2;

+ 5 - 9
evpp/TcpServer.h

@@ -75,19 +75,15 @@ public:
         }
     }
 
-    int withTLS(const char* cert_file, const char* key_file) {
-        if (cert_file) {
-            hssl_ctx_init_param_t param;
-            memset(&param, 0, sizeof(param));
-            param.crt_file = cert_file;
-            param.key_file = key_file;
-            param.endpoint = HSSL_SERVER;
-            if (hssl_ctx_init(&param) == NULL) {
+    int withTLS(hssl_ctx_opt_t* opt = NULL) {
+        tls = true;
+        if (opt) {
+            opt->endpoint = HSSL_SERVER;
+            if (hssl_ctx_init(opt) == NULL) {
                 fprintf(stderr, "hssl_ctx_init failed!\n");
                 return -1;
             }
         }
-        tls = true;
         return 0;
     }
 

+ 3 - 1
evpp/TcpServer_test.cpp

@@ -1,7 +1,9 @@
 /*
  * TcpServer_test.cpp
  *
- * @build: make evpp
+ * @build   make evpp
+ * @server  bin/TcpServer_test 1234
+ * @client  bin/TcpClient_test 1234
  *
  */
 

+ 3 - 1
evpp/UdpClient_test.cpp

@@ -1,7 +1,9 @@
 /*
  * UdpClient_test.cpp
  *
- * @build: make evpp
+ * @build   make evpp
+ * @server  bin/UdpServer_test 1234
+ * @client  bin/UdpClient_test 1234
  *
  */
 

+ 3 - 1
evpp/UdpServer_test.cpp

@@ -1,7 +1,9 @@
 /*
  * UdpServer_test.cpp
  *
- * @build: make evpp
+ * @build   make evpp
+ * @server  bin/UdpServer_test 1234
+ * @client  bin/UdpClient_test 1234
  *
  */
 

+ 1 - 0
examples/protorpc/protorpc_client.cpp

@@ -71,6 +71,7 @@ public:
         setConnectTimeout(5000);
 
         reconn_setting_t reconn;
+        reconn_setting_init(&reconn);
         reconn.min_delay = 1000;
         reconn.max_delay = 10000;
         reconn.delay_policy = 2;

+ 1 - 0
examples/websocket_client_test.cpp

@@ -32,6 +32,7 @@ int main(int argc, char** argv) {
 
     // reconnect: 1,2,4,8,10,10,10...
     reconn_setting_t reconn;
+    reconn_setting_init(&reconn);
     reconn.min_delay = 1000;
     reconn.max_delay = 10000;
     reconn.delay_policy = 2;

+ 1 - 1
http/client/WebSocketClient.cpp

@@ -51,7 +51,7 @@ int WebSocketClient::open(const char* _url, const http_headers& headers) {
     // wss
     bool wss = strncmp(url.c_str(), "wss", 3) == 0;
     if (wss) {
-        channel->enableSSL();
+        withTLS();
     }
 
     for (auto& header : headers) {