ScummVM API documentation
localwebserver.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H
23 #define BACKENDS_NETWORKING_SDL_NET_LOCALWEBSERVER_H
24 
25 #include "backends/networking/sdl_net/client.h"
26 #include "backends/networking/sdl_net/handlers/basehandler.h"
27 #include "backends/networking/sdl_net/handlers/createdirectoryhandler.h"
28 #include "backends/networking/sdl_net/handlers/downloadfilehandler.h"
29 #include "backends/networking/sdl_net/handlers/filesajaxpagehandler.h"
30 #include "backends/networking/sdl_net/handlers/filespagehandler.h"
31 #include "backends/networking/sdl_net/handlers/indexpagehandler.h"
32 #include "backends/networking/sdl_net/handlers/listajaxhandler.h"
33 #include "backends/networking/sdl_net/handlers/resourcehandler.h"
34 #include "backends/networking/sdl_net/handlers/uploadfilehandler.h"
35 #include "common/hash-str.h"
36 #include "common/mutex.h"
37 #include "common/singleton.h"
38 #include "common/scummsys.h"
39 
40 #ifdef USE_CLOUD
41 #include "backends/networking/sdl_net/handlers/connectcloudhandler.h"
42 #endif // USE_CLOUD
43 
44 namespace Common {
45 class SeekableReadStream;
46 }
47 
48 typedef struct _SDLNet_SocketSet *SDLNet_SocketSet;
49 typedef struct _TCPsocket *TCPsocket;
50 
51 namespace Networking {
52 
53 #define NETWORKING_LOCALWEBSERVER_ENABLE_PORT_OVERRIDE
54 
55 class LocalWebserver : public Common::Singleton<LocalWebserver> {
56  static const uint32 FRAMES_PER_SECOND = 20;
57  static const uint32 TIMER_INTERVAL = 1000000 / FRAMES_PER_SECOND;
58  static const uint32 MAX_CONNECTIONS = 10;
59 
60  friend void localWebserverTimer(void *); //calls handle()
61 
62  SDLNet_SocketSet _set;
63  TCPsocket _serverSocket;
64  Client _client[MAX_CONNECTIONS];
65  uint32 _clients;
66  bool _timerStarted, _stopOnIdle, _minimalMode;
68  BaseHandler *_defaultHandler;
69  IndexPageHandler _indexPageHandler;
70  FilesPageHandler _filesPageHandler;
71  CreateDirectoryHandler _createDirectoryHandler;
72  DownloadFileHandler _downloadFileHandler;
73  UploadFileHandler _uploadFileHandler;
74  ListAjaxHandler _listAjaxHandler;
75  FilesAjaxPageHandler _filesAjaxPageHandler;
76 #ifdef USE_CLOUD
77  ConnectCloudHandler _connectCloudHandler;
78 #endif // USE_CLOUD
79  ResourceHandler _resourceHandler;
80  uint32 _idlingFrames;
81  Common::Mutex _handleMutex;
82  Common::String _address;
83  uint32 _serverPort;
84 
85  void startTimer(int interval = TIMER_INTERVAL);
86  void stopTimer();
87  void handle();
88  void handleClient(uint32 i);
89  void acceptClient();
90  void resolveAddress(void *ipAddress);
91  void addPathHandler(const Common::String &path, BaseHandler *handler);
92 
93 public:
94  static const uint32 DEFAULT_SERVER_PORT = 12345;
95 
97  ~LocalWebserver() override;
98 
99  void start(bool useMinimalMode = false);
100  void stop();
101  void stopOnIdle();
102 
103  Common::String getAddress();
104  IndexPageHandler &indexPageHandler();
105  bool isRunning();
106  static uint32 getPort();
107 
108 #ifdef USE_CLOUD
109  void setStorageConnectionCallback(Networking::ErrorCallback cb) { _connectCloudHandler.setStorageConnectionCallback(cb); }
110 #endif // USE_CLOUD
111 
112  static void setClientGetHandler(Client &client, const Common::String &response, long code = 200, const char *mimeType = nullptr);
113  static void setClientGetHandler(Client &client, Common::SeekableReadStream *responseStream, long code = 200, const char *mimeType = nullptr);
114  static void setClientRedirectHandler(Client &client, const Common::String &response, const Common::String &location, const char *mimeType = nullptr);
115  static void setClientRedirectHandler(Client &client, Common::SeekableReadStream *responseStream, const Common::String &location, const char *mimeType = nullptr);
116  static Common::String urlDecode(const Common::String &value);
117  static Common::String urlEncodeQueryParameterValue(const Common::String &value);
118 };
119 
121 #define LocalServer Networking::LocalWebserver::instance()
122 
123 } // End of namespace Networking
124 
125 #endif
Definition: connectcloudhandler.h:32
Definition: resourcehandler.h:29
Definition: client.h:75
Definition: str.h:59
Definition: localwebserver.h:55
Definition: createdirectoryhandler.h:29
Definition: scummvmcloud.h:32
Definition: uploadfilehandler.h:29
Definition: basehandler.h:29
Definition: stream.h:745
Definition: downloadfilehandler.h:29
Definition: hashmap.h:85
Definition: filesajaxpagehandler.h:29
Definition: algorithm.h:29
Definition: filespagehandler.h:29
Definition: mutex.h:67
Definition: callback.h:49
Definition: listajaxhandler.h:30
Definition: indexpagehandler.h:31
Definition: singleton.h:42