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

Side by Side Diff: device/serial/data_pipe_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: 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 unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef DEVICE_SERIAL_DATA_PIPE_RECEIVER_H_
6 #define DEVICE_SERIAL_DATA_PIPE_RECEIVER_H_
7
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "device/serial/buffer.h"
12 #include "device/serial/data_pipe.mojom.h"
13 #include "mojo/public/cpp/system/data_pipe.h"
14
15 namespace device {
16
17 class AsyncWaiter;
18
19 // A wrapper around the consumer end of a data pipe.
20 class DataPipeReceiver : public base::RefCounted<DataPipeReceiver>,
21 public serial::DataPipeReceiver,
22 public mojo::ErrorHandler {
23 public:
24 typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback;
25 typedef base::Callback<void(int32_t error)> ReceiveErrorCallback;
26
27 // Constructs a DataPipeReceiver to receive data from |producer|, using a data
28 // pipe with a buffer size of |buffer_size|, with connections errors reported
raymes 2014/08/05 06:26:45 connections->connection
Sam McNally 2014/08/05 07:26:33 Done.
29 // as |connection_error_value|.
30 DataPipeReceiver(mojo::InterfacePtr<serial::DataPipeProducer> producer,
31 uint32_t buffer_size,
32 int32_t connection_error_value);
33
34 // 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 // receive in progress, |callback| or |error_callback| is invalid or there has
37 // been a connection error, this will have no effect and return false.
38 bool Receive(const ReceiveDataCallback& callback,
39 const ReceiveErrorCallback& error_callback);
40
41 private:
42 class Buffer;
43 friend class base::RefCounted<DataPipeReceiver>;
44
45 enum State {
46 STATE_IDLE,
47 STATE_WAITING_FOR_DATA,
48 STATE_WAITING_FOR_BUFFER,
49 STATE_PAUSED,
50 STATE_SHUT_DOWN,
51 };
52
53 virtual ~DataPipeReceiver();
54 void Done(uint32_t bytes_consumed);
55 void OnDoneWaiting(MojoResult result);
56
57 void ReceiveInternal();
58 void RetryReceive();
59 void DispatchData(const void* data, uint32_t num_bytes);
60 void DispatchError(int32_t error);
61
62 // serial::DataPipeReceiver override.
63 virtual void OnError(uint32_t bytes_since_last_error, int32_t error) OVERRIDE;
64
65 // mojo::ErrorHandler override.
66 virtual void OnConnectionError() OVERRIDE;
67
68 mojo::InterfacePtr<serial::DataPipeProducer> producer_;
69 const int32_t connection_error_value_;
70 mojo::ScopedDataPipeConsumerHandle handle_;
71 ReceiveDataCallback receive_callback_;
72 ReceiveErrorCallback receive_error_callback_;
73 uint32_t bytes_since_last_error_;
74 bool pending_error_;
75 uint32_t error_offset_;
76 int32_t error_;
77 State state_;
78 scoped_ptr<AsyncWaiter> waiter_;
79 base::WeakPtrFactory<DataPipeReceiver> weak_factory_;
80
81 DISALLOW_COPY_AND_ASSIGN(DataPipeReceiver);
82 };
83
84 } // namespace device
85
86 #endif // DEVICE_SERIAL_DATA_PIPE_RECEIVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698