// Go to next buffer if there still are some bytes to write
if (left != 0) {
- if (write_head_->write_pos_ == kBufferLength) {
- Buffer* next = new Buffer();
- next->next_ = write_head_->next_;
- write_head_->next_ = next;
- }
+ TryAllocateForWrite();
write_head_ = write_head_->next_;
}
}
}
+void NodeBIO::TryAllocateForWrite() {
+ // If write head is full, next buffer is either read head or not empty.
+ if (write_head_->write_pos_ == kBufferLength &&
+ (write_head_->next_ == read_head_ ||
+ write_head_->next_->write_pos_ != 0)) {
+ Buffer* next = new Buffer();
+ next->next_ = write_head_->next_;
+ write_head_->next_ = next;
+ }
+}
+
+
void NodeBIO::Reset() {
while (read_head_->read_pos_ != read_head_->write_pos_) {
assert(read_head_->write_pos_ > read_head_->read_pos_);
~NodeBIO();
+ // Allocate new buffer for write if needed
+ void TryAllocateForWrite();
+
// Read `len` bytes maximum into `out`, return actual number of read bytes
size_t Read(char* out, size_t size);