hewei.it 4 rokov pred
rodič
commit
ce483e03a0

+ 1 - 1
echo-servers/pingpong_client.cpp

@@ -5,7 +5,7 @@
 #include "hv/EventLoopThreadPool.h"
 using namespace hv;
 
-static const char options[] = "hH:p:c:t:s:";
+static const char options[] = "hH:p:t:c:s:b:";
 
 static const char detail_options[] = R"(
   -h                Print help

+ 4 - 1
evpp/TcpClient.h

@@ -110,10 +110,13 @@ public:
             if (onConnection) {
                 onConnection(channel);
             }
-            channel = NULL;
             // reconnect
             if (enable_reconnect) {
                 startReconnect();
+            } else {
+                channel = NULL;
+                // NOTE: channel should be destroyed,
+                // so in this lambda function, no code should be added below.
             }
         };
         return channel->startConnect();

+ 5 - 2
http/server/HttpServer.cpp

@@ -172,6 +172,7 @@ static void on_recv(hio_t* io, void* _buf, int readbytes) {
 
     // Upgrade:
     bool upgrade = false;
+    HttpHandler::ProtocolType upgrade_protocol = HttpHandler::UNKNOWN;
     auto iter_upgrade = req->headers.find("upgrade");
     if (iter_upgrade != req->headers.end()) {
         upgrade = true;
@@ -194,7 +195,7 @@ static void on_recv(hio_t* io, void* _buf, int readbytes) {
                 ws_encode_key(iter_key->second.c_str(), ws_accept);
                 res->headers[SEC_WEBSOCKET_ACCEPT] = ws_accept;
             }
-            handler->protocol = HttpHandler::WEBSOCKET;
+            upgrade_protocol = HttpHandler::WEBSOCKET;
         }
         // h2/h2c
         else if (strnicmp(upgrade_proto, "h2", 2) == 0) {
@@ -227,6 +228,7 @@ static void on_recv(hio_t* io, void* _buf, int readbytes) {
     char* data = NULL;
     size_t len = 0;
     while (handler->GetSendData(&data, &len)) {
+        // printf("%.*s\n", (int)len, data);
         if (data && len) {
             hio_write(io, data, len);
         }
@@ -241,8 +243,9 @@ static void on_recv(hio_t* io, void* _buf, int readbytes) {
         res->status_code, res->status_message());
 
     // switch protocol to websocket
-    if (upgrade && handler->protocol == HttpHandler::WEBSOCKET) {
+    if (upgrade && upgrade_protocol == HttpHandler::WEBSOCKET) {
         WebSocketHandler* ws = handler->SwitchWebSocket();
+        handler->protocol = HttpHandler::WEBSOCKET;
         ws->channel.reset(new WebSocketChannel(io, WS_SERVER));
         ws->parser->onMessage = std::bind(websocket_onmessage, std::placeholders::_1, std::placeholders::_2, io);
         // NOTE: need to reset callbacks