Almost working async file streamer
This commit is contained in:
+66
-69
@@ -2,89 +2,86 @@
|
||||
|
||||
#include "../examples/helpers/AsyncFileReader.h"
|
||||
|
||||
AsyncFileReader asyncFileReader("/home/alexhultman/sintel_small.mp4");
|
||||
#include <experimental/filesystem>
|
||||
|
||||
/*inline std::string slurp(const std::string &path) {
|
||||
std::ostringstream buf;
|
||||
std::ifstream input (path.c_str());
|
||||
buf << input.rdbuf();
|
||||
return buf.str();
|
||||
}
|
||||
struct AsyncFileStreamer {
|
||||
|
||||
std::string sintelMovie = slurp("/home/alexhultman/sintel_small.mp4");*/
|
||||
std::map<std::string_view, AsyncFileReader *> asyncFileReaders;
|
||||
std::string root;
|
||||
|
||||
void streamFile(uWS::HttpResponse<false> *res) {
|
||||
//int offset = res->getWriteOffset();
|
||||
|
||||
std::cout << "streamFile called with offset: " << res->getWriteOffset() << std::endl;
|
||||
|
||||
/* Peek from cache */
|
||||
std::string_view chunk = asyncFileReader.peek(res->getWriteOffset());
|
||||
if (!chunk.length() || res->tryEnd(chunk, asyncFileReader.getFileSize())) {
|
||||
// request new chunk
|
||||
std::cout << "Requesting new chunk!" << std::endl;
|
||||
|
||||
asyncFileReader.request(res->getWriteOffset(), [res](std::string_view chunk) {
|
||||
/* We were aborted */
|
||||
if (!chunk.length()) {
|
||||
std::cout << "Async File Read request was aborted!" << std::endl;
|
||||
// close the socket here?
|
||||
// we need a way to NOT resume a paused socket! essentially close!
|
||||
} else {
|
||||
|
||||
// just call streamIt!
|
||||
std::cout << "Got requested data, calling streamFile now!" << std::endl;
|
||||
streamFile(res);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
std::cout << "Could not write everything, setting onWritable!" << std::endl;
|
||||
// we have chunk but we could not write everything, so retry this in onWritable
|
||||
res->onWritable([res](int offset) {
|
||||
std::cout << "We are writable now, calling StreamFile" << std::endl;
|
||||
streamFile(res);
|
||||
return false;
|
||||
});
|
||||
AsyncFileStreamer(std::string root) : root(root) {
|
||||
// for all files in this path, init the map of AsyncFileReaders
|
||||
updateRootCache();
|
||||
}
|
||||
}
|
||||
|
||||
void updateRootCache() {
|
||||
// todo: if the root folder changes, we want to reload the cache
|
||||
for(auto &p : std::experimental::filesystem::recursive_directory_iterator(root)) {
|
||||
std::string url = p.path().string().substr(root.length());
|
||||
if (url == "/index.html") {
|
||||
url = "/";
|
||||
}
|
||||
|
||||
char *key = new char[url.length()];
|
||||
memcpy(key, url.data(), url.length());
|
||||
asyncFileReaders[std::string_view(key, url.length())] = new AsyncFileReader(p.path().string());
|
||||
}
|
||||
}
|
||||
|
||||
void streamFile(uWS::HttpResponse<false> *res, std::string_view url) {
|
||||
auto it = asyncFileReaders.find(url);
|
||||
if (it == asyncFileReaders.end()) {
|
||||
std::cout << "Did not find file: " << url << std::endl;
|
||||
} else {
|
||||
streamFile(res, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
static void streamFile(uWS::HttpResponse<false> *res, AsyncFileReader *asyncFileReader) {
|
||||
/* Peek from cache */
|
||||
std::string_view chunk = asyncFileReader->peek(res->getWriteOffset());
|
||||
if (!chunk.length() || res->tryEnd(chunk, asyncFileReader->getFileSize())) {
|
||||
/* Request new chunk */
|
||||
// todo: we need to abort this callback if peer closed!
|
||||
// this also means Loop::defer needs to support aborting
|
||||
asyncFileReader->request(res->getWriteOffset(), [res, asyncFileReader](std::string_view chunk) {
|
||||
/* We were aborted for some reason */
|
||||
if (!chunk.length()) {
|
||||
// todo: make sure to check for is_closed internally after all callbacks!
|
||||
res->close();
|
||||
} else {
|
||||
streamFile(res, asyncFileReader);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
/* We failed writing everything, so let's continue when we can */
|
||||
res->onWritable([res, asyncFileReader](int offset) {
|
||||
|
||||
// här kan skiten avbrytas!
|
||||
|
||||
streamFile(res, asyncFileReader);
|
||||
// todo: I don't really know what this is supposed to mean?
|
||||
return false;
|
||||
})->onAborted([]() {
|
||||
std::cout << "ABORTED!" << std::endl;
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
//std::cout << "Sintel movie is " << sintelMovie.length() << " bytes" << std::endl;
|
||||
|
||||
// läs in hela sintel-filmen här, testa strömmarna med den sen!
|
||||
AsyncFileStreamer *asyncFileStreamer = new AsyncFileStreamer("/home/alexhultman/v0.15/public");
|
||||
|
||||
uWS::/*SSL*/App(/*{
|
||||
.key_file_name = "/home/alexhultman/uWebSockets/misc/ssl/key.pem",
|
||||
.cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem",
|
||||
.dh_params_file_name = "/home/alexhultman/dhparams.pem",
|
||||
.passphrase = "1234"
|
||||
}*/).get("/", [](auto *res, auto *req) {
|
||||
}*/).get("/*", [asyncFileStreamer](auto *res, auto *req) {
|
||||
|
||||
res->writeStatus(uWS::HTTP_200_OK);
|
||||
res->writeHeader("Content-Type", "text/html;charset=utf-8");
|
||||
|
||||
// buffer it up and end by draining it
|
||||
//res->end(sintelMovie);
|
||||
|
||||
// stream it here
|
||||
/*if (!res->tryEnd(sintelMovie)) {
|
||||
res->onWritable([res](int offset) {
|
||||
return res->tryEnd(std::string_view(sintelMovie).substr(offset));
|
||||
})->onAborted([]() {
|
||||
// was it really aborted?
|
||||
std::cout << "Streaming was aborted" << std::endl;
|
||||
});
|
||||
}*/
|
||||
|
||||
// end can be called two times if the total length is longer then given
|
||||
res->write("<h1>Hallå!</h1>Din user-agent är: ");
|
||||
res->end(req->getHeader("user-agent"));
|
||||
|
||||
}).get("/async/sintel.mkv", [](auto *res, auto *req) {
|
||||
|
||||
// this should be enough, we can even delay this thing!
|
||||
streamFile(res);
|
||||
// depending on the file type we want to also add mime!
|
||||
asyncFileStreamer->streamFile(res, req->getUrl());
|
||||
|
||||
}).listen(3000, [](auto *token) {
|
||||
if (token) {
|
||||
|
||||
Reference in New Issue
Block a user