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

Unified Diff: chrome/browser/extensions/api/socket/socket.cc

Issue 10310170: Improve socket.write() to make sure all data will be sent out. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix compile error on Mac Created 8 years, 7 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
« no previous file with comments | « chrome/browser/extensions/api/socket/socket.h ('k') | chrome/browser/extensions/api/socket/tcp_socket.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/extensions/api/socket/socket.cc
diff --git a/chrome/browser/extensions/api/socket/socket.cc b/chrome/browser/extensions/api/socket/socket.cc
index 5dc62b2db8e56fd01af3df8dde0ba649cd550da0..e1e30042507bfd77ab68b5001f75393bf44bd5d3 100644
--- a/chrome/browser/extensions/api/socket/socket.cc
+++ b/chrome/browser/extensions/api/socket/socket.cc
@@ -25,6 +25,55 @@ Socket::~Socket() {
DCHECK(!is_connected_);
}
+void Socket::Write(scoped_refptr<net::IOBuffer> io_buffer,
+ int byte_count,
+ const CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
+ write_queue_.push(WriteRequest(io_buffer, byte_count, callback));
+ WriteData();
+}
+
+void Socket::WriteData() {
+ // IO is pending.
+ if (io_buffer_write_.get())
+ return;
+
+ WriteRequest& request = write_queue_.front();
+
+ DCHECK(request.byte_count > request.bytes_written);
+ io_buffer_write_ = new net::WrappedIOBuffer(
+ request.io_buffer->data() + request.bytes_written);
+ int result = WriteImpl(
+ io_buffer_write_.get(),
+ request.byte_count - request.bytes_written,
+ base::Bind(&Socket::OnWriteComplete, base::Unretained(this)));
+
+ if (result != net::ERR_IO_PENDING)
+ OnWriteComplete(result);
+}
+
+void Socket::OnWriteComplete(int result) {
+ io_buffer_write_ = NULL;
+
+ WriteRequest& request = write_queue_.front();
+
+ if (result >= 0) {
+ request.bytes_written += result;
+ if (request.bytes_written < request.byte_count) {
+ WriteData();
+ return;
+ }
+ DCHECK(request.bytes_written == request.byte_count);
+ result = request.bytes_written;
+ }
+
+ request.callback.Run(result);
+ write_queue_.pop();
+
+ if (!write_queue_.empty())
+ WriteData();
+}
+
// static
bool Socket::StringAndPortToIPEndPoint(const std::string& ip_address_str,
int port,
« no previous file with comments | « chrome/browser/extensions/api/socket/socket.h ('k') | chrome/browser/extensions/api/socket/tcp_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698