浏览代码

support #603: HttpServer::loop() return EventLoopPtr

ithewei 1 年之前
父节点
当前提交
90594bce74
共有 2 个文件被更改,包括 40 次插入0 次删除
  1. 31 0
      http/server/HttpServer.cpp
  2. 9 0
      http/server/HttpServer.h

+ 31 - 0
http/server/HttpServer.cpp

@@ -278,3 +278,34 @@ int http_server_stop(http_server_t* server) {
     server->privdata = NULL;
     return 0;
 }
+
+namespace hv {
+
+std::shared_ptr<hv::EventLoop> HttpServer::loop(int idx) {
+    HttpServerPrivdata* privdata = (HttpServerPrivdata*)privdata;
+    if (privdata == NULL) return NULL;
+    std::lock_guard<std::mutex> locker(privdata->mutex_);
+    if (privdata->loops.empty()) return NULL;
+    if (idx >= 0 && idx < (int)privdata->loops.size()) {
+        return privdata->loops[idx];
+    }
+    EventLoop* cur = currentThreadEventLoop;
+    for (auto& loop : privdata->loops) {
+        if (loop.get() == cur) return loop;
+    }
+    return NULL;
+}
+
+size_t HttpServer::connectionNum() {
+    HttpServerPrivdata* privdata = (HttpServerPrivdata*)privdata;
+    if (privdata == NULL) return 0;
+    std::lock_guard<std::mutex> locker(privdata->mutex_);
+    if (privdata->loops.empty()) return 0;
+    size_t total = 0;
+    for (auto& loop : privdata->loops) {
+        total += loop->connectionNum;
+    }
+    return total;
+}
+
+}

+ 9 - 0
http/server/HttpServer.h

@@ -3,9 +3,11 @@
 
 #include "hexport.h"
 #include "hssl.h"
+// #include "EventLoop.h"
 #include "HttpService.h"
 // #include "WebSocketServer.h"
 namespace hv {
+class EventLoop;
 struct WebSocketService;
 }
 using hv::HttpService;
@@ -94,6 +96,8 @@ public:
         this->service = service;
     }
 
+    std::shared_ptr<hv::EventLoop> loop(int idx = -1);
+
     void setHost(const char* host = "0.0.0.0") {
         if (host) strcpy(this->host, host);
     }
@@ -115,6 +119,11 @@ public:
         this->worker_threads = num;
     }
 
+    void setMaxWorkerConnectionNum(uint32_t num) {
+        this->worker_connections = num;
+    }
+    size_t connectionNum();
+
     // SSL/TLS
     int setSslCtx(hssl_ctx_t ssl_ctx) {
         this->ssl_ctx = ssl_ctx;