diff options
Diffstat (limited to 'backends')
-rw-r--r-- | backends/module.mk | 1 | ||||
-rw-r--r-- | backends/networking/sdl_net/indexpagehandler.cpp | 56 | ||||
-rw-r--r-- | backends/networking/sdl_net/indexpagehandler.h | 47 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.cpp | 23 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.h | 13 |
5 files changed, 136 insertions, 4 deletions
diff --git a/backends/module.mk b/backends/module.mk index 0f1eea0090..ecdca2e0a7 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -62,6 +62,7 @@ ifdef USE_SDL_NET MODULE_OBJS += \ networking/sdl_net/client.o \ networking/sdl_net/getclienthandler.o \ + networking/sdl_net/indexpagehandler.o \ networking/sdl_net/localwebserver.o endif diff --git a/backends/networking/sdl_net/indexpagehandler.cpp b/backends/networking/sdl_net/indexpagehandler.cpp new file mode 100644 index 0000000000..58c4761f59 --- /dev/null +++ b/backends/networking/sdl_net/indexpagehandler.cpp @@ -0,0 +1,56 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#include "backends/networking/sdl_net/indexpagehandler.h" +#include "backends/networking/sdl_net/localwebserver.h" +#include "gui/storagewizarddialog.h" + +namespace Networking { + +IndexPageHandler::IndexPageHandler(): CommandSender(nullptr) {} + +IndexPageHandler::~IndexPageHandler() { + LocalServer.removePathHandler("/"); +} + +void IndexPageHandler::handle(Client &client) { + Common::String code = client.queryParameter("code"); + + if (code == "") { + LocalWebserver::setClientGetHandler(client, "<html><head><title>ScummVM</title></head><body>This is a local webserver index page.</body></html>"); + return; + } + + _code = code; + sendCommand(GUI::kStorageCodePassedCmd, 0); + LocalWebserver::setClientGetHandler(client, "<html><head><title>ScummVM</title></head><body>ScummVM got the code and already connects to your cloud storage!</body></html>"); +} + +void IndexPageHandler::addPathHandler(LocalWebserver &server) { + // we can't use LocalServer yet, because IndexPageHandler is created while LocalWebserver is created + // (thus no _instance is available and it causes stack overflow) + server.addPathHandler("/", new Common::Callback<IndexPageHandler, Client &>(this, &IndexPageHandler::handle)); +} + +Common::String IndexPageHandler::code() { return _code; } + +} // End of namespace Networking diff --git a/backends/networking/sdl_net/indexpagehandler.h b/backends/networking/sdl_net/indexpagehandler.h new file mode 100644 index 0000000000..5a1d2d7cbc --- /dev/null +++ b/backends/networking/sdl_net/indexpagehandler.h @@ -0,0 +1,47 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifndef BACKENDS_NETWORKING_SDL_NET_INDEXPAGEHANDLER_H +#define BACKENDS_NETWORKING_SDL_NET_INDEXPAGEHANDLER_H + +#include "backends/networking/sdl_net/client.h" +#include "gui/object.h" + +namespace Networking { +class LocalWebserver; + +class IndexPageHandler: public GUI::CommandSender { + Common::String _code; + + void handle(Client &client); + +public: + IndexPageHandler(); + virtual ~IndexPageHandler(); + + void addPathHandler(LocalWebserver &server); + Common::String code(); +}; + +} // End of namespace Networking + +#endif diff --git a/backends/networking/sdl_net/localwebserver.cpp b/backends/networking/sdl_net/localwebserver.cpp index 689bab4cb9..971f00af2a 100644 --- a/backends/networking/sdl_net/localwebserver.cpp +++ b/backends/networking/sdl_net/localwebserver.cpp @@ -40,7 +40,9 @@ DECLARE_SINGLETON(Networking::LocalWebserver); namespace Networking { -LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false), _clients(0) {} +LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false), _stopOnIdle(false), _clients(0) { + _indexPageHandler.addPathHandler(*this); +} LocalWebserver::~LocalWebserver() { stop(); @@ -110,11 +112,20 @@ void LocalWebserver::stop() { } } +void LocalWebserver::stopOnIdle() { _stopOnIdle = true; } + void LocalWebserver::addPathHandler(Common::String path, ClientHandler handler) { if (_pathHandlers.contains(path)) warning("LocalWebserver::addPathHandler: path already had a handler"); _pathHandlers[path] = handler; } +void LocalWebserver::removePathHandler(Common::String path) { + if (!_pathHandlers.contains(path)) warning("LocalWebserver::removePathHandler: no handler known for this path"); + _pathHandlers.erase(path); +} + +IndexPageHandler &LocalWebserver::indexPageHandler() { return _indexPageHandler; } + void LocalWebserver::handle() { int numready = SDLNet_CheckSockets(_set, 0); if (numready == -1) { @@ -123,6 +134,16 @@ void LocalWebserver::handle() { for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) handleClient(i); + + if (_stopOnIdle) { + bool idle = true; + for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) + if (_client[i].state() != INVALID) { + idle = false; + break; + } + if (idle) stop(); + } } void LocalWebserver::handleClient(uint32 i) { diff --git a/backends/networking/sdl_net/localwebserver.h b/backends/networking/sdl_net/localwebserver.h index bda0bfd7c1..2ad83f7b7a 100644 --- a/backends/networking/sdl_net/localwebserver.h +++ b/backends/networking/sdl_net/localwebserver.h @@ -24,6 +24,7 @@ #define BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H #include "backends/networking/sdl_net/client.h" +#include "backends/networking/sdl_net/indexpagehandler.h" #include "common/callback.h" #include "common/hash-str.h" #include "common/singleton.h" @@ -48,23 +49,29 @@ class LocalWebserver : public Common::Singleton<LocalWebserver> { TCPsocket _serverSocket; Client _client[MAX_CONNECTIONS]; int _clients; - bool _timerStarted; + bool _timerStarted, _stopOnIdle; Common::HashMap<Common::String, ClientHandler> _pathHandlers; + IndexPageHandler _indexPageHandler; void startTimer(int interval = TIMER_INTERVAL); void stopTimer(); void handle(); void handleClient(uint32 i); void acceptClient(); - void setClientGetHandler(Client &client, Common::String response, long code = 200); - + public: LocalWebserver(); virtual ~LocalWebserver(); void start(); void stop(); + void stopOnIdle(); void addPathHandler(Common::String path, ClientHandler handler); + void removePathHandler(Common::String path); + + IndexPageHandler &indexPageHandler(); + + static void setClientGetHandler(Client &client, Common::String response, long code = 200); }; /** Shortcut for accessing the local webserver. */ |