OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/serial/data_sink_receiver.h" | 5 #include "device/serial/data_sink_receiver.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 bool cancelled_; | 43 bool cancelled_; |
44 | 44 |
45 // If |cancelled_|, contains the cancellation error to report. | 45 // If |cancelled_|, contains the cancellation error to report. |
46 int32_t cancellation_error_; | 46 int32_t cancellation_error_; |
47 }; | 47 }; |
48 | 48 |
49 // A frame of data received from the client. | 49 // A frame of data received from the client. |
50 class DataSinkReceiver::DataFrame { | 50 class DataSinkReceiver::DataFrame { |
51 public: | 51 public: |
52 explicit DataFrame(mojo::Array<uint8_t> data, | 52 explicit DataFrame(mojo::Array<uint8_t> data, |
53 const serial::DataSink::OnDataCallback& callback); | 53 const mojo::Callback<void(uint32_t, int32_t)>& callback); |
54 | 54 |
55 // Returns the number of unconsumed bytes remaining of this data frame. | 55 // Returns the number of unconsumed bytes remaining of this data frame. |
56 uint32_t GetRemainingBytes(); | 56 uint32_t GetRemainingBytes(); |
57 | 57 |
58 // Returns a pointer to the remaining data to be consumed. | 58 // Returns a pointer to the remaining data to be consumed. |
59 const char* GetData(); | 59 const char* GetData(); |
60 | 60 |
61 // Reports that |bytes_read| bytes have been consumed. | 61 // Reports that |bytes_read| bytes have been consumed. |
62 void OnDataConsumed(uint32_t bytes_read); | 62 void OnDataConsumed(uint32_t bytes_read); |
63 | 63 |
64 // Reports that an error occurred. | 64 // Reports that an error occurred. |
65 void ReportError(uint32_t bytes_read, int32_t error); | 65 void ReportError(uint32_t bytes_read, int32_t error); |
66 | 66 |
67 private: | 67 private: |
68 mojo::Array<uint8_t> data_; | 68 mojo::Array<uint8_t> data_; |
69 uint32_t offset_; | 69 uint32_t offset_; |
70 const serial::DataSink::OnDataCallback callback_; | 70 const mojo::Callback<void(uint32_t, int32_t)> callback_; |
71 }; | 71 }; |
72 | 72 |
73 DataSinkReceiver::DataSinkReceiver( | 73 DataSinkReceiver::DataSinkReceiver( |
74 mojo::InterfaceRequest<serial::DataSink> request, | 74 mojo::InterfaceRequest<serial::DataSink> request, |
75 const ReadyCallback& ready_callback, | 75 const ReadyCallback& ready_callback, |
76 const CancelCallback& cancel_callback, | 76 const CancelCallback& cancel_callback, |
77 const ErrorCallback& error_callback) | 77 const ErrorCallback& error_callback) |
78 : binding_(this, std::move(request)), | 78 : binding_(this, std::move(request)), |
79 ready_callback_(ready_callback), | 79 ready_callback_(ready_callback), |
80 cancel_callback_(cancel_callback), | 80 cancel_callback_(cancel_callback), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 buffer_in_use_->Cancel(error); | 114 buffer_in_use_->Cancel(error); |
115 if (!cancel_callback_.is_null()) | 115 if (!cancel_callback_.is_null()) |
116 cancel_callback_.Run(error); | 116 cancel_callback_.Run(error); |
117 return; | 117 return; |
118 } | 118 } |
119 ReportError(0, error); | 119 ReportError(0, error); |
120 } | 120 } |
121 | 121 |
122 void DataSinkReceiver::OnData( | 122 void DataSinkReceiver::OnData( |
123 mojo::Array<uint8_t> data, | 123 mojo::Array<uint8_t> data, |
124 const serial::DataSink::OnDataCallback& callback) { | 124 const mojo::Callback<void(uint32_t, int32_t)>& callback) { |
125 if (current_error_) { | 125 if (current_error_) { |
126 callback.Run(0, current_error_); | 126 callback.Run(0, current_error_); |
127 return; | 127 return; |
128 } | 128 } |
129 pending_data_buffers_.push( | 129 pending_data_buffers_.push( |
130 linked_ptr<DataFrame>(new DataFrame(std::move(data), callback))); | 130 linked_ptr<DataFrame>(new DataFrame(std::move(data), callback))); |
131 if (!buffer_in_use_) | 131 if (!buffer_in_use_) |
132 RunReadyCallback(); | 132 RunReadyCallback(); |
133 } | 133 } |
134 | 134 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 int32_t error) { | 249 int32_t error) { |
250 scoped_refptr<DataSinkReceiver> receiver = receiver_; | 250 scoped_refptr<DataSinkReceiver> receiver = receiver_; |
251 receiver_ = nullptr; | 251 receiver_ = nullptr; |
252 receiver->DoneWithError(bytes_read, error); | 252 receiver->DoneWithError(bytes_read, error); |
253 buffer_ = NULL; | 253 buffer_ = NULL; |
254 buffer_size_ = 0; | 254 buffer_size_ = 0; |
255 } | 255 } |
256 | 256 |
257 DataSinkReceiver::DataFrame::DataFrame( | 257 DataSinkReceiver::DataFrame::DataFrame( |
258 mojo::Array<uint8_t> data, | 258 mojo::Array<uint8_t> data, |
259 const serial::DataSink::OnDataCallback& callback) | 259 const mojo::Callback<void(uint32_t, int32_t)>& callback) |
260 : data_(std::move(data)), offset_(0), callback_(callback) { | 260 : data_(std::move(data)), offset_(0), callback_(callback) { |
261 DCHECK_LT(0u, data_.size()); | 261 DCHECK_LT(0u, data_.size()); |
262 } | 262 } |
263 | 263 |
264 // Returns the number of uncomsumed bytes remaining of this data frame. | 264 // Returns the number of uncomsumed bytes remaining of this data frame. |
265 uint32_t DataSinkReceiver::DataFrame::GetRemainingBytes() { | 265 uint32_t DataSinkReceiver::DataFrame::GetRemainingBytes() { |
266 return static_cast<uint32_t>(data_.size() - offset_); | 266 return static_cast<uint32_t>(data_.size() - offset_); |
267 } | 267 } |
268 | 268 |
269 // Returns a pointer to the remaining data to be consumed. | 269 // Returns a pointer to the remaining data to be consumed. |
270 const char* DataSinkReceiver::DataFrame::GetData() { | 270 const char* DataSinkReceiver::DataFrame::GetData() { |
271 DCHECK_LT(offset_, data_.size()); | 271 DCHECK_LT(offset_, data_.size()); |
272 return reinterpret_cast<const char*>(&data_[0]) + offset_; | 272 return reinterpret_cast<const char*>(&data_[0]) + offset_; |
273 } | 273 } |
274 | 274 |
275 void DataSinkReceiver::DataFrame::OnDataConsumed(uint32_t bytes_read) { | 275 void DataSinkReceiver::DataFrame::OnDataConsumed(uint32_t bytes_read) { |
276 offset_ += bytes_read; | 276 offset_ += bytes_read; |
277 DCHECK_LE(offset_, data_.size()); | 277 DCHECK_LE(offset_, data_.size()); |
278 if (offset_ == data_.size()) | 278 if (offset_ == data_.size()) |
279 callback_.Run(offset_, 0); | 279 callback_.Run(offset_, 0); |
280 } | 280 } |
281 void DataSinkReceiver::DataFrame::ReportError(uint32_t bytes_read, | 281 void DataSinkReceiver::DataFrame::ReportError(uint32_t bytes_read, |
282 int32_t error) { | 282 int32_t error) { |
283 offset_ += bytes_read; | 283 offset_ += bytes_read; |
284 DCHECK_LE(offset_, data_.size()); | 284 DCHECK_LE(offset_, data_.size()); |
285 callback_.Run(offset_, error); | 285 callback_.Run(offset_, error); |
286 } | 286 } |
287 | 287 |
288 } // namespace device | 288 } // namespace device |
OLD | NEW |