From 65d072c7b06342d665ead68e6e321f0d7d8c4d00 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 11 Apr 2018 21:33:52 -0700 Subject: [PATCH] http: Add some operations to avoid blocking. --- ext/native/net/sinks.cpp | 9 +++++++-- ext/native/net/sinks.h | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ext/native/net/sinks.cpp b/ext/native/net/sinks.cpp index aaac2f1888..d8d47dd080 100644 --- a/ext/native/net/sinks.cpp +++ b/ext/native/net/sinks.cpp @@ -201,6 +201,11 @@ bool InputSink::Empty() { return valid_ == 0; } +bool InputSink::TryFill() { + Fill(); + return !Empty(); +} + OutputSink::OutputSink(size_t fd) : fd_(fd), read_(0), write_(0), valid_(0) { fd_util::SetNonBlocking((int)fd_, true); } @@ -316,7 +321,7 @@ bool OutputSink::Block() { return true; } -bool OutputSink::Flush() { +bool OutputSink::Flush(bool allowBlock) { while (valid_ > 0) { size_t avail = std::min(BUFFER_SIZE - read_, valid_); @@ -325,7 +330,7 @@ bool OutputSink::Flush() { if (bytes == 0) { // This may also drain. Either way, keep looping. - if (!Block()) { + if (!allowBlock || !Block()) { return false; } } diff --git a/ext/native/net/sinks.h b/ext/native/net/sinks.h index 0138d74a6c..308c784fb0 100644 --- a/ext/native/net/sinks.h +++ b/ext/native/net/sinks.h @@ -21,6 +21,7 @@ public: bool Skip(size_t bytes); bool Empty(); + bool TryFill(); private: void Fill(); @@ -49,7 +50,7 @@ public: bool PushCRLF(const std::string &s); bool Printf(const char *fmt, ...); - bool Flush(); + bool Flush(bool allowBlock = true); bool Empty();