| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 #include "mojo/public/cpp/bindings/lib/synchronous_connector.h" | |
| 6 | |
| 7 #include <mojo/system/handle.h> | |
| 8 #include <mojo/system/time.h> | |
| 9 | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "mojo/public/cpp/bindings/message.h" | |
| 13 #include "mojo/public/cpp/environment/logging.h" | |
| 14 #include "mojo/public/cpp/system/message_pipe.h" | |
| 15 #include "mojo/public/cpp/system/wait.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 namespace internal { | |
| 19 | |
| 20 SynchronousConnector::SynchronousConnector(ScopedMessagePipeHandle handle) | |
| 21 : handle_(std::move(handle)) {} | |
| 22 | |
| 23 SynchronousConnector::~SynchronousConnector() {} | |
| 24 | |
| 25 bool SynchronousConnector::Write(Message* msg_to_send) { | |
| 26 MOJO_DCHECK(handle_.is_valid()); | |
| 27 MOJO_DCHECK(msg_to_send); | |
| 28 | |
| 29 auto result = WriteMessageRaw( | |
| 30 handle_.get(), msg_to_send->data(), msg_to_send->data_num_bytes(), | |
| 31 msg_to_send->mutable_handles()->empty() | |
| 32 ? nullptr | |
| 33 : reinterpret_cast<const MojoHandle*>( | |
| 34 msg_to_send->mutable_handles()->data()), | |
| 35 static_cast<uint32_t>(msg_to_send->mutable_handles()->size()), | |
| 36 MOJO_WRITE_MESSAGE_FLAG_NONE); | |
| 37 | |
| 38 switch (result) { | |
| 39 case MOJO_RESULT_OK: | |
| 40 break; | |
| 41 | |
| 42 case MOJO_RESULT_INVALID_ARGUMENT: | |
| 43 case MOJO_RESULT_RESOURCE_EXHAUSTED: | |
| 44 case MOJO_RESULT_FAILED_PRECONDITION: | |
| 45 case MOJO_RESULT_UNIMPLEMENTED: | |
| 46 case MOJO_RESULT_BUSY: | |
| 47 default: | |
| 48 MOJO_LOG(WARNING) << "WriteMessageRaw unsuccessful. error = " << result; | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 bool SynchronousConnector::BlockingRead(Message* received_msg) { | |
| 56 MOJO_DCHECK(handle_.is_valid()); | |
| 57 MOJO_DCHECK(received_msg); | |
| 58 | |
| 59 MojoResult rv = Wait(handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 60 MOJO_DEADLINE_INDEFINITE, nullptr); | |
| 61 | |
| 62 if (rv != MOJO_RESULT_OK) { | |
| 63 MOJO_LOG(WARNING) << "Failed waiting for a response. error = " << rv; | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 rv = ReadMessage(handle_.get(), received_msg); | |
| 68 if (rv != MOJO_RESULT_OK) { | |
| 69 MOJO_LOG(WARNING) << "Failed reading the response message. error = " << rv; | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 } // namespace internal | |
| 77 } // namespace mojo | |
| OLD | NEW |