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.
This commit is contained in:
Alexander Tkachev 2016-07-20 15:55:30 +06:00
parent b1264df120
commit 39865e6e6c
8 changed files with 112 additions and 8 deletions

View File

@ -30,6 +30,7 @@
#include "common/timer.h"
#include "common/translation.h"
#include <SDL/SDL_net.h>
#include <common/config-manager.h>
#ifdef POSIX
#include <sys/types.h>
@ -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
);
}
}

View File

@ -50,7 +50,7 @@ namespace Networking {
class LocalWebserver : public Common::Singleton<LocalWebserver> {
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<LocalWebserver> {
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);

View File

@ -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

View File

@ -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;

View File

@ -583,6 +583,15 @@
height = 'Globals.Line.Height'
/>
</layout>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '10' center = 'true'>
<widget name = 'ServerPortDesc'
type = 'OptionsLabel'
/>
<widget name = 'ServerPortEditText'
width = '70'
height = 'Globals.Line.Height'
/>
</layout>
</layout>
</dialog>

View File

@ -596,6 +596,17 @@
height = 'Globals.Line.Height'
/>
</layout>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '6' center = 'true'>
<widget name = 'ServerPortDesc'
width = '80'
height = 'Globals.Line.Height'
textalign = 'right'
/>
<widget name = 'ServerPortEditText'
width = '60'
height = '16'
/>
</layout>
</layout>
</dialog>

View File

@ -597,6 +597,15 @@
height = 'Globals.Line.Height'
/>
</layout>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '10' center = 'true'>
<widget name = 'ServerPortDesc'
type = 'OptionsLabel'
/>
<widget name = 'ServerPortEditText'
width = '70'
height = 'Globals.Line.Height'
/>
</layout>
</layout>
</dialog>

View File

@ -594,6 +594,17 @@
height = 'Globals.Line.Height'
/>
</layout>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '6' center = 'true'>
<widget name = 'ServerPortDesc'
width = '80'
height = 'Globals.Line.Height'
textalign = 'right'
/>
<widget name = 'ServerPortEditText'
width = '60'
height = '16'
/>
</layout>
</layout>
</dialog>