| 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 #ifndef DEVICE_SERIAL_DATA_SENDER_H_ | 5 #ifndef DEVICE_SERIAL_DATA_SENDER_H_ |
| 6 #define DEVICE_SERIAL_DATA_SENDER_H_ | 6 #define DEVICE_SERIAL_DATA_SENDER_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/memory/linked_ptr.h" | 11 #include "base/memory/linked_ptr.h" |
| 12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 13 #include "device/serial/buffer.h" | 13 #include "device/serial/buffer.h" |
| 14 #include "device/serial/data_stream.mojom.h" | 14 #include "device/serial/data_stream.mojom.h" |
| 15 #include "mojo/public/cpp/system/data_pipe.h" | 15 #include "mojo/public/cpp/system/data_pipe.h" |
| 16 | 16 |
| 17 namespace device { | 17 namespace device { |
| 18 | 18 |
| 19 class AsyncWaiter; | |
| 20 | |
| 21 // A DataSender sends data to a DataSink. | 19 // A DataSender sends data to a DataSink. |
| 22 class DataSender : public serial::DataSinkClient, public mojo::ErrorHandler { | 20 class DataSender : public serial::DataSinkClient, public mojo::ErrorHandler { |
| 23 public: | 21 public: |
| 24 typedef base::Callback<void(uint32_t bytes_sent)> DataSentCallback; | 22 typedef base::Callback<void(uint32_t bytes_sent)> DataSentCallback; |
| 25 typedef base::Callback<void(uint32_t bytes_sent, int32_t error)> | 23 typedef base::Callback<void(uint32_t bytes_sent, int32_t error)> |
| 26 SendErrorCallback; | 24 SendErrorCallback; |
| 27 typedef base::Callback<void()> CancelCallback; | 25 typedef base::Callback<void()> CancelCallback; |
| 28 | 26 |
| 29 // Constructs a DataSender to send data to |sink|, using a data pipe with a | 27 // Constructs a DataSender to send data to |sink|, using a buffer size of |
| 30 // buffer size of |buffer_size|, with connection errors reported as | 28 // |buffer_size|, with connection errors reported as |fatal_error_value|. |
| 31 // |fatal_error_value|. | |
| 32 DataSender(mojo::InterfacePtr<serial::DataSink> sink, | 29 DataSender(mojo::InterfacePtr<serial::DataSink> sink, |
| 33 uint32_t buffer_size, | 30 uint32_t buffer_size, |
| 34 int32_t fatal_error_value); | 31 int32_t fatal_error_value); |
| 35 | 32 |
| 36 virtual ~DataSender(); | 33 virtual ~DataSender(); |
| 37 | 34 |
| 38 // Starts an asynchronous send of |data|. If the send completes successfully, | 35 // Starts an asynchronous send of |data|. If the send completes successfully, |
| 39 // |callback| will be called. Otherwise, |error_callback| will be called with | 36 // |callback| will be called. Otherwise, |error_callback| will be called with |
| 40 // the error. If there is a cancel in progress or there has been a connection | 37 // the error. If there is a cancel in progress or there has been a connection |
| 41 // error, this will not start a send and returns false. It is the caller's | 38 // error, this will not start a send and returns false. It is the caller's |
| 42 // responsibility to ensure |data| remains valid until one of |callback| and | 39 // responsibility to ensure |data| remains valid until one of |callback| and |
| 43 // |error_callback| is called. | 40 // |error_callback| is called. |
| 44 bool Send(const base::StringPiece& data, | 41 bool Send(const base::StringPiece& data, |
| 45 const DataSentCallback& callback, | 42 const DataSentCallback& callback, |
| 46 const SendErrorCallback& error_callback); | 43 const SendErrorCallback& error_callback); |
| 47 | 44 |
| 48 // Requests the cancellation of any in-progress sends. Calls to Send() will | 45 // Requests the cancellation of any in-progress sends. Calls to Send() will |
| 49 // fail until |callback| is called. | 46 // fail until |callback| is called. |
| 50 bool Cancel(int32_t error, const CancelCallback& callback); | 47 bool Cancel(int32_t error, const CancelCallback& callback); |
| 51 | 48 |
| 52 private: | 49 private: |
| 53 class PendingSend; | 50 class PendingSend; |
| 54 | 51 |
| 55 // serial::DataSinkClient overrides. | 52 // serial::DataSinkClient overrides. |
| 56 virtual void ReportBytesSent(uint32_t bytes_sent) override; | 53 virtual void ReportBytesSent(uint32_t bytes_sent) override; |
| 57 virtual void ReportBytesSentAndError( | 54 virtual void ReportBytesSentAndError( |
| 58 uint32_t bytes_sent, | 55 uint32_t bytes_sent, |
| 59 int32_t error, | 56 int32_t error, |
| 60 const mojo::Callback<void(uint32_t)>& callback) override; | 57 const mojo::Callback<void()>& callback) override; |
| 61 | 58 |
| 62 // mojo::ErrorHandler override. | 59 // mojo::ErrorHandler override. |
| 63 virtual void OnConnectionError() override; | 60 virtual void OnConnectionError() override; |
| 64 | 61 |
| 65 // Copies data from |pending_sends_| into the data pipe and starts |waiter_| | 62 // Sends up to |available_buffer_capacity_| bytes of data from |
| 66 // waiting if the pipe is full. When a PendingSend in |pending_sends_| has | 63 // |pending_sends_| to |sink_|. When a PendingSend in |pending_sends_| has |
| 67 // been fully copied into the data pipe, it moves to |sends_awaiting_ack_|. | 64 // been fully copied transmitted to |sink_|, it moves to |
| 65 // |sends_awaiting_ack_|. |
| 68 void SendInternal(); | 66 void SendInternal(); |
| 69 | 67 |
| 70 // Invoked when |handle_| is ready for writes. Calls SendInternal(). | |
| 71 void OnDoneWaiting(MojoResult result); | |
| 72 | |
| 73 // Dispatches a cancel callback if one is pending. | 68 // Dispatches a cancel callback if one is pending. |
| 74 void RunCancelCallback(); | 69 void RunCancelCallback(); |
| 75 | 70 |
| 76 // Shuts down this DataSender and dispatches fatal errors to all pending | 71 // Shuts down this DataSender and dispatches fatal errors to all pending |
| 77 // operations. | 72 // operations. |
| 78 void ShutDown(); | 73 void ShutDown(); |
| 79 | 74 |
| 80 // The control connection to the data sink. | 75 // The control connection to the data sink. |
| 81 mojo::InterfacePtr<serial::DataSink> sink_; | 76 mojo::InterfacePtr<serial::DataSink> sink_; |
| 82 | 77 |
| 83 // The data connection to the data sink. | |
| 84 mojo::ScopedDataPipeProducerHandle handle_; | |
| 85 | |
| 86 // The error value to report in the event of a fatal error. | 78 // The error value to report in the event of a fatal error. |
| 87 const int32_t fatal_error_value_; | 79 const int32_t fatal_error_value_; |
| 88 | 80 |
| 89 // A waiter used to wait until |handle_| is writable if we are waiting. | 81 // A queue of PendingSend that have not yet been fully sent to |sink_|. |
| 90 scoped_ptr<AsyncWaiter> waiter_; | |
| 91 | |
| 92 // A queue of PendingSend that have not yet been fully written to the data | |
| 93 // pipe. | |
| 94 std::queue<linked_ptr<PendingSend> > pending_sends_; | 82 std::queue<linked_ptr<PendingSend> > pending_sends_; |
| 95 | 83 |
| 96 // A queue of PendingSend that have been written to the data pipe, but have | 84 // A queue of PendingSend that have been sent to |sink_|, but have not yet |
| 97 // not yet been acked by the DataSink. | 85 // been acked by the DataSink. |
| 98 std::queue<linked_ptr<PendingSend> > sends_awaiting_ack_; | 86 std::queue<linked_ptr<PendingSend> > sends_awaiting_ack_; |
| 99 | 87 |
| 100 // The callback to report cancel completion if a cancel operation is in | 88 // The callback to report cancel completion if a cancel operation is in |
| 101 // progress. | 89 // progress. |
| 102 CancelCallback pending_cancel_; | 90 CancelCallback pending_cancel_; |
| 103 | 91 |
| 92 // The number of bytes available for buffering in the DataSink. |
| 93 uint32_t available_buffer_capacity_; |
| 94 |
| 104 // Whether we have encountered a fatal error and shut down. | 95 // Whether we have encountered a fatal error and shut down. |
| 105 bool shut_down_; | 96 bool shut_down_; |
| 106 | 97 |
| 107 DISALLOW_COPY_AND_ASSIGN(DataSender); | 98 DISALLOW_COPY_AND_ASSIGN(DataSender); |
| 108 }; | 99 }; |
| 109 | 100 |
| 110 } // namespace device | 101 } // namespace device |
| 111 | 102 |
| 112 #endif // DEVICE_SERIAL_DATA_SENDER_H_ | 103 #endif // DEVICE_SERIAL_DATA_SENDER_H_ |
| OLD | NEW |