Skip to content

Commit

Permalink
support #603: HttpServer::loop() return EventLoopPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Aug 12, 2024
1 parent 6e0f701 commit 90594bc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
31 changes: 31 additions & 0 deletions http/server/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 9 additions & 0 deletions http/server/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,6 +96,8 @@ class HttpServer : public http_server_t {
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);
}
Expand All @@ -115,6 +119,11 @@ class HttpServer : public http_server_t {
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;
Expand Down

0 comments on commit 90594bc

Please sign in to comment.