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.
5 #ifndef DEVICE_SERIAL_DATA_SENDER_H_
6 #define DEVICE_SERIAL_DATA_SENDER_H_
10 #include "base/callback.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/strings/string_piece.h"
13 #include "device/serial/buffer.h"
14 #include "device/serial/data_stream.mojom.h"
15 #include "mojo/public/cpp/system/data_pipe.h"
19 // A DataSender sends data to a DataSink.
20 class DataSender
: public serial::DataSinkClient
, public mojo::ErrorHandler
{
22 typedef base::Callback
<void(uint32_t bytes_sent
)> DataSentCallback
;
23 typedef base::Callback
<void(uint32_t bytes_sent
, int32_t error
)>
25 typedef base::Callback
<void()> CancelCallback
;
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|.
29 DataSender(mojo::InterfacePtr
<serial::DataSink
> sink
,
31 int32_t fatal_error_value
);
33 ~DataSender() override
;
35 // Starts an asynchronous send of |data|. If the send completes successfully,
36 // |callback| will be called. Otherwise, |error_callback| will be called with
37 // the error. If there is a cancel in progress or there has been a connection
38 // error, this will not start a send and returns false. It is the caller's
39 // responsibility to ensure |data| remains valid until one of |callback| and
40 // |error_callback| is called.
41 bool Send(const base::StringPiece
& data
,
42 const DataSentCallback
& callback
,
43 const SendErrorCallback
& error_callback
);
45 // Requests the cancellation of any in-progress sends. Calls to Send() will
46 // fail until |callback| is called.
47 bool Cancel(int32_t error
, const CancelCallback
& callback
);
52 // serial::DataSinkClient overrides.
53 void ReportBytesSent(uint32_t bytes_sent
) override
;
54 void ReportBytesSentAndError(uint32_t bytes_sent
,
56 const mojo::Callback
<void()>& callback
) override
;
58 // mojo::ErrorHandler override.
59 void OnConnectionError() override
;
61 // Sends up to |available_buffer_capacity_| bytes of data from
62 // |pending_sends_| to |sink_|. When a PendingSend in |pending_sends_| has
63 // been fully copied transmitted to |sink_|, it moves to
64 // |sends_awaiting_ack_|.
67 // Dispatches a cancel callback if one is pending.
68 void RunCancelCallback();
70 // Shuts down this DataSender and dispatches fatal errors to all pending
74 // The control connection to the data sink.
75 mojo::InterfacePtr
<serial::DataSink
> sink_
;
77 // The error value to report in the event of a fatal error.
78 const int32_t fatal_error_value_
;
80 // A queue of PendingSend that have not yet been fully sent to |sink_|.
81 std::queue
<linked_ptr
<PendingSend
> > pending_sends_
;
83 // A queue of PendingSend that have been sent to |sink_|, but have not yet
84 // been acked by the DataSink.
85 std::queue
<linked_ptr
<PendingSend
> > sends_awaiting_ack_
;
87 // The callback to report cancel completion if a cancel operation is in
89 CancelCallback pending_cancel_
;
91 // The number of bytes available for buffering in the DataSink.
92 uint32_t available_buffer_capacity_
;
94 // Whether we have encountered a fatal error and shut down.
97 DISALLOW_COPY_AND_ASSIGN(DataSender
);
100 } // namespace device
102 #endif // DEVICE_SERIAL_DATA_SENDER_H_