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 "net/base/io_buffer.h" | |
| 9 #include "net/socket/stream_socket.h" | |
| 10 | |
| 11 namespace gcm { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // TODO(zea): consider having dynamically-sized buffers if this becomes too | |
| 16 // expensive. | |
| 17 const uint32 kDefaultBufferSize = 8*1024; | |
| 18 | |
| 19 } // namespace | |
| 20 | |
| 21 SocketInputStream::SocketInputStream(net::StreamSocket* socket) | |
| 22 : socket_(socket), | |
| 23 io_buffer_(new net::IOBuffer(kDefaultBufferSize)), | |
| 24 read_buffer_(new net::DrainableIOBuffer(io_buffer_.get(), | |
| 25 kDefaultBufferSize)), | |
| 26 next_pos_(0), | |
| 27 last_error_(net::OK), | |
| 28 weak_ptr_factory_(this) { | |
| 29 DCHECK(socket->IsConnected()); | |
| 30 } | |
| 31 | |
| 32 SocketInputStream::~SocketInputStream() { | |
| 33 } | |
| 34 | |
| 35 bool SocketInputStream::Next(const void** data, int* size) { | |
| 36 DCHECK_NE(GetState(), CLOSED); | |
|
akalin
2013/10/12 03:18:36
i'd probably do
if (GetState() != EMPTY && GetSta
Nicolas Zea
2013/10/14 23:39:15
Done.
| |
| 37 DCHECK_NE(GetState(), READING); | |
| 38 | |
| 39 if (GetState() == EMPTY) { | |
| 40 DVLOG(1) << "No unread data remaining, ending read."; | |
| 41 return false; | |
| 42 } | |
| 43 | |
| 44 DCHECK_EQ(GetState(), READY) | |
| 45 << " Input stream must have pending data before reading."; | |
| 46 DCHECK_LT(next_pos_, read_buffer_->BytesConsumed()); | |
| 47 *data = io_buffer_->data() + next_pos_; | |
| 48 *size = read_buffer_->BytesConsumed() - next_pos_; | |
| 49 next_pos_ = read_buffer_->BytesConsumed(); | |
| 50 DVLOG(1) << "Consuming " << *size << " bytes in input buffer."; | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 void SocketInputStream::BackUp(int count) { | |
| 55 DCHECK(GetState() == READY || GetState() == EMPTY); | |
| 56 DCHECK_GT(count, 0); | |
| 57 DCHECK_LE(count, next_pos_); | |
| 58 | |
| 59 next_pos_ -= count; | |
| 60 DVLOG(1) << "Backing up " << count << " bytes in input buffer. " | |
| 61 << "Current position now at " << next_pos_ | |
| 62 << " of " << read_buffer_->BytesConsumed(); | |
| 63 } | |
| 64 | |
| 65 bool SocketInputStream::Skip(int count) { | |
| 66 NOTIMPLEMENTED(); | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 int64 SocketInputStream::ByteCount() const { | |
| 71 DCHECK_NE(GetState(), CLOSED); | |
| 72 DCHECK_NE(GetState(), READING); | |
| 73 return read_buffer_->BytesConsumed() - next_pos_; | |
| 74 } | |
| 75 | |
| 76 net::Error SocketInputStream::Refresh(const base::Closure& callback, | |
| 77 int byte_limit) { | |
| 78 DCHECK_NE(GetState(), CLOSED); | |
| 79 DCHECK_NE(GetState(), READING); | |
| 80 DCHECK_GT(byte_limit, 0); | |
| 81 | |
| 82 if (byte_limit > read_buffer_->BytesRemaining()) { | |
| 83 NOTREACHED() << "Out of buffer space, closing input stream."; | |
| 84 CloseStream(net::ERR_UNEXPECTED, base::Closure()); | |
| 85 return net::OK; | |
| 86 } | |
| 87 | |
| 88 if (!socket_->IsConnected()) { | |
| 89 LOG(ERROR) << "Socket was disconnected, closing input stream"; | |
| 90 CloseStream(net::ERR_CONNECTION_CLOSED, base::Closure()); | |
| 91 return net::OK; | |
| 92 } | |
| 93 | |
| 94 DVLOG(1) << "Refreshing input stream, limit of " << byte_limit << " bytes."; | |
| 95 int result = socket_->Read( | |
| 96 read_buffer_, | |
| 97 byte_limit, | |
| 98 base::Bind(&SocketInputStream::RefreshCompletionCallback, | |
| 99 weak_ptr_factory_.GetWeakPtr(), | |
| 100 callback)); | |
| 101 DVLOG(1) << "Read returned " << result; | |
| 102 if (result == net::ERR_IO_PENDING) { | |
| 103 last_error_ = net::ERR_IO_PENDING; | |
| 104 return net::ERR_IO_PENDING; | |
| 105 } | |
| 106 | |
| 107 RefreshCompletionCallback(base::Closure(), result); | |
| 108 return net::OK; | |
| 109 } | |
| 110 | |
| 111 void SocketInputStream::RebuildBuffer() { | |
| 112 DVLOG(1) << "Rebuilding input stream, consumed " | |
| 113 << next_pos_ << " bytes."; | |
| 114 DCHECK_NE(GetState(), READING); | |
| 115 DCHECK_NE(GetState(), CLOSED); | |
| 116 | |
| 117 int unread_data_size = 0; | |
| 118 const void* unread_data_ptr = NULL; | |
| 119 Next(&unread_data_ptr, &unread_data_size); | |
| 120 ResetInternal(); | |
| 121 | |
| 122 if (unread_data_ptr != io_buffer_->data()) { | |
| 123 DVLOG(1) << "Have " << unread_data_size | |
| 124 << " unread bytes remaining, shifting."; | |
| 125 // Move any remaining unread data to the start of the buffer; | |
| 126 std::memmove(io_buffer_->data(), unread_data_ptr, unread_data_size); | |
| 127 } else { | |
| 128 DVLOG(1) << "Have " << unread_data_size << " unread bytes remaining."; | |
| 129 } | |
| 130 read_buffer_->DidConsume(unread_data_size); | |
| 131 } | |
| 132 | |
| 133 net::Error SocketInputStream::last_error() const { | |
| 134 return last_error_; | |
| 135 } | |
| 136 | |
| 137 SocketInputStream::State SocketInputStream::GetState() const { | |
| 138 if (last_error_ < net::ERR_IO_PENDING) | |
| 139 return CLOSED; | |
| 140 | |
| 141 if (last_error_ == net::ERR_IO_PENDING) | |
| 142 return READING; | |
| 143 | |
| 144 DCHECK_EQ(last_error_, net::OK); | |
| 145 if (read_buffer_->BytesConsumed() == next_pos_) | |
| 146 return EMPTY; | |
| 147 | |
| 148 return READY; | |
| 149 } | |
| 150 | |
| 151 void SocketInputStream::RefreshCompletionCallback( | |
| 152 const base::Closure& callback, int result) { | |
| 153 // If an error occurred before the completion callback could complete, ignore | |
| 154 // the result. | |
| 155 if (GetState() == CLOSED) | |
| 156 return; | |
| 157 | |
| 158 // Result == 0 implies EOF, which is treated as an error. | |
|
akalin
2013/10/12 03:18:36
i'd probably structure it as:
if (result == 0)
Nicolas Zea
2013/10/14 23:39:15
Done.
| |
| 159 if (result < net::OK || result == 0) { | |
| 160 DVLOG(1) << "Failed to refresh socket: " << result; | |
| 161 if (result == 0) | |
| 162 result = net::ERR_UNEXPECTED; | |
| 163 CloseStream(static_cast<net::Error>(result), callback); | |
| 164 return; | |
| 165 } | |
| 166 | |
| 167 DCHECK_GT(result, 0); | |
| 168 last_error_ = net::OK; | |
| 169 read_buffer_->DidConsume(result); | |
| 170 | |
| 171 DVLOG(1) << "Refresh complete with " << result << " new bytes. " | |
| 172 << "Current position " << next_pos_ | |
| 173 << " of " << read_buffer_->BytesConsumed() << "."; | |
| 174 | |
| 175 if (!callback.is_null()) | |
| 176 callback.Run(); | |
| 177 } | |
| 178 | |
| 179 void SocketInputStream::ResetInternal() { | |
| 180 read_buffer_->SetOffset(0); | |
| 181 next_pos_ = 0; | |
| 182 last_error_ = net::OK; | |
| 183 weak_ptr_factory_.InvalidateWeakPtrs(); // Invalidate any callbacks. | |
| 184 } | |
| 185 | |
| 186 void SocketInputStream::CloseStream(net::Error error, | |
| 187 const base::Closure& callback) { | |
| 188 DCHECK_LT(error, net::ERR_IO_PENDING); | |
| 189 ResetInternal(); | |
| 190 last_error_ = error; | |
| 191 LOG(ERROR) << "Closing stream with result " << error; | |
| 192 if (!callback.is_null()) | |
| 193 callback.Run(); | |
| 194 } | |
| 195 | |
| 196 SocketOutputStream::SocketOutputStream(net::StreamSocket* socket) | |
| 197 : socket_(socket), | |
| 198 io_buffer_(new net::IOBuffer(kDefaultBufferSize)), | |
| 199 write_buffer_(new net::DrainableIOBuffer(io_buffer_.get(), | |
| 200 kDefaultBufferSize)), | |
| 201 buffer_used_(0), | |
| 202 last_error_(net::OK), | |
| 203 weak_ptr_factory_(this) { | |
| 204 DCHECK(socket->IsConnected()); | |
| 205 } | |
| 206 | |
| 207 SocketOutputStream::~SocketOutputStream() { | |
| 208 } | |
| 209 | |
| 210 bool SocketOutputStream::Next(void** data, int* size) { | |
| 211 DCHECK_NE(GetState(), CLOSED); | |
| 212 DCHECK_NE(GetState(), FLUSHING); | |
| 213 if (buffer_used_ == write_buffer_->size()) | |
| 214 return false; | |
| 215 | |
| 216 *data = write_buffer_->data() + buffer_used_; | |
| 217 *size = write_buffer_->size() - buffer_used_; | |
| 218 buffer_used_ = write_buffer_->size(); | |
| 219 return true; | |
| 220 } | |
| 221 | |
| 222 void SocketOutputStream::BackUp(int count) { | |
| 223 DCHECK_GE(count, 0); | |
| 224 if (count > buffer_used_) | |
| 225 buffer_used_ = 0; | |
| 226 buffer_used_ -= count; | |
| 227 DVLOG(1) << "Backing up " << count << " bytes in output buffer. " | |
| 228 << buffer_used_ << " bytes used."; | |
| 229 } | |
| 230 | |
| 231 int64 SocketOutputStream::ByteCount() const { | |
| 232 DCHECK_NE(GetState(), CLOSED); | |
| 233 DCHECK_NE(GetState(), FLUSHING); | |
| 234 return buffer_used_; | |
| 235 } | |
| 236 | |
| 237 net::Error SocketOutputStream::Flush(const base::Closure& callback) { | |
| 238 DCHECK_EQ(GetState(), READY); | |
| 239 | |
| 240 if (!socket_->IsConnected()) { | |
| 241 LOG(ERROR) << "Socket was disconnected, closing output stream"; | |
| 242 last_error_ = net::ERR_CONNECTION_CLOSED; | |
| 243 return net::OK; | |
| 244 } | |
| 245 | |
| 246 DVLOG(1) << "Flushing " << buffer_used_ << " bytes into socket."; | |
| 247 int result = socket_->Write( | |
| 248 write_buffer_, | |
| 249 buffer_used_, | |
| 250 base::Bind(&SocketOutputStream::FlushCompletionCallback, | |
| 251 weak_ptr_factory_.GetWeakPtr(), | |
| 252 callback)); | |
| 253 DVLOG(1) << "Write returned " << result; | |
| 254 if (result == net::ERR_IO_PENDING) { | |
| 255 last_error_ = net::ERR_IO_PENDING; | |
| 256 return net::ERR_IO_PENDING; | |
| 257 } | |
| 258 | |
| 259 FlushCompletionCallback(base::Closure(), result); | |
| 260 return net::OK; | |
| 261 } | |
| 262 | |
| 263 SocketOutputStream::State SocketOutputStream::GetState() const{ | |
| 264 if (last_error_ < net::ERR_IO_PENDING) | |
| 265 return CLOSED; | |
| 266 | |
| 267 if (last_error_ == net::ERR_IO_PENDING) | |
| 268 return FLUSHING; | |
| 269 | |
| 270 DCHECK_EQ(last_error_, net::OK); | |
| 271 if (buffer_used_ == 0) | |
| 272 return EMPTY; | |
| 273 | |
| 274 return READY; | |
| 275 } | |
| 276 | |
| 277 net::Error SocketOutputStream::last_error() const { | |
| 278 return last_error_; | |
| 279 } | |
| 280 | |
| 281 void SocketOutputStream::FlushCompletionCallback( | |
| 282 const base::Closure& callback, int result) { | |
| 283 // If an error occurred before the completion callback could complete, ignore | |
| 284 // the result. | |
| 285 if (GetState() == CLOSED) | |
| 286 return; | |
| 287 | |
| 288 if (result < net::OK) { | |
|
akalin
2013/10/12 03:18:36
just double checking -- does this do what you want
Nicolas Zea
2013/10/14 23:39:15
The Socket::Write documentation doesn't mention 0
| |
| 289 LOG(ERROR) << "Failed to flush socket."; | |
| 290 last_error_ = static_cast<net::Error>(result); | |
| 291 if (!callback.is_null()) | |
| 292 callback.Run(); | |
| 293 return; | |
| 294 } | |
| 295 | |
| 296 DCHECK_GT(result, net::OK); | |
| 297 last_error_ = net::OK; | |
| 298 | |
| 299 if (write_buffer_->BytesConsumed() + result < buffer_used_) { | |
| 300 DVLOG(1) << "Partial flush complete. Retrying."; | |
| 301 // Only a partial write was completed. Flush again to finish the write. | |
| 302 write_buffer_->DidConsume(result); | |
| 303 Flush(callback); | |
| 304 return; | |
| 305 } | |
| 306 | |
| 307 DVLOG(1) << "Socket flush complete."; | |
| 308 write_buffer_->SetOffset(0); | |
| 309 buffer_used_ = 0; | |
| 310 if (!callback.is_null()) | |
| 311 callback.Run(); | |
| 312 } | |
| 313 | |
| 314 } // namespace gcm | |
| OLD | NEW |