OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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_PUBLIC_BINDINGS_LIB_CONNECTOR_H_ |
| 6 #define MOJO_PUBLIC_BINDINGS_LIB_CONNECTOR_H_ |
| 7 |
| 8 #include <queue> |
| 9 |
| 10 #include "mojo/public/bindings/lib/bindings_support.h" |
| 11 #include "mojo/public/bindings/lib/message.h" |
| 12 #include "mojo/public/system/core.h" |
| 13 |
| 14 namespace mojo { |
| 15 |
| 16 // The Connector class is responsible for performing read/write operations on a |
| 17 // MessagePipe. It writes messages it receives through the MessageReceiver |
| 18 // interface that it subclasses, and it forwards messages it reads through the |
| 19 // MessageReceiver interface assigned as its incoming receiver. |
| 20 // |
| 21 // NOTE: MessagePipe I/O is non-blocking. |
| 22 // |
| 23 class Connector : public MessageReceiver { |
| 24 public: |
| 25 // The Connector does not take ownership of |message_pipe|. |
| 26 // TODO(darin): Perhaps it should take ownership. |
| 27 explicit Connector(Handle message_pipe); |
| 28 virtual ~Connector(); |
| 29 |
| 30 // Sets the receiver to handle messages read from the message pipe. The |
| 31 // Connector will only read messages from the pipe if an incoming receiver |
| 32 // has been set. |
| 33 void SetIncomingReceiver(MessageReceiver* receiver); |
| 34 |
| 35 // Returns true if an error was encountered while reading from or writing to |
| 36 // the message pipe. |
| 37 bool EncounteredError() const { return error_; } |
| 38 |
| 39 // MessageReceiver implementation: |
| 40 virtual bool Accept(Message* message) MOJO_OVERRIDE; |
| 41 |
| 42 private: |
| 43 class Callback : public BindingsSupport::AsyncWaitCallback { |
| 44 public: |
| 45 Callback(); |
| 46 |
| 47 void Cancel(); |
| 48 void SetOwnerToNotify(Connector* owner); |
| 49 bool IsPending() const; |
| 50 |
| 51 virtual void OnHandleReady(MojoResult result) MOJO_OVERRIDE; |
| 52 |
| 53 private: |
| 54 Connector* owner_; |
| 55 }; |
| 56 friend class Callback; |
| 57 |
| 58 void OnHandleReady(Callback* callback, MojoResult result); |
| 59 void WaitToReadMore(); |
| 60 void WaitToWriteMore(); |
| 61 void ReadMore(); |
| 62 void WriteMore(); |
| 63 |
| 64 Handle message_pipe_; |
| 65 MessageReceiver* incoming_receiver_; |
| 66 |
| 67 Callback read_callback_; |
| 68 Callback write_callback_; |
| 69 |
| 70 bool error_; |
| 71 |
| 72 std::queue<Message> write_queue_; |
| 73 }; |
| 74 |
| 75 } // namespace mojo |
| 76 |
| 77 #endif // MOJO_PUBLIC_BINDINGS_LIB_CONNECTOR_H_ |
OLD | NEW |