Skip to content

Commit

Permalink
Add DLL unloading endpoint to API server
Browse files Browse the repository at this point in the history
- Added '/unload_dll' POST endpoint to allow DLL unloading via WebSocket communication.
- Implemented 'unloadDllEndpoint' function to parse and send DLL paths to the WebSocket server for unloading.
- Ensured WebSocket connection management and error handling during DLL unload requests.

This update enables dynamic DLL unloading through the API server, enhancing the flexibility and control over active DLL resources on the WebSocket server.
  • Loading branch information
arkadiusz committed Oct 30, 2024
1 parent b1c97cc commit 460bc2c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/api_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ void ApiServer::handlePost(http_request request) {
if (path == "/load_dll") {
loadDllEndpoint(request);
}
else if (path == "/unload_dll") {
unloadDllEndpoint(request);
}
else if (path == "/start_websocket") {
startWebSocket(request);
}
Expand Down Expand Up @@ -185,6 +188,56 @@ void ApiServer::loadDllEndpoint(const http_request& request) {
}).wait();
}

// Function for unloading the DLL from the WebSocket server
void ApiServer::unloadDllEndpoint(const http_request& request) {
std::cout << "Unload DLL endpoint called" << std::endl;

request.extract_json().then([=](json::value jsonData) {
if (!jsonData.has_field(U("dllPaths")) || !jsonData[U("dllPaths")].is_array()) {
request.reply(status_codes::BadRequest, "Missing dllPaths parameter or it is not an array");
return;
}

// Collect DLL paths
std::vector<json::value> dllPathVector;
for (const auto& dllPath : jsonData[U("dllPaths")].as_array()) {
dllPathVector.push_back(dllPath);
}

std::string wsUri = "ws://127.0.0.1:8081"; // Standard-WebSocket-URI
if (jsonData.has_field(U("wsUri"))) {
wsUri = jsonData[U("wsUri")].as_string();
}

// Connect to the WebSocket server
web::websockets::client::websocket_client ws_client;

try {
ws_client.connect(wsUri).wait();

// Send JSON message to unload the DLLs
json::value message;
message[U("unloadDllPaths")] = json::value::array(dllPathVector);

web::websockets::client::websocket_outgoing_message msg;
msg.set_utf8_message(message.serialize());

ws_client.send(msg).wait();
ws_client.close().wait();

json::value response;
response[U("status")] = json::value::string(U("DLL paths sent for unloading"));
request.reply(status_codes::OK, response);
}
catch (const web::websockets::client::websocket_exception& e) {
json::value response;
response[U("status")] = json::value::string(U("Failed to send DLL paths for unloading"));
response[U("error")] = json::value::string(e.what());
request.reply(status_codes::InternalError, response);
}
}).wait();
}

// Starts the WebSocket server by executing the provided path to `wsDll` executable.
void ApiServer::startWebSocket(const http_request& request) {
request.extract_json().then([this, &request](json::value jsonData) {
Expand Down
3 changes: 3 additions & 0 deletions src/api_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class ApiServer {
// Function for loading the DLL via the WebSocket server
void loadDllEndpoint(const http_request& request);

// Function for unloading the DLL from the WebSocket server
void unloadDllEndpoint(const http_request& request);

// Starts the WebSocket server
void startWebSocket(const http_request& request);

Expand Down

0 comments on commit 460bc2c

Please sign in to comment.