From 39865e6e6cf67e3b5a3867540dde76969b244246 Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Wed, 20 Jul 2016 15:55:30 +0600 Subject: CLOUD: Add port override for LocalWebserver It's enabled only when NETWORKING_LOCALWEBSERVER_ENABLE_PORT_OVERRIDE is defined. It's not defined, because override means we have to reconfigure our redirect links somehow to use the override port. --- backends/networking/sdl_net/localwebserver.cpp | 20 ++++++-- backends/networking/sdl_net/localwebserver.h | 4 +- gui/options.cpp | 53 +++++++++++++++++++++- gui/options.h | 3 ++ gui/themes/scummclassic/classic_layout.stx | 9 ++++ gui/themes/scummclassic/classic_layout_lowres.stx | 11 +++++ gui/themes/scummmodern/scummmodern_layout.stx | 9 ++++ .../scummmodern/scummmodern_layout_lowres.stx | 11 +++++ 8 files changed, 112 insertions(+), 8 deletions(-) diff --git a/backends/networking/sdl_net/localwebserver.cpp b/backends/networking/sdl_net/localwebserver.cpp index 1480418b70..7c3edfc17f 100644 --- a/backends/networking/sdl_net/localwebserver.cpp +++ b/backends/networking/sdl_net/localwebserver.cpp @@ -30,6 +30,7 @@ #include "common/timer.h" #include "common/translation.h" #include +#include #ifdef POSIX #include @@ -48,7 +49,7 @@ DECLARE_SINGLETON(Networking::LocalWebserver); namespace Networking { LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false), - _stopOnIdle(false), _clients(0), _idlingFrames(0) { + _stopOnIdle(false), _clients(0), _idlingFrames(0), _serverPort(DEFAULT_SERVER_PORT) { addPathHandler("/", _indexPageHandler.getHandler()); addPathHandler("/files", _filesPageHandler.getHandler()); addPathHandler("/create", _createDirectoryHandler.getHandler()); @@ -84,6 +85,7 @@ void LocalWebserver::stopTimer() { void LocalWebserver::start() { _handleMutex.lock(); + _serverPort = getPort(); _stopOnIdle = false; if (_timerStarted) { _handleMutex.unlock(); @@ -93,7 +95,7 @@ void LocalWebserver::start() { // Create a listening TCP socket IPaddress ip; - if (SDLNet_ResolveHost(&ip, NULL, SERVER_PORT) == -1) { + if (SDLNet_ResolveHost(&ip, NULL, _serverPort) == -1) { error("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); } @@ -166,6 +168,14 @@ bool LocalWebserver::isRunning() { return result; } +uint32 LocalWebserver::getPort() { +#ifdef NETWORKING_LOCALWEBSERVER_ENABLE_PORT_OVERRIDE + if (ConfMan.hasKey("local_server_port")) + return ConfMan.getInt("local_server_port"); +#endif + return DEFAULT_SERVER_PORT; +} + void LocalWebserver::handle() { _handleMutex.lock(); int numready = SDLNet_CheckSockets(_set, 0); @@ -241,7 +251,7 @@ void LocalWebserver::resolveAddress(void *ipAddress) { IPaddress *ip = (IPaddress *)ipAddress; // not resolved - _address = Common::String::format("http://127.0.0.1:%u/ (unresolved)", SERVER_PORT); + _address = Common::String::format("http://127.0.0.1:%u/ (unresolved)", _serverPort); // default way (might work everywhere, surely works on Windows) const char *name = SDLNet_ResolveIP(ip); @@ -249,13 +259,13 @@ void LocalWebserver::resolveAddress(void *ipAddress) { warning("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); } else { IPaddress localIp; - if (SDLNet_ResolveHost(&localIp, name, SERVER_PORT) == -1) { + if (SDLNet_ResolveHost(&localIp, name, _serverPort) == -1) { warning("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); } else { _address = Common::String::format( "http://%u.%u.%u.%u:%u/", localIp.host & 0xFF, (localIp.host >> 8) & 0xFF, (localIp.host >> 16) & 0xFF, (localIp.host >> 24) & 0xFF, - SERVER_PORT + _serverPort ); } } diff --git a/backends/networking/sdl_net/localwebserver.h b/backends/networking/sdl_net/localwebserver.h index 1185cda890..3512583605 100644 --- a/backends/networking/sdl_net/localwebserver.h +++ b/backends/networking/sdl_net/localwebserver.h @@ -50,7 +50,7 @@ namespace Networking { class LocalWebserver : public Common::Singleton { static const uint32 FRAMES_PER_SECOND = 20; static const uint32 TIMER_INTERVAL = 1000000 / FRAMES_PER_SECOND; - static const uint32 SERVER_PORT = 12345; + static const uint32 DEFAULT_SERVER_PORT = 12345; static const uint32 MAX_CONNECTIONS = 10; friend void localWebserverTimer(void *); //calls handle() @@ -73,6 +73,7 @@ class LocalWebserver : public Common::Singleton { uint32 _idlingFrames; Common::Mutex _handleMutex; Common::String _address; + uint32 _serverPort; void startTimer(int interval = TIMER_INTERVAL); void stopTimer(); @@ -95,6 +96,7 @@ public: Common::String getAddress(); IndexPageHandler &indexPageHandler(); bool isRunning(); + static uint32 getPort(); static void setClientGetHandler(Client &client, Common::String response, long code = 200, const char *mimeType = nullptr); static void setClientGetHandler(Client &client, Common::SeekableReadStream *responseStream, long code = 200, const char *mimeType = nullptr); diff --git a/gui/options.cpp b/gui/options.cpp index ac548f53c7..cbcb42c69c 100644 --- a/gui/options.cpp +++ b/gui/options.cpp @@ -43,6 +43,7 @@ #include "audio/mixer.h" #include "audio/fmopl.h" #include "widgets/scrollcontainer.h" +#include "widgets/edittext.h" #ifdef USE_LIBCURL #include "backends/cloud/cloudmanager.h" @@ -1317,6 +1318,10 @@ GlobalOptionsDialog::GlobalOptionsDialog(LauncherDialog *launcher) _runServerButton = new ButtonWidget(container, "GlobalOptions_Cloud_Container.RunServerButton", _("Run server"), _("Run local webserver"), kRunServerCmd); _serverInfoLabel = new StaticTextWidget(container, "GlobalOptions_Cloud_Container.ServerInfoLabel", _("Not running")); + uint32 port = Networking::LocalWebserver::getPort(); + _serverPortDesc = new StaticTextWidget(container, "GlobalOptions_Cloud_Container.ServerPortDesc", _("Server's port:"), _("Which port is used by server")); + _serverPort = new EditTextWidget(container, "GlobalOptions_Cloud_Container.ServerPortEditText", Common::String::format("%u", port), 0); + setupCloudTab(); _redrawCloudTab = false; #ifdef USE_SDL_NET @@ -1489,7 +1494,17 @@ void GlobalOptionsDialog::close() { } } #endif - +#ifdef USE_SDL_NET +#ifdef NETWORKING_LOCALWEBSERVER_ENABLE_PORT_OVERRIDE + // save server's port + uint32 port = Networking::LocalWebserver::getPort(); + if (_serverPort) { + uint64 contents = _serverPort->getEditString().asUint64(); + if (contents != 0) port = contents; + } + ConfMan.setInt("local_server_port", port); +#endif +#endif } OptionsDialog::close(); } @@ -1647,6 +1662,17 @@ void GlobalOptionsDialog::handleCommand(CommandSender *sender, uint32 cmd, uint3 #ifdef USE_SDL_NET case kRunServerCmd: { +#ifdef NETWORKING_LOCALWEBSERVER_ENABLE_PORT_OVERRIDE + // save server's port + uint32 port = Networking::LocalWebserver::getPort(); + if (_serverPort) { + uint64 contents = _serverPort->getEditString().asUint64(); + if (contents != 0) port = contents; + } + ConfMan.setInt("local_server_port", port); + ConfMan.flushToDisk(); +#endif + if (LocalServer.isRunning()) LocalServer.stopOnIdle(); else LocalServer.start(); break; @@ -1787,13 +1813,19 @@ void GlobalOptionsDialog::setupCloudTab() { //determine original widget's positions int16 x, y; uint16 w, h; - int serverButtonY, serverInfoY; + int serverButtonY, serverInfoY, serverPortDescY, serverPortY; if (!g_gui.xmlEval()->getWidgetData("GlobalOptions_Cloud_Container.RunServerButton", x, y, w, h)) warning("GlobalOptions_Cloud_Container.RunServerButton's position is undefined"); serverButtonY = y; if (!g_gui.xmlEval()->getWidgetData("GlobalOptions_Cloud_Container.ServerInfoLabel", x, y, w, h)) warning("GlobalOptions_Cloud_Container.ServerInfoLabel's position is undefined"); serverInfoY = y; + if (!g_gui.xmlEval()->getWidgetData("GlobalOptions_Cloud_Container.ServerPortDesc", x, y, w, h)) + warning("GlobalOptions_Cloud_Container.ServerPortDesc's position is undefined"); + serverPortDescY = y; + if (!g_gui.xmlEval()->getWidgetData("GlobalOptions_Cloud_Container.ServerPortEditText", x, y, w, h)) + warning("GlobalOptions_Cloud_Container.ServerPortEditText's position is undefined"); + serverPortY = y; bool serverIsRunning = LocalServer.isRunning(); @@ -1809,9 +1841,26 @@ void GlobalOptionsDialog::setupCloudTab() { if (serverIsRunning) _serverInfoLabel->setLabel(LocalServer.getAddress()); else _serverInfoLabel->setLabel(_("Not running")); } +#ifdef NETWORKING_LOCALWEBSERVER_ENABLE_PORT_OVERRIDE + if (_serverPortDesc) { + _serverPortDesc->setVisible(true); + _serverPortDesc->setPos(_serverPortDesc->getRelX(), serverLabelPosition + serverPortDescY - serverInfoY); + _serverPortDesc->setEnabled(!serverIsRunning); + } + if (_serverPort) { + _serverPort->setVisible(true); + _serverPort->setPos(_serverPort->getRelX(), serverLabelPosition + serverPortY - serverInfoY); + _serverPort->setEnabled(!serverIsRunning); + } +#else + if (_serverPortDesc) _serverPortDesc->setVisible(false); + if (_serverPort) _serverPort->setVisible(false); +#endif #else if (_runServerButton) _runServerButton->setVisible(false); if (_serverInfoLabel) _serverInfoLabel->setVisible(false); + if (_serverPortDesc) _serverPortDesc->setVisible(false); + if (_serverPort) _serverPort->setVisible(false); #endif } #endif diff --git a/gui/options.h b/gui/options.h index a7e98adff6..ea8b252db7 100644 --- a/gui/options.h +++ b/gui/options.h @@ -45,6 +45,7 @@ namespace GUI { class LauncherDialog; class CheckboxWidget; +class EditTextWidget; class PopUpWidget; class SliderWidget; class StaticTextWidget; @@ -267,6 +268,8 @@ protected: ButtonWidget *_storageDownloadButton; ButtonWidget *_runServerButton; StaticTextWidget *_serverInfoLabel; + StaticTextWidget *_serverPortDesc; + EditTextWidget *_serverPort; bool _redrawCloudTab; #ifdef USE_SDL_NET bool _serverWasRunning; diff --git a/gui/themes/scummclassic/classic_layout.stx b/gui/themes/scummclassic/classic_layout.stx index 545b3fe357..1e3d09d10e 100644 --- a/gui/themes/scummclassic/classic_layout.stx +++ b/gui/themes/scummclassic/classic_layout.stx @@ -583,6 +583,15 @@ height = 'Globals.Line.Height' /> + + + + diff --git a/gui/themes/scummclassic/classic_layout_lowres.stx b/gui/themes/scummclassic/classic_layout_lowres.stx index 27a3ecd26d..2ae008a80c 100644 --- a/gui/themes/scummclassic/classic_layout_lowres.stx +++ b/gui/themes/scummclassic/classic_layout_lowres.stx @@ -596,6 +596,17 @@ height = 'Globals.Line.Height' /> + + + + diff --git a/gui/themes/scummmodern/scummmodern_layout.stx b/gui/themes/scummmodern/scummmodern_layout.stx index 5f4ef6f14d..03fa58acb7 100644 --- a/gui/themes/scummmodern/scummmodern_layout.stx +++ b/gui/themes/scummmodern/scummmodern_layout.stx @@ -597,6 +597,15 @@ height = 'Globals.Line.Height' /> + + + + diff --git a/gui/themes/scummmodern/scummmodern_layout_lowres.stx b/gui/themes/scummmodern/scummmodern_layout_lowres.stx index 24476e3619..b25edcb2b0 100644 --- a/gui/themes/scummmodern/scummmodern_layout_lowres.stx +++ b/gui/themes/scummmodern/scummmodern_layout_lowres.stx @@ -594,6 +594,17 @@ height = 'Globals.Line.Height' /> + + + + -- cgit v1.2.3