diff --git a/examples/helpers/AsyncFileReader.h b/examples/helpers/AsyncFileReader.h index 7364b86..b64c643 100644 --- a/examples/helpers/AsyncFileReader.h +++ b/examples/helpers/AsyncFileReader.h @@ -19,12 +19,13 @@ private: std::function pendingReadCb; int fileSize; + std::string fileName; std::ifstream fin; uWS::Loop *loop; public: /* Construct a demo async. file reader for fileName */ - AsyncFileReader(std::string fileName) { + AsyncFileReader(std::string fileName) : fileName(fileName) { fin.open(fileName, std::ios::binary); // get fileSize @@ -53,7 +54,14 @@ public: if (hasCache && offset >= cacheOffset && ((offset - cacheOffset) < cache.length())) { /* Cache hit */ //std::cout << "Cache hit!" << std::endl; - return std::string_view(cache.data() + offset - cacheOffset, cache.length() - offset + cacheOffset); + + if (fileSize - offset < cache.length()) { + std::cout << "LESS THAN WHAT WE HAVE!" << std::endl; + } + + int chunkSize = std::min(fileSize - offset, cache.length() - offset + cacheOffset); + + return std::string_view(cache.data() + offset - cacheOffset, chunkSize); } else { /* Cache miss */ //std::cout << "Cache miss!" << std::endl; @@ -77,17 +85,28 @@ public: hasCache = false; std::async(std::launch::async, [this, cb, offset]() { - std::cout << "ASYNC Caching 1 MB at offset = " << offset << std::endl; + //std::cout << "ASYNC Caching 1 MB at offset = " << offset << std::endl; + // den har stängts! öppna igen! + if (!fin.good()) { + fin.close(); + std::cout << "Reopening fin!" << std::endl; + fin.open(fileName, std::ios::binary); + } fin.seekg(offset, fin.beg); fin.read(cache.data(), cache.length()); + cacheOffset = offset; - loop->defer([this, cb]() { + loop->defer([this, cb, offset]() { - int chunkSize = std::min(cache.length(), fileSize - offset); + int chunkSize = std::min(cache.length(), fileSize - offset); + + if (chunkSize == 0) { + std::cout << "Zero size!?" << std::endl; + } if (chunkSize != cache.length()) { std::cout << "LESS THAN A CACHE 1 MB!" << std::endl; diff --git a/main.cpp b/main.cpp index 8edae8e..8daa033 100644 --- a/main.cpp +++ b/main.cpp @@ -48,7 +48,7 @@ int main(int argc, char **argv) { /* 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; + //std::cout << "We came here!" << std::endl; /* We were aborted */ if (!chunk.length()) { @@ -66,7 +66,7 @@ int main(int argc, char **argv) { // what if we resumed before we paused! we cannot do that! - std::cout << "PAusing stream out due to empty cache!" << std::endl; + //std::cout << "PAusing stream out due to empty cache!" << std::endl; return uWS::HTTP_STREAM_PAUSE; } }, asyncFileReader.getFileSize()); diff --git a/src/HttpContext.h b/src/HttpContext.h index bc4a402..e0fc479 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -135,7 +135,7 @@ private: /* Handle HTTP write out */ static_dispatch(us_ssl_socket_context_on_writable, us_socket_context_on_writable)(getSocketContext(), [](auto *s) { - std::cout << "Writable event!" << std::endl; + //std::cout << "Writable event!" << std::endl; /* Silence any spurious writable events due to SSL_read failing to write */ AsyncSocket *asyncSocket = (AsyncSocket *) s; @@ -170,7 +170,7 @@ private: auto [msg_more, chunk] = httpResponseData->outStream(httpResponseData->offset); if (chunk.length() == 0) { - std::cout << "onwritable paused!" << std::endl; + //std::cout << "onwritable paused!" << std::endl; httpResponseData->state |= HttpResponseData::HTTP_PAUSED_STREAM_OUT; break; } diff --git a/src/HttpResponse.h b/src/HttpResponse.h index df8154b..62187ac 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -100,7 +100,7 @@ public: return; } - std::cout << "Resume called and we really are paused" << std::endl; + //std::cout << "Resume called and we really are paused" << std::endl; /* Remove paused status */ httpResponseData->state &= ~HttpResponseData::HTTP_PAUSED_STREAM_OUT; @@ -122,7 +122,7 @@ public: // break on pause! if (chunk.length() == 0) { - std::cout << "Resume paused!" << std::endl; + //std::cout << "Resume paused!" << std::endl; httpResponseData->state |= HttpResponseData::HTTP_PAUSED_STREAM_OUT; break; } diff --git a/src/Loop.h b/src/Loop.h index 41cf964..7c9108a 100644 --- a/src/Loop.h +++ b/src/Loop.h @@ -16,7 +16,7 @@ namespace uWS { struct Loop { private: static void wakeupCb(us_loop *loop) { - std::cout << "wakeupCB called" << std::endl; + //std::cout << "wakeupCB called" << std::endl; LoopData *loopData = (LoopData *) us_loop_ext(loop); /* Swap current deferQueue */ @@ -83,13 +83,13 @@ public: void defer(std::function cb) { LoopData *loopData = (LoopData *) us_loop_ext((us_loop *) this); - std::cout << "defer called" << std::endl; + //std::cout << "defer called" << std::endl; //if (std::thread::get_id() == ) // todo: add fast path for same thread id loopData->deferMutex.lock(); loopData->deferQueues[loopData->currentDeferQueue].emplace_back(cb); loopData->deferMutex.unlock(); - std::cout << "us_wakeup_loop called" << std::endl; + //std::cout << "us_wakeup_loop called" << std::endl; us_wakeup_loop((us_loop *) this); }