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