Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 MOJO_SERVICES_NETWORK_WEB_SOCKET_DATA_PIPE_QUEUE_H_ | |
| 6 #define MOJO_SERVICES_NETWORK_WEB_SOCKET_DATA_PIPE_QUEUE_H_ | |
| 7 | |
| 8 #include "base/callback.h" | |
| 9 #include "base/memory/scoped_vector.h" | |
| 10 #include "mojo/common/handle_watcher.h" | |
| 11 #include "mojo/public/cpp/system/data_pipe.h" | |
| 12 | |
| 13 namespace mojo { | |
| 14 | |
| 15 // This class simplifies the handling of multiple Reads on a DataPipe. It reads | |
| 16 // the data in the expected chunk size, calling the callback once a full chunk | |
| 17 // is ready. Callbacks are owned by this class, and are guaranteed not to be | |
| 18 // called after this class is destroyed. | |
| 19 class WebSocketReadQueue { | |
|
darin (slow to review)
2014/09/19 05:40:08
nit: I would probably create two separate files he
Matt Perry
2014/09/19 19:50:45
Done.
| |
| 20 public: | |
| 21 WebSocketReadQueue(DataPipeConsumerHandle handle); | |
| 22 ~WebSocketReadQueue(); | |
| 23 | |
| 24 void Read(uint32_t num_bytes, base::Callback<void(const char*)> callback); | |
| 25 | |
| 26 private: | |
| 27 struct Operation; | |
| 28 | |
| 29 void TryToRead(); | |
| 30 void Wait(); | |
| 31 void OnHandleReady(MojoResult result); | |
| 32 | |
| 33 DataPipeConsumerHandle handle_; | |
| 34 common::HandleWatcher handle_watcher_; | |
| 35 ScopedVector<Operation> queue_; | |
| 36 bool is_waiting_; | |
| 37 }; | |
| 38 | |
| 39 // Similar to the class above, but for writes. Each chunk of data is written at | |
| 40 // once, as soon as the DataPipe is ready. The callback is called once the write | |
| 41 // is finished. | |
| 42 class WebSocketWriteQueue { | |
| 43 public: | |
| 44 WebSocketWriteQueue(DataPipeProducerHandle handle); | |
| 45 ~WebSocketWriteQueue(); | |
| 46 | |
| 47 void Write(const char* data, | |
| 48 uint32_t num_bytes, | |
| 49 base::Callback<void(const char*)> callback); | |
| 50 | |
| 51 private: | |
| 52 struct Operation; | |
| 53 | |
| 54 MojoResult TryToWrite(); | |
| 55 void Wait(); | |
| 56 void OnHandleReady(MojoResult result); | |
| 57 | |
| 58 DataPipeProducerHandle handle_; | |
| 59 common::HandleWatcher handle_watcher_; | |
| 60 ScopedVector<Operation> queue_; | |
| 61 bool is_waiting_; | |
| 62 }; | |
| 63 | |
| 64 } // namespace mojo | |
| 65 | |
| 66 #endif // MOJO_SERVICES_NETWORK_WEB_SOCKET_DATA_PIPE_QUEUE_H_ | |
| OLD | NEW |