Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ struct TemplatedApp {
}

/* Register event handler for accepted FD. Can be used together with adoptSocket. */
TemplatedApp &&preOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(struct us_socket_context_t *, LIBUS_SOCKET_DESCRIPTOR)) {
TemplatedApp &&preOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(struct us_socket_context_t *, LIBUS_SOCKET_DESCRIPTOR, char *, int)) {
httpContext->onPreOpen(handler);
return std::move(static_cast<TemplatedApp &&>(*this));
}
Expand All @@ -614,7 +614,7 @@ struct TemplatedApp {
/* Add this app to httpContextData list over child apps and set onPreOpen */
httpContext->getSocketContextData()->childApps.push_back((void *) app);

httpContext->onPreOpen([](struct us_socket_context_t *context, LIBUS_SOCKET_DESCRIPTOR fd) -> LIBUS_SOCKET_DESCRIPTOR {
httpContext->onPreOpen([](struct us_socket_context_t *context, LIBUS_SOCKET_DESCRIPTOR fd, char *ip, int ip_length) -> LIBUS_SOCKET_DESCRIPTOR {

HttpContext<SSL> *httpContext = (HttpContext<SSL> *) context;

Expand All @@ -634,9 +634,9 @@ struct TemplatedApp {
//std::cout << "Loop is " << receivingApp->getLoop() << std::endl;


receivingApp->getLoop()->defer([fd, receivingApp]() {
receivingApp->getLoop()->defer([fd, ipStore = std::vector(ip, ip + ip_length), receivingApp]() {
//std::cout << "About to adopt socket " << fd << " on receivingApp " << receivingApp << std::endl;
receivingApp->adoptSocket(fd);
receivingApp->adoptSocket(fd, std::string_view(ipStore.data(), ipStore.size()));
//std::cout << "Done " << std::endl;
});

Expand All @@ -650,8 +650,8 @@ struct TemplatedApp {
}

/* adopt an externally accepted socket */
TemplatedApp &&adoptSocket(LIBUS_SOCKET_DESCRIPTOR accepted_fd) {
httpContext->adoptAcceptedSocket(accepted_fd);
TemplatedApp &&adoptSocket(LIBUS_SOCKET_DESCRIPTOR accepted_fd, std::string_view ip = std::string_view()) {
httpContext->adoptAcceptedSocket(accepted_fd, (char *) ip.data(), (int) ip.length());
return std::move(static_cast<TemplatedApp &&>(*this));
}

Expand Down
6 changes: 3 additions & 3 deletions src/HttpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,13 @@ struct HttpContext {
return us_socket_context_listen_unix(SSL, getSocketContext(), path, options, sizeof(HttpResponseData<SSL>));
}

void onPreOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(struct us_socket_context_t *, LIBUS_SOCKET_DESCRIPTOR)) {
void onPreOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(struct us_socket_context_t *, LIBUS_SOCKET_DESCRIPTOR, char *, int)) {
us_socket_context_on_pre_open(SSL, getSocketContext(), handler);
}

/* Adopt an externally accepted socket into this HttpContext */
us_socket_t *adoptAcceptedSocket(LIBUS_SOCKET_DESCRIPTOR accepted_fd) {
return us_adopt_accepted_socket(SSL, getSocketContext(), accepted_fd, sizeof(HttpResponseData<SSL>), 0, 0);
us_socket_t *adoptAcceptedSocket(LIBUS_SOCKET_DESCRIPTOR accepted_fd, char *ip, int ip_length) {
return us_adopt_accepted_socket(SSL, getSocketContext(), accepted_fd, sizeof(HttpResponseData<SSL>), ip, ip_length);
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/LocalCluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ struct LocalCluster {

cb(*app);

app->preOpen([](struct us_socket_context_t *context, LIBUS_SOCKET_DESCRIPTOR fd) -> LIBUS_SOCKET_DESCRIPTOR {
app->preOpen([](struct us_socket_context_t *context, LIBUS_SOCKET_DESCRIPTOR fd, char *ip, int ip_length) -> LIBUS_SOCKET_DESCRIPTOR {

std::ignore = context;

/* Distribute this socket in round robin fashion */
//std::cout << "About to load balance " << fd << " to " << roundRobin << std::endl;

auto receivingApp = apps[roundRobin];
apps[roundRobin]->getLoop()->defer([fd, receivingApp]() {
receivingApp->adoptSocket(fd);
apps[roundRobin]->getLoop()->defer([fd, ipStore = std::vector(ip, ip + ip_length), receivingApp]() {
receivingApp->adoptSocket(fd, std::string_view(ipStore.data(), ipStore.size()));
});

roundRobin = (roundRobin + 1) % hardwareConcurrency;
Expand Down