Fix OnPushComplete logic

Should not Push the same thing again.
This commit is contained in:
klzgrad 2019-06-24 01:19:15 +08:00
parent 7f5b005ccd
commit f07623e943
6 changed files with 21 additions and 15 deletions

View File

@ -241,7 +241,7 @@ int HttpProxySocket::DoLoop(int last_io_result) {
int HttpProxySocket::DoHeaderRead() { int HttpProxySocket::DoHeaderRead() {
next_state_ = STATE_HEADER_READ_COMPLETE; next_state_ = STATE_HEADER_READ_COMPLETE;
handshake_buf_ = new IOBuffer(kBufferSize); handshake_buf_ = base::MakeRefCounted<IOBuffer>(kBufferSize);
return transport_->Read(handshake_buf_.get(), kBufferSize, io_callback_); return transport_->Read(handshake_buf_.get(), kBufferSize, io_callback_);
} }
@ -293,7 +293,7 @@ int HttpProxySocket::DoHeaderWrite() {
// Adds padding. // Adds padding.
int padding_size = base::RandInt(kMinPaddingSize, kMaxPaddingSize); int padding_size = base::RandInt(kMinPaddingSize, kMaxPaddingSize);
header_write_size_ = kResponseHeaderSize + padding_size + 4; header_write_size_ = kResponseHeaderSize + padding_size + 4;
handshake_buf_ = new IOBuffer(header_write_size_); handshake_buf_ = base::MakeRefCounted<IOBuffer>(header_write_size_);
char* p = handshake_buf_->data(); char* p = handshake_buf_->data();
std::memcpy(p, kResponseHeader, kResponseHeaderSize); std::memcpy(p, kResponseHeader, kResponseHeaderSize);
std::memset(p + kResponseHeaderSize, '.', padding_size); std::memset(p + kResponseHeaderSize, '.', padding_size);

View File

@ -12,7 +12,7 @@
#include <string> #include <string>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/scoped_refptr.h"
#include "net/base/completion_once_callback.h" #include "net/base/completion_once_callback.h"
#include "net/base/completion_repeating_callback.h" #include "net/base/completion_repeating_callback.h"
#include "net/base/host_port_pair.h" #include "net/base/host_port_pair.h"

View File

@ -277,13 +277,13 @@ void NaiveConnection::Pull(Direction from, Direction to) {
int read_size = kBufferSize; int read_size = kBufferSize;
if (from == pad_direction_ && num_paddings_[from] < kFirstPaddings) { if (from == pad_direction_ && num_paddings_[from] < kFirstPaddings) {
auto* buffer = new GrowableIOBuffer; auto buffer = base::MakeRefCounted<GrowableIOBuffer>();
buffer->SetCapacity(kBufferSize); buffer->SetCapacity(kBufferSize);
buffer->set_offset(kPaddingHeaderSize); buffer->set_offset(kPaddingHeaderSize);
read_buffers_[from] = buffer; read_buffers_[from] = buffer;
read_size = kBufferSize - kPaddingHeaderSize - kMaxPaddingSize; read_size = kBufferSize - kPaddingHeaderSize - kMaxPaddingSize;
} else { } else {
read_buffers_[from] = new IOBuffer(kBufferSize); read_buffers_[from] = base::MakeRefCounted<IOBuffer>(kBufferSize);
} }
DCHECK(sockets_[from]); DCHECK(sockets_[from]);
@ -331,7 +331,7 @@ void NaiveConnection::Push(Direction from, Direction to, int size) {
} }
} }
if (!trivial_padding) { if (!trivial_padding) {
auto* unpadded_buffer = new IOBuffer(kBufferSize); auto unpadded_buffer = base::MakeRefCounted<IOBuffer>(kBufferSize);
char* unpadded_ptr = unpadded_buffer->data(); char* unpadded_ptr = unpadded_buffer->data();
for (int i = 0; i < size;) { for (int i = 0; i < size;) {
if (num_paddings_[from] >= kFirstPaddings && if (num_paddings_[from] >= kFirstPaddings &&
@ -392,8 +392,8 @@ void NaiveConnection::Push(Direction from, Direction to, int size) {
} }
} }
write_buffers_[to] = new DrainableIOBuffer(read_buffers_[from].get(), write_buffers_[to] = base::MakeRefCounted<DrainableIOBuffer>(
write_offset + write_size); std::move(read_buffers_[from]), write_offset + write_size);
if (write_offset) { if (write_offset) {
write_buffers_[to]->DidConsume(write_offset); write_buffers_[to]->DidConsume(write_offset);
} }
@ -484,7 +484,13 @@ void NaiveConnection::OnPushComplete(Direction from, Direction to, int result) {
write_buffers_[to]->DidConsume(result); write_buffers_[to]->DidConsume(result);
int size = write_buffers_[to]->BytesRemaining(); int size = write_buffers_[to]->BytesRemaining();
if (size > 0) { if (size > 0) {
Push(from, to, size); int rv = sockets_[to]->Write(
write_buffers_[to].get(), size,
base::BindRepeating(&NaiveConnection::OnPushComplete,
weak_ptr_factory_.GetWeakPtr(), from, to),
traffic_annotation_);
if (rv != ERR_IO_PENDING)
OnPushComplete(from, to, rv);
return; return;
} }
} }

View File

@ -10,7 +10,7 @@
#include <string> #include <string>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "net/base/completion_once_callback.h" #include "net/base/completion_once_callback.h"

View File

@ -285,7 +285,7 @@ int Socks5ServerSocket::DoGreetRead() {
int handshake_buf_len = greet_read_header_size_ - bytes_received_; int handshake_buf_len = greet_read_header_size_ - bytes_received_;
DCHECK_LT(0, handshake_buf_len); DCHECK_LT(0, handshake_buf_len);
handshake_buf_ = new IOBuffer(handshake_buf_len); handshake_buf_ = base::MakeRefCounted<IOBuffer>(handshake_buf_len);
return transport_->Read(handshake_buf_.get(), handshake_buf_len, return transport_->Read(handshake_buf_.get(), handshake_buf_len,
io_callback_); io_callback_);
} }
@ -348,7 +348,7 @@ int Socks5ServerSocket::DoGreetWrite() {
next_state_ = STATE_GREET_WRITE_COMPLETE; next_state_ = STATE_GREET_WRITE_COMPLETE;
int handshake_buf_len = buffer_.size() - bytes_sent_; int handshake_buf_len = buffer_.size() - bytes_sent_;
DCHECK_LT(0, handshake_buf_len); DCHECK_LT(0, handshake_buf_len);
handshake_buf_ = new IOBuffer(handshake_buf_len); handshake_buf_ = base::MakeRefCounted<IOBuffer>(handshake_buf_len);
std::memcpy(handshake_buf_->data(), &buffer_.data()[bytes_sent_], std::memcpy(handshake_buf_->data(), &buffer_.data()[bytes_sent_],
handshake_buf_len); handshake_buf_len);
return transport_->Write(handshake_buf_.get(), handshake_buf_len, return transport_->Write(handshake_buf_.get(), handshake_buf_len,
@ -385,7 +385,7 @@ int Socks5ServerSocket::DoHandshakeRead() {
int handshake_buf_len = read_header_size_ - bytes_received_; int handshake_buf_len = read_header_size_ - bytes_received_;
DCHECK_LT(0, handshake_buf_len); DCHECK_LT(0, handshake_buf_len);
handshake_buf_ = new IOBuffer(handshake_buf_len); handshake_buf_ = base::MakeRefCounted<IOBuffer>(handshake_buf_len);
return transport_->Read(handshake_buf_.get(), handshake_buf_len, return transport_->Read(handshake_buf_.get(), handshake_buf_len,
io_callback_); io_callback_);
} }
@ -506,7 +506,7 @@ int Socks5ServerSocket::DoHandshakeWrite() {
int handshake_buf_len = buffer_.size() - bytes_sent_; int handshake_buf_len = buffer_.size() - bytes_sent_;
DCHECK_LT(0, handshake_buf_len); DCHECK_LT(0, handshake_buf_len);
handshake_buf_ = new IOBuffer(handshake_buf_len); handshake_buf_ = base::MakeRefCounted<IOBuffer>(handshake_buf_len);
std::memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], handshake_buf_len); std::memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], handshake_buf_len);
return transport_->Write(handshake_buf_.get(), handshake_buf_len, return transport_->Write(handshake_buf_.get(), handshake_buf_len,
io_callback_, traffic_annotation_); io_callback_, traffic_annotation_);

View File

@ -12,7 +12,7 @@
#include <string> #include <string>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/scoped_refptr.h"
#include "net/base/completion_once_callback.h" #include "net/base/completion_once_callback.h"
#include "net/base/completion_repeating_callback.h" #include "net/base/completion_repeating_callback.h"
#include "net/base/host_port_pair.h" #include "net/base/host_port_pair.h"