From b42105d7e09d2bd4a922a203a980abb045bab4f2 Mon Sep 17 00:00:00 2001 From: klzgrad Date: Mon, 24 Jun 2019 01:19:15 +0800 Subject: [PATCH] Fix OnPushComplete logic Should not Push the same thing again. --- src/net/tools/naive/http_proxy_socket.cc | 4 ++-- src/net/tools/naive/http_proxy_socket.h | 2 +- src/net/tools/naive/naive_connection.cc | 18 ++++++++++++------ src/net/tools/naive/naive_connection.h | 2 +- src/net/tools/naive/socks5_server_socket.cc | 8 ++++---- src/net/tools/naive/socks5_server_socket.h | 2 +- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/net/tools/naive/http_proxy_socket.cc b/src/net/tools/naive/http_proxy_socket.cc index fd6e6a055d..6d2053e52e 100644 --- a/src/net/tools/naive/http_proxy_socket.cc +++ b/src/net/tools/naive/http_proxy_socket.cc @@ -241,7 +241,7 @@ int HttpProxySocket::DoLoop(int last_io_result) { int HttpProxySocket::DoHeaderRead() { next_state_ = STATE_HEADER_READ_COMPLETE; - handshake_buf_ = new IOBuffer(kBufferSize); + handshake_buf_ = base::MakeRefCounted(kBufferSize); return transport_->Read(handshake_buf_.get(), kBufferSize, io_callback_); } @@ -293,7 +293,7 @@ int HttpProxySocket::DoHeaderWrite() { // Adds padding. int padding_size = base::RandInt(kMinPaddingSize, kMaxPaddingSize); header_write_size_ = kResponseHeaderSize + padding_size + 4; - handshake_buf_ = new IOBuffer(header_write_size_); + handshake_buf_ = base::MakeRefCounted(header_write_size_); char* p = handshake_buf_->data(); std::memcpy(p, kResponseHeader, kResponseHeaderSize); std::memset(p + kResponseHeaderSize, '.', padding_size); diff --git a/src/net/tools/naive/http_proxy_socket.h b/src/net/tools/naive/http_proxy_socket.h index f7595de631..62d846018e 100644 --- a/src/net/tools/naive/http_proxy_socket.h +++ b/src/net/tools/naive/http_proxy_socket.h @@ -12,7 +12,7 @@ #include #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_repeating_callback.h" #include "net/base/host_port_pair.h" diff --git a/src/net/tools/naive/naive_connection.cc b/src/net/tools/naive/naive_connection.cc index 9bbf5e6d58..2654c8075a 100644 --- a/src/net/tools/naive/naive_connection.cc +++ b/src/net/tools/naive/naive_connection.cc @@ -277,13 +277,13 @@ void NaiveConnection::Pull(Direction from, Direction to) { int read_size = kBufferSize; if (from == pad_direction_ && num_paddings_[from] < kFirstPaddings) { - auto* buffer = new GrowableIOBuffer; + auto buffer = base::MakeRefCounted(); buffer->SetCapacity(kBufferSize); buffer->set_offset(kPaddingHeaderSize); read_buffers_[from] = buffer; read_size = kBufferSize - kPaddingHeaderSize - kMaxPaddingSize; } else { - read_buffers_[from] = new IOBuffer(kBufferSize); + read_buffers_[from] = base::MakeRefCounted(kBufferSize); } DCHECK(sockets_[from]); @@ -331,7 +331,7 @@ void NaiveConnection::Push(Direction from, Direction to, int size) { } } if (!trivial_padding) { - auto* unpadded_buffer = new IOBuffer(kBufferSize); + auto unpadded_buffer = base::MakeRefCounted(kBufferSize); char* unpadded_ptr = unpadded_buffer->data(); for (int i = 0; i < size;) { 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_offset + write_size); + write_buffers_[to] = base::MakeRefCounted( + std::move(read_buffers_[from]), write_offset + write_size); if (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); int size = write_buffers_[to]->BytesRemaining(); 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; } } diff --git a/src/net/tools/naive/naive_connection.h b/src/net/tools/naive/naive_connection.h index f16abfb81e..19ff875bb6 100644 --- a/src/net/tools/naive/naive_connection.h +++ b/src/net/tools/naive/naive_connection.h @@ -10,7 +10,7 @@ #include #include "base/macros.h" -#include "base/memory/ref_counted.h" +#include "base/memory/scoped_refptr.h" #include "base/memory/weak_ptr.h" #include "base/time/time.h" #include "net/base/completion_once_callback.h" diff --git a/src/net/tools/naive/socks5_server_socket.cc b/src/net/tools/naive/socks5_server_socket.cc index bd6f1a9929..3aa37b3fff 100644 --- a/src/net/tools/naive/socks5_server_socket.cc +++ b/src/net/tools/naive/socks5_server_socket.cc @@ -285,7 +285,7 @@ int Socks5ServerSocket::DoGreetRead() { int handshake_buf_len = greet_read_header_size_ - bytes_received_; DCHECK_LT(0, handshake_buf_len); - handshake_buf_ = new IOBuffer(handshake_buf_len); + handshake_buf_ = base::MakeRefCounted(handshake_buf_len); return transport_->Read(handshake_buf_.get(), handshake_buf_len, io_callback_); } @@ -348,7 +348,7 @@ int Socks5ServerSocket::DoGreetWrite() { next_state_ = STATE_GREET_WRITE_COMPLETE; int handshake_buf_len = buffer_.size() - bytes_sent_; DCHECK_LT(0, handshake_buf_len); - handshake_buf_ = new IOBuffer(handshake_buf_len); + handshake_buf_ = base::MakeRefCounted(handshake_buf_len); std::memcpy(handshake_buf_->data(), &buffer_.data()[bytes_sent_], 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_; DCHECK_LT(0, handshake_buf_len); - handshake_buf_ = new IOBuffer(handshake_buf_len); + handshake_buf_ = base::MakeRefCounted(handshake_buf_len); return transport_->Read(handshake_buf_.get(), handshake_buf_len, io_callback_); } @@ -505,7 +505,7 @@ int Socks5ServerSocket::DoHandshakeWrite() { int handshake_buf_len = buffer_.size() - bytes_sent_; DCHECK_LT(0, handshake_buf_len); - handshake_buf_ = new IOBuffer(handshake_buf_len); + handshake_buf_ = base::MakeRefCounted(handshake_buf_len); std::memcpy(handshake_buf_->data(), &buffer_[bytes_sent_], handshake_buf_len); return transport_->Write(handshake_buf_.get(), handshake_buf_len, io_callback_, traffic_annotation_); diff --git a/src/net/tools/naive/socks5_server_socket.h b/src/net/tools/naive/socks5_server_socket.h index 8842f89fe1..1f1a888c5d 100644 --- a/src/net/tools/naive/socks5_server_socket.h +++ b/src/net/tools/naive/socks5_server_socket.h @@ -12,7 +12,7 @@ #include #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_repeating_callback.h" #include "net/base/host_port_pair.h"