Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "google_apis/gcm/base/socket_stream.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "net/base/io_buffer.h" | |
| 10 #include "net/base/net_errors.h" | |
| 11 #include "net/socket/stream_socket.h" | |
| 12 | |
| 13 namespace gcm { | |
| 14 | |
| 15 // TODO(zea): consider having dynamically sized buffers if this becomes too | |
| 16 // expensive. | |
| 17 const uint32 kDefaultBufferSize = 8*1024; | |
| 18 | |
| 19 SocketInputStream::SocketInputStream(base::TimeDelta read_timeout, | |
| 20 net::StreamSocket* socket) | |
| 21 : socket_(socket), | |
| 22 io_buffer_(new net::IOBufferWithSize(kDefaultBufferSize)), | |
| 23 drainable_io_buffer_(new net::DrainableIOBuffer(io_buffer_.get(), | |
| 24 io_buffer_->size())), | |
| 25 buffer_read_pos_(0), | |
| 26 buffer_write_pos_(0), | |
| 27 limit_(0), | |
| 28 backup_bytes_(0), | |
| 29 skipped_bytes_(0), | |
| 30 last_error_(net::OK), | |
| 31 state_(EMPTY), | |
| 32 read_timeout_(read_timeout), | |
| 33 weak_ptr_factory_(this) { | |
| 34 DCHECK(socket->IsConnected()); | |
| 35 } | |
| 36 | |
| 37 SocketInputStream::~SocketInputStream() { | |
| 38 } | |
| 39 | |
| 40 bool SocketInputStream::Next(const void** data, int* size) { | |
| 41 DCHECK_NE(state_, CLOSED); | |
| 42 DCHECK_NE(state_, READING); | |
| 43 | |
| 44 if (backup_bytes_ > 0) { | |
| 45 *size = backup_bytes_; | |
| 46 *data = io_buffer_->data() + buffer_read_pos_ - backup_bytes_; | |
| 47 backup_bytes_ = 0; | |
| 48 return true; | |
| 49 } | |
| 50 | |
| 51 if (limit_ != 0 && buffer_read_pos_ >= limit_) { | |
| 52 DVLOG(1) << "Reached buffer limit, ending read."; | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 DCHECK_EQ(state_, READY) | |
| 57 << " Input stream must have pending data before reading."; | |
| 58 DCHECK_NE(buffer_write_pos_, buffer_read_pos_); | |
| 59 *data = io_buffer_->data() + buffer_read_pos_; | |
| 60 *size = buffer_write_pos_ - buffer_read_pos_; | |
| 61 buffer_read_pos_ = buffer_write_pos_; | |
| 62 state_ = EMPTY; | |
| 63 DVLOG(1) << "Consuming " << *size << " bytes in input buffer."; | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 void SocketInputStream::BackUp(int count) { | |
| 68 DCHECK(state_ == READY || state_ == EMPTY); | |
| 69 DCHECK_GE(backup_bytes_, 0); | |
| 70 DCHECK_LE(backup_bytes_, buffer_read_pos_); | |
| 71 DCHECK_EQ(skipped_bytes_, 0); | |
| 72 | |
| 73 backup_bytes_ += count; | |
| 74 state_ = READY; | |
| 75 DVLOG(1) << "Backing up " << count << " bytes in input buffer. " | |
| 76 << "Current position now at " << buffer_read_pos_ - backup_bytes_ | |
| 77 << " of " << buffer_write_pos_; | |
| 78 } | |
| 79 | |
| 80 bool SocketInputStream::Skip(int count) { | |
| 81 DCHECK_EQ(state_, READY); | |
| 82 DCHECK_GT(count, 0); | |
| 83 DVLOG(1) << "Skipping " << count << " bytes in stream."; | |
| 84 | |
| 85 if (backup_bytes_ >= count) { | |
| 86 // We have more data left over than we're trying to skip. Just chop it. | |
| 87 backup_bytes_ -= count; | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 count -= backup_bytes_; | |
| 92 backup_bytes_ = 0; | |
| 93 skipped_bytes_ += count; | |
| 94 state_ = EMPTY; | |
| 95 | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 int64 SocketInputStream::ByteCount() const { | |
| 100 DCHECK_NE(state_, CLOSED); | |
| 101 DCHECK_NE(state_, READING); | |
| 102 return buffer_write_pos_; | |
| 103 } | |
| 104 | |
| 105 void SocketInputStream::Refresh(const base::Closure& callback) { | |
| 106 DCHECK_NE(state_, CLOSED); | |
| 107 DCHECK_NE(state_, READING); | |
| 108 | |
| 109 if (buffer_read_pos_ - backup_bytes_ == io_buffer_->size()) { | |
| 110 LOG(ERROR) << "Out of buffer space, closing input stream."; | |
| 111 CloseStream(net::ERR_UNEXPECTED, callback); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 if (!socket_->IsConnected()) { | |
| 116 LOG(ERROR) << "Socket was disconnected, closing input stream"; | |
| 117 CloseStream(net::ERR_CONNECTION_CLOSED, callback); | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 state_ = READING; | |
| 122 int read_limit = | |
| 123 limit_ == 0 ? | |
| 124 drainable_io_buffer_->BytesRemaining() : | |
| 125 limit_ - buffer_read_pos_; | |
| 126 read_limit = std::min(read_limit, drainable_io_buffer_->BytesRemaining()); | |
| 127 | |
| 128 if (read_limit <= buffer_write_pos_ - (buffer_read_pos_ - backup_bytes_)) { | |
| 129 state_ = READY; | |
| 130 if (read_timeout_timer_.IsRunning()) | |
| 131 read_timeout_timer_.Reset(); | |
| 132 DVLOG(1) << "Already have enough data for read limit."; | |
| 133 if (!callback.is_null()) callback.Run(); | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 DVLOG(1) << "Refreshing input stream, limit of " << read_limit << " bytes."; | |
| 138 DCHECK_GT(read_limit, 0); | |
| 139 int result = socket_->Read( | |
| 140 drainable_io_buffer_, | |
| 141 read_limit, | |
| 142 base::Bind(&SocketInputStream::RefreshCompletionCallback, | |
| 143 weak_ptr_factory_.GetWeakPtr(), | |
| 144 callback)); | |
| 145 DVLOG(1) << "Read returned " << result; | |
| 146 if (result != net::ERR_IO_PENDING) | |
| 147 RefreshCompletionCallback(callback, result); | |
| 148 } | |
| 149 | |
| 150 void SocketInputStream::Rebuild() { | |
| 151 DCHECK_EQ(skipped_bytes_, 0); | |
| 152 DVLOG(1) << "Resetting input stream, consumed " | |
| 153 << buffer_read_pos_ - backup_bytes_ << " bytes."; | |
| 154 DCHECK_NE(state_, READING); | |
| 155 DCHECK_NE(state_, CLOSED); | |
| 156 | |
| 157 int last_read_pos = buffer_read_pos_ - backup_bytes_; | |
| 158 char* unread_data_ptr = &(io_buffer_->data()[last_read_pos]); | |
|
Ryan Sleevi
2013/09/17 19:43:30
Why not io_buffer_->data() + last_read_pos?
Nicolas Zea
2013/09/25 01:21:27
Done.
| |
| 159 int unread_buffer_size = buffer_write_pos_ - last_read_pos; | |
| 160 ResetInternal(); | |
| 161 | |
| 162 if (unread_buffer_size > 0) { | |
| 163 buffer_write_pos_ = unread_buffer_size; | |
| 164 drainable_io_buffer_->SetOffset(buffer_write_pos_); | |
| 165 state_ = READY; | |
| 166 | |
| 167 if (last_read_pos != 0) { | |
| 168 DVLOG(1) << "Have " << buffer_write_pos_ | |
| 169 << " unread bytes remaining, shifting."; | |
| 170 // Move any remaining unread data to the start of the buffer; | |
| 171 std::memmove(io_buffer_->data(), unread_data_ptr, buffer_write_pos_); | |
| 172 } else { | |
| 173 DVLOG(1) << "Have " << buffer_write_pos_ << " unread bytes remaining."; | |
| 174 } | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 void SocketInputStream::GetNextMessage(int limit, | |
| 179 const base::Closure& callback) { | |
| 180 DCHECK_GE(limit, 0); | |
| 181 DCHECK_LT(limit, io_buffer_->size()); | |
| 182 DVLOG(1) << "Setting read limit to " << limit; | |
| 183 DVLOG(1) << " Current pos: " << buffer_read_pos_; | |
| 184 DVLOG(1) << " Current backup: " << backup_bytes_; | |
| 185 DVLOG(1) << " Current size: " << buffer_write_pos_; | |
| 186 limit_ = limit; | |
| 187 | |
| 188 // Set up the timeout timer. Note that if this does get triggered, the | |
| 189 // IOBuffer used in the Read call will remain owned by the socket itself | |
| 190 // until the read completes (which may never happen). A timeout must therefore | |
| 191 // be a fatal error for the input stream. | |
| 192 read_timeout_timer_.Start( | |
| 193 FROM_HERE, | |
| 194 read_timeout_, | |
| 195 base::Bind(&SocketInputStream::RefreshCompletionCallback, | |
| 196 weak_ptr_factory_.GetWeakPtr(), | |
| 197 callback, | |
| 198 net::ERR_TIMED_OUT)); | |
| 199 Refresh(callback); | |
| 200 } | |
| 201 | |
| 202 int SocketInputStream::last_error() const { | |
| 203 return last_error_; | |
| 204 } | |
| 205 | |
| 206 SocketInputStream::State SocketInputStream::state() const { | |
| 207 return state_; | |
| 208 } | |
| 209 | |
| 210 void SocketInputStream::RefreshCompletionCallback( | |
| 211 const base::Closure& callback, int result) { | |
| 212 DCHECK_EQ(state_, READING); | |
| 213 // TODO(zea): should the timeout be per read or per message? For now it's | |
| 214 // per read, and reset on every read completion (successful or not). | |
| 215 if (read_timeout_timer_.IsRunning()) | |
| 216 read_timeout_timer_.Reset(); | |
| 217 | |
| 218 if (state_ == CLOSED) { | |
| 219 // An error occured before the completion callback could complete. Ignore | |
| 220 // the result. | |
| 221 return; | |
| 222 } | |
| 223 | |
| 224 if (result < net::OK) { | |
| 225 DVLOG(1) << "Failed to refresh socket: " << result; | |
| 226 CloseStream(result, callback); | |
| 227 return; | |
| 228 } | |
| 229 DCHECK_GT(result, 0); | |
| 230 | |
| 231 state_ = READY; | |
| 232 buffer_write_pos_ += result; | |
| 233 int bytes_to_skip = std::min(skipped_bytes_, result); | |
| 234 buffer_read_pos_ += bytes_to_skip; | |
| 235 skipped_bytes_ = std::max(skipped_bytes_ - result, 0); | |
| 236 drainable_io_buffer_->SetOffset(buffer_write_pos_); | |
| 237 if (buffer_read_pos_ - backup_bytes_ == buffer_write_pos_) | |
| 238 state_ = EMPTY; | |
| 239 | |
| 240 DVLOG(1) << "Refresh complete with " << result << " new bytes. " | |
| 241 << "Current position " << buffer_read_pos_ - backup_bytes_ | |
| 242 << " of " << buffer_write_pos_ << "."; | |
| 243 | |
| 244 // If a limit is set, kick off another refresh to get the rest of the message | |
| 245 // data. | |
| 246 if (limit_ != 0 && buffer_write_pos_ < limit_) { | |
| 247 read_timeout_timer_.Start( | |
| 248 FROM_HERE, | |
| 249 read_timeout_, | |
| 250 base::Bind(&SocketInputStream::RefreshCompletionCallback, | |
| 251 weak_ptr_factory_.GetWeakPtr(), | |
| 252 callback, | |
| 253 net::ERR_TIMED_OUT)); | |
| 254 Refresh(callback); | |
| 255 return; | |
| 256 } | |
| 257 | |
| 258 if (!callback.is_null()) callback.Run(); | |
| 259 } | |
| 260 | |
| 261 void SocketInputStream::ResetInternal() { | |
| 262 weak_ptr_factory_.InvalidateWeakPtrs(); // Invalidate any callbacks. | |
| 263 buffer_write_pos_ = 0; | |
| 264 buffer_read_pos_ = 0; | |
| 265 backup_bytes_ = 0; | |
| 266 skipped_bytes_ = 0; | |
| 267 state_ = EMPTY; | |
| 268 limit_ = 0; | |
| 269 | |
| 270 last_error_ = net::OK; | |
| 271 | |
| 272 // Reset the offset by creating a new one. Note that DrainableIOBuffers don't | |
| 273 // actually allocate their own buffer memory like normal IOBuffers. This will | |
| 274 // just reset the pointers to point to the beginning of io_buffer_'s data. | |
| 275 drainable_io_buffer_ = new net::DrainableIOBuffer(io_buffer_.get(), | |
| 276 io_buffer_->size()); | |
| 277 } | |
| 278 | |
| 279 void SocketInputStream::CloseStream(int result, const base::Closure& callback) { | |
| 280 ResetInternal(); | |
| 281 state_ = CLOSED; | |
| 282 last_error_ = result; | |
| 283 if (!callback.is_null()) callback.Run(); | |
| 284 } | |
| 285 | |
| 286 SocketOutputStream::SocketOutputStream(net::StreamSocket* socket) | |
| 287 : socket_(socket), | |
| 288 io_buffer_(new net::IOBufferWithSize(kDefaultBufferSize)), | |
| 289 drainable_io_buffer_(new net::DrainableIOBuffer(io_buffer_.get(), | |
| 290 io_buffer_->size())), | |
| 291 buffer_used_(0), | |
| 292 state_(EMPTY), | |
| 293 weak_ptr_factory_(this) { | |
| 294 DCHECK(socket->IsConnected()); | |
| 295 } | |
| 296 | |
| 297 SocketOutputStream::~SocketOutputStream() { | |
| 298 } | |
| 299 | |
| 300 bool SocketOutputStream::Next(void** data, int* size) { | |
| 301 DCHECK_NE(state_, CLOSED); | |
| 302 DCHECK_NE(state_, FLUSHING); | |
| 303 if (buffer_used_ == io_buffer_->size()) | |
| 304 return false; | |
| 305 | |
| 306 *data = io_buffer_->data() + buffer_used_; | |
| 307 *size = io_buffer_->size() - buffer_used_; | |
| 308 buffer_used_ = io_buffer_->size(); | |
| 309 state_ = READY; | |
| 310 return true; | |
| 311 } | |
| 312 | |
| 313 void SocketOutputStream::BackUp(int count) { | |
| 314 DCHECK_GE(count, 0); | |
| 315 if (count > buffer_used_) | |
| 316 buffer_used_ = 0; | |
| 317 buffer_used_ -= count; | |
| 318 DVLOG(1) << "Backing up " << count << " bytes in output buffer. " | |
| 319 << buffer_used_ << " bytes used."; | |
| 320 } | |
| 321 | |
| 322 int64 SocketOutputStream::ByteCount() const { | |
| 323 DCHECK_NE(state_, CLOSED); | |
| 324 DCHECK_NE(state_, FLUSHING); | |
| 325 return buffer_used_; | |
| 326 } | |
| 327 | |
| 328 void SocketOutputStream::Flush(const base::Closure& callback) { | |
| 329 DCHECK_EQ(state_, READY); | |
| 330 state_ = FLUSHING; | |
| 331 | |
| 332 if (!socket_->IsConnected()) { | |
| 333 LOG(ERROR) << "Socket was disconnected, closing output stream"; | |
| 334 last_error_ = net::ERR_CONNECTION_CLOSED; | |
| 335 state_ = CLOSED; | |
| 336 if (!callback.is_null()) callback.Run(); | |
| 337 return; | |
| 338 } | |
| 339 | |
| 340 DVLOG(1) << "Flushing " << buffer_used_ << " bytes into socket."; | |
| 341 int result = socket_->Write( | |
| 342 drainable_io_buffer_, | |
| 343 buffer_used_, | |
| 344 base::Bind(&SocketOutputStream::FlushCompletionCallback, | |
| 345 weak_ptr_factory_.GetWeakPtr(), | |
| 346 callback)); | |
| 347 DVLOG(1) << "Write returned " << result; | |
| 348 if (result != net::ERR_IO_PENDING) | |
| 349 FlushCompletionCallback(callback, result); | |
| 350 } | |
| 351 | |
| 352 SocketOutputStream::State SocketOutputStream::state() const{ | |
| 353 return state_; | |
| 354 } | |
| 355 | |
| 356 int SocketOutputStream::last_error() const { | |
| 357 return last_error_; | |
| 358 } | |
| 359 | |
| 360 void SocketOutputStream::FlushCompletionCallback( | |
| 361 const base::Closure& callback, int result) { | |
| 362 DCHECK_EQ(state_, FLUSHING); | |
| 363 if (result < net::OK) { | |
| 364 LOG(ERROR) << "Failed to flush socket."; | |
| 365 last_error_ = result; | |
| 366 state_ = CLOSED; | |
| 367 if (!callback.is_null()) callback.Run(); | |
| 368 return; | |
| 369 } | |
| 370 | |
| 371 state_ = READY; | |
| 372 if (drainable_io_buffer_->BytesConsumed() + result < buffer_used_) { | |
| 373 DVLOG(1) << "Partial flush complete. Retrying."; | |
| 374 // Only a partial write was completed. Flush again to finish the write. | |
| 375 drainable_io_buffer_->SetOffset( | |
| 376 drainable_io_buffer_->BytesConsumed() + result); | |
| 377 Flush(callback); | |
| 378 return; | |
| 379 } | |
| 380 | |
| 381 DVLOG(1) << "Socket flush complete."; | |
| 382 drainable_io_buffer_ = new net::DrainableIOBuffer(io_buffer_.get(), | |
| 383 io_buffer_->size()); | |
| 384 state_ = EMPTY; | |
| 385 buffer_used_ = 0; | |
| 386 if (!callback.is_null()) callback.Run(); | |
| 387 } | |
| 388 | |
| 389 } // namespace gcm | |
| OLD | NEW |