OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "google_apis/gcm/base/socket_stream.h" | 5 #include "google_apis/gcm/base/socket_stream.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
10 #include "net/socket/stream_socket.h" | 10 #include "net/socket/stream_socket.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 DCHECK_LT(next_pos_, read_buffer_->BytesConsumed()); | 49 DCHECK_LT(next_pos_, read_buffer_->BytesConsumed()); |
50 *data = io_buffer_->data() + next_pos_; | 50 *data = io_buffer_->data() + next_pos_; |
51 *size = UnreadByteCount(); | 51 *size = UnreadByteCount(); |
52 next_pos_ = read_buffer_->BytesConsumed(); | 52 next_pos_ = read_buffer_->BytesConsumed(); |
53 DVLOG(1) << "Consuming " << *size << " bytes in input buffer."; | 53 DVLOG(1) << "Consuming " << *size << " bytes in input buffer."; |
54 return true; | 54 return true; |
55 } | 55 } |
56 | 56 |
57 void SocketInputStream::BackUp(int count) { | 57 void SocketInputStream::BackUp(int count) { |
58 DCHECK(GetState() == READY || GetState() == EMPTY); | 58 DCHECK(GetState() == READY || GetState() == EMPTY); |
59 DCHECK_GT(count, 0); | 59 // TODO(zea): investigating crbug.com/409985 |
60 DCHECK_LE(count, next_pos_); | 60 CHECK_GT(count, 0); |
| 61 CHECK_LE(count, next_pos_); |
61 | 62 |
62 next_pos_ -= count; | 63 next_pos_ -= count; |
63 DVLOG(1) << "Backing up " << count << " bytes in input buffer. " | 64 DVLOG(1) << "Backing up " << count << " bytes in input buffer. " |
64 << "Current position now at " << next_pos_ | 65 << "Current position now at " << next_pos_ |
65 << " of " << read_buffer_->BytesConsumed(); | 66 << " of " << read_buffer_->BytesConsumed(); |
66 } | 67 } |
67 | 68 |
68 bool SocketInputStream::Skip(int count) { | 69 bool SocketInputStream::Skip(int count) { |
69 NOTIMPLEMENTED(); | 70 NOTIMPLEMENTED(); |
70 return false; | 71 return false; |
71 } | 72 } |
72 | 73 |
73 int64 SocketInputStream::ByteCount() const { | 74 int64 SocketInputStream::ByteCount() const { |
74 DCHECK_NE(GetState(), CLOSED); | 75 DCHECK_NE(GetState(), CLOSED); |
75 DCHECK_NE(GetState(), READING); | 76 DCHECK_NE(GetState(), READING); |
76 return next_pos_; | 77 return next_pos_; |
77 } | 78 } |
78 | 79 |
79 size_t SocketInputStream::UnreadByteCount() const { | 80 int SocketInputStream::UnreadByteCount() const { |
80 DCHECK_NE(GetState(), CLOSED); | 81 DCHECK_NE(GetState(), CLOSED); |
81 DCHECK_NE(GetState(), READING); | 82 DCHECK_NE(GetState(), READING); |
82 return read_buffer_->BytesConsumed() - next_pos_; | 83 return read_buffer_->BytesConsumed() - next_pos_; |
83 } | 84 } |
84 | 85 |
85 net::Error SocketInputStream::Refresh(const base::Closure& callback, | 86 net::Error SocketInputStream::Refresh(const base::Closure& callback, |
86 int byte_limit) { | 87 int byte_limit) { |
87 DCHECK_NE(GetState(), CLOSED); | 88 DCHECK_NE(GetState(), CLOSED); |
88 DCHECK_NE(GetState(), READING); | 89 DCHECK_NE(GetState(), READING); |
89 DCHECK_GT(byte_limit, 0); | 90 DCHECK_GT(byte_limit, 0); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 131 |
131 if (unread_data_ptr != io_buffer_->data()) { | 132 if (unread_data_ptr != io_buffer_->data()) { |
132 DVLOG(1) << "Have " << unread_data_size | 133 DVLOG(1) << "Have " << unread_data_size |
133 << " unread bytes remaining, shifting."; | 134 << " unread bytes remaining, shifting."; |
134 // Move any remaining unread data to the start of the buffer; | 135 // Move any remaining unread data to the start of the buffer; |
135 std::memmove(io_buffer_->data(), unread_data_ptr, unread_data_size); | 136 std::memmove(io_buffer_->data(), unread_data_ptr, unread_data_size); |
136 } else { | 137 } else { |
137 DVLOG(1) << "Have " << unread_data_size << " unread bytes remaining."; | 138 DVLOG(1) << "Have " << unread_data_size << " unread bytes remaining."; |
138 } | 139 } |
139 read_buffer_->DidConsume(unread_data_size); | 140 read_buffer_->DidConsume(unread_data_size); |
| 141 // TODO(zea): investigating crbug.com/409985 |
| 142 CHECK_GE(UnreadByteCount(), 0); |
140 } | 143 } |
141 | 144 |
142 net::Error SocketInputStream::last_error() const { | 145 net::Error SocketInputStream::last_error() const { |
143 return last_error_; | 146 return last_error_; |
144 } | 147 } |
145 | 148 |
146 SocketInputStream::State SocketInputStream::GetState() const { | 149 SocketInputStream::State SocketInputStream::GetState() const { |
147 if (last_error_ < net::ERR_IO_PENDING) | 150 if (last_error_ < net::ERR_IO_PENDING) |
148 return CLOSED; | 151 return CLOSED; |
149 | 152 |
(...skipping 22 matching lines...) Expand all Loading... |
172 | 175 |
173 if (result < net::OK) { | 176 if (result < net::OK) { |
174 DVLOG(1) << "Failed to refresh socket: " << result; | 177 DVLOG(1) << "Failed to refresh socket: " << result; |
175 CloseStream(static_cast<net::Error>(result), callback); | 178 CloseStream(static_cast<net::Error>(result), callback); |
176 return; | 179 return; |
177 } | 180 } |
178 | 181 |
179 DCHECK_GT(result, 0); | 182 DCHECK_GT(result, 0); |
180 last_error_ = net::OK; | 183 last_error_ = net::OK; |
181 read_buffer_->DidConsume(result); | 184 read_buffer_->DidConsume(result); |
| 185 // TODO(zea): investigating crbug.com/409985 |
| 186 CHECK_GT(UnreadByteCount(), 0); |
182 | 187 |
183 DVLOG(1) << "Refresh complete with " << result << " new bytes. " | 188 DVLOG(1) << "Refresh complete with " << result << " new bytes. " |
184 << "Current position " << next_pos_ | 189 << "Current position " << next_pos_ |
185 << " of " << read_buffer_->BytesConsumed() << "."; | 190 << " of " << read_buffer_->BytesConsumed() << "."; |
186 | 191 |
187 if (!callback.is_null()) | 192 if (!callback.is_null()) |
188 callback.Run(); | 193 callback.Run(); |
189 } | 194 } |
190 | 195 |
191 void SocketInputStream::ResetInternal() { | 196 void SocketInputStream::ResetInternal() { |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 } | 328 } |
324 | 329 |
325 DVLOG(1) << "Socket flush complete."; | 330 DVLOG(1) << "Socket flush complete."; |
326 write_buffer_->SetOffset(0); | 331 write_buffer_->SetOffset(0); |
327 next_pos_ = 0; | 332 next_pos_ = 0; |
328 if (!callback.is_null()) | 333 if (!callback.is_null()) |
329 callback.Run(); | 334 callback.Run(); |
330 } | 335 } |
331 | 336 |
332 } // namespace gcm | 337 } // namespace gcm |
OLD | NEW |