From 38d7b5d6c35c2223631015e51a7173fecb1014dc Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 18 Feb 2016 01:05:58 -0500 Subject: [PATCH] Fix returning to the app screen after stopping the stream. Remove messages that aren't meant to be handled in NaCl. --- main.cpp | 54 ++++++++++++++++------------------------------ static/js/index.js | 12 ++++------- 2 files changed, 22 insertions(+), 44 deletions(-) diff --git a/main.cpp b/main.cpp index a6f5d91..0b5982a 100644 --- a/main.cpp +++ b/main.cpp @@ -6,14 +6,13 @@ #include "ppapi/cpp/input_event.h" -// you pair to a target -#define PAIR_DIRECTIVE "pair:" -// you need to show the apps of a target -#define SHOW_GAMES_DIRECTIVE "showAppsPushed:" -// you need to use a certain target to start a certain gameID -#define START_STREAM_DIRECTIVE "setGFEHostIPField:" -// No parameters. just request a stop. -#define STOP_DIRECTIVE "stopPushed" + +// Requests the NaCl module to connection to the server specified after the : +#define MSG_START_REQUEST "startRequest:" +// Requests the NaCl module stop streaming +#define MSG_STOP_REQUEST "stopRequest" +// Sent by the NaCl module when the stream has stopped whether user-requested or not +#define MSG_STREAM_TERMINATED "streamTerminated" MoonlightInstance* g_Instance; @@ -49,10 +48,9 @@ void MoonlightInstance::OnConnectionStopped(uint32_t error) { pthread_join(g_Instance->m_ConnectionThread, NULL); pthread_join(g_Instance->m_GamepadThread, NULL); - pp::Var response("Connection terminated"); + // Notify the JS code that the stream has ended + pp::Var response(MSG_STREAM_TERMINATED); g_Instance->PostMessage(response); - - printf("Connection teardown complete\n"); } void MoonlightInstance::StopConnection() { @@ -117,36 +115,20 @@ void* MoonlightInstance::ConnectionThreadFunc(void* context) { // hook from javascript into the CPP code. void MoonlightInstance::HandleMessage(const pp::Var& var_message) { + // Ignore the message if it is not a string. if (!var_message.is_string()) - return; // Ignore the message if it is not a string. + return; + std::string message = var_message.AsString(); - - if(strncmp(message.c_str(), PAIR_DIRECTIVE, strlen(PAIR_DIRECTIVE)) == 0) { - handlePair(message); - } else if (strncmp(message.c_str(), SHOW_GAMES_DIRECTIVE, strlen(SHOW_GAMES_DIRECTIVE)) == 0) { - handleShowGames(message); - } else if (strncmp(message.c_str(), START_STREAM_DIRECTIVE, strlen(START_STREAM_DIRECTIVE)) == 0) { + if (message.substr(0, strlen(MSG_START_REQUEST)) == MSG_START_REQUEST) { handleStartStream(message); - } else if (strncmp(message.c_str(), STOP_DIRECTIVE, strlen(STOP_DIRECTIVE)) == 0) { + } else if (message.substr(0, strlen(MSG_STOP_REQUEST)) == MSG_STOP_REQUEST) { handleStopStream(message); } else { pp::Var response("Unhandled message received: " + message); PostMessage(response); } - -} - -void MoonlightInstance::handlePair(std::string pairMessage) { - pp::Var response("Pair button pushed. Pairing is unimplemented."); - PostMessage(response); - std::string intendedHost = pairMessage.substr(pairMessage.find(PAIR_DIRECTIVE) + strlen(PAIR_DIRECTIVE)); -} - -void MoonlightInstance::handleShowGames(std::string showGamesMessage) { - pp::Var response("Show Games button pushed. Show Games is unimplemented"); - PostMessage(response); - std::string host = showGamesMessage.substr(showGamesMessage.find(SHOW_GAMES_DIRECTIVE) + strlen(SHOW_GAMES_DIRECTIVE)); } void MoonlightInstance::handleStartStream(std::string startStreamMessage) { @@ -161,16 +143,16 @@ void MoonlightInstance::handleStartStream(std::string startStreamMessage) { m_ServerMajorVersion = 4; - // Store the host, which is between two colons - m_Host = startStreamMessage.substr(strlen(START_STREAM_DIRECTIVE), startStreamMessage.substr(strlen(START_STREAM_DIRECTIVE)).find(":")); + // Store the host from the start message + m_Host = startStreamMessage.substr(strlen(MSG_START_REQUEST)); // Start the worker thread to establish the connection pthread_create(&m_ConnectionThread, NULL, MoonlightInstance::ConnectionThreadFunc, this); } void MoonlightInstance::handleStopStream(std::string stopStreamMessage) { - pp::Var response("Stop button pushed. Ignoring."); - PostMessage(response); + // Begin connection teardown + StopConnection(); } bool MoonlightInstance::Init(uint32_t argc, diff --git a/static/js/index.js b/static/js/index.js index 726ccd4..88ee339 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -24,7 +24,6 @@ function hideAllWorkflowDivs() { // pair button was pushed. pass what the user entered into the GFEHostIPField. function pairPushed() { - common.naclModule.postMessage('pair:' + document.getElementById('GFEHostIPField').value); } // someone pushed the "show apps" button. @@ -37,7 +36,6 @@ function showAppsPushed() { var e = document.getElementById("selectHost"); target = e.options[e.selectedIndex].value; } - common.naclModule.postMessage('showAppsPushed:' + target); // we just finished the hostSettings section. expose the next one showAppsMode(); } @@ -60,9 +58,7 @@ function startPushed() { var e = document.getElementById("selectHost"); target = e.options[e.selectedIndex].value; } - var gameIDDropdown = document.getElementById("selectGame"); - var gameID = gameIDDropdown[gameIDDropdown.selectedIndex].value; - common.naclModule.postMessage('setGFEHostIPField:' + target + ":" + gameID); + common.naclModule.postMessage('startRequest:' + target); // we just finished the gameSelection section. only expose the NaCl section playGameMode(); } @@ -95,17 +91,17 @@ function fullscreenNaclModule() { // user pushed the stop button. we should stop. function stopPushed() { - common.naclModule.postMessage('stopPushed'); + common.naclModule.postMessage('stopRequested'); } // hook from main.cpp into the javascript function handleMessage(msg) { - var quitStreamString = "quitStream"; + var quitStreamString = "streamTerminated"; var logEl = document.getElementById('logField'); logEl.innerHTML = msg.data; console.log("message received: " + msg.data); if (msg.data.lastIndexOf(quitStreamString, 0) === 0) { - console.log("quit stream received. returning to 'show apps' screen.") + console.log("Stream termination message received. returning to 'show apps' screen.") showAppsMode(); } }