Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(767)

Unified Diff: net/socket/buffered_write_stream_socket.cc

Issue 9433015: Add a force pipelining option to load flags. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: More tests Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/socket/buffered_write_stream_socket.cc
diff --git a/net/socket/buffered_write_stream_socket.cc b/net/socket/buffered_write_stream_socket.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3deb85073dff31d0018df6dd57fd0f906106dece
--- /dev/null
+++ b/net/socket/buffered_write_stream_socket.cc
@@ -0,0 +1,137 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/socket/buffered_write_stream_socket.h"
+
+#include "base/bind.h"
+#include "base/location.h"
+#include "base/message_loop.h"
+#include "net/base/io_buffer.h"
+#include "net/base/net_errors.h"
+
+namespace net {
+
+BufferedWriteStreamSocket::BufferedWriteStreamSocket(
+ StreamSocket* socket_to_wrap)
+ : wrapped_socket_(socket_to_wrap),
+ io_buffer_(new GrowableIOBuffer()),
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
+ pending_callback_(false),
+ error_(0) {
+}
+
+BufferedWriteStreamSocket::~BufferedWriteStreamSocket() {
+}
+
+int BufferedWriteStreamSocket::Read(IOBuffer* buf, int buf_len,
+ const CompletionCallback& callback) {
+ return wrapped_socket_->Read(buf, buf_len, callback);
+}
+
+int BufferedWriteStreamSocket::Write(IOBuffer* buf, int buf_len,
+ const CompletionCallback& callback) {
+ if (error_) {
+ return error_;
+ }
+ int old_capacity = io_buffer_->capacity();
+ io_buffer_->SetCapacity(old_capacity + buf_len);
+ memcpy(io_buffer_->data() + old_capacity, buf->data(), buf_len);
+ if (!pending_callback_) {
+ MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(&BufferedWriteStreamSocket::DoDelayedWrite,
+ weak_factory_.GetWeakPtr()));
+ pending_callback_ = true;
+ }
+ return buf_len;
+}
+
+bool BufferedWriteStreamSocket::SetReceiveBufferSize(int32 size) {
+ return wrapped_socket_->SetReceiveBufferSize(size);
+}
+
+bool BufferedWriteStreamSocket::SetSendBufferSize(int32 size) {
+ return wrapped_socket_->SetSendBufferSize(size);
+}
+
+int BufferedWriteStreamSocket::Connect(const CompletionCallback& callback) {
+ return wrapped_socket_->Connect(callback);
+}
+
+void BufferedWriteStreamSocket::Disconnect() {
+ wrapped_socket_->Disconnect();
+}
+
+bool BufferedWriteStreamSocket::IsConnected() const {
+ return wrapped_socket_->IsConnected();
+}
+
+bool BufferedWriteStreamSocket::IsConnectedAndIdle() const {
+ return wrapped_socket_->IsConnectedAndIdle();
+}
+
+int BufferedWriteStreamSocket::GetPeerAddress(AddressList* address) const {
+ return wrapped_socket_->GetPeerAddress(address);
+}
+
+int BufferedWriteStreamSocket::GetLocalAddress(IPEndPoint* address) const {
+ return wrapped_socket_->GetLocalAddress(address);
+}
+
+const BoundNetLog& BufferedWriteStreamSocket::NetLog() const {
+ return wrapped_socket_->NetLog();
+}
+
+void BufferedWriteStreamSocket::SetSubresourceSpeculation() {
+ wrapped_socket_->SetSubresourceSpeculation();
+}
+
+void BufferedWriteStreamSocket::SetOmniboxSpeculation() {
+ wrapped_socket_->SetOmniboxSpeculation();
+}
+
+bool BufferedWriteStreamSocket::WasEverUsed() const {
+ return wrapped_socket_->WasEverUsed();
+}
+
+bool BufferedWriteStreamSocket::UsingTCPFastOpen() const {
+ return wrapped_socket_->UsingTCPFastOpen();
+}
+
+int64 BufferedWriteStreamSocket::NumBytesRead() const {
+ return wrapped_socket_->NumBytesRead();
+}
+
+base::TimeDelta BufferedWriteStreamSocket::GetConnectTimeMicros() const {
+ return wrapped_socket_->GetConnectTimeMicros();
+}
+
+void BufferedWriteStreamSocket::DoDelayedWrite() {
+ int result = wrapped_socket_->Write(
+ io_buffer_, io_buffer_->RemainingCapacity(),
+ base::Bind(&BufferedWriteStreamSocket::OnIOComplete,
+ base::Unretained(this)));
mmenke 2012/02/24 16:36:39 This does not look safe to me. If we get ERR_IO_P
James Simonsen 2012/02/24 23:58:51 Nice catch! I went with a backup buffer. It's the
mmenke 2012/02/25 00:32:43 Great, I much prefer this approach..
+ if (result == ERR_IO_PENDING) {
+ pending_callback_ = true;
+ } else {
+ OnIOComplete(result);
+ }
+}
+
+void BufferedWriteStreamSocket::OnIOComplete(int result) {
+ pending_callback_ = false;
+ if (result < 0) {
+ error_ = result;
+ io_buffer_->SetCapacity(0);
+ } else {
+ io_buffer_->set_offset(io_buffer_->offset() + result);
+ if (io_buffer_->RemainingCapacity()) {
+ DoDelayedWrite();
+ } else {
+ io_buffer_->SetCapacity(0);
+ }
+ }
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698