I want to add a https webserver to my main application, running in a local network, no need to access or receive from other networks.
It should be able to receive and provide simple text data, as well as binary data (movies or pictures) and is probably accessed from multiple clients at any given time.
This webserver is created with the sample code from "eidheims Simple Web Server" on GitHub. I stripped it of its boost parts, using only ASIO standalone, and included it in the wxWidgets 3.1.0 minimal sample.
The Webserver class derives from wxThread
Code: Select all
class WebServer : public wxThread
Code: Select all
WebServer *webServer = new WebServer();
if (webServer->Create() == wxTHREAD_NO_ERROR)
{
if (webServer->Run() != wxTHREAD_NO_ERROR)
{
wxMessageBox("Fehler beim starten des Webserver-Threads!");
}
}
But now Im wondering how to exchange data between the threads "thread safe". After the webserver got a POST I want to react to this data with the main application (other thread) and send a response accordingly:
Code: Select all
m_webserver.resource["^/fullJson$"]["POST"] = [this](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request)
{
wxString sRet = UsePostedData(sBuff);
*response << "HTTP/1.1 200 OK\r\n"
<< "Content-Length: " << sRet.length() << "\r\n\r\n"
<< sRet;
}
catch (const exception &e)
{
stringstream ssRet;
ssRet << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
response->write(ssRet);
};
I thought about adding the data to a dynamic array, which I query and clean with a timer from the main thread. But how then should I respond to the query if the webserver already finished the request (asynchron).
I fear Im lacking the understanding of how multi threading is supposed to work, at least in combination with a webserver.
Some clarity, anyone? Thank you!
Have a great one
Natu