Background HTTP server in plugin
AnsweredAm trying to develop a plugin that can receive metadata via an HTTP server and then pass that data to the NX Server.
As a POC I'm just using Crow to test requests and responses. The problem is this. If I simply call the server from the DeviceAgent constructor the server works but my TestCamera video hangs. If I try and put Crow into a separate thread, when I change the plugin and restart the NX Server service it keeps restarting every few seconds.
Here is my calling code
DeviceAgent::DeviceAgent(const nx::sdk::IDeviceInfo* deviceInfo) :
// Call the DeviceAgent helper class constructor telling it to verbosely report to stderr.
ConsumingDeviceAgent(deviceInfo, /*enableOutput*/ true)
{
vnet * vnetptr;
std::thread th(&vnet::InitialiseHTTPServer, vnetptr);
}
and here is the code being called in a class called vnet
void vnet::InitialiseHTTPServer()
{
crow::SimpleApp app; //define your crow application
//define your endpoint at the root directory
CROW_ROUTE(app, "/")([]() {
return "Hello world";
});
//set the port, set the app to run on multiple threads, and run the app
app.port(9090).run_async();
}
Do you have a better method of utilising a small HTTP server within the plugin?
-
Hi,
In principle though is there any reason why threading would upset the server service starting?
A bug in your code, most probably. Lost pointer or something similar.
For example, in your code snippet, vnet * vnetptr is a local variable and is destroyed once the DeviceAgent::DeviceAgent() method exits. Probably, you should declare and use a private member of the vnet * vnetptr type inside the DeviceAgent class.
-
Thanks. Actually I may just have solved the issue I was inquiring about. It's all a bit experimental at the moment as I find my way around the plugin, but I seem to have my server running again and the NX Server is stable. Will obviously get back if I'm stuck, but I'll bear your thoughts in mind when doing any threading.
Please sign in to leave a comment.
Comments
4 comments