Flutter Engine
The Flutter Engine
skiaserve.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
10
13
15
16#include "microhttpd.h"
17
18#include <errno.h>
19
20#if !defined _WIN32
21#include <sys/socket.h>
22#include <arpa/inet.h>
23#endif
24
25using namespace skia_private;
26using namespace Response;
27
28static DEFINE_int(port, 8888, "The port to listen on.");
29static DEFINE_string(address, "127.0.0.1", "The address to bind to.");
30static DEFINE_bool(hosted, false, "Running in hosted mode on debugger.skia.org.");
31
33public:
35 // Register handlers
36 fHandlers.push_back(new RootHandler);
37 fHandlers.push_back(new PostHandler);
38 fHandlers.push_back(new ImgHandler);
39 fHandlers.push_back(new ClipAlphaHandler);
40 fHandlers.push_back(new EnableGPUHandler);
41 fHandlers.push_back(new CmdHandler);
42 fHandlers.push_back(new InfoHandler);
43 fHandlers.push_back(new DownloadHandler);
44 fHandlers.push_back(new DataHandler);
45 fHandlers.push_back(new BreakHandler);
46 fHandlers.push_back(new OpsHandler);
47 fHandlers.push_back(new OpBoundsHandler);
48 fHandlers.push_back(new ColorModeHandler);
49 fHandlers.push_back(new QuitHandler);
50 }
51
53 for (int i = 0; i < fHandlers.size(); i++) { delete fHandlers[i]; }
54 }
55
56 // This is clearly not efficient for a large number of urls and handlers
57 int invoke(Request* request, MHD_Connection* connection, const char* url, const char* method,
58 const char* upload_data, size_t* upload_data_size) const {
59 for (int i = 0; i < fHandlers.size(); i++) {
60 if (fHandlers[i]->canHandle(method, url)) {
61 return fHandlers[i]->handle(request, connection, url, method, upload_data,
62 upload_data_size);
63 }
64 }
65 return MHD_NO;
66 }
67
68private:
69 TArray<UrlHandler*> fHandlers;
70};
71
73
74int answer_to_connection(void* cls, struct MHD_Connection* connection,
75 const char* url, const char* method, const char* version,
76 const char* upload_data, size_t* upload_data_size,
77 void** con_cls) {
78 SkDebugf("New %s request for %s using version %s\n", method, url, version);
79
80 Request* request = reinterpret_cast<Request*>(cls);
81 int result = kUrlManager.invoke(request, connection, url, method, upload_data,
82 upload_data_size);
83 if (MHD_NO == result) {
84 fprintf(stderr, "Invalid method and / or url: %s %s\n", method, url);
85 }
86 return result;
87}
88
91 Request request(SkString("/data")); // This simple server has one request
92
93 struct sockaddr_in address;
94 address.sin_family = AF_INET;
95 address.sin_port = htons(FLAGS_port);
96 int result = inet_pton(AF_INET, FLAGS_address[0], &address.sin_addr);
97 if (result != 1) {
98 printf("inet_pton for %s:%d failed with return %d %s\n",
99 FLAGS_address[0], FLAGS_port, result, strerror(errno));
100 return 1;
101 }
102
103 printf("Visit http://%s:%d in your browser.\n", FLAGS_address[0], FLAGS_port);
104
105 struct MHD_Daemon* daemon;
106 daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY
107#ifdef SK_DEBUG
108 | MHD_USE_DEBUG
109#endif
110 , FLAGS_port, nullptr, nullptr,
111 &answer_to_connection, &request,
112 MHD_OPTION_SOCK_ADDR, &address,
113 MHD_OPTION_END);
114 if (nullptr == daemon) {
115 SkDebugf("Could not initialize daemon\n");
116 return 1;
117 }
118
119 if (FLAGS_hosted) {
120 while (1) {
121 SkDebugf("loop\n");
122 #if defined(SK_BUILD_FOR_WIN)
123 Sleep(60 * 1000);
124 #else
125 sleep(60);
126 #endif
127 }
128 } else {
129 getchar();
130 }
131 MHD_stop_daemon(daemon);
132 return 0;
133}
134
135#if !defined SK_BUILD_FOR_IOS
136int main(int argc, char** argv) {
138 return skiaserve_main();
139}
140#endif
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define SK_DEBUG
static void Parse(int argc, const char *const *argv)
static void Init()
Definition: SkGraphics.cpp:22
int invoke(Request *request, MHD_Connection *connection, const char *url, const char *method, const char *upload_data, size_t *upload_data_size) const
Definition: skiaserve.cpp:57
int size() const
Definition: SkTArray.h:421
GAsyncResult * result
char ** argv
Definition: library.h:9
std::string printf(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: SkSLString.cpp:83
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service port
Definition: switches.h:87
int main(int argc, char **argv)
Definition: skiaserve.cpp:136
static DEFINE_string(address, "127.0.0.1", "The address to bind to.")
int skiaserve_main()
Definition: skiaserve.cpp:89
const UrlManager kUrlManager
Definition: skiaserve.cpp:72
int answer_to_connection(void *cls, struct MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
Definition: skiaserve.cpp:74
static DEFINE_bool(hosted, false, "Running in hosted mode on debugger.skia.org.")
static DEFINE_int(port, 8888, "The port to listen on.")