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

Side by Side Diff: chrome/browser/extensions/api/socket/tcp_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: Update 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/socket/tcp_socket.h" 5 #include "chrome/browser/extensions/api/socket/tcp_socket.h"
6 6
7 #include "chrome/browser/extensions/api/api_resource.h" 7 #include "chrome/browser/extensions/api/api_resource.h"
8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h"
9 #include "net/base/address_list.h" 9 #include "net/base/address_list.h"
10 #include "net/base/ip_endpoint.h" 10 #include "net/base/ip_endpoint.h"
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 io_buffer = new net::IOBuffer(count); 108 io_buffer = new net::IOBuffer(count);
109 result = socket_->Read(io_buffer.get(), count, 109 result = socket_->Read(io_buffer.get(), count,
110 base::Bind(&TCPSocket::OnReadComplete, base::Unretained(this), 110 base::Bind(&TCPSocket::OnReadComplete, base::Unretained(this),
111 io_buffer)); 111 io_buffer));
112 } while (false); 112 } while (false);
113 113
114 if (result != net::ERR_IO_PENDING) 114 if (result != net::ERR_IO_PENDING)
115 OnReadComplete(io_buffer, result); 115 OnReadComplete(io_buffer, result);
116 } 116 }
117 117
118 void TCPSocket::Write(scoped_refptr<net::IOBuffer> io_buffer,
119 int byte_count,
120 const CompletionCallback& callback) {
121 DCHECK(!callback.is_null());
122
123 if (!write_callback_.is_null()) {
124 // TODO(penghuang): Put requests in a pending queue to support multiple
125 // write calls.
126 callback.Run(net::ERR_IO_PENDING);
127 return;
128 } else {
129 write_callback_ = callback;
130 }
131
132 int result = net::ERR_FAILED;
133 do {
134 if (!socket_.get() || !socket_->IsConnected()) {
135 result = net::ERR_SOCKET_NOT_CONNECTED;
136 break;
137 }
138
139 result = socket_->Write(
140 io_buffer.get(), byte_count,
141 base::Bind(&TCPSocket::OnWriteComplete, base::Unretained(this)));
142 } while (false);
143
144 if (result != net::ERR_IO_PENDING)
145 OnWriteComplete(result);
146 }
147
148 void TCPSocket::RecvFrom(int count, 118 void TCPSocket::RecvFrom(int count,
149 const RecvFromCompletionCallback& callback) { 119 const RecvFromCompletionCallback& callback) {
150 callback.Run(net::ERR_FAILED, NULL, NULL, 0); 120 callback.Run(net::ERR_FAILED, NULL, NULL, 0);
151 } 121 }
152 122
153 void TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer, 123 void TCPSocket::SendTo(scoped_refptr<net::IOBuffer> io_buffer,
154 int byte_count, 124 int byte_count,
155 const std::string& address, 125 const std::string& address,
156 int port, 126 int port,
157 const CompletionCallback& callback) { 127 const CompletionCallback& callback) {
158 callback.Run(net::ERR_FAILED); 128 callback.Run(net::ERR_FAILED);
159 } 129 }
160 130
131 int TCPSocket::WriteImpl(net::IOBuffer* io_buffer,
132 int io_buffer_size,
133 const net::CompletionCallback& callback) {
134 if (!socket_.get() || !socket_->IsConnected())
135 return net::ERR_SOCKET_NOT_CONNECTED;
136 else
137 return socket_->Write(io_buffer, io_buffer_size, callback);
138 }
139
161 void TCPSocket::OnConnectComplete(int result) { 140 void TCPSocket::OnConnectComplete(int result) {
162 DCHECK(!connect_callback_.is_null()); 141 DCHECK(!connect_callback_.is_null());
163 DCHECK(!is_connected_); 142 DCHECK(!is_connected_);
164 is_connected_ = result == net::OK; 143 is_connected_ = result == net::OK;
165 connect_callback_.Run(result); 144 connect_callback_.Run(result);
166 connect_callback_.Reset(); 145 connect_callback_.Reset();
167 } 146 }
168 147
169 void TCPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer, 148 void TCPSocket::OnReadComplete(scoped_refptr<net::IOBuffer> io_buffer,
170 int result) { 149 int result) {
171 DCHECK(!read_callback_.is_null()); 150 DCHECK(!read_callback_.is_null());
172 read_callback_.Run(result, io_buffer); 151 read_callback_.Run(result, io_buffer);
173 read_callback_.Reset(); 152 read_callback_.Reset();
174 } 153 }
175 154
176 void TCPSocket::OnWriteComplete(int result) {
177 DCHECK(!write_callback_.is_null());
178 write_callback_.Run(result);
179 write_callback_.Reset();
180 }
181
182 } // namespace extensions 155 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698