Reopen closed file fixes binary diffs

This commit is contained in:
Alex Hultman
2018-09-23 23:52:12 +02:00
parent 8106a0f628
commit 49f503c570
5 changed files with 33 additions and 14 deletions
+24 -5
View File
@@ -19,12 +19,13 @@ private:
std::function<void(std::string_view)> 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<int>(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<int>(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;
+2 -2
View File
@@ -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());
+2 -2
View File
@@ -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<SSL> *asyncSocket = (AsyncSocket<SSL> *) 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<SSL>::HTTP_PAUSED_STREAM_OUT;
break;
}
+2 -2
View File
@@ -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<SSL>::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<SSL>::HTTP_PAUSED_STREAM_OUT;
break;
}
+3 -3
View File
@@ -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<void()> 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);
}