diff options
author | Alexander Tkachev | 2016-06-16 19:34:57 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | 43071c09723472483af4b69cf454ca75a8cd4613 (patch) | |
tree | 857d4e07bf836d1841e1d68dd2803831ab80f59d /backends | |
parent | 0def9c50a7d05fc1743b622ffb5fdbba58e697e6 (diff) | |
download | scummvm-rg350-43071c09723472483af4b69cf454ca75a8cd4613.tar.gz scummvm-rg350-43071c09723472483af4b69cf454ca75a8cd4613.tar.bz2 scummvm-rg350-43071c09723472483af4b69cf454ca75a8cd4613.zip |
CLOUD: Update LocalWebserver
* fix handling connections;
* fix idling strategy;
* add setClientGetHandler() for SeekableReadStream;
* add determineMimeType().
Diffstat (limited to 'backends')
-rw-r--r-- | backends/networking/sdl_net/localwebserver.cpp | 59 | ||||
-rw-r--r-- | backends/networking/sdl_net/localwebserver.h | 9 |
2 files changed, 53 insertions, 15 deletions
diff --git a/backends/networking/sdl_net/localwebserver.cpp b/backends/networking/sdl_net/localwebserver.cpp index 971f00af2a..21ffe6786b 100644 --- a/backends/networking/sdl_net/localwebserver.cpp +++ b/backends/networking/sdl_net/localwebserver.cpp @@ -40,7 +40,8 @@ DECLARE_SINGLETON(Networking::LocalWebserver); namespace Networking { -LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false), _stopOnIdle(false), _clients(0) { +LocalWebserver::LocalWebserver(): _set(nullptr), _serverSocket(nullptr), _timerStarted(false), + _stopOnIdle(false), _clients(0), _idlingFrames(0) { _indexPageHandler.addPathHandler(*this); } @@ -135,15 +136,15 @@ 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(); - } + _clients = 0; + for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) + if (_client[i].state() != INVALID) + ++_clients; + + if (_clients == 0) ++_idlingFrames; + else _idlingFrames = 0; + + if (_idlingFrames > FRAMES_PER_SECOND && _stopOnIdle) stop(); } void LocalWebserver::handleClient(uint32 i) { @@ -179,17 +180,47 @@ void LocalWebserver::acceptClient() { SDLNet_TCP_Close(client); return; } - - _client[_clients++].open(_set, client); + + ++_clients; + for (uint32 i = 0; i < MAX_CONNECTIONS; ++i) + if (_client[i].state() == INVALID) { + _client[i].open(_set, client); + break; + } } -void LocalWebserver::setClientGetHandler(Client &client, Common::String response, long code) { +void LocalWebserver::setClientGetHandler(Client &client, Common::String response, long code, const char *mimeType) { byte *data = new byte[response.size()]; memcpy(data, response.c_str(), response.size()); Common::MemoryReadStream *stream = new Common::MemoryReadStream(data, response.size(), DisposeAfterUse::YES); - GetClientHandler *handler = new GetClientHandler(stream); + setClientGetHandler(client, stream, code, mimeType); +} + +void LocalWebserver::setClientGetHandler(Client &client, Common::SeekableReadStream *responseStream, long code, const char *mimeType) { + GetClientHandler *handler = new GetClientHandler(responseStream); handler->setResponseCode(code); + if (mimeType) handler->setHeader("Content-Type", mimeType); client.setHandler(handler); } +const char *LocalWebserver::determineMimeType(Common::String &filename) { + // text + if (filename.hasSuffix(".html")) return "text/html"; + if (filename.hasSuffix(".css")) return "text/css"; + if (filename.hasSuffix(".txt")) return "text/plain"; + if (filename.hasSuffix(".js")) return "application/javascript"; + + // images + if (filename.hasSuffix(".jpeg") || filename.hasSuffix(".jpg") || filename.hasSuffix(".jpe")) return "image/jpeg"; + if (filename.hasSuffix(".gif")) return "image/gif"; + if (filename.hasSuffix(".png")) return "image/png"; + if (filename.hasSuffix(".svg")) return "image/svg+xml"; + if (filename.hasSuffix(".tiff")) return "image/tiff"; + if (filename.hasSuffix(".ico")) return "image/vnd.microsoft.icon"; + if (filename.hasSuffix(".wbmp")) return "image/vnd.wap.wbmp"; + + if (filename.hasSuffix(".zip")) return "application/zip"; + return "application/octet-stream"; +} + } // End of namespace Networking diff --git a/backends/networking/sdl_net/localwebserver.h b/backends/networking/sdl_net/localwebserver.h index 2ad83f7b7a..f51c33b418 100644 --- a/backends/networking/sdl_net/localwebserver.h +++ b/backends/networking/sdl_net/localwebserver.h @@ -30,6 +30,10 @@ #include "common/singleton.h" #include "common/scummsys.h" +namespace Common { +class SeekableReadStream; +} + typedef struct _SDLNet_SocketSet *SDLNet_SocketSet; typedef struct _TCPsocket *TCPsocket; @@ -52,6 +56,7 @@ class LocalWebserver : public Common::Singleton<LocalWebserver> { bool _timerStarted, _stopOnIdle; Common::HashMap<Common::String, ClientHandler> _pathHandlers; IndexPageHandler _indexPageHandler; + uint32 _idlingFrames; void startTimer(int interval = TIMER_INTERVAL); void stopTimer(); @@ -71,7 +76,9 @@ public: IndexPageHandler &indexPageHandler(); - static void setClientGetHandler(Client &client, Common::String response, long code = 200); + 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); + static const char *determineMimeType(Common::String &filename); }; /** Shortcut for accessing the local webserver. */ |