Skip to content

Commit 9701ccd

Browse files
committed
DPL GUI: prepare ancillary classes to enable remote GUI
* Allow CORS connection on the websocket * Setup the remote GUI if a non DPL connection on the websocket port happens. * Invoke pollGUIPostRender and protect against empty frames in the beginning.
1 parent 2851a6f commit 9701ccd

5 files changed

Lines changed: 27 additions & 10 deletions

File tree

Framework/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ o2_add_library(Framework
2929
src/CommonServices.cxx
3030
src/CommonMessageBackends.cxx
3131
src/CommonDriverServices.cxx
32+
src/ControlWebSocketHandler.cxx
3233
src/CompletionPolicy.cxx
3334
src/CompletionPolicyHelpers.cxx
3435
src/ComputingQuotaEvaluator.cxx

Framework/Core/src/DPLWebSocket.cxx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "DriverServerContext.h"
1818
#include "DriverClientContext.h"
1919
#include "GuiCallbackContext.h"
20+
#include "ControlWebSocketHandler.h"
2021
#include "HTTPParser.h"
2122
#include <algorithm>
2223
#include <atomic>
@@ -212,6 +213,7 @@ void populateHeader(std::map<std::string, std::string>& headers, std::string_vie
212213
void remoteGuiCallback(uv_timer_s* ctx)
213214
{
214215
GuiRenderer* renderer = reinterpret_cast<GuiRenderer*>(ctx->data);
216+
assert(renderer);
215217

216218
void* frame = nullptr;
217219
void* draw_data = nullptr;
@@ -220,9 +222,10 @@ void remoteGuiCallback(uv_timer_s* ctx)
220222
uint64_t frameLatency = frameStart - renderer->gui->frameLast;
221223

222224
// if less than 15ms have passed reuse old frame
223-
if (frameLatency / 1000000 > 15) {
225+
if (renderer->gui->lastFrame == nullptr || frameLatency / 1000000 > 15) {
224226
renderer->gui->plugin->pollGUIPreRender(renderer->gui->window, (float)frameLatency / 1000000000.0f);
225227
draw_data = renderer->gui->plugin->pollGUIRender(renderer->gui->callback);
228+
renderer->gui->plugin->pollGUIPostRender(renderer->gui->window, draw_data);
226229
} else {
227230
draw_data = renderer->gui->lastFrame;
228231
}
@@ -288,6 +291,16 @@ void WSDPLHandler::endHeaders()
288291
}
289292
} else {
290293
LOG(info) << "Connection not bound to a PID";
294+
GuiRenderer* renderer = new GuiRenderer;
295+
renderer->gui = mServerContext->gui;
296+
renderer->handler = this;
297+
uv_timer_init(mServerContext->loop, &(renderer->drawTimer));
298+
renderer->drawTimer.data = renderer;
299+
uv_timer_start(&(renderer->drawTimer), remoteGuiCallback, 0, 200);
300+
mHandler = std::make_unique<GUIWebSocketHandler>(*mServerContext, renderer);
301+
mHandler->headers(mHeaders);
302+
mServerContext->gui->renderers.insert(renderer);
303+
LOGP(info, "RemoteGUI connected, {} running", mServerContext->gui->renderers.size());
291304
}
292305
}
293306

Framework/Core/src/GuiCallbackContext.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ class WSDPLHandler;
2525

2626
struct GuiRenderer {
2727
uv_timer_t drawTimer;
28-
WSDPLHandler* handler;
29-
GuiCallbackContext* gui;
28+
WSDPLHandler* handler = nullptr;
29+
GuiCallbackContext* gui = nullptr;
3030
};
3131

3232
struct GuiCallbackContext {
3333
uint64_t frameLast;
34-
float* frameLatency;
35-
float* frameCost;
36-
void* lastFrame;
37-
DebugGUI* plugin;
38-
void* window;
39-
bool* guiQuitRequested;
34+
float* frameLatency = nullptr;
35+
float* frameCost = nullptr;
36+
void* lastFrame = nullptr;
37+
DebugGUI* plugin = nullptr;
38+
void* window = nullptr;
39+
bool* guiQuitRequested = nullptr;
4040
std::function<void(void)> callback;
4141
std::set<GuiRenderer*> renderers;
4242
};

Framework/Core/src/HTTPParser.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ std::string encode_websocket_handshake_reply(char const* nonce)
220220
"HTTP/1.1 101 Switching Protocols\r\n"
221221
"Upgrade: websocket\r\n"
222222
"Connection: Upgrade\r\n"
223+
"Access-Control-Allow-Origin: \"*\"\r\n"
223224
"Sec-WebSocket-Accept: {}\r\n\r\n";
224225
return fmt::format(res, HTTPParserHelpers::calculateAccept(nonce));
225226
}

Framework/Core/test/test_HTTPParser.cxx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ BOOST_AUTO_TEST_CASE(HTTPParser1)
168168
"HTTP/1.1 101 Switching Protocols\r\n"
169169
"Upgrade: websocket\r\n"
170170
"Connection: Upgrade\r\n"
171+
"Access-Control-Allow-Origin: \"*\"\r\n"
171172
"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n");
172173

173174
DPLClientParser parser;
@@ -176,7 +177,7 @@ BOOST_AUTO_TEST_CASE(HTTPParser1)
176177
BOOST_REQUIRE_EQUAL(std::string(parser.mReplyCode), std::string("101"));
177178
BOOST_REQUIRE_EQUAL(std::string(parser.mReplyMessage), std::string("Switching Protocols"));
178179
BOOST_REQUIRE_EQUAL(std::string(parser.mReplyVersion), std::string("HTTP/1.1"));
179-
BOOST_REQUIRE_EQUAL(parser.mHeaders.size(), 3);
180+
BOOST_REQUIRE_EQUAL(parser.mHeaders.size(), 4);
180181
BOOST_REQUIRE_EQUAL(parser.mHeaders["Sec-WebSocket-Accept"], "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
181182
BOOST_REQUIRE_EQUAL(parser.mBody, "");
182183
}
@@ -329,6 +330,7 @@ BOOST_AUTO_TEST_CASE(HTTPParser1)
329330
"HTTP/1.1 101 Switching Protocols\r\n"
330331
"Upgrade: websocket\r\n"
331332
"Connection: Upgrade\r\n"
333+
"Access-Control-Allow-Origin: \"*\"\r\n"
332334
"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n";
333335
int someSeed = 123;
334336
std::string result = encode_websocket_handshake_request("/chat", "myprotocol", 13, "dGhlIHNhbXBsZSBub25jZQ==");

0 commit comments

Comments
 (0)