浏览代码

optimize code

hewei.it 4 年之前
父节点
当前提交
6a0b181db9
共有 3 个文件被更改,包括 39 次插入13 次删除
  1. 9 3
      examples/http_server_test.cpp
  2. 26 8
      examples/websocket_server_test.cpp
  3. 4 2
      http/server/HttpServer.cpp

+ 9 - 3
examples/http_server_test.cpp

@@ -12,7 +12,7 @@
  *
  * @build   ./configure --with-openssl && make clean && make
  *
- * @server  bin/http_server_test
+ * @server  bin/http_server_test 8080
  *
  * @client  curl -v http://127.0.0.1:8080/ping
  *          curl -v https://127.0.0.1:8443/ping --insecure
@@ -22,9 +22,15 @@
  */
 #define TEST_HTTPS 0
 
-int main() {
+int main(int argc, char** argv) {
     HV_MEMCHECK;
 
+    int port = 0;
+    if (argc > 1) {
+        port = atoi(argv[1]);
+    }
+    if (port == 0) port = 8080;
+
     HttpService router;
     router.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
         return resp->String("pong");
@@ -55,7 +61,7 @@ int main() {
 
     http_server_t server;
     server.service = &router;
-    server.port = 8080;
+    server.port = port;
 #if TEST_HTTPS
     server.https_port = 8443;
     hssl_ctx_init_param_t param;

+ 26 - 8
examples/websocket_server_test.cpp

@@ -28,6 +28,22 @@
 
 using namespace hv;
 
+class MyContext {
+public:
+    MyContext() {
+        timerID = INVALID_TIMER_ID;
+    }
+    ~MyContext() {
+    }
+
+    int handleMessage(const std::string& msg) {
+        printf("onmessage: %s\n", msg.c_str());
+        return msg.size();
+    }
+
+    TimerID timerID;
+};
+
 int main(int argc, char** argv) {
     if (argc < 2) {
         printf("Usage: %s port\n", argv[0]);
@@ -35,12 +51,12 @@ int main(int argc, char** argv) {
     }
     int port = atoi(argv[1]);
 
-    TimerID timerID = INVALID_TIMER_ID;
     WebSocketServerCallbacks ws;
-    ws.onopen = [&timerID](const WebSocketChannelPtr& channel, const std::string& url) {
+    ws.onopen = [](const WebSocketChannelPtr& channel, const std::string& url) {
         printf("onopen: GET %s\n", url.c_str());
+        MyContext* ctx = channel->newContext<MyContext>();
         // send(time) every 1s
-        timerID = setInterval(1000, [channel](TimerID id) {
+        ctx->timerID = setInterval(1000, [channel](TimerID id) {
             char str[DATETIME_FMT_BUFLEN] = {0};
             datetime_t dt = datetime_now();
             datetime_fmt(&dt, str);
@@ -48,14 +64,16 @@ int main(int argc, char** argv) {
         });
     };
     ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
-        printf("onmessage: %s\n", msg.c_str());
+        MyContext* ctx = channel->getContext<MyContext>();
+        ctx->handleMessage(msg);
     };
-    ws.onclose = [&timerID](const WebSocketChannelPtr& channel) {
+    ws.onclose = [](const WebSocketChannelPtr& channel) {
         printf("onclose\n");
-        if (timerID != INVALID_TIMER_ID) {
-            killTimer(timerID);
-            timerID = INVALID_TIMER_ID;
+        MyContext* ctx = channel->getContext<MyContext>();
+        if (ctx->timerID != INVALID_TIMER_ID) {
+            killTimer(ctx->timerID);
         }
+        channel->deleteContext<MyContext>();
     };
 
     websocket_server_t server;

+ 4 - 2
http/server/HttpServer.cpp

@@ -376,12 +376,14 @@ int http_server_stop(http_server_t* server) {
             continue;
         }
         // wait for all loops running
+        bool all_loops_running = true;
         for (auto& loop : privdata->loops) {
             if (loop->status() < hv::Status::kRunning) {
-                continue;
+                all_loops_running = false;
+                break;
             }
         }
-        break;
+        if (all_loops_running) break;
     }
 
     // stop all loops