Skip to content

Commit

Permalink
Make network interface request handler and sender callback optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
linuscu committed Oct 11, 2024
1 parent 5c2f4b8 commit e58d050
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
22 changes: 14 additions & 8 deletions packages/network/include/network/NetworkInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,27 @@ namespace l::network {
int32_t numMaxParallellRequestConnections,
int32_t defaultResponseSize,
int32_t timeout,
std::function<l::concurrency::RunnableResult(bool success, std::string_view queryArguments, l::network::Request<T>&)> handler
std::function<l::concurrency::RunnableResult(bool success, std::string_view queryArguments, l::network::Request<T>&)> handler = nullptr
) {
auto network = mNetworkManager.lock();
if (network) {
auto it = mInterfaces.find(interfaceName.data());
if (it != mInterfaces.end()) {
it->second.AddEndpoint(queryName, endpointString);

auto handlerWrapped = [&, cb = handler, name = std::string(interfaceName)](bool success, std::string_view args, l::network::Request<T>& request) {
SetNetworkStatus(name, success);
return cb(success, args, request);
};

for (int i = 0; i < numMaxParallellRequestConnections; i++) {
network->CreateRequestTemplate(std::make_unique<l::network::Request<T>>(queryName, "", defaultResponseSize, handlerWrapped, timeout));
if (handler) {
auto handlerWrapped = [&, cb = handler, name = std::string(interfaceName)](bool success, std::string_view args, l::network::Request<T>& request) {
SetNetworkStatus(name, success);
return cb(success, args, request);
};
for (int i = 0; i < numMaxParallellRequestConnections; i++) {
network->CreateRequestTemplate(std::make_unique<l::network::Request<T>>(queryName, "", defaultResponseSize, handlerWrapped, timeout));
}
}
else {
for (int i = 0; i < numMaxParallellRequestConnections; i++) {
network->CreateRequestTemplate(std::make_unique<l::network::Request<T>>(queryName, "", defaultResponseSize, nullptr, timeout));
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/network/include/network/NetworkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ namespace l::network {
CompleteRequest(mSuccess);
}

auto result = mHandler(mSuccess, mRequestQueryArgs, *this);
l::concurrency::RunnableResult result = mSuccess ? l::concurrency::RunnableResult::SUCCESS : l::concurrency::RunnableResult::FAILURE;
if (mHandler) {
result = mHandler(mSuccess, mRequestQueryArgs, *this);
}
if (cb) {
cb(result == l::concurrency::RunnableResult::SUCCESS, mRequestQueryArgs);
}
Expand Down

0 comments on commit e58d050

Please sign in to comment.