Chromium Code Reviews| Index: mojo/services/network/web_socket_data_pipe_queue.h |
| diff --git a/mojo/services/network/web_socket_data_pipe_queue.h b/mojo/services/network/web_socket_data_pipe_queue.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6bd389b8810373aeaae14d81316c3ed4b5280987 |
| --- /dev/null |
| +++ b/mojo/services/network/web_socket_data_pipe_queue.h |
| @@ -0,0 +1,66 @@ |
| +// 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 MOJO_SERVICES_NETWORK_WEB_SOCKET_DATA_PIPE_QUEUE_H_ |
| +#define MOJO_SERVICES_NETWORK_WEB_SOCKET_DATA_PIPE_QUEUE_H_ |
| + |
| +#include "base/callback.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "mojo/common/handle_watcher.h" |
| +#include "mojo/public/cpp/system/data_pipe.h" |
| + |
| +namespace mojo { |
| + |
| +// This class simplifies the handling of multiple Reads on a DataPipe. It reads |
| +// the data in the expected chunk size, calling the callback once a full chunk |
| +// is ready. Callbacks are owned by this class, and are guaranteed not to be |
| +// called after this class is destroyed. |
| +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.
|
| + public: |
| + WebSocketReadQueue(DataPipeConsumerHandle handle); |
| + ~WebSocketReadQueue(); |
| + |
| + void Read(uint32_t num_bytes, base::Callback<void(const char*)> callback); |
| + |
| + private: |
| + struct Operation; |
| + |
| + void TryToRead(); |
| + void Wait(); |
| + void OnHandleReady(MojoResult result); |
| + |
| + DataPipeConsumerHandle handle_; |
| + common::HandleWatcher handle_watcher_; |
| + ScopedVector<Operation> queue_; |
| + bool is_waiting_; |
| +}; |
| + |
| +// Similar to the class above, but for writes. Each chunk of data is written at |
| +// once, as soon as the DataPipe is ready. The callback is called once the write |
| +// is finished. |
| +class WebSocketWriteQueue { |
| + public: |
| + WebSocketWriteQueue(DataPipeProducerHandle handle); |
| + ~WebSocketWriteQueue(); |
| + |
| + void Write(const char* data, |
| + uint32_t num_bytes, |
| + base::Callback<void(const char*)> callback); |
| + |
| + private: |
| + struct Operation; |
| + |
| + MojoResult TryToWrite(); |
| + void Wait(); |
| + void OnHandleReady(MojoResult result); |
| + |
| + DataPipeProducerHandle handle_; |
| + common::HandleWatcher handle_watcher_; |
| + ScopedVector<Operation> queue_; |
| + bool is_waiting_; |
| +}; |
| + |
| +} // namespace mojo |
| + |
| +#endif // MOJO_SERVICES_NETWORK_WEB_SOCKET_DATA_PIPE_QUEUE_H_ |