| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef DEVICE_SERIAL_DATA_SENDER_H_ | |
| 6 #define DEVICE_SERIAL_DATA_SENDER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <queue> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/linked_ptr.h" | |
| 15 #include "base/strings/string_piece.h" | |
| 16 #include "device/serial/buffer.h" | |
| 17 #include "device/serial/data_stream.mojom.h" | |
| 18 #include "mojo/public/cpp/system/data_pipe.h" | |
| 19 | |
| 20 namespace device { | |
| 21 | |
| 22 // A DataSender sends data to a DataSink. | |
| 23 class DataSender { | |
| 24 public: | |
| 25 typedef base::Callback<void(uint32_t bytes_sent)> DataSentCallback; | |
| 26 typedef base::Callback<void(uint32_t bytes_sent, int32_t error)> | |
| 27 SendErrorCallback; | |
| 28 typedef base::Callback<void()> CancelCallback; | |
| 29 | |
| 30 // Constructs a DataSender to send data to |sink|, using a buffer size of | |
| 31 // |buffer_size|, with connection errors reported as |fatal_error_value|. | |
| 32 DataSender(mojo::InterfacePtr<serial::DataSink> sink, | |
| 33 uint32_t buffer_size, | |
| 34 int32_t fatal_error_value); | |
| 35 | |
| 36 ~DataSender(); | |
| 37 | |
| 38 // Starts an asynchronous send of |data|. If the send completes successfully, | |
| 39 // |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 | |
| 41 // 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 | |
| 43 // |error_callback| is called. | |
| 44 bool Send(const base::StringPiece& data, | |
| 45 const DataSentCallback& callback, | |
| 46 const SendErrorCallback& error_callback); | |
| 47 | |
| 48 // Requests the cancellation of any in-progress sends. Calls to Send() will | |
| 49 // fail until |callback| is called. | |
| 50 bool Cancel(int32_t error, const CancelCallback& callback); | |
| 51 | |
| 52 private: | |
| 53 class PendingSend; | |
| 54 | |
| 55 // Invoked when a PendingSend completes. | |
| 56 void SendComplete(); | |
| 57 | |
| 58 // Invoked when a PendingSend fails with |error|. | |
| 59 void SendFailed(int32_t error); | |
| 60 | |
| 61 // mojo error handler | |
| 62 void OnConnectionError(); | |
| 63 | |
| 64 // Dispatches a cancel callback if one is pending. | |
| 65 void RunCancelCallback(); | |
| 66 | |
| 67 // Shuts down this DataSender and dispatches fatal errors to all pending | |
| 68 // operations. | |
| 69 void ShutDown(); | |
| 70 | |
| 71 // The control connection to the data sink. | |
| 72 mojo::InterfacePtr<serial::DataSink> sink_; | |
| 73 | |
| 74 // The error value to report in the event of a fatal error. | |
| 75 const int32_t fatal_error_value_; | |
| 76 | |
| 77 // A queue of PendingSend that have been sent to |sink_|, but have not yet | |
| 78 // been acked by the DataSink. | |
| 79 std::queue<linked_ptr<PendingSend>> sends_awaiting_ack_; | |
| 80 | |
| 81 // The callback to report cancel completion if a cancel operation is in | |
| 82 // progress. | |
| 83 CancelCallback pending_cancel_; | |
| 84 | |
| 85 // Whether we have encountered a fatal error and shut down. | |
| 86 bool shut_down_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(DataSender); | |
| 89 }; | |
| 90 | |
| 91 } // namespace device | |
| 92 | |
| 93 #endif // DEVICE_SERIAL_DATA_SENDER_H_ | |
| OLD | NEW |