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 #ifdef USE_LIBCURL
42 #include "backends/networking/sdl_net/handlers/connectcloudhandler.h"
43 #endif // USE_LIBCURL
44 #endif // USE_CLOUD
45 
46 namespace Common {
47 class SeekableReadStream;
48 }
49 
50 typedef struct _SDLNet_SocketSet *SDLNet_SocketSet;
51 typedef struct _TCPsocket *TCPsocket;
52 
53 namespace Networking {
54 
55 #define NETWORKING_LOCALWEBSERVER_ENABLE_PORT_OVERRIDE
56 
57 class LocalWebserver : public Common::Singleton<LocalWebserver> {
58  static const uint32 FRAMES_PER_SECOND = 20;
59  static const uint32 TIMER_INTERVAL = 1000000 / FRAMES_PER_SECOND;
60  static const uint32 MAX_CONNECTIONS = 10;
61 
62  friend void localWebserverTimer(void *); //calls handle()
63 
64  SDLNet_SocketSet _set;
65  TCPsocket _serverSocket;
66  Client _client[MAX_CONNECTIONS];
67  uint32 _clients;
68  bool _timerStarted, _stopOnIdle, _minimalMode;
70  BaseHandler *_defaultHandler;
71  IndexPageHandler _indexPageHandler;
72  FilesPageHandler _filesPageHandler;
73  CreateDirectoryHandler _createDirectoryHandler;
74  DownloadFileHandler _downloadFileHandler;
75  UploadFileHandler _uploadFileHandler;
76  ListAjaxHandler _listAjaxHandler;
77  FilesAjaxPageHandler _filesAjaxPageHandler;
78 #ifdef USE_CLOUD
79 #ifdef USE_LIBCURL
80  ConnectCloudHandler _connectCloudHandler;
81 #endif // USE_LIBCURL
82 #endif // USE_CLOUD
83  ResourceHandler _resourceHandler;
84  uint32 _idlingFrames;
85  Common::Mutex _handleMutex;
86  Common::String _address;
87  uint32 _serverPort;
88 
89  void startTimer(int interval = TIMER_INTERVAL);
90  void stopTimer();
91  void handle();
92  void handleClient(uint32 i);
93  void acceptClient();
94  void resolveAddress(void *ipAddress);
95  void addPathHandler(const Common::String &path, BaseHandler *handler);
96 
97 public:
98  static const uint32 DEFAULT_SERVER_PORT = 12345;
99 
100  LocalWebserver();
101  ~LocalWebserver() override;
102 
103  void start(bool useMinimalMode = false);
104  void stop();
105  void stopOnIdle();
106 
107  Common::String getAddress();
108  IndexPageHandler &indexPageHandler();
109  bool isRunning();
110  static uint32 getPort();
111 
112 #ifdef USE_CLOUD
113 #ifdef USE_LIBCURL
114  void setStorageConnectionCallback(Networking::ErrorCallback cb) { _connectCloudHandler.setStorageConnectionCallback(cb); }
115 #endif // USE_LIBCURL
116 #endif // USE_CLOUD
117 
118  static void setClientGetHandler(Client &client, const Common::String &response, long code = 200, const char *mimeType = nullptr);
119  static void setClientGetHandler(Client &client, Common::SeekableReadStream *responseStream, long code = 200, const char *mimeType = nullptr);
120  static void setClientRedirectHandler(Client &client, const Common::String &response, const Common::String &location, const char *mimeType = nullptr);
121  static void setClientRedirectHandler(Client &client, Common::SeekableReadStream *responseStream, const Common::String &location, const char *mimeType = nullptr);
122  static Common::String urlDecode(const Common::String &value);
123  static Common::String urlEncodeQueryParameterValue(const Common::String &value);
124 };
125 
127 #define LocalServer Networking::LocalWebserver::instance()
128 
129 } // End of namespace Networking
130 
131 #endif
Definition: connectcloudhandler.h:32
Definition: resourcehandler.h:29
Definition: client.h:75
Definition: str.h:59
Definition: localwebserver.h:57
Definition: createdirectoryhandler.h:29
Definition: scummvmcloud.h:31
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