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 |
33 DataSender(mojo::InterfacePtr<serial::DataSink> sink, | |
34 uint32_t buffer_size, | |
35 int32_t fatal_error_value, | |
36 bool use_data_pipe); | |
raymes
2014/10/17 01:55:42
Looks like this was added before changing to suppo
Sam McNally
2014/10/20 05:12:58
Done.
| |
37 | |
36 virtual ~DataSender(); | 38 virtual ~DataSender(); |
37 | 39 |
38 // Starts an asynchronous send of |data|. If the send completes successfully, | 40 // Starts an asynchronous send of |data|. If the send completes successfully, |
39 // |callback| will be called. Otherwise, |error_callback| will be called with | 41 // |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 | 42 // 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 | 43 // 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 | 44 // responsibility to ensure |data| remains valid until one of |callback| and |
43 // |error_callback| is called. | 45 // |error_callback| is called. |
44 bool Send(const base::StringPiece& data, | 46 bool Send(const base::StringPiece& data, |
45 const DataSentCallback& callback, | 47 const DataSentCallback& callback, |
46 const SendErrorCallback& error_callback); | 48 const SendErrorCallback& error_callback); |
47 | 49 |
48 // Requests the cancellation of any in-progress sends. Calls to Send() will | 50 // Requests the cancellation of any in-progress sends. Calls to Send() will |
49 // fail until |callback| is called. | 51 // fail until |callback| is called. |
50 bool Cancel(int32_t error, const CancelCallback& callback); | 52 bool Cancel(int32_t error, const CancelCallback& callback); |
51 | 53 |
52 private: | 54 private: |
53 class PendingSend; | 55 class PendingSend; |
54 | 56 |
55 // serial::DataSinkClient overrides. | 57 // serial::DataSinkClient overrides. |
56 virtual void ReportBytesSent(uint32_t bytes_sent) override; | 58 virtual void ReportBytesSent(uint32_t bytes_sent) override; |
57 virtual void ReportBytesSentAndError( | 59 virtual void ReportBytesSentAndError( |
58 uint32_t bytes_sent, | 60 uint32_t bytes_sent, |
59 int32_t error, | 61 int32_t error, |
60 const mojo::Callback<void(uint32_t)>& callback) override; | 62 const mojo::Callback<void()>& callback) override; |
61 | 63 |
62 // mojo::ErrorHandler override. | 64 // mojo::ErrorHandler override. |
63 virtual void OnConnectionError() override; | 65 virtual void OnConnectionError() override; |
64 | 66 |
65 // Copies data from |pending_sends_| into the data pipe and starts |waiter_| | 67 // Sends up to |available_buffer_capacity_| bytes of data from |
66 // waiting if the pipe is full. When a PendingSend in |pending_sends_| has | 68 // |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_|. | 69 // been fully copied into the data pipe, it moves to |sends_awaiting_ack_|. |
68 void SendInternal(); | 70 void SendInternal(); |
69 | 71 |
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. | 72 // Dispatches a cancel callback if one is pending. |
74 void RunCancelCallback(); | 73 void RunCancelCallback(); |
75 | 74 |
76 // Shuts down this DataSender and dispatches fatal errors to all pending | 75 // Shuts down this DataSender and dispatches fatal errors to all pending |
77 // operations. | 76 // operations. |
78 void ShutDown(); | 77 void ShutDown(); |
79 | 78 |
80 // The control connection to the data sink. | 79 // The control connection to the data sink. |
81 mojo::InterfacePtr<serial::DataSink> sink_; | 80 mojo::InterfacePtr<serial::DataSink> sink_; |
82 | 81 |
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. | 82 // The error value to report in the event of a fatal error. |
87 const int32_t fatal_error_value_; | 83 const int32_t fatal_error_value_; |
88 | 84 |
89 // A waiter used to wait until |handle_| is writable if we are waiting. | 85 // 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_; | 86 std::queue<linked_ptr<PendingSend> > pending_sends_; |
95 | 87 |
96 // A queue of PendingSend that have been written to the data pipe, but have | 88 // A queue of PendingSend that have been sent to |sink_|, but have not yet |
97 // not yet been acked by the DataSink. | 89 // been acked by the DataSink. |
98 std::queue<linked_ptr<PendingSend> > sends_awaiting_ack_; | 90 std::queue<linked_ptr<PendingSend> > sends_awaiting_ack_; |
99 | 91 |
100 // The callback to report cancel completion if a cancel operation is in | 92 // The callback to report cancel completion if a cancel operation is in |
101 // progress. | 93 // progress. |
102 CancelCallback pending_cancel_; | 94 CancelCallback pending_cancel_; |
103 | 95 |
96 // The number of bytes that have been sent to the sink, but have not been | |
97 // acknowledged. | |
raymes
2014/10/17 01:55:42
It looks like the description might be slightly wr
Sam McNally
2014/10/20 05:12:58
Done. It originally counted up.
| |
98 uint32_t available_buffer_capacity_; | |
99 | |
104 // Whether we have encountered a fatal error and shut down. | 100 // Whether we have encountered a fatal error and shut down. |
105 bool shut_down_; | 101 bool shut_down_; |
106 | 102 |
107 DISALLOW_COPY_AND_ASSIGN(DataSender); | 103 DISALLOW_COPY_AND_ASSIGN(DataSender); |
108 }; | 104 }; |
109 | 105 |
110 } // namespace device | 106 } // namespace device |
111 | 107 |
112 #endif // DEVICE_SERIAL_DATA_SENDER_H_ | 108 #endif // DEVICE_SERIAL_DATA_SENDER_H_ |
OLD | NEW |