Permalink
Browse files

http: Add some operations to avoid blocking.

  • Loading branch information...
unknownbrackets committed Apr 12, 2018
1 parent e4aef72 commit 65d072c7b06342d665ead68e6e321f0d7d8c4d00
Showing with 9 additions and 3 deletions.
  1. +7 −2 ext/native/net/sinks.cpp
  2. +2 −1 ext/native/net/sinks.h
View
@@ -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;
}
}
View
@@ -21,6 +21,7 @@ class InputSink {
bool Skip(size_t bytes);
bool Empty();
bool TryFill();
private:
void Fill();
@@ -49,7 +50,7 @@ class OutputSink {
bool PushCRLF(const std::string &s);
bool Printf(const char *fmt, ...);
bool Flush();
bool Flush(bool allowBlock = true);
bool Empty();

0 comments on commit 65d072c

Please sign in to comment.