diff options
author | Alexander Tkachev | 2016-06-15 16:38:37 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 0af97e59bc63538d18d2241285b68ed287ccd87c (patch) | |
tree | dd6a0a2ef7cfc0346365d380e27eeddd4f2218ae | |
parent | 9f7bea156aa4c763fd637a9a12ff89e124b51a6c (diff) | |
download | scummvm-rg350-0af97e59bc63538d18d2241285b68ed287ccd87c.tar.gz scummvm-rg350-0af97e59bc63538d18d2241285b68ed287ccd87c.tar.bz2 scummvm-rg350-0af97e59bc63538d18d2241285b68ed287ccd87c.zip |
CLOUD: Add LocalWebserver
Available as LocalServer singleton. It's being started and stopped by
StorageWizardDialog. It doesn't handle clients yet, though.
-rw-r--r-- | backends/cloud/cloudmanager.cpp | 11 | ||||
-rw-r--r-- | backends/module.mk | 5 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.cpp | 144 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.h | 65 | ||||
-rw-r--r-- | base/main.cpp | 6 | ||||
-rw-r--r-- | gui/storagewizarddialog.cpp | 20 | ||||
-rw-r--r-- | gui/storagewizarddialog.h | 4 |
7 files changed, 242 insertions, 13 deletions
diff --git a/backends/cloud/cloudmanager.cpp b/backends/cloud/cloudmanager.cpp index 8b104dfb20..96521178ed 100644 --- a/backends/cloud/cloudmanager.cpp +++ b/backends/cloud/cloudmanager.cpp @@ -253,17 +253,8 @@ SavesSyncRequest *CloudManager::syncSaves(Storage::BoolCallback callback, Networ } void CloudManager::testFeature() { - Storage *storage = getCurrentStorage(); + //Storage *storage = getCurrentStorage(); //if (storage) storage->info(nullptr, nullptr); - GoogleDrive::GoogleDriveStorage *gd = dynamic_cast<GoogleDrive::GoogleDriveStorage *>(storage); - if (gd) { - } - //gd->resolveFileId("firstfolder/subfolder", nullptr, nullptr); - //gd->listDirectoryById("appDataFolder", nullptr, nullptr); - //gd->listDirectoryById("1LWq-r1IwegkJJ0eZpswGlyjj8nu6XyUmosvxD7L0F9X3", nullptr, nullptr); - //gd->createDirectoryWithParentId("1LWq-r1IwegkJJ0eZpswGlyjj8nu6XyUmosvxD7L0F9X3", "subfolder", nullptr, nullptr); - //gd->createDirectoryWithParentId("appDataFolder", "firstfolder", nullptr, nullptr); - else debug("FAILURE"); } bool CloudManager::isWorking() { diff --git a/backends/module.mk b/backends/module.mk index 95334fef65..6fb8758650 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -58,6 +58,11 @@ MODULE_OBJS += \ networking/curl/request.o endif +ifdef USE_SDL_NET +MODULE_OBJS += \ + networking/sdl_net/localwebserver.o +endif + ifdef USE_ELF_LOADER MODULE_OBJS += \ plugins/elf/arm-loader.o \ diff --git a/backends/networking/sdl_net/localwebserver.cpp b/backends/networking/sdl_net/localwebserver.cpp new file mode 100644 index 0000000000..681485701b --- /dev/null +++ b/backends/networking/sdl_net/localwebserver.cpp @@ -0,0 +1,144 @@ +/* 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. +* +*/ + +#define FORBIDDEN_SYMBOL_ALLOW_ALL + +#include "backends/networking/sdl_net/localwebserver.h" +#include "common/str.h" +#include "common/system.h" +#include "common/timer.h" +#include "common/textconsole.h" +#include <SDL/SDL_net.h> + +namespace Common { + +DECLARE_SINGLETON(Networking::LocalWebserver); + +} + +namespace Networking { + +LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false), _clients(0) { + for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) + _clientSocket[i] = nullptr; +} + +LocalWebserver::~LocalWebserver() { + stop(); +} + +void localWebserverTimer(void *ignored) { + LocalServer.handle(); +} + +void LocalWebserver::startTimer(int interval) { + Common::TimerManager *manager = g_system->getTimerManager(); + if (manager->installTimerProc(localWebserverTimer, interval, 0, "Networking::LocalWebserver's Timer")) { + _timerStarted = true; + } else { + warning("Failed to install Networking::LocalWebserver's timer"); + } +} + +void LocalWebserver::stopTimer() { + Common::TimerManager *manager = g_system->getTimerManager(); + manager->removeTimerProc(localWebserverTimer); + _timerStarted = false; +} + +void LocalWebserver::start() { + if (_timerStarted) return; + startTimer(); + + // Create a listening TCP socket + IPaddress ip; + if (SDLNet_ResolveHost(&ip, NULL, SERVER_PORT) == -1) { + error("SDLNet_ResolveHost: %s\n", SDLNet_GetError()); + } + _serverSocket = SDLNet_TCP_Open(&ip); + if (!_serverSocket) { + error("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); + } + + // Create a socket set + _set = SDLNet_AllocSocketSet(MAX_CONNECTIONS + 1); //one more for our server socket + if (!_set) { + error("SDLNet_AllocSocketSet: %s\n", SDLNet_GetError()); + } + + int numused = SDLNet_TCP_AddSocket(_set, _serverSocket); + if (numused == -1) { + error("SDLNet_AddSocket: %s\n", SDLNet_GetError()); + } +} + +void LocalWebserver::stop() { + if (_timerStarted) stopTimer(); + + if (_set) { + SDLNet_FreeSocketSet(_set); + _set = nullptr; + } + + if (_serverSocket) { + SDLNet_TCP_Close(_serverSocket); + _serverSocket = nullptr; + } + + for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) + if (_clientSocket[i]) { + SDLNet_TCP_Close(_clientSocket[i]); + _clientSocket[i] = nullptr; + } + + _clients = 0; +} + +void LocalWebserver::handle() { + int numready = SDLNet_CheckSockets(_set, 0); + if (numready == -1) { + error("SDLNet_CheckSockets: %s\n", SDLNet_GetError()); + } else if (numready) { + if (SDLNet_SocketReady(_serverSocket)) { + TCPsocket client = SDLNet_TCP_Accept(_serverSocket); + if (client) { + if (_clients == MAX_CONNECTIONS) { //drop the connection + SDLNet_TCP_Close(client); + } else { + int numused = SDLNet_TCP_AddSocket(_set, _serverSocket); + if (numused == -1) { + error("SDLNet_AddSocket: %s\n", SDLNet_GetError()); + } + _clientSocket[_clients++] = client; + } + } + } + + for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) { + if (!_clientSocket[i]) continue; + if (!SDLNet_SocketReady(_clientSocket[i])) continue; + //TODO: handle client + } + } +} + +} // End of namespace Networking diff --git a/backends/networking/sdl_net/localwebserver.h b/backends/networking/sdl_net/localwebserver.h new file mode 100644 index 0000000000..a11be0a938 --- /dev/null +++ b/backends/networking/sdl_net/localwebserver.h @@ -0,0 +1,65 @@ +/* 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_LOCALWEBSERVER_H +#define BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H + +#include "common/singleton.h" +#include "common/scummsys.h" + +typedef struct _SDLNet_SocketSet *SDLNet_SocketSet; +typedef struct _TCPsocket *TCPsocket; + +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 MAX_CONNECTIONS = 10; + + friend void localWebserverTimer(void *); //calls handle() + + SDLNet_SocketSet _set; + TCPsocket _serverSocket; + TCPsocket _clientSocket[MAX_CONNECTIONS]; + int _clients; + bool _timerStarted; + + void startTimer(int interval = TIMER_INTERVAL); + void stopTimer(); + void handle(); + +public: + LocalWebserver(); + virtual ~LocalWebserver(); + + void start(); + void stop(); +}; + +/** Shortcut for accessing the local webserver. */ +#define LocalServer Networking::LocalWebserver::instance() + +} // End of namespace Networking + +#endif diff --git a/base/main.cpp b/base/main.cpp index 4edc7a957a..6c91305381 100644 --- a/base/main.cpp +++ b/base/main.cpp @@ -72,6 +72,9 @@ #ifdef USE_LIBCURL #include "backends/networking/curl/connectionmanager.h" #endif +#ifdef USE_SDL_NET +#include "backends/networking/sdl_net/localwebserver.h" +#endif #if defined(_WIN32_WCE) #include "backends/platform/wince/CELauncherDialog.h" @@ -596,6 +599,9 @@ extern "C" int scummvm_main(int argc, const char * const argv[]) { launcherDialog(); } } +#ifdef USE_SDL_NET + Networking::LocalWebserver::destroy(); +#endif #ifdef USE_LIBCURL Networking::ConnectionManager::destroy(); #endif diff --git a/gui/storagewizarddialog.cpp b/gui/storagewizarddialog.cpp index 46be812f6d..579591ba18 100644 --- a/gui/storagewizarddialog.cpp +++ b/gui/storagewizarddialog.cpp @@ -24,9 +24,11 @@ #include "gui/widgets/list.h" #include "gui/widget.h" #include "gui/gui-manager.h" - -#include "common/translation.h" #include "backends/cloud/cloudmanager.h" +#ifdef USE_SDL_NET +#include "backends/networking/sdl_net/localwebserver.h" +#endif +#include "common/translation.h" #include "widgets/edittext.h" namespace GUI { @@ -64,6 +66,20 @@ StorageWizardDialog::StorageWizardDialog(uint32 storageId): Dialog("GlobalOption _connectWidget = new ButtonWidget(this, "GlobalOptions_Cloud_ConnectionWizard.ConnectButton", _("Connect"), 0, kConnectCmd); } +void StorageWizardDialog::open() { + Dialog::open(); +#ifdef USE_SDL_NET + LocalServer.start(); +#endif +} + +void StorageWizardDialog::close() { +#ifdef USE_SDL_NET + LocalServer.stop(); +#endif + Dialog::close(); +} + void StorageWizardDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) { switch (cmd) { case kCodeBoxCmd: { diff --git a/gui/storagewizarddialog.h b/gui/storagewizarddialog.h index 93e368463c..fa45f922d9 100644 --- a/gui/storagewizarddialog.h +++ b/gui/storagewizarddialog.h @@ -66,7 +66,9 @@ class StorageWizardDialog : public Dialog { public: StorageWizardDialog(uint32 storageId); - void handleCommand(CommandSender *sender, uint32 cmd, uint32 data); + virtual void open(); + virtual void close(); + virtual void handleCommand(CommandSender *sender, uint32 cmd, uint32 data); }; } // End of namespace GUI |