Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(869)

Unified Diff: device/serial/data_receiver.h

Issue 437933002: Add data pipe wrappers to be used to implement serial receive. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@serial-buffer
Patch Set: address comments Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/serial/data_receiver.h
diff --git a/device/serial/data_receiver.h b/device/serial/data_receiver.h
new file mode 100644
index 0000000000000000000000000000000000000000..c0bf7e1e13a79c209be1fe285a748a21594ba8a0
--- /dev/null
+++ b/device/serial/data_receiver.h
@@ -0,0 +1,78 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef DEVICE_SERIAL_DATA_RECEIVER_H_
+#define DEVICE_SERIAL_DATA_RECEIVER_H_
+
+#include "base/callback.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/weak_ptr.h"
+#include "device/serial/buffer.h"
+#include "device/serial/data_stream.mojom.h"
+#include "mojo/public/cpp/system/data_pipe.h"
+
+namespace device {
+
+class AsyncWaiter;
+
+// A DataReceiver receives data from a DataSource.
+class DataReceiver : public base::RefCounted<DataReceiver>,
+ public serial::DataSourceClient,
+ public mojo::ErrorHandler {
+ public:
+ typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback;
+ typedef base::Callback<void(int32_t error)> ReceiveErrorCallback;
+
+ // Constructs a DataReceiver to receive data from |source|, using a data
+ // pipe with a buffer size of |buffer_size|, with connection errors reported
+ // as |fatal_error_value|.
+ DataReceiver(mojo::InterfacePtr<serial::DataSource> source,
+ uint32_t buffer_size,
+ int32_t fatal_error_value);
+
+ // Begins a receive operation. If this returns true, exactly one of |callback|
+ // and |error_callback| will eventually be called. If there is already a
+ // receive in progress, |callback| or |error_callback| is invalid or there has
+ // been a connection error, this will have no effect and return false.
+ bool Receive(const ReceiveDataCallback& callback,
+ const ReceiveErrorCallback& error_callback);
+
+ private:
+ class PendingReceive;
+ struct PendingError;
+ friend class base::RefCounted<DataReceiver>;
+
+ virtual ~DataReceiver();
+
+ // serial::DataSourceClient override.
+ virtual void OnError(uint32_t bytes_since_last_error, int32_t error) OVERRIDE;
+
+ // mojo::ErrorHandler override.
+ virtual void OnConnectionError() OVERRIDE;
+
+ void Done(uint32_t bytes_consumed);
raymes 2014/08/06 05:46:43 Please add comments for each of these methods and
Sam McNally 2014/08/06 08:28:14 Done.
+ void OnDoneWaiting(MojoResult result);
+
+ void ReceiveInternal();
+
+ bool CheckBytesReceived(uint32_t num_bytes);
+
+ void ShutDown();
+
+ mojo::InterfacePtr<serial::DataSource> source_;
+ const int32_t fatal_error_value_;
+ mojo::ScopedDataPipeConsumerHandle handle_;
+ uint32_t bytes_received_;
+ bool shut_down_;
+ scoped_ptr<AsyncWaiter> waiter_;
+ scoped_ptr<PendingReceive> receive_;
+ scoped_ptr<PendingError> error_;
+ base::WeakPtrFactory<DataReceiver> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(DataReceiver);
+};
+
+} // namespace device
+
+#endif // DEVICE_SERIAL_DATA_RECEIVER_H_

Powered by Google App Engine
This is Rietveld 408576698