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_RECEIVER_H_ | 5 #ifndef DEVICE_SERIAL_DATA_RECEIVER_H_ |
6 #define DEVICE_SERIAL_DATA_RECEIVER_H_ | 6 #define DEVICE_SERIAL_DATA_RECEIVER_H_ |
7 | 7 |
8 #include <queue> | |
9 | |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/memory/linked_ptr.h" | |
9 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
10 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
11 #include "device/serial/buffer.h" | 14 #include "device/serial/buffer.h" |
12 #include "device/serial/data_stream.mojom.h" | 15 #include "device/serial/data_stream.mojom.h" |
13 #include "mojo/public/cpp/system/data_pipe.h" | 16 #include "mojo/public/cpp/system/data_pipe.h" |
14 | 17 |
15 namespace device { | 18 namespace device { |
16 | 19 |
17 class AsyncWaiter; | |
18 | |
19 // A DataReceiver receives data from a DataSource. | 20 // A DataReceiver receives data from a DataSource. |
20 class DataReceiver : public base::RefCounted<DataReceiver>, | 21 class DataReceiver : public base::RefCounted<DataReceiver>, |
21 public serial::DataSourceClient, | 22 public serial::DataSourceClient, |
22 public mojo::ErrorHandler { | 23 public mojo::ErrorHandler { |
23 public: | 24 public: |
24 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback; | 25 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback; |
25 typedef base::Callback<void(int32_t error)> ReceiveErrorCallback; | 26 typedef base::Callback<void(int32_t error)> ReceiveErrorCallback; |
26 | 27 |
27 // Constructs a DataReceiver to receive data from |source|, using a data | 28 // Constructs a DataReceiver to receive data from |source|, using a buffer |
28 // pipe with a buffer size of |buffer_size|, with connection errors reported | 29 // size of |buffer_size|, with connection errors reported as |
29 // as |fatal_error_value|. | 30 // |fatal_error_value|. |
30 DataReceiver(mojo::InterfacePtr<serial::DataSource> source, | 31 DataReceiver(mojo::InterfacePtr<serial::DataSource> source, |
31 uint32_t buffer_size, | 32 uint32_t buffer_size, |
32 int32_t fatal_error_value); | 33 int32_t fatal_error_value); |
33 | 34 |
34 // Begins a receive operation. If this returns true, exactly one of |callback| | 35 // Begins a receive operation. If this returns true, exactly one of |callback| |
35 // and |error_callback| will eventually be called. If there is already a | 36 // and |error_callback| will eventually be called. If there is already a |
36 // receive in progress or there has been a connection error, this will have no | 37 // receive in progress or there has been a connection error, this will have no |
37 // effect and return false. | 38 // effect and return false. |
38 bool Receive(const ReceiveDataCallback& callback, | 39 bool Receive(const ReceiveDataCallback& callback, |
39 const ReceiveErrorCallback& error_callback); | 40 const ReceiveErrorCallback& error_callback); |
40 | 41 |
41 private: | 42 private: |
42 class PendingReceive; | 43 class PendingReceive; |
43 struct PendingError; | 44 struct DataFrame; |
44 friend class base::RefCounted<DataReceiver>; | 45 friend class base::RefCounted<DataReceiver>; |
45 | 46 |
46 virtual ~DataReceiver(); | 47 virtual ~DataReceiver(); |
47 | 48 |
48 // serial::DataSourceClient override. Invoked by the DataSource to report | 49 // serial::DataSourceClient overrides. |
49 // errors. | 50 // Invoked by the DataSource to report errors. |
50 virtual void OnError(uint32_t bytes_since_last_error, int32_t error) override; | 51 virtual void OnError(int32_t error) override; |
52 // Invoked by the DataSource transmit data. | |
53 virtual void OnData(mojo::Array<uint8_t> data) override; | |
51 | 54 |
52 // mojo::ErrorHandler override. Calls ShutDown(). | 55 // mojo::ErrorHandler override. Calls ShutDown(). |
53 virtual void OnConnectionError() override; | 56 virtual void OnConnectionError() override; |
54 | 57 |
55 // Invoked by the PendingReceive to report that the user is done with the | 58 // Invoked by the PendingReceive to report that the user is done with the |
56 // receive buffer, having read |bytes_read| bytes from it. | 59 // receive buffer, having read |bytes_read| bytes from it. |
57 void Done(uint32_t bytes_read); | 60 void Done(uint32_t bytes_read); |
58 | 61 |
59 // Invoked when |handle_| is ready to read. Unless an error has occurred, this | |
60 // calls ReceiveInternal(). | |
61 void OnDoneWaiting(MojoResult result); | |
62 | |
63 // The implementation of Receive(). If a |pending_error_| is ready to | 62 // The implementation of Receive(). If a |pending_error_| is ready to |
64 // dispatch, it does so. Otherwise, this attempts to read from |handle_| and | 63 // dispatch, it does so. Otherwise, this attempts to read from |handle_| and |
65 // dispatches the contents to |pending_receive_|. If |handle_| is not ready, | 64 // dispatches the contents to |pending_receive_|. If |handle_| is not ready, |
66 // |waiter_| is used to wait until it is ready. If an error occurs in reading | 65 // |waiter_| is used to wait until it is ready. If an error occurs in reading |
67 // |handle_|, ShutDown() is called. | 66 // |handle_|, ShutDown() is called. |
68 void ReceiveInternal(); | 67 void ReceiveInternal(); |
69 | 68 |
70 // We may have been notified of an error that occurred at some future point in | |
71 // the stream. We should never be able to read past the point at which the | |
72 // error occurred until we have dealt with the error and called Resume() on | |
73 // the DataSource. If this has occurred, something bad has happened on the | |
74 // service side, so we shut down. | |
75 bool CheckErrorNotInReadRange(uint32_t num_bytes); | |
76 | |
77 // Called when we encounter a fatal error. If a receive is in progress, | 69 // Called when we encounter a fatal error. If a receive is in progress, |
78 // |fatal_error_value_| will be reported to the user. | 70 // |fatal_error_value_| will be reported to the user. |
79 void ShutDown(); | 71 void ShutDown(); |
80 | 72 |
81 // The control connection to the data source. | 73 // The control connection to the data source. |
82 mojo::InterfacePtr<serial::DataSource> source_; | 74 mojo::InterfacePtr<serial::DataSource> source_; |
83 | 75 |
84 // The data connection to the data source. | |
85 mojo::ScopedDataPipeConsumerHandle handle_; | |
86 | |
87 // The error value to report in the event of a fatal error. | 76 // The error value to report in the event of a fatal error. |
88 const int32_t fatal_error_value_; | 77 const int32_t fatal_error_value_; |
89 | 78 |
90 // The number of bytes received from the data source. | |
91 uint32_t bytes_received_; | |
92 | |
93 // Whether we have encountered a fatal error and shut down. | 79 // Whether we have encountered a fatal error and shut down. |
94 bool shut_down_; | 80 bool shut_down_; |
95 | 81 |
96 // A waiter used to wait until |handle_| is readable if we are waiting. | |
97 scoped_ptr<AsyncWaiter> waiter_; | |
98 | |
99 // The current pending receive operation if there is one. | 82 // The current pending receive operation if there is one. |
100 scoped_ptr<PendingReceive> pending_receive_; | 83 scoped_ptr<PendingReceive> pending_receive_; |
101 | 84 |
102 // The current pending error if there is one. | 85 // The queue of pending data buffers received from the DataSource. |
103 scoped_ptr<PendingError> pending_error_; | 86 std::queue<linked_ptr<DataFrame>> pending_data_buffers_; |
raymes
2014/10/27 03:02:22
Should we call it pending_data_frames_? And update
Sam McNally
2014/10/27 05:39:14
Done.
| |
104 | 87 |
105 base::WeakPtrFactory<DataReceiver> weak_factory_; | 88 base::WeakPtrFactory<DataReceiver> weak_factory_; |
106 | 89 |
107 DISALLOW_COPY_AND_ASSIGN(DataReceiver); | 90 DISALLOW_COPY_AND_ASSIGN(DataReceiver); |
108 }; | 91 }; |
109 | 92 |
110 } // namespace device | 93 } // namespace device |
111 | 94 |
112 #endif // DEVICE_SERIAL_DATA_RECEIVER_H_ | 95 #endif // DEVICE_SERIAL_DATA_RECEIVER_H_ |
OLD | NEW |