Skip to content

Commit a5c26ac

Browse files
authored
adoptSocket: add optional ip parameter (#1921)
for the ip to be correctly set in the open handler
1 parent 360c276 commit a5c26ac

3 files changed

Lines changed: 12 additions & 12 deletions

File tree

src/App.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ struct TemplatedApp {
593593
}
594594

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

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

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

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

636636

637-
receivingApp->getLoop()->defer([fd, receivingApp]() {
637+
receivingApp->getLoop()->defer([fd, ipStore = std::string(ip, ip + ip_length), receivingApp]() {
638638
//std::cout << "About to adopt socket " << fd << " on receivingApp " << receivingApp << std::endl;
639-
receivingApp->adoptSocket(fd);
639+
receivingApp->adoptSocket(fd, std::string_view(ipStore));
640640
//std::cout << "Done " << std::endl;
641641
});
642642

@@ -650,8 +650,8 @@ struct TemplatedApp {
650650
}
651651

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

src/HttpContext.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,13 @@ struct HttpContext {
506506
return us_socket_context_listen_unix(SSL, getSocketContext(), path, options, sizeof(HttpResponseData<SSL>));
507507
}
508508

509-
void onPreOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(struct us_socket_context_t *, LIBUS_SOCKET_DESCRIPTOR)) {
509+
void onPreOpen(LIBUS_SOCKET_DESCRIPTOR (*handler)(struct us_socket_context_t *, LIBUS_SOCKET_DESCRIPTOR, char *, int)) {
510510
us_socket_context_on_pre_open(SSL, getSocketContext(), handler);
511511
}
512512

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

src/LocalCluster.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ struct LocalCluster {
3434

3535
cb(*app);
3636

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

3939
std::ignore = context;
4040

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

4444
auto receivingApp = apps[roundRobin];
45-
apps[roundRobin]->getLoop()->defer([fd, receivingApp]() {
46-
receivingApp->adoptSocket(fd);
45+
apps[roundRobin]->getLoop()->defer([fd, ipStore = std::string(ip, ip + ip_length), receivingApp]() {
46+
receivingApp->adoptSocket(fd, std::string_view(ipStore));
4747
});
4848

4949
roundRobin = (roundRobin + 1) % hardwareConcurrency;

0 commit comments

Comments
 (0)