This source file includes following definitions.
- drop_writes_
- CloseMessagePipe
- Accept
- AcceptWithResponder
- CallOnHandleReady
- OnHandleReady
- WaitToReadMore
- ReadMore
#include "mojo/public/cpp/bindings/lib/connector.h"
#include <assert.h>
#include <stdlib.h>
#include "mojo/public/cpp/bindings/error_handler.h"
namespace mojo {
namespace internal {
Connector::Connector(ScopedMessagePipeHandle message_pipe,
MojoAsyncWaiter* waiter)
: error_handler_(NULL),
waiter_(waiter),
message_pipe_(message_pipe.Pass()),
incoming_receiver_(NULL),
async_wait_id_(0),
error_(false),
drop_writes_(false) {
WaitToReadMore();
}
Connector::~Connector() {
if (async_wait_id_)
waiter_->CancelWait(waiter_, async_wait_id_);
}
void Connector::CloseMessagePipe() {
Close(message_pipe_.Pass());
}
bool Connector::Accept(Message* message) {
if (error_)
return false;
if (drop_writes_)
return true;
MojoResult rv = WriteMessageRaw(
message_pipe_.get(),
message->data(),
message->data_num_bytes(),
message->mutable_handles()->empty() ? NULL :
reinterpret_cast<const MojoHandle*>(
&message->mutable_handles()->front()),
static_cast<uint32_t>(message->mutable_handles()->size()),
MOJO_WRITE_MESSAGE_FLAG_NONE);
switch (rv) {
case MOJO_RESULT_OK:
message->mutable_handles()->clear();
break;
case MOJO_RESULT_FAILED_PRECONDITION:
drop_writes_ = true;
break;
default:
return false;
}
return true;
}
bool Connector::AcceptWithResponder(Message* message,
MessageReceiver* responder) {
assert(false);
return false;
}
void Connector::CallOnHandleReady(void* closure, MojoResult result) {
Connector* self = static_cast<Connector*>(closure);
self->OnHandleReady(result);
}
void Connector::OnHandleReady(MojoResult result) {
async_wait_id_ = 0;
if (result == MOJO_RESULT_OK) {
ReadMore();
} else {
error_ = true;
}
if (error_ && error_handler_)
error_handler_->OnError();
}
void Connector::WaitToReadMore() {
async_wait_id_ = waiter_->AsyncWait(waiter_,
message_pipe_.get().value(),
MOJO_WAIT_FLAG_READABLE,
MOJO_DEADLINE_INDEFINITE,
&Connector::CallOnHandleReady,
this);
}
void Connector::ReadMore() {
while (true) {
MojoResult rv;
rv = ReadAndDispatchMessage(message_pipe_.get(), incoming_receiver_, NULL);
if (rv == MOJO_RESULT_SHOULD_WAIT) {
WaitToReadMore();
break;
}
if (rv != MOJO_RESULT_OK) {
error_ = true;
break;
}
}
}
}
}