http: Add some operations to avoid blocking.

This commit is contained in:
Unknown W. Brackets committed 2018-06-07 14:11:52 -07:00
1 parent e4aef7209f
commit 65d072c7b0
2 files changed
+9 -3

No files matched your search

+7 -2
View File
@@ -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;
}
}
+2 -1
View File
@@ -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();