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 "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h" | 15 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h" |
16 | 16 |
17 namespace device { | 17 namespace device { |
18 | 18 |
19 // A DataSender sends data to a DataSink. | 19 // A DataSender sends data to a DataSink. |
20 class DataSender : public serial::DataSinkClient, public mojo::ErrorHandler { | 20 class DataSender : public serial::DataSinkClient, public mojo::ErrorHandler { |
21 public: | 21 public: |
22 typedef base::Callback<void(uint32_t bytes_sent)> DataSentCallback; | 22 typedef base::Callback<void(uint32_t bytes_sent)> DataSentCallback; |
23 typedef base::Callback<void(uint32_t bytes_sent, int32_t error)> | 23 typedef base::Callback<void(uint32_t bytes_sent, int32_t error)> |
24 SendErrorCallback; | 24 SendErrorCallback; |
25 typedef base::Callback<void()> CancelCallback; | 25 typedef base::Callback<void()> CancelCallback; |
26 | 26 |
27 // Constructs a DataSender to send data to |sink|, using a buffer size of | 27 // Constructs a DataSender to send data to |sink|, using a buffer size of |
28 // |buffer_size|, with connection errors reported as |fatal_error_value|. | 28 // |buffer_size|, with connection errors reported as |fatal_error_value|. |
29 DataSender(mojo::InterfacePtr<serial::DataSink> sink, | 29 DataSender(mojo::InterfacePtr<serial::DataSink> sink, |
30 mojo::InterfaceRequest<serial::DataSinkClient> client, | |
31 uint32_t buffer_size, | 30 uint32_t buffer_size, |
32 int32_t fatal_error_value); | 31 int32_t fatal_error_value); |
33 | 32 |
34 ~DataSender() override; | 33 ~DataSender() override; |
35 | 34 |
36 // Starts an asynchronous send of |data|. If the send completes successfully, | 35 // Starts an asynchronous send of |data|. If the send completes successfully, |
37 // |callback| will be called. Otherwise, |error_callback| will be called with | 36 // |callback| will be called. Otherwise, |error_callback| will be called with |
38 // 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 |
39 // 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 |
40 // responsibility to ensure |data| remains valid until one of |callback| and | 39 // responsibility to ensure |data| remains valid until one of |callback| and |
(...skipping 26 matching lines...) Expand all Loading... |
67 | 66 |
68 // Dispatches a cancel callback if one is pending. | 67 // Dispatches a cancel callback if one is pending. |
69 void RunCancelCallback(); | 68 void RunCancelCallback(); |
70 | 69 |
71 // Shuts down this DataSender and dispatches fatal errors to all pending | 70 // Shuts down this DataSender and dispatches fatal errors to all pending |
72 // operations. | 71 // operations. |
73 void ShutDown(); | 72 void ShutDown(); |
74 | 73 |
75 // The control connection to the data sink. | 74 // The control connection to the data sink. |
76 mojo::InterfacePtr<serial::DataSink> sink_; | 75 mojo::InterfacePtr<serial::DataSink> sink_; |
77 mojo::Binding<serial::DataSinkClient> client_; | |
78 | 76 |
79 // The error value to report in the event of a fatal error. | 77 // The error value to report in the event of a fatal error. |
80 const int32_t fatal_error_value_; | 78 const int32_t fatal_error_value_; |
81 | 79 |
82 // A queue of PendingSend that have not yet been fully sent to |sink_|. | 80 // A queue of PendingSend that have not yet been fully sent to |sink_|. |
83 std::queue<linked_ptr<PendingSend> > pending_sends_; | 81 std::queue<linked_ptr<PendingSend> > pending_sends_; |
84 | 82 |
85 // A queue of PendingSend that have been sent to |sink_|, but have not yet | 83 // A queue of PendingSend that have been sent to |sink_|, but have not yet |
86 // been acked by the DataSink. | 84 // been acked by the DataSink. |
87 std::queue<linked_ptr<PendingSend> > sends_awaiting_ack_; | 85 std::queue<linked_ptr<PendingSend> > sends_awaiting_ack_; |
88 | 86 |
89 // The callback to report cancel completion if a cancel operation is in | 87 // The callback to report cancel completion if a cancel operation is in |
90 // progress. | 88 // progress. |
91 CancelCallback pending_cancel_; | 89 CancelCallback pending_cancel_; |
92 | 90 |
93 // The number of bytes available for buffering in the DataSink. | 91 // The number of bytes available for buffering in the DataSink. |
94 uint32_t available_buffer_capacity_; | 92 uint32_t available_buffer_capacity_; |
95 | 93 |
96 // Whether we have encountered a fatal error and shut down. | 94 // Whether we have encountered a fatal error and shut down. |
97 bool shut_down_; | 95 bool shut_down_; |
98 | 96 |
99 DISALLOW_COPY_AND_ASSIGN(DataSender); | 97 DISALLOW_COPY_AND_ASSIGN(DataSender); |
100 }; | 98 }; |
101 | 99 |
102 } // namespace device | 100 } // namespace device |
103 | 101 |
104 #endif // DEVICE_SERIAL_DATA_SENDER_H_ | 102 #endif // DEVICE_SERIAL_DATA_SENDER_H_ |
OLD | NEW |