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

Unified Diff: google_apis/gcm/base/socket_stream.cc

Issue 23684017: [GCM] Initial work to set up directory structure and introduce socket integration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 2 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: google_apis/gcm/base/socket_stream.cc
diff --git a/google_apis/gcm/base/socket_stream.cc b/google_apis/gcm/base/socket_stream.cc
new file mode 100644
index 0000000000000000000000000000000000000000..56d7ef2320af87c9b47c9e2de70ae05a79b88a91
--- /dev/null
+++ b/google_apis/gcm/base/socket_stream.cc
@@ -0,0 +1,305 @@
+// Copyright (c) 2013 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 "google_apis/gcm/base/socket_stream.h"
+
+#include "base/callback.h"
+#include "base/message_loop/message_loop.h"
akalin 2013/10/11 16:47:22 if you remove all task posting, you can remove thi
Nicolas Zea 2013/10/11 18:28:32 Done.
+#include "net/base/io_buffer.h"
+#include "net/socket/stream_socket.h"
+
+namespace gcm {
+
+namespace {
+
+// TODO(zea): consider having dynamically-sized buffers if this becomes too
+// expensive.
+const uint32 kDefaultBufferSize = 8*1024;
+
+} // namespace
+
+SocketInputStream::SocketInputStream(net::StreamSocket* socket)
+ : socket_(socket),
+ io_buffer_(new net::IOBuffer(kDefaultBufferSize)),
+ read_buffer_(new net::DrainableIOBuffer(io_buffer_.get(),
+ kDefaultBufferSize)),
+ next_pos_(0),
+ last_error_(net::OK),
+ state_(EMPTY),
+ weak_ptr_factory_(this) {
+ DCHECK(socket->IsConnected());
+}
+
+SocketInputStream::~SocketInputStream() {
+}
+
+bool SocketInputStream::Next(const void** data, int* size) {
+ DCHECK_NE(state_, CLOSED);
+ DCHECK_NE(state_, READING);
+
+ if (state_ == EMPTY) {
+ DVLOG(1) << "No unread data remaining, ending read.";
+ return false;
+ }
+
+ DCHECK_EQ(state_, READY)
+ << " Input stream must have pending data before reading.";
+ DCHECK_LT(next_pos_, read_buffer_->BytesConsumed());
+ *data = io_buffer_->data() + next_pos_;
+ *size = read_buffer_->BytesConsumed() - next_pos_;
+ next_pos_ = read_buffer_->BytesConsumed();
+ state_ = EMPTY;
+ DVLOG(1) << "Consuming " << *size << " bytes in input buffer.";
+ return true;
+}
+
+void SocketInputStream::BackUp(int count) {
+ DCHECK(state_ == READY || state_ == EMPTY);
+ DCHECK_GT(count, 0);
+ DCHECK_LE(count, next_pos_);
+
+ next_pos_ -= count;
+ state_ = READY;
+ DVLOG(1) << "Backing up " << count << " bytes in input buffer. "
+ << "Current position now at " << next_pos_
+ << " of " << read_buffer_->BytesConsumed();
+}
+
+bool SocketInputStream::Skip(int count) {
+ NOTIMPLEMENTED();
+ return false;
+}
+
+int64 SocketInputStream::ByteCount() const {
+ DCHECK_NE(state_, CLOSED);
+ DCHECK_NE(state_, READING);
+ return read_buffer_->BytesConsumed() - next_pos_;
+}
+
+net::Error SocketInputStream::Refresh(const base::Closure& callback,
+ int byte_limit) {
+ DCHECK_NE(state_, CLOSED);
+ DCHECK_NE(state_, READING);
+ DCHECK_GT(byte_limit, 0);
+
+ if (byte_limit > read_buffer_->BytesRemaining()) {
+ NOTREACHED() << "Out of buffer space, closing input stream.";
+ CloseStream(net::ERR_UNEXPECTED, base::Closure());
+ return net::OK;
+ }
+
+ if (!socket_->IsConnected()) {
+ LOG(ERROR) << "Socket was disconnected, closing input stream";
+ CloseStream(net::ERR_CONNECTION_CLOSED, base::Closure());
+ return net::OK;
+ }
+
+ state_ = READING;
+
+ DVLOG(1) << "Refreshing input stream, limit of " << byte_limit << " bytes.";
+ int result = socket_->Read(
+ read_buffer_,
+ byte_limit,
+ base::Bind(&SocketInputStream::RefreshCompletionCallback,
+ weak_ptr_factory_.GetWeakPtr(),
+ callback));
+ DVLOG(1) << "Read returned " << result;
+ if (result != net::ERR_IO_PENDING) {
+ RefreshCompletionCallback(base::Closure(), result);
+ return net::OK;
+ }
+ return net::ERR_IO_PENDING;
+}
+
+void SocketInputStream::RebuildBuffer() {
+ DVLOG(1) << "Rebuilding input stream, consumed "
+ << next_pos_ << " bytes.";
+ DCHECK_NE(state_, READING);
+ DCHECK_NE(state_, CLOSED);
+
+ int unread_data_size = 0;
+ const void* unread_data_ptr = NULL;
+ Next(&unread_data_ptr, &unread_data_size);
+ ResetInternal();
+
+ if (unread_data_ptr != io_buffer_->data()) {
+ DVLOG(1) << "Have " << unread_data_size
+ << " unread bytes remaining, shifting.";
+ // Move any remaining unread data to the start of the buffer;
+ std::memmove(io_buffer_->data(), unread_data_ptr, unread_data_size);
+ } else {
+ DVLOG(1) << "Have " << unread_data_size << " unread bytes remaining.";
+ }
+ read_buffer_->DidConsume(unread_data_size);
+ if (unread_data_size > 0)
+ state_ = READY;
+}
+
+net::Error SocketInputStream::last_error() const {
+ return last_error_;
+}
+
+SocketInputStream::State SocketInputStream::state() const {
+ return state_;
+}
+
+void SocketInputStream::RefreshCompletionCallback(
+ const base::Closure& callback, int result) {
+ DCHECK_EQ(state_, READING);
akalin 2013/10/11 18:47:09 is this DCHECK still valid? you immediately check
Nicolas Zea 2013/10/11 23:41:29 Done.
+ if (state_ == CLOSED) {
+ // An error occured before the completion callback could complete. Ignore
+ // the result.
+ return;
+ }
+
+ // Result == 0 implies EOF, which is treated as an error.
+ if (result < net::OK || result == 0) {
+ DVLOG(1) << "Failed to refresh socket: " << result;
+ CloseStream(static_cast<net::Error>(result), callback);
akalin 2013/10/11 16:47:22 if result == 0, then you'll call this with 'net::O
Nicolas Zea 2013/10/11 18:28:32 Done.
+ return;
+ }
+ DCHECK_GT(result, 0);
+
+ state_ = READY;
+ read_buffer_->DidConsume(result);
+ if (next_pos_ == read_buffer_->BytesConsumed())
+ state_ = EMPTY;
+
+ DVLOG(1) << "Refresh complete with " << result << " new bytes. "
+ << "Current position " << next_pos_
+ << " of " << read_buffer_->BytesConsumed() << ".";
+
+ if (!callback.is_null())
+ callback.Run();
+}
+
+void SocketInputStream::ResetInternal() {
+ read_buffer_->SetOffset(0);
+ next_pos_ = 0;
+ last_error_ = net::OK;
+ state_ = EMPTY;
+ weak_ptr_factory_.InvalidateWeakPtrs(); // Invalidate any callbacks.
+}
+
+void SocketInputStream::CloseStream(net::Error error,
+ const base::Closure& callback) {
+ ResetInternal();
+ state_ = CLOSED;
+ last_error_ = error;
+ LOG(ERROR) << "Closing stream with result " << error;
+ if (!callback.is_null())
+ callback.Run();
+}
+
+SocketOutputStream::SocketOutputStream(net::StreamSocket* socket)
+ : socket_(socket),
+ io_buffer_(new net::IOBuffer(kDefaultBufferSize)),
+ write_buffer_(new net::DrainableIOBuffer(io_buffer_.get(),
+ kDefaultBufferSize)),
+ buffer_used_(0),
+ state_(EMPTY),
+ weak_ptr_factory_(this) {
+ DCHECK(socket->IsConnected());
+}
+
+SocketOutputStream::~SocketOutputStream() {
+}
+
+bool SocketOutputStream::Next(void** data, int* size) {
+ DCHECK_NE(state_, CLOSED);
+ DCHECK_NE(state_, FLUSHING);
+ if (buffer_used_ == write_buffer_->size())
+ return false;
+
+ *data = write_buffer_->data() + buffer_used_;
+ *size = write_buffer_->size() - buffer_used_;
+ buffer_used_ = write_buffer_->size();
+ state_ = READY;
+ return true;
+}
+
+void SocketOutputStream::BackUp(int count) {
+ DCHECK_GE(count, 0);
+ if (count > buffer_used_)
+ buffer_used_ = 0;
+ buffer_used_ -= count;
+ DVLOG(1) << "Backing up " << count << " bytes in output buffer. "
+ << buffer_used_ << " bytes used.";
+}
+
+int64 SocketOutputStream::ByteCount() const {
+ DCHECK_NE(state_, CLOSED);
+ DCHECK_NE(state_, FLUSHING);
+ return buffer_used_;
+}
+
+void SocketOutputStream::Flush(const base::Closure& callback) {
+ DCHECK_EQ(state_, READY);
+ state_ = FLUSHING;
+
+ if (!socket_->IsConnected()) {
+ LOG(ERROR) << "Socket was disconnected, closing output stream";
+ last_error_ = net::ERR_CONNECTION_CLOSED;
+ state_ = CLOSED;
+ if (!callback.is_null())
+ callback.Run();
+ return;
+ }
+
+ DVLOG(1) << "Flushing " << buffer_used_ << " bytes into socket.";
+ int result = socket_->Write(
+ write_buffer_,
+ buffer_used_,
+ base::Bind(&SocketOutputStream::FlushCompletionCallback,
+ weak_ptr_factory_.GetWeakPtr(),
+ callback));
+ DVLOG(1) << "Write returned " << result;
+ if (result != net::ERR_IO_PENDING) {
+ base::MessageLoop::current()->PostTask(
akalin 2013/10/11 16:47:22 avoid task-posting analogous to SocketInputStream?
Nicolas Zea 2013/10/11 18:28:32 Done.
+ FROM_HERE,
+ base::Bind(&SocketOutputStream::FlushCompletionCallback,
+ weak_ptr_factory_.GetWeakPtr(),
+ callback,
+ result));
+ }
+}
+
+SocketOutputStream::State SocketOutputStream::state() const{
+ return state_;
+}
+
+net::Error SocketOutputStream::last_error() const {
+ return last_error_;
+}
+
+void SocketOutputStream::FlushCompletionCallback(
+ const base::Closure& callback, int result) {
+ DCHECK_EQ(state_, FLUSHING);
+ if (result < net::OK) {
+ LOG(ERROR) << "Failed to flush socket.";
+ last_error_ = static_cast<net::Error>(result);
+ state_ = CLOSED;
+ if (!callback.is_null())
+ callback.Run();
+ return;
+ }
+
+ state_ = READY;
+ if (write_buffer_->BytesConsumed() + result < buffer_used_) {
+ DVLOG(1) << "Partial flush complete. Retrying.";
+ // Only a partial write was completed. Flush again to finish the write.
+ write_buffer_->DidConsume(result);
+ Flush(callback);
+ return;
+ }
+
+ DVLOG(1) << "Socket flush complete.";
+ write_buffer_->SetOffset(0);
+ state_ = EMPTY;
+ buffer_used_ = 0;
+ if (!callback.is_null())
+ callback.Run();
+}
+
+} // namespace gcm

Powered by Google App Engine
This is Rietveld 408576698