diff --git a/main.cpp b/main.cpp
index ec6b240..852f7ef 100644
--- a/main.cpp
+++ b/main.cpp
@@ -19,100 +19,30 @@ int main(int argc, char **argv) {
res->write("
Hallå!
Din user-agent är: ");
res->end(req->getHeader("user-agent"));
-// }).get("/endless", [](auto *res, auto *req) {
-// /* This route doesn't specify any length and only returns 1 char per chunk */
-// res->write([](int offset) {
-// std::string_view data("Hello, world
");
-// if (offset < data.length()) {
-// return std::make_pair(offset < data.length() - 1, data.substr(offset, 1));
-// }
-// return uWS::HTTP_STREAM_FIN;
-// });
}).get("/async/sintel.mkv", [](auto *res, auto *req) {
- // res->write(asyncFileReader.stream(res))
- // asyncFileReader.getAsStream()
+ // asyncFileReader.getFileSize()
+ /* Peek from cache */
+ std::string_view chunk = asyncFileReader.peek(/*offset*/ 0);
+ if (chunk.length()) {
+ /* We had parts of this file cached already */
+ res->write(chunk);
+ } else {
+ /* We had nothing readily available right now, request async chunk and pause the stream until we have */
+ asyncFileReader.request(/*offset*/ 0, [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 {
+ /* We finally got the data, resume stream with this chunk */
+ res->write(chunk);
+ }
+ });
+ }
- // res->write(data, length) -> bool (identical to nodejs)
- // res->tryWrite(data, length) -> int (what we will use mostly)
- // both operate like uSockets, they are paused by default and will trigger onWritable if set
-
- // res->onWritable(lambda) -> the same as "drain" even in Node.js, call tryWrite here, or do nothing to hang/pause
-
- // resume is gone, it will resume automatically as needed, just like uSockets do automatically
-
- // res will be somewhat like AsyncSocket but with certain middle functions for formatting the data (think Content-Length and Chunked)
-
-
- // res->onAborted(lambda) will be called when closed, for now all events lie on the res
-
- // res->onData() will probably be the same?
-
- //res->writeStatus(uWS::HTTP_200_OK)->end("Hello Sintel!");
-
- // om vi håller headers i en buffer och data i en annan?
-
- // om end eller tryEnd inte kommer före write swappar den till chunked!
- res->write("Hallå! Din user-agent är: "); // -> skriver till cork-buffern "Transfer-Encoding: chunked" och ett chunk
- res->end(req->getHeader("user-agent"));
-
- // write före end går över till chunked, från där kan man extenda till SSE men websockets är bättre format (binärt, mindre)
-
- // hur fungerar detta med server-sent-events?
-
-
- // end efter write blir ett noll-segment i chunked!
-
- /*if (!res->tryEnd()) {
-
- }*/
-
-
-
- /* This route streams back chunks of data in delayed fashion */
-// res->writeStatus(uWS::HTTP_200_OK)->write([res](int offset) {
-
-// /* Handle broken stream */
-// if (offset == -1) {
-// std::cout << "Stream was closed by peer!" << std::endl;
-// return uWS::HTTP_STREAM_IGNORE;
-// }
-
-// /* Peek from cache */
-// std::string_view chunk = asyncFileReader.peek(offset);
-// if (chunk.length()) {
-// /* We had parts of this file cached already */
-// return std::pair(false, chunk);
-// } else {
-
-// //std::string_view outerChunk;
-
-// /* We had nothing readily available right now, request async chunk and pause the stream until we have */
-// asyncFileReader.request(offset, [res](std::string_view chunk) {
-
-// //std::cout << "We came here!" << std::endl;
-
-// /* 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 {
-// /* We finally got the data, resume stream with this chunk */
-// res->resume(chunk);
-// }
-// });
-
-// //std::cout << "Returning chunk of size: " << outerChunk.length() << std::endl;
-// //return std::pair(false, outerChunk);
-
-// // what if we resumed before we paused! we cannot do that!
-
-// //std::cout << "PAusing stream out due to empty cache!" << std::endl;
-// return uWS::HTTP_STREAM_PAUSE;
-// }
-// }, asyncFileReader.getFileSize());
}).listen(3000, [](auto *token) {
if (token) {
std::cout << "Listening on port " << 3000 << std::endl;
diff --git a/src/App.h b/src/App.h
index c09ea38..90f3f45 100644
--- a/src/App.h
+++ b/src/App.h
@@ -32,6 +32,11 @@ public:
return *this;
}
+ TemplatedApp &unhandled(std::function *, HttpRequest *)> handler) {
+ //httpContext->onGet(pattern, handler);
+ return *this;
+ }
+
TemplatedApp &listen(int port, std::function handler) {
handler(httpContext->listen(nullptr, port, 0));
return *this;
diff --git a/src/HttpResponseData.h b/src/HttpResponseData.h
index 3e32a09..1a4be6a 100644
--- a/src/HttpResponseData.h
+++ b/src/HttpResponseData.h
@@ -16,13 +16,17 @@ struct HttpResponseData : HttpParser, AsyncSocketData {
private:
/* Bits of status */
enum {
- HTTP_STATUS_SENT = 1,
- HTTP_WRITE_CALLED = 2,
- HTTP_KNOWN_STREAM_OUT_SIZE = 4,
- HTTP_PAUSED_STREAM_OUT = 8,
- HTTP_ENDED_STREAM_OUT = 16
+ HTTP_STATUS_SENT = 1, // used
+ HTTP_WRITE_CALLED = 2, // used
+ HTTP_KNOWN_STREAM_OUT_SIZE = 4, // not used
+ HTTP_PAUSED_STREAM_OUT = 8, // not used
+ HTTP_ENDED_STREAM_OUT = 16 // not used
};
+ /* Per socket event handlers */
+ std::function onWritable;
+ //std::function onData;
+
std::function inStream;
std::function(int)> outStream;
/* Outgoing offset */