blob: 6e9c1b9deb97c563d149ba38370218a6b3dc5b18 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
// Copyright 2013 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_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
#define MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
#include "mojo/public/cpp/bindings/lib/message_queue.h"
#include "mojo/public/cpp/bindings/message.h"
#include "mojo/public/cpp/environment/default_async_waiter.h"
#include "mojo/public/cpp/system/core.h"
namespace mojo {
class ErrorHandler;
namespace internal {
// The Connector class is responsible for performing read/write operations on a
// MessagePipe. It writes messages it receives through the MessageReceiver
// interface that it subclasses, and it forwards messages it reads through the
// MessageReceiver interface assigned as its incoming receiver.
//
// NOTE: MessagePipe I/O is non-blocking.
//
class Connector : public MessageReceiver {
public:
// The Connector takes ownership of |message_pipe|.
explicit Connector(ScopedMessagePipeHandle message_pipe,
MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter());
virtual ~Connector();
// Sets the receiver to handle messages read from the message pipe. The
// Connector will read messages from the pipe regardless of whether or not an
// incoming receiver has been set.
void set_incoming_receiver(MessageReceiver* receiver) {
incoming_receiver_ = receiver;
}
// Errors from incoming receivers will force the connector into an error
// state, where no more messages will be processed. This method is used
// during testing to prevent that from happening.
void set_enforce_errors_from_incoming_receiver(bool enforce) {
enforce_errors_from_incoming_receiver_ = enforce;
}
// Sets the error handler to receive notifications when an error is
// encountered while reading from the pipe or waiting to read from the pipe.
void set_error_handler(ErrorHandler* error_handler) {
error_handler_ = error_handler;
}
// Returns true if an error was encountered while reading from the pipe or
// waiting to read from the pipe.
bool encountered_error() const { return error_; }
// Closes the pipe, triggering the error state. Connector is put into a
// quiescent state.
void CloseMessagePipe();
// Releases the pipe, not triggering the error state. Connector is put into
// a quiescent state.
ScopedMessagePipeHandle ReleaseMessagePipe();
// MessageReceiver implementation:
virtual bool Accept(Message* message) MOJO_OVERRIDE;
virtual bool AcceptWithResponder(Message* message, MessageReceiver* responder)
MOJO_OVERRIDE;
private:
static void CallOnHandleReady(void* closure, MojoResult result);
void OnHandleReady(MojoResult result);
void WaitToReadMore();
void ReadMore();
ErrorHandler* error_handler_;
MojoAsyncWaiter* waiter_;
ScopedMessagePipeHandle message_pipe_;
MessageReceiver* incoming_receiver_;
MojoAsyncWaitID async_wait_id_;
bool error_;
bool drop_writes_;
bool enforce_errors_from_incoming_receiver_;
MOJO_DISALLOW_COPY_AND_ASSIGN(Connector);
};
} // namespace internal
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_CONNECTOR_H_
|